Presentation is loading. Please wait.

Presentation is loading. Please wait.

An Illustration of Kruskal’s Algorithm Prepared Spring 2006 by Sean Szumlanski Computer Science II, University of Central Florida.

Similar presentations


Presentation on theme: "An Illustration of Kruskal’s Algorithm Prepared Spring 2006 by Sean Szumlanski Computer Science II, University of Central Florida."— Presentation transcript:

1 An Illustration of Kruskal’s Algorithm Prepared Spring 2006 by Sean Szumlanski Computer Science II, University of Central Florida

2 A B C D E 4 9 4 15 34 2 Note that there may be more than one valid MST for a graph. A deterministic algorithm will always produce the same MST for a given graph, but precisely which MST it produces will depend on your tie-breaking mechanisms. We will see an illustration of this here. Goal: find a minimum spanning tree (MST) for the given graph.

3 A B C D E 4 9 4 15 34 2 Initially, all vertices are in disjoint sets, as indicated by the disjoint green blobs on which they lie.

4 A B C D E 4 9 4 15 34 2 The algorithm now proceeds as follows: we select the smallest edge in the graph and ask, “are the vertices connected by this edge in the same set?” In this case, the smallest edge is the edge with weight 1, which currently connects vertices from disjoint sets.

5 A B C D E 4 9 4 15 34 2 Because the edge in question connects vertices in disjoint sets, we add it to the MST and union the two sets that the vertices are in (indicated by the green blobs expanding and uniting) The process now repeats; we select the edge with the next smallest weight (the edge with a weight of 2) and ask whether it connects two vertices from disjoint sets. The bolded edge is now included in the MST.

6 A B C D E 4 9 4 15 34 2 Once again, since the edge in question connects vertices in disjoint sets, that edge is added to the MST and we union the two sets that the vertices are in The process repeats again; we select the edge with the next smallest weight (3) and ask whether it connects two vertices from disjoint sets. The bolded edge is now included in the MST, as well.

7 A B C D E 4 9 4 15 34 2 Because the edge in question connects vertices in disjoint sets, we add it to the MST and union the two sets that the vertices are in (indicated by the green blobs expanding and uniting) The algorithm continues; we select one of the edges with a weight of 4 (it doesn’t matter which one, as long as our algorithm breaks such ties deterministically) and ask whether it connects two vertices from disjoint sets. The bolded edge is now included in the MST, as well.

8 A B C D E 4 9 4 15 34 2 Suppose we choose the edge connecting vertices A and B. Because vertices A and B are already in the same set, we do not add that edge to the MST. We simply discard it and move on. The algorithm proceeds; we select one of the other edges with a weight of 4 and ask whether it connects two vertices from disjoint sets. This edge is discarded.

9 A B C D E 4 9 4 15 34 2 Suppose we choose the edge connecting vertices A and D. Because vertices A and D are in disjoint sets, we add the edge to the MST and union the two respective sets. Notice that the union operation doesn’t just assimilate D into the set with A, B, and E. It assimilates everything that was in D’s set, including vertex C. This edge is added.

10 A B C D E 4 1 3 2 Now that all vertices are within the same set, the algorithm terminates and we have our MST. Resulting MST

11 A B C D E 1 34 2 Note that on slide 9 (or even on slide 8, for that matter), we could have considered the edge from E to C instead. If we had, this would have been our resulting (and perfectly valid) MST: This choice would, of course, have been made by the algorithm in some deterministic way.


Download ppt "An Illustration of Kruskal’s Algorithm Prepared Spring 2006 by Sean Szumlanski Computer Science II, University of Central Florida."

Similar presentations


Ads by Google