Loading...
Given a connected, undirected, weighted graph, find a subset of edges forming a spanning tree of minimum total weight. For disconnected graphs, Kruskal yields a minimum spanning forest.
Efficiently maintains components as edges are added.
1KRUSKAL(G=(V,E), w):2 sort E by non-decreasing w3 make-set(v) for each v in V4 MST ← ∅5 for each (u,v) in E (in order):6 if find(u) ≠ find(v):7 MST ← MST ∪ {(u,v)}8 union(u,v)9 // else discard (cycle)10 return MST
Since m ≤ n^2, log m = O(log n). Sorting dominates for typical implementations.