Minimum Spanning Trees
A spanning tree connects all vertices with exactly V-1 edges and no cycles. A Minimum Spanning Tree (MST) does this with minimum total edge weight. MST algorithms solve practical problems: network design, clustering, approximation algorithms, and more. This article covers Union-Find (the key data structure), Kruskal's greedy edge selection, Prim's greedy vertex growth, and their applications.
π At a Glance
| Algorithm | Time | Space | Best For |
|---|---|---|---|
| Kruskal | O(E log E) | O(V) | Sparse graphs, edge list input |
| Prim (binary heap) | O(E log V) | O(V) | Dense graphs, adjacency list |
| Prim (Fibonacci heap) | O(E + V log V) | O(V) | Very dense graphs |
| BorΕ―vka | O(E log V) | O(V) | Parallel implementation |
π― What You'll Learn
After reading this article, you will be able to:
- Implement Union-Find with path compression and union by rank
- Apply Kruskal's algorithm for edge-based MST construction
- Use Prim's algorithm for vertex-based MST growth
- Understand MST properties and their proofs
- Solve practical problems using MST (clustering, network design)
- Choose the right algorithm based on graph characteristics
π₯ Production Story: The Network Infrastructure Optimization
A telecom company needed to connect 50 data centers with fiber optic cables. Running cables between all pairs would cost billions. They needed the minimum-cost network that still connected everything.
JAVA(18 lines)CodeLoading syntax highlighter...
JAVA(35 lines)CodeLoading syntax highlighter...
JAVA(24 lines)CodeLoading syntax highlighter...
π§ Mental Model: MST Properties
TEXT(37 lines)CodeLoading syntax highlighter...
π¬ Deep Dive: Union-Find (Disjoint Set Union)
Basic Implementation
JAVA(69 lines)CodeLoading syntax highlighter...
Union-Find Visualization
TEXT(31 lines)CodeLoading syntax highlighter...
Union-Find with Size (Alternative to Rank)
JAVA(41 lines)CodeLoading syntax highlighter...
π¬ Deep Dive: Kruskal's Algorithm
The Algorithm
JAVA(41 lines)CodeLoading syntax highlighter...
Kruskal's Visualization
TEXT(24 lines)CodeLoading syntax highlighter...
π¬ Deep Dive: Prim's Algorithm
Priority Queue Implementation
JAVA(57 lines)CodeLoading syntax highlighter...
Prim's with Adjacency Matrix (Dense Graphs)
JAVA(41 lines)CodeLoading syntax highlighter...
Prim's Visualization
TEXT(24 lines)CodeLoading syntax highlighter...
π¬ Deep Dive: BorΕ―vka's Algorithm
Parallel-Friendly MST
JAVA(52 lines)CodeLoading syntax highlighter...
Algorithm Comparison
| Aspect | Kruskal | Prim | BorΕ―vka |
|---|---|---|---|
| Strategy | Sort edges, add if no cycle | Grow tree from vertex | Each component finds min edge |
| Data structure | Union-Find | Priority Queue | Union-Find |
| Better for | Sparse graphs | Dense graphs | Parallel systems |
| Edge format | Edge list | Adjacency list | Edge list |
π¬ Deep Dive: MST Applications
Clustering (Single-Linkage)
JAVA(58 lines)CodeLoading syntax highlighter...
Network Design with Steiner Tree Approximation
JAVA(35 lines)CodeLoading syntax highlighter...
Maximum Bottleneck Path
JAVA(51 lines)CodeLoading syntax highlighter...
β οΈ Common Mistakes
Mistake 1: Union-Find Without Optimizations
JAVA(15 lines)CodeLoading syntax highlighter...
Mistake 2: Forgetting MST Has V-1 Edges
JAVA(14 lines)CodeLoading syntax highlighter...
Mistake 3: Not Handling Disconnected Graphs
JAVA(9 lines)CodeLoading syntax highlighter...
Mistake 4: Wrong Prim's Priority Queue Update
JAVA(16 lines)CodeLoading syntax highlighter...
π Debug This: The Broken MST
These implementations have bugs. Find and fix them:
JAVA(68 lines)CodeLoading syntax highlighter...
π‘ Hint
Look for:
- Missing return assignment in path compression
- Missing cycle check before adding edge
- Missing check for
inMST[px] == inMST[py]in union - Order of operations in Prim's
β Solution
JAVA(88 lines)CodeLoading syntax highlighter...
-
Union-Find find():
find(parent[x])without assignment doesn't compress path. Must beparent[x] = find(parent[x]). -
Kruskal's: Adding every edge without checking if union succeeds creates cycles and includes edges between already-connected vertices.
-
Prim's: Must mark
inMST[u] = trueBEFORE updating neighbors, otherwise we might update keys of vertices we just processed. Also need to check!inMST[v]before updating key.
π» Exercises
Exercise 1: Min Cost to Connect All Points (LeetCode 1584) ββ
Connect all points with minimum total Manhattan distance.
JAVA(9 lines)CodeLoading syntax highlighter...
Exercise 2: Number of Operations to Make Network Connected (LeetCode 1319) ββ
Find minimum cables to move to connect all computers.
JAVA(10 lines)CodeLoading syntax highlighter...
π‘ Hint
Need at least n-1 cables. Extra cables (creating cycles) can be reused. Count components with Union-Find.
Exercise 3: Find Critical Edges in MST βββ
Find edges that must be in every MST and edges that can be in some MST.
JAVA(9 lines)CodeLoading syntax highlighter...
Exercise 4: Second Minimum Spanning Tree ββββ
Find the MST with second smallest total weight.
JAVA(9 lines)CodeLoading syntax highlighter...
π‘ Hint
For each edge in MST, try removing it and finding MST of remaining graph. Second MST is the minimum of these attempts (plus: for each edge not in MST, try adding it and removing one edge from the cycle formed).
Exercise 5: Minimum Cost to Reach Destination with Stops ββββ
Combined MST and shortest path problem.
JAVA(9 lines)CodeLoading syntax highlighter...
π€ Interview Questions
Question 1: When would you use Kruskal's vs Prim's?
| Use Kruskal's when: | Use Prim's when: |
|---|---|
| Sparse graph (E << VΒ²) | Dense graph (E β VΒ²) |
| Edges given as list | Adjacency list/matrix available |
| Need to process edges by weight | Starting from specific vertex |
| Already have Union-Find | Don't want Union-Find overhead |
- Kruskal: O(E log E) - dominated by sorting
- Prim (binary heap): O(E log V)
- Prim (Fibonacci heap): O(E + V log V)
- Prim (adjacency matrix): O(VΒ²)
For E β VΒ²: Prim's O(VΒ²) matrix version beats Kruskal's O(VΒ² log V). For E β V: Kruskal's and Prim's are comparable.
Question 2: Explain Union-Find optimizations
Two optimizations make Union-Find nearly O(1):
JAVA(5 lines)CodeLoading syntax highlighter...
Flattens tree during find operations.
JAVA(11 lines)CodeLoading syntax highlighter...
Keeps trees balanced.
Question 3: How would you find if an edge is in MST?
Two approaches:
- Remove edge (u,v) from graph
- Find path from u to v
- If all edges on path have weight < w, edge is NOT in any MST
- If path has edge with weight > w, edge IS in some MST
- Build MST using Kruskal's
- For edge not in MST: add it (creates cycle), check if it's max in cycle
- If it's the unique max, it's not in any MST
- If tied for max, it could be in some MST
Question 4: What are MST applications beyond network design?
-
Clustering: Single-linkage clustering removes k-1 longest MST edges for k clusters
-
Image segmentation: Pixels as vertices, edge weights = difference. MST finds natural boundaries.
-
Approximation algorithms: MST gives 2-approximation for Traveling Salesman (metric TSP)
-
Taxonomy: Phylogenetic trees in biology
-
Circuit design: Minimum wire length for connecting components
-
Bottleneck paths: MST contains maximum-bottleneck path between any two vertices
-
Maze generation: Random MST on grid creates perfect maze
Question 5: How do you handle MST with multiple edge weight ties?
When edges have equal weights:
-
MST may not be unique: Multiple valid MSTs with same total weight
-
Kruskal's behavior: Among equal-weight edges, order is implementation-dependent (stable sort preserves input order)
-
Finding all MSTs: Replace each tied edge with alternatives and enumerate
-
Critical vs pseudo-critical edges:
- Critical: Must be in every MST
- Pseudo-critical: In some but not all MSTs
- Neither: In no MST
JAVA(9 lines)CodeLoading syntax highlighter...
π Quick Reference
Union-Find Template
JAVA(23 lines)CodeLoading syntax highlighter...
Kruskal's Template
JAVA(13 lines)CodeLoading syntax highlighter...
Prim's Template
JAVA(20 lines)CodeLoading syntax highlighter...
π Review Schedule
| Day | Focus | Time |
|---|---|---|
| Day 1 | Read article, implement Union-Find | 60 min |
| Day 3 | Implement Kruskal's from scratch | 30 min |
| Day 7 | Implement Prim's with priority queue | 30 min |
| Day 14 | Solve MST LeetCode problems | 45 min |
| Day 30 | Review and implement clustering | 20 min |
π What's Next?
- Strongly Connected Components (Tarjan, Kosaraju)
- Articulation points and bridges
- Max flow (Ford-Fulkerson, Edmonds-Karp)
- Bipartite matching