Skip to main content

7.8 Minimum Spanning Trees: Kruskal's & Prim's Foundations

πŸ“šModule 07 β€’ Graphs & Network TraversalsTopic 7.8⏱️13 min read
🎯High-Yield For:Semester Exams β€’ Technical Interviews β€’ Engineering Foundations

πŸ’‘ Core Intuition: The 3-Second Mental Model​

🍳 The Everyday Analogy: Connecting Cities With Minimum Fiber-Optic Cable​

Suppose a telecom company must link NN towns with high-speed fiber cables using strictly Nβˆ’1N - 1 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 G=(V,E)G = (V, E) touches every vertex using strictly ∣Vβˆ£βˆ’1|V| - 1 acyclic edges. A Minimum Spanning Tree (MST) minimizes edge weight sum βˆ‘w(e)\sum w(e). Crucially, Kruskal's Algorithm (O(Elog⁑E)\mathcal{O}(E \log E)) is powered directly by Topic 7.7 (DSU), while Prim's Algorithm (O(Elog⁑V)\mathcal{O}(E \log V)) 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 (S,Vβˆ–S)(S, V \setminus S). The strictly lightest edge crossing that cut is guaranteed safe inside every MST!
  • Cycle Property: Around ANY closed cycle CC, 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

Sparse Winner

Kruskal (Edge Sort + DSU)

πŸ”—
Dominant Architecture / DomainEdge-List Sparse Networks (E ~ V)
  • β€’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.
"Ideal whenever edges arrive already sorted or graph is sparse."
Dense Winner

Prim (Min-Heap + Adjacency List)

🌲
Dominant Architecture / DomainDense Networks (Fibonacci Heap: E + V log V)
  • β€’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.
"Beats Kruskal on dense complete graphs via O(V^2) un-heaped key scan."

🏭 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​

πŸ“Problem Statement: Kruskal Weight Total & Uniqueness Conditiontheorem

Consider a connected weighted undirected graph on 66 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.
  1. Trace Kruskal's edge acceptance order and compute the exact MST total weight.
  2. 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:​

  1. Kruskal's Edge Trace (Sorted Weights: 1, 2, 3, 4, 5, 6, 7, 8):

    • Pick (1, 3) w=1 β€…β€ŠβŸΉβ€…β€Š\implies {1, 3} (1 edge)
    • Pick (3, 5) w=2 β€…β€ŠβŸΉβ€…β€Š\implies {1, 3, 5} (2 edges)
    • Pick (1, 2) w=3 β€…β€ŠβŸΉβ€…β€Š\implies {1, 2, 3, 5} (3 edges)
    • Pick (4, 5) w=4 β€…β€ŠβŸΉβ€…β€Š\implies {1, 2, 3, 4, 5} (4 edges)
    • Check (2, 4) w=5 β€…β€ŠβŸΉβ€…β€Š\implies find(2) == find(4) REJECT (Cycle {1,2,4,5,3})!
    • Pick (4, 6) w=6 β€…β€ŠβŸΉβ€…β€Š\implies {1..6} (V - 1 = 5 edges reached β€” STOP!).
    • Exact MST Weight = 1+2+3+4+6=161 + 2 + 3 + 4 + 6 = \mathbf{16}.
  2. Distinct Weights β€…β€ŠβŸΉβ€…β€Š\implies 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 Vβˆ’1V - 1 edges!


🎯 Exam & Interview Pitfall Check​

Core Conceptual Questions
  1. 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 Vβˆ’1V - 1 edges, never deleting edges unless disconnected!
  2. If you add a constant +10 to EVERY edge weight in a connected graph, can the MST edge set change?
    • NO! Because every spanning tree has identical size Vβˆ’1V - 1 edges, adding +10 raises every spanning tree's cost identically by +10 * (V - 1), preserving relative rankings! (Wait: shortest path between s and t CAN change when adding +10, because paths vary in hop counts!)
Common Interview Traps: Architectural Trade-Offs
  • Assuming MST Minimizes Pairwise Shortest Paths Between Vertices: An MST minimizes total tree weight sum, NOT shortest distance between two nodes (1 -> 3 in the example above directly vs via 5 might differ completely from Dijkstra paths)!
  • Running Kruskal Without Counting edgesAccepted == V - 1: Ignoring early stopping scans all (V2)\binom{V}{2} remaining heavy edges unnecessarily; terminating as soon as accepted == V - 1 saves work!
  • Forgetting Negative Weights Shift Shortest Paths Under +C While Leaving MST Unchanged: Always check whether a problem asks for MST (invariant under +C+C) vs. Shortest Path (sensitive to +CΓ—hops+C \times \text{hops})!

πŸš€ 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.

πŸš€ Level Up Your Engineering Mindset

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
Official Hub

Discord Community

Start at #welcome-and-rules, say hello in #general-chat, clear doubts, and collaborate with engineers worldwide.

Join Discord Server→
⚑
Daily Knowledge Drops

Daily Visual Deep-Dives

Bite-sized system design schematics, core OS internals, and DSA intuition served right into your feeds every single day.

πŸ’‘Help keep Binary Dose free for students, or share your articles and notes.