18.04.2013 Views

The.Algorithm.Design.Manual.Springer-Verlag.1998

The.Algorithm.Design.Manual.Springer-Verlag.1998

The.Algorithm.Design.Manual.Springer-Verlag.1998

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Prim's <strong>Algorithm</strong><br />

inserted edge (x,y), consisted of a set of edges that was a subtree of a minimum spanning tree , but choosing edge<br />

(x,y) took us away from a minimum spanning tree. But how could it? <strong>The</strong>re must be a path p from x to y in , using an edge<br />

, where is in but is not. This edge must have weight at least that of (x,y), or else Prim's algorithm would<br />

have selected it instead of (x,y) when it had the chance. Inserting (x,y) and deleting from leaves a spanning tree no<br />

larger than before, meaning that Prim's algorithm could not have made a fatal mistake in selecting edge (x,y). <strong>The</strong>refore, by<br />

contradiction, Prim's algorithm has to construct a minimum spanning tree.<br />

Prim's algorithm is correct, but how efficient is it? That depends on which data structures are used to implement it, but it<br />

should be clear that O(nm) time suffices. In each of n iterations, we will scan through all the m edges and test whether the<br />

current edge joins a tree with a non-tree vertex and whether this is the smallest edge seen thus far. By maintaining a Boolean<br />

flag along with each vertex to denote whether it is in the tree or not, this test can be performed in constant time. In fact, better<br />

data structures lead to a faster, , implementation by avoiding the need to sweep through more than n edges in any<br />

iteration.<br />

Next: Kruskal's <strong>Algorithm</strong> Up: Minimum Spanning Trees Previous: Minimum Spanning Trees<br />

<strong>Algorithm</strong>s<br />

Mon Jun 2 23:33:50 EDT 1997<br />

file:///E|/BOOK/BOOK2/NODE74.HTM (2 of 2) [19/1/2003 1:29:14]

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

Saved successfully!

Ooh no, something went wrong!