Chapter 8. The Minimum Spanning Tree Problem
Classical MST algorithms (Kruskal, Prim) and the union-find primitive.
This page: Classical forms. View LLP forms »
This page collects the classical implementations of the algorithms developed in this chapter. The lattice-linear (LLP) reformulations and chapter setup live on the LLP companion page.
Kruskal
The classical edge-sorted scan with union-by-rank and path compression. $O(m \log n)$. The edges below must already be sorted by weight (the sort itself is a generic preprocessing step, not part of this function).
Time complexity: $O(m \log m)$ for the edge sort, $O(m \alpha(n))$ for the union-find work, where $n$ is the number of vertices and $m$ is the number of edges.
Prim
Linear-scan Prim using a weight matrix; returns parent[], where
parent[v] is the predecessor of $v$ in the MST and is $-1$ for the root.
$O(n^2)$ — slower than the heap variant on sparse graphs but cache-friendly.
Time complexity: $O(n^2)$ with this linear-scan version, $O((n + m) \log n)$ with a binary heap, where $n$ is the number of vertices and $m$ is the number of edges.
Boruvka
Grows many fragments at once: in each round, every current component finds its cheapest outgoing edge via a full edge scan, all such edges are added together, and components merge. Component identity is recomputed each round by a BFS over the tree edges chosen so far, labeling each vertex with the least-numbered vertex in its component. The number of components at least halves every round, so the algorithm halts within $O(\log n)$ rounds.
Time complexity: $O(m \log n)$ — $O(\log n)$ rounds, each doing $O(m)$ work to find every component's minimum outgoing edge.
UnionFind
The standard disjoint-set data structure with path compression in find
and union-by-rank in union. Both operations are amortized $O(\alpha(n))$
where $\alpha$ is the inverse Ackermann function.
Time complexity: $O(\alpha(n))$ amortized per find / union with path compression and union by rank — effectively constant for all practical $n$.