21.11.2014 Views

CHAPTER 25 Weighted Graphs and Applications Objectives • To ...

CHAPTER 25 Weighted Graphs and Applications Objectives • To ...

CHAPTER 25 Weighted Graphs and Applications Objectives • To ...

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Figure <strong>25</strong>.5<br />

The tree in (c) <strong>and</strong> (d) are minimum spanning trees of the graph in (a).<br />

The problem of finding a minimum spanning tree has many applications. Consider a company with<br />

branches in many cities. The company wants to lease telephone lines to connect all branches together. The<br />

phone company charges different amounts of money to connect different pairs of cities. There are many<br />

ways to connect all branches together. The cheapest way is to find a spanning tree with the minimum total<br />

rates.<br />

<strong>25</strong>.4.1 Minimum Spanning Tree Algorithms<br />

How do you find a minimum spanning tree? There are several well-known algorithms for doing so. This<br />

section introduces Prim’s algorithm. Prim’s algorithm starts with a spanning tree T that contains an<br />

arbitrary vertex. The algorithm exp<strong>and</strong>s the tree by adding a vertex with the smallest edge incident to a<br />

vertex already in the tree. The algorithm is described in Listing <strong>25</strong>.4.<br />

Listing <strong>25</strong>.4 Prim’s Minimum Spanning Tree Algorithm<br />

Input: A connected undirected weighted G = (V, E)<br />

Output: MST (a minimum spanning tree)<br />

1 MST minimumSpanningTree()<br />

2 {<br />

3 Let T be a set for the vertices in the spanning tree;<br />

4 Set cost[v] = infinity for each vertex v;<br />

5 Pick an arbitrary vertex, say s <strong>and</strong> set cost[s] = 0 <strong>and</strong> parent[s]<br />

= -1;<br />

6<br />

7 while (size of T < n)<br />

8 {<br />

9 Find u not in T with the smallest cost[u];<br />

16

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!