23.11.2014 Views

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

13.7.1 Kruskal's Algorithm<br />

The reason Proposition 13.25 is so important is that it can be used as the basis for<br />

build<strong>in</strong>g a m<strong>in</strong>imum spann<strong>in</strong>g tree. In Kruskal's algorithm, it is used to build the<br />

m<strong>in</strong>imum spann<strong>in</strong>g tree <strong>in</strong> clusters. Initially, each vertex is <strong>in</strong> its own cluster all by<br />

itself. The algorithm then considers each edge <strong>in</strong> turn, ordered by <strong>in</strong>creas<strong>in</strong>g weight.<br />

If an edge e connects two different clusters, then e is added to the set of edges of the<br />

m<strong>in</strong>imum spann<strong>in</strong>g tree, <strong>and</strong> the two clusters connected by e are merged <strong>in</strong>to a<br />

s<strong>in</strong>gle cluster. If, on the other h<strong>and</strong>, e connects two vertices that are already <strong>in</strong> the<br />

same cluster, then e is discarded. Once the algorithm has added enough edges to<br />

form a spann<strong>in</strong>g tree, it term<strong>in</strong>ates <strong>and</strong> outputs this tree as the m<strong>in</strong>imum spann<strong>in</strong>g<br />

tree.<br />

We give pseudo-code for Kruskal's MST algorithm <strong>in</strong> Code Fragment 13.17 <strong>and</strong> we<br />

show the work<strong>in</strong>g of this algorithm <strong>in</strong> Figures 13.18, 13.19, <strong>and</strong> 13.20.<br />

Code Fragment 13.17: Kruskal's algorithm for the<br />

MST problem.<br />

As mentioned before, the correctness of Kruskal's algorithm follows from the<br />

crucial fact about m<strong>in</strong>imum spann<strong>in</strong>g trees, Proposition 13.25. Each time Kruskal's<br />

algorithm adds an edge (v,u) to the m<strong>in</strong>imum spann<strong>in</strong>g tree T, we can def<strong>in</strong>e a<br />

partition<strong>in</strong>g of the set of vertices V (as <strong>in</strong> the proposition) by lett<strong>in</strong>g V 1 be the<br />

cluster conta<strong>in</strong><strong>in</strong>g v <strong>and</strong> lett<strong>in</strong>g V 2 conta<strong>in</strong> the rest of the vertices <strong>in</strong> V. This clearly<br />

def<strong>in</strong>es a disjo<strong>in</strong>t partition<strong>in</strong>g of the vertices of V <strong>and</strong>, more importantly, s<strong>in</strong>ce we<br />

are extract<strong>in</strong>g edges from Q <strong>in</strong> order by their weights, e must be a m<strong>in</strong>imum-weight<br />

edge with one vertex <strong>in</strong> V 1 <strong>and</strong> the other <strong>in</strong> V 2 . Thus, Kruskal's algorithm always<br />

adds a valid m<strong>in</strong>imum spann<strong>in</strong>g tree edge.<br />

855

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

Saved successfully!

Ooh no, something went wrong!