Minimum Spanning Trees: Kruskal's and Prim's Algorithms
Anchor (Master): Cormen, Leiserson, Rivest, and Stein 2022 Introduction to Algorithms 4e (MIT Press) Ch. 19 and Ch. 21 (Kruskal, Prim, Boruvka, the role of the cut property, Fibonacci heap speedup for Prim giving , and the Tarjan Cheriyan survey of MST algorithms)
Intuition Beginner
Imagine you are building a road network connecting several towns. You want every town reachable from every other town, and you want to minimise total road length. The roads form a tree (no cycles needed — any cycle would mean a redundant road you could remove). This is the minimum spanning tree problem.
Two natural greedy strategies solve it. The first, due to Kruskal, sorts all roads from shortest to longest and builds the network by adding each road if it connects two previously disconnected groups. The second, due to Prim, starts from one town and repeatedly adds the shortest road connecting an already-connected town to a not-yet-connected town.
Both strategies work, and the reason is a simple property: at every step, the shortest road connecting the "already built" part to the "not yet built" part must be in the optimal network. If it were not, you could swap it in and swap a longer road out, reducing the total length. This cut property is the key to everything.
Visual Beginner
A small graph with edge weights, showing how Kruskal and Prim build the MST.
A ---2--- B ---3--- C
| | |
4 1 5
| | |
D ---6--- E ---7--- FKruskal processes edges in order of weight: DE(1), AB(2), BE(3), AD(4), CF(5), DE-already-connected(6), EF(7). The MST has edges {DE, AB, BE, AD, CF} with total weight .
Prim starting from A: AB(2), then BE(1, from B), then AD(4, from A or D), then DE(1, connecting D to E... but D already connected via A-AD and E via B-BE, so skip). The tree grows: A-B, B-E, A-D, B-C, C-F.
| step | Kruskal adds | weight | Prim adds | weight |
|---|---|---|---|---|
| 1 | DE | 1 | AB | 2 |
| 2 | AB | 2 | BE | 1 |
| 3 | BE | 3 | AD | 4 |
| 4 | AD | 4 | BC | 3 |
| 5 | BC | 3 | CF | 5 |
| 6 | CF | 5 | done |
Worked example Beginner
Consider a graph with 4 vertices and 5 edges: with edges , , , , .
Kruskal sorts edges by weight: , , , , . Process each:
- Add (weight 1). Components: .
- Add (weight 2). Connects and . Components: .
- Add (weight 3). Connects and . All connected. Stop.
MST edges: . Total weight: .
Prim starting from : the lightest edge from is (weight 2). Add it. From , the lightest edges are (weight 1) and (weight 3). Pick . From , the lightest unused edge to is (weight 3). Add it. MST edges: . Total weight: . Same tree.
Check your understanding Beginner
Formal definition Intermediate+
Let be a connected undirected graph with edge weight function . A spanning tree of is a subgraph that is a tree (connected and acyclic) with . A minimum spanning tree (MST) is a spanning tree minimising .
A cut of is a partition of the vertex set. An edge crosses the cut if one endpoint is in and the other in .
Cut property: For any cut , the minimum-weight edge crossing the cut belongs to some MST of .
Cycle property: For any cycle in , the maximum-weight edge on does not belong to any MST of .
These two properties are the foundation of all MST algorithms [CLRS Ch. 19]. The cut property guarantees that certain edges must be in an MST (light edges crossing cuts), and the cycle property guarantees that certain edges cannot be in any MST (heavy edges on cycles).
Counterexamples to common slips Intermediate+
"The MST always includes the globally minimum-weight edge." It does. The cut where is an endpoint of the minimum-weight edge has as its lightest crossing edge, so is in some MST by the cut property.
"Kruskal and Prim always produce the same MST." They always produce a minimum spanning tree, but if edge weights are not all distinct, they may produce different MSTs. Both will be minimum-weight.
"Prim's algorithm requires a specific starting vertex to work correctly." Any starting vertex works. The cut property applies at every step regardless of where the tree started growing.
Key theorem with proof Intermediate+
Theorem (correctness of Kruskal and Prim). Both Kruskal's algorithm and Prim's algorithm produce a minimum spanning tree.
Proof (via the cut property). Kruskal: process edges in order of increasing weight. When Kruskal adds edge , let be the connected component containing at that moment. Then (otherwise would create a cycle and be skipped). The edge is the lightest edge crossing the cut , because all lighter edges have already been processed and either added (in which case and would already be connected) or skipped (because their endpoints were already connected). By the cut property, is in some MST. Since Kruskal adds exactly edges without creating cycles, the result is a spanning tree, and every edge in it is in some MST.
Prim: at each step, Prim adds the lightest edge where is in the current tree and is not. The cut separates the tree vertices from the rest. The edge is the lightest edge crossing this cut (by Prim's selection rule). By the cut property, is in some MST. By induction, every edge Prim adds is in some MST, and the result is a spanning tree.
Running times. Kruskal with union-find (path compression + union by rank): sorting takes , and union-find operations take where is the inverse Ackermann function. Total: since for simple graphs and .
Prim with binary heap: each of the extract-min operations takes , and each of the decrease-key operations takes . Total: .
Prim with Fibonacci heap: extract-min takes amortised and decrease-key takes amortised. Total: .
Bridge. This theorem is the foundational reason greedy algorithms work for MST: the cut property guarantees that local optimality (picking the lightest crossing edge) leads to global optimality, and this is exactly the structural insight that distinguishes MST from harder optimisation problems like TSP where greedy fails. The union-find data structure in Kruskal builds toward the network flow algorithms in 47.04.03 where connectivity management is equally central, and it appears again in the analysis of approximation algorithms 47.04.08 where the MST serves as a lower bound for the TSP tour. The central insight is that the matroid structure of spanning trees makes greedy optimal — the independent sets (forests) form a matroid, and the greedy algorithm is optimal for any matroid. Putting these together, the cut property and cycle property are the two faces of this matroid structure: the cut property says adding the minimum-weight independent element is safe, and the cycle property says removing the maximum-weight dependent element is safe.
Exercises Intermediate+
Advanced results Master
Beyond the basic algorithms, MST theory includes the Boruvka algorithm, linear-time special cases, and deep connections to matroid theory.
Theorem 1 (Boruvka's algorithm). Boruvka's algorithm processes all vertices simultaneously: in each phase, every component adds its lightest outgoing edge. Each phase at least halves the number of components, so phases suffice. Each phase takes time. Total: . The advantage is natural parallelism — each component's lightest edge can be found independently [CLRS Ch. 19].
Theorem 2 (linear-time MST for integer weights). When edge weights are integers in a bounded range, MST can be computed in time using a combination of Boruvka phases and radix sort. More generally, the MST can be found in time using the Fredman-Tarjan algorithm, which is effectively linear for all practical inputs.
Theorem 3 (MST and the matroid intersection). The spanning trees of a graph form a graphic matroid: the independent sets are the forests, and the weight function defines a linear objective. The greedy algorithm is optimal for any matroid, which is why Kruskal works. The matroid perspective explains why MST is "easy" while Steiner tree (finding a minimum-weight tree spanning a subset of vertices) is NP-hard: Steiner tree is not a matroid optimisation problem.
Theorem 4 (sensitivity analysis). Given an MST and an edge , define the replacement weight as the minimum weight of any edge that can replace in without losing the spanning property. Then remains an MST as long as does not increase beyond . Computing all replacement weights takes time using the max-edge-weight queries on the MST, which connects to the lowest common ancestor data structure.
Theorem 5 (MST verification). Given a spanning tree , verifying whether is an MST can be done in time, without knowing whether came from any particular algorithm. The algorithm checks that for every non-tree edge , the maximum-weight edge on the path in between the endpoints of has weight . If this holds for all non-tree edges, is minimum, by the cycle property.
Synthesis. The foundational reason MST has such efficient algorithms is the matroid structure of spanning trees: the cut property and cycle property together guarantee that greedy is optimal, and this is exactly the structural condition that separates MST from NP-hard problems like Steiner tree where the matroid property breaks down. The union-find data structure in Kruskal builds toward the more advanced connectivity data structures used in dynamic graph algorithms, and the Fibonacci heap speedup for Prim appears again in shortest-path algorithms where the same decrease-key-heavy access pattern dominates. The central insight is that the cut property provides a local optimality condition — each edge added is the best choice for its cut — and putting these together, every step of Kruskal and Prim is independently justified without needing to look ahead. The bridge is that the matroid greedy theorem generalises beyond MST to any problem where the feasible solutions form a matroid, and this is dual to the greedy algorithms for scheduling and Huffman coding covered in 47.04.01, where similar exchange arguments justify local optimality.
Full proof set Master
Proposition 1 (cut property). For any cut of , the minimum-weight edge crossing the cut belongs to some MST.
Proof. Let be the lightest crossing edge. Let be any MST. If , done. Otherwise, contains a cycle . Some edge of also crosses the cut. Since , the tree satisfies , so is an MST containing .
Proposition 2 (cycle property). For any cycle , the maximum-weight edge on is in no MST.
Proof. Let be the heaviest edge on . Suppose for MST . Removing disconnects into components . Some other edge of crosses this cut. Then has , contradicting minimality.
Proposition 3 (Kruskal correctness). Kruskal's algorithm produces an MST.
Proof. Process edges in increasing weight order. When edge is added, let be the component containing one endpoint. Edge is the lightest edge crossing the cut , since all lighter edges were already processed (and either added to connect these components or skipped because the components were already connected). By the cut property, is in some MST. After adding edges, the result is a spanning tree.
Proposition 4 (Prim correctness). Prim's algorithm produces an MST.
Proof. At each step, let be the set of tree vertices. Prim adds the lightest edge crossing . By the cut property, this edge is in some MST. By induction, every added edge is in some MST, and the result after steps is a spanning tree.
Proposition 5 (running time of Prim with Fibonacci heap). Prim with a Fibonacci heap runs in .
Proof. Each vertex is extracted once: extract-min operations at amortised each. Each edge causes at most one decrease-key: operations at amortised each. Initialisation: . Total: .
Connections Master
Amortized analysis
47.04.01provides the tools for analysing the union-find data structure in Kruskal and the Fibonacci heap in Prim. The path-compression analysis of union-find is a foundational amortised analysis.Graph theory
40.04.01supplies the definitions of trees, cuts, and cycles that underpin the cut and cycle properties. The exchange arguments in the proofs are graph-theoretic at their core.Network flow
47.04.03extends the connectivity management ideas from MST to the more complex max-flow setting, where the cut property becomes the max-flow min-cut theorem.Approximation algorithms
47.04.08use the MST as a lower bound for the TSP tour: the TSP tour minus one edge is a spanning tree, so . This gives the 2-approximation for metric TSP.
Historical & philosophical context Master
The MST problem has a rich history spanning multiple independent discoveries. Otakar Boruvka formulated and solved the problem in 1926 for the electrification of Moravia, making it one of the earliest combinatorial optimisation problems. Joseph Kruskal published his algorithm in 1956 in "On the Shortest Spanning Subtree of a Graph and the Traveling Salesman Problem" (Proceedings of the AMS). Robert Prim published his algorithm in 1957, though it had been independently discovered by Vojtech Jarnik in 1930 [CLRS Ch. 19].
The matroid connection was recognised in the 1960s, when it became clear that the greedy algorithm's success for MST was not accidental but reflected a deep algebraic structure. The development of efficient union-find data structures by Tarjan and van Leeuwen in the 1970s gave Kruskal's algorithm its modern union-find cost.
Bibliography Master
@article{kruskal1956shortest,
author = {Kruskal, Joseph B.},
title = {On the shortest spanning subtree of a graph and the traveling salesman problem},
journal = {Proceedings of the American Mathematical Society},
volume = {7},
number = {1},
year = {1956},
pages = {48--50}
}
@article{prim1957shortest,
author = {Prim, Robert C.},
title = {Shortest connection networks and some generalizations},
journal = {Bell System Technical Journal},
volume = {36},
number = {6},
year = {1957},
pages = {1389--1401}
}
@article{boruvka1926ojistem,
author = {Bor\r{u}vka, Otakar},
title = {O jist\'{e}m probl\'{e}mu minim\'{a}ln\'{i}m},
journal = {Pr\'{a}ce Moravsk\'{e} P\v{r}\'{i}rodov\v{e}deck\'{e} Spole\v{c}nosti},
volume = {3},
year = {1926},
pages = {37--58}
}
@book{cormen2022introduction,
author = {Cormen, Thomas H. and Leiserson, Charles E. and Rivest, Ronald L. and Stein, Clifford},
title = {Introduction to Algorithms},
publisher = {MIT Press},
edition = {4th},
year = {2022}
}