Loading...
Add edges in increasing weight, skipping those that form a cycle. Greedy is optimal by the cut property: the lightest edge crossing any cut belongs to an MST.
1procedure KRUSKAL(G)2 sort all edges by weight3 create DSU for vertices4 MST ← ∅5 for each edge (u, v, w) in increasing weight do6 if find(u) ≠ find(v) then7 MST ← MST ∪ {(u, v)}; union(u, v)8 else9 skip edge // would form cycle10 end if11 end for12 return MST13end procedure