Minimum Spanning Trees: Kruskal's & Prim's Algorithms
Graph Theory
Chapter 8 · Minimum Spanning Trees: Kruskal's & Prim's Algorithms
Chapters 6 and 7 answered "what's the cheapest way from one vertex to the others?" This chapter asks a genuinely different question: given a weighted, undirected, connected graph, what's the cheapest possible way to connect every vertex to every other, using as few edges as it takes and nothing more?
What a Minimum Spanning Tree Actually Is
A spanning tree of a connected graph with V vertices is a subgraph that connects all V vertices using exactly V − 1 edges, with no cycles — precisely the tree definition Chapter 4's own finding-box already established. A Minimum Spanning Tree (MST) is the spanning tree whose edge weights sum to the smallest possible total, out of every spanning tree the graph could have.
Method 1: Kruskal's Algorithm
Sort every edge in the graph by weight, ascending. Go through them one at a time: add an edge to the MST unless both of its endpoints are already connected to each other through edges already added — adding it anyway would create a cycle, which a tree can never have.
find(x) returns x's component; union(x, y) merges two components into one. An edge is safe to add exactly when find(u) != find(v) — its two endpoints are still in different components.
Graph: A-B(4), A-C(2), B-C(1), B-D(5), C-D(8), C-E(10), D-E(2), B-E(6).
B-C(1): different components — add. A-C(2): different components — add. D-E(2): different components — add. A-B(4): A and B already connected via B-C-A — skip, would form a cycle. B-D(5): different components (the {A,B,C} group and the {D,E} group) — add. Every remaining edge (B-E, C-D, C-E) now connects vertices already in the same component — all skipped. Final MST: {B-C, A-C, D-E, B-D}, total weight 10.
Method 2: Prim's Algorithm
Start from any single vertex — it's the entire "tree" so far. Repeatedly find the cheapest edge connecting a vertex already in the tree to one that isn't, add that edge and vertex, and repeat until every vertex is included. A priority queue does the "cheapest available edge" lookup, exactly like Dijkstra's own priority queue found the cheapest available distance.
Same graph, starting from A:
A-C(2) — add C. Frontier from {A,C}: cheapest is C-B(1) — add B. Frontier from {A,B,C}: cheapest is B-D(5) — add D. Frontier from {A,B,C,D}: cheapest is D-E(2) — add E. Final MST: {A-C, C-B, B-D, D-E}, total weight 10 — the exact same total, and in fact the exact same set of edges, as Kruskal's algorithm found, just discovered in a different order.
Real Relevance: Network Design
Laying fiber or cable to connect a set of offices, data centers, or towns at minimum total cost is precisely the MST problem — vertices are sites, edge weight is the cost of a direct link between two sites, and the MST is the cheapest possible way to make sure every site can reach every other (even if only indirectly, through other sites on the tree). The same shape shows up in circuit board wiring and in clustering algorithms that group data points by cutting the most expensive edges out of an MST.
Kruskal's & Prim's in Code
Hands-On Exercises
Using Kruskal's algorithm, find the MST of the graph with edges P-Q(3), P-R(1), Q-R(4), Q-S(2), R-S(5). Show which edges are added and which are skipped, and the total weight.
Four offices need network cabling: Office1-Office2(8), Office1-Office3(5), Office2-Office3(3), Office2-Office4(9), Office3-Office4(4). Using Prim's algorithm starting from Office1, find the minimum-cost cabling plan and its total cost.
Explain why Kruskal's algorithm needs a Union-Find structure to detect cycles, while Prim's algorithm never risks creating a cycle at all — even without any cycle-checking step of its own.
📄 View solutionChapter 8 Quick Reference
- Spanning tree: connects all V vertices using exactly V−1 edges, no cycles (Chapter 4's own tree definition)
- Kruskal's algorithm: sort all edges by weight; add each one unless it connects two vertices already in the same component (Union-Find)
- Prim's algorithm: grow one tree from a start vertex, always adding the cheapest edge on the current frontier — a close cousin of Dijkstra
- Both algorithms always agree on the total MST weight; they agree on the exact edges too unless weight ties allow more than one valid MST
- Real use: minimum-cost network/cable design, circuit wiring, clustering
- Next chapter: Trees as a special case of graphs — structure and traversal