11.07.2015 Views

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

396 Chap. 11 GraphsPrim’s algorithm is an example of a greedy algorithm. At each step in thefor loop, we select the least-cost edge th<strong>at</strong> connects some marked vertex to someunmarked vertex. The algorithm does not otherwise check th<strong>at</strong> the MST reallyshould include this least-cost edge. This leads to an important question: DoesPrim’s algorithm work correctly? Clearly it gener<strong>at</strong>es a spanning tree (becauseeach pass through the for loop adds one edge <strong>and</strong> one unmarked vertex to thespanning tree until all vertices have been added), but does this tree have minimumcost?Theorem 11.1 Prim’s algorithm produces a minimum-cost spanning tree.Proof: We will use a proof by contradiction. Let G = (V, E) be a graph for whichPrim’s algorithm does not gener<strong>at</strong>e an MST. Define an ordering on the verticesaccording to the order in which they were added by Prim’s algorithm to the MST:v 0 , v 1 , ..., v n−1 . Let edge e i connect (v x , v i ) for some x < i <strong>and</strong> i ≥ 1. Let e j be thelowest numbered (first) edge added by Prim’s algorithm such th<strong>at</strong> the set of edgesselected so far cannot be extended to form an MST for G. In other words, e j is thefirst edge where Prim’s algorithm “went wrong.” Let T be the “true” MST. Call v p(p < j) the vertex connected by edge e j , th<strong>at</strong> is, e j = (v p , v j ).Because T is a tree, there exists some p<strong>at</strong>h in T connecting v p <strong>and</strong> v j . Theremust be some edge e ′ in this p<strong>at</strong>h connecting vertices v u <strong>and</strong> v w , with u < j <strong>and</strong>w ≥ j. Because e j is not part of T, adding edge e j to T forms a cycle. Edge e ′ mustbe of lower cost than edge e j , because Prim’s algorithm did not gener<strong>at</strong>e an MST.This situ<strong>at</strong>ion is illustr<strong>at</strong>ed in Figure 11.23. However, Prim’s algorithm would haveselected the least-cost edge available. It would have selected e ′ , not e j . Thus, it is acontradiction th<strong>at</strong> Prim’s algorithm would have selected the wrong edge, <strong>and</strong> thus,Prim’s algorithm must be correct.✷Example 11.3 For the graph of Figure 11.20, assume th<strong>at</strong> we begin bymarking Vertex A. From A, the least-cost edge leads to Vertex C. Vertex C<strong>and</strong> edge (A, C) are added to the MST. At this point, our c<strong>and</strong>id<strong>at</strong>e edgesconnecting the MST (Vertices A <strong>and</strong> C) with the rest of the graph are (A, E),(C, B), (C, D), <strong>and</strong> (C, F). From these choices, the least-cost edge from theMST is (C, D). So we add Vertex D to the MST. For the next iter<strong>at</strong>ion, ouredge choices are (A, E), (C, B), (C, F), <strong>and</strong> (D, F). Because edges (C, F)<strong>and</strong> (D, F) happen to have equal cost, it is an arbitrary decision as to whichgets selected. Say we pick (C, F). The next step marks Vertex E <strong>and</strong> addsedge (F, E) to the MST. Following in this manner, Vertex B (through edge(C, B)) is marked. At this point, the algorithm termin<strong>at</strong>es.

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

Saved successfully!

Ooh no, something went wrong!