This preview shows page 1. Sign up to view the full content.
Unformatted text preview: ! After adding n1 edges, you have one tree, the MST 38 connect two trees. Kruskal’s MST Algorithm Idea: Grow a forest out of edges that do not create a
cycle. Pick an edge with the smallest weight. G=(V,E)
v 39 MST 2 v3
5 v2
v5 v4 v6 v7 10
7 v4
8
v6 v3 3 1 4 v1 v2 2 v1 v5
4 1 6
v7 Kruskal code
void Graph::kruskal(){
int edgesAccepted = 0; E heap ops DisjSet s(NUM_VERTICES); while (edgesAccepted < NUM_VERTICES – 1){
e = smallest weight edge not deleted yet;
// edge e = (u, v) 2E finds uset = s.find(u);
vset = s.find(v);
if (uset != vset){
edgesAccepted++;
41 s.unionSets(uset, vset); V unions Strategy for Kruskal’s EL = sorted set of edges ascending by weight Foreach edge e in EL T1 = tree for head(e) T2 = tree for tail(e) If (T1 != T2) add e to the output (the MST)
Combine trees T1 and T2 Seems simple, no? 42 But, how do you keep track of what trees a node is in? kruskal(edgelist,n) { sort(edgelist) f
or i = 1 to n makeset(i) count = 0 i = 1 while (count < n 1) { if (findset(edgelist[i].v) != findset(edgelist[i].w)) { println(edgelist[i].v +...
View Full
Document
 Spring '10
 HORTON
 Algorithms

Click to edit the document details