7.8 Minimum Spanning Trees: Kruskal's & Prim's Foundations
π‘ Core Intuition: The 3-Second Mental Modelβ
π³ The Everyday Analogy: Connecting Cities With Minimum Fiber-Optic Cableβ
Suppose a telecom company must link towns with high-speed fiber cables using strictly links (zero redundant loops!) while minimizing total cable cost:
- Kruskal's Approach (Cheapest Link Anywhere): Sort every possible road globally from cheapest to priciest. Lay down the next cheapest cable unless its endpoints are already linked via a detour (
DSU find(u) == find(v)). - Prim's Approach (Grow Outward From HQ): Start at City
A, look strictly at cables crossing out to cities not yet connected, pick the single cheapest cable (Min-Heap extractMin), pull that city inside the grid, and repeat!
π» Bridging to Computer Science: Data Structures Powering MSTsβ
A Spanning Tree of a connected undirected graph touches every vertex using strictly acyclic edges. A Minimum Spanning Tree (MST) minimizes edge weight sum . Crucially, Kruskal's Algorithm () is powered directly by Topic 7.7 (DSU), while Prim's Algorithm () is powered directly by Topic 6.9 (Binary Min-Heap)!
βοΈ The Cut Property, Kruskal's Forest & Prim's Frontierβ
Both MST algorithms succeed greedily because of two classic theorems:
- Cut Property: Partition vertices into ANY two non-empty subsets . The strictly lightest edge crossing that cut is guaranteed safe inside every MST!
- Cycle Property: Around ANY closed cycle , the strictly heaviest edge can NEVER belong to any MST!
Kruskal's Algorithm vs. Prim's Algorithm
Choosing the faster MST solver by edge density and backing data structure
Kruskal (Edge Sort + DSU)
- β’Sorts all |E| edges once: O(E log E) = O(E log V).
- β’Filters cycles cleanly in O(E * alpha(V)) via Union-Find.
- β’Works naturally across disconnected graphs (finds Minimum Spanning Forest!).
- β’Intermediate state holds disconnected sub-forests converging together.
Prim (Min-Heap + Adjacency List)
- β’Grows strictly as a SINGLE connected tree from chosen start vertex.
- β’Binary Min-Heap runs in O((V + E) log V); dense array scan runs O(V^2) (beats E log E when E ~ V^2!).
- β’Does not need global edge sorting up front.
- β’Handles only single connected component from root.
π Real-World Production Context: Cable Grids, Clustering & Approximationβ
Minimum Spanning Trees in Networks & ML
Connecting nodes cleanly with minimum cost shapes physical grids, broadcast overlays, and clustering
Broadcast Layer-2 Spanning Tree Protocol (STP)
Ethernet Switching Loops- Computes a loop-free spanning tree across redundant LAN switch cables to stop broadcast storms.
- Blocks backup links dynamically unless active links drop.
Single-Linkage Hierarchical Clustering
ML / Data Mining- Building Kruskal's MST and dropping the heaviest K - 1 edges immediately partitions data into K optimal clusters.
- Exact equivalence between dendrogram clustering and Union-Find edge merges.
Metric TSP 2-Approximation
Logistics Routing- Preorder walk of a Euclidean MST guarantees a Traveling Salesman tour strictly <= 2x Optimal in polynomial time.
- Christofides' extension improves approximation ratio down to 1.5x.
π Numerical Applications & Worked Examplesβ
Consider a connected weighted undirected graph on vertices {1, 2, 3, 4, 5, 6} with edge weights:
(1, 2): 3,(1, 3): 1,(2, 3): 7,(2, 4): 5,(3, 5): 2,(4, 5): 4,(4, 6): 6,(5, 6): 8.
- Trace Kruskal's edge acceptance order and compute the exact MST total weight.
- Suppose edge weights of an arbitrary connected graph are strictly distinct (
no two edges share identical weight). Is its MST guaranteed unique?
Step-by-Step Solution:β
-
Kruskal's Edge Trace (Sorted Weights:
1, 2, 3, 4, 5, 6, 7, 8):- Pick
(1, 3)w=1{1, 3}(1 edge) - Pick
(3, 5)w=2{1, 3, 5}(2 edges) - Pick
(1, 2)w=3{1, 2, 3, 5}(3 edges) - Pick
(4, 5)w=4{1, 2, 3, 4, 5}(4 edges) - Check
(2, 4)w=5find(2) == find(4)REJECT (Cycle{1,2,4,5,3})! - Pick
(4, 6)w=6{1..6}(V - 1 = 5 edgesreached β STOP!). - Exact MST Weight = .
- Pick
-
Distinct Weights Unique MST: YES! Because every Cut and every Cycle possesses strictly one unique minimum/maximum edge without ties, all valid MSTs must contain the exact same edges!
π― Exam & Interview Pitfall Checkβ
- Can a Minimum Spanning Tree contain negative edge weights?
- Yes! Neither Kruskal's sorting nor Prim's cut selection assumes non-negative weights β negative edges work seamlessly (unlike Dijkstra shortest paths!). Note: Kruskal/Prim MUST ALWAYS pick edges, never deleting edges unless disconnected!
- If you add a constant
+10to EVERY edge weight in a connected graph, can the MST edge set change?- NO! Because every spanning tree has identical size edges, adding
+10raises every spanning tree's cost identically by+10 * (V - 1), preserving relative rankings! (Wait: shortest path betweensandtCAN change when adding+10, because paths vary in hop counts!)
- NO! Because every spanning tree has identical size edges, adding
- Assuming MST Minimizes Pairwise Shortest Paths Between Vertices: An MST minimizes total tree weight sum, NOT shortest distance between two nodes (
1 -> 3in the example above directly vs via5might differ completely from Dijkstra paths)! - Running
KruskalWithout CountingedgesAccepted == V - 1: Ignoring early stopping scans all remaining heavy edges unnecessarily; terminating as soon asaccepted == V - 1saves work! - Forgetting Negative Weights Shift Shortest Paths Under
+CWhile Leaving MST Unchanged: Always check whether a problem asks for MST (invariant under ) vs. Shortest Path (sensitive to )!
π Navigation & Next Stepsβ
Next, we finish Module 07 by comparing Single-Source Weighted Shortest Paths via Topic 7.9 β Weighted Shortest Paths: Dijkstra's & Bellman-Ford Foundations.
Never Study in Isolation. Join Binary Dose.
Connect with fellow engineers, clear conceptual doubts, and get 2 visual breakdowns & engineering mental models daily across our community channels.
Discord Community
Start at #welcome-and-rules, say hello in #general-chat, clear doubts, and collaborate with engineers worldwide.