Pattern #61
Minimum Spanning Tree
Connect every node with minimum total edge cost using Kruskal or Prim, while preserving the V − 1 edge and no-cycle invariants.
Must Solve
15 core MST questions.
- 1.Min Cost to Connect All Pointsmedium
- 2.Connecting Cities With Minimum Costmedium
- 3.Optimize Water Distribution in a Villagemedium
- 4.Minimum Cost to Connect All Nodesmedium
- 5.Kruskal's Algorithmmedium
- 6.Prim's Algorithmmedium
- 7.Find Critical and Pseudo-Critical Edges in MSThard
- 8.Minimum Cost to Connect Points with Manhattan Distancehard
- 9.Minimum Spanning Tree from Edge Listhard
- 10.Minimum Spanning Tree from Adjacency Listhard
- 11.Check Whether an Edge Belongs to an MSThard
- 12.Minimum Cost Network Connectionhard
- 13.Minimum Cost to Connect Islands / Componentshard
- 14.Maximum Spanning Tree — Varianthard
- 15.Minimum Spanning Foresthard
Also Important
6 advanced network-design variants.
- 16.Second Minimum Spanning Tree — Advancedmedium
- 17.MST with Pre-Connected Componentsmedium
- 18.MST with Optional Connectionsmedium
- 19.MST with Virtual Nodemedium
- 20.Manhattan MST Optimization — Advancedmedium
- 21.Dynamic MST — Advancedmedium
How to Think
Minimum Spanning TreeMSTKruskal + Union FindPrim + Min HeapShortest Path, not MSTSpanning tree:
• contains every vertex
• has exactly V − 1 edges
• is connected
• has no cycle
MST minimizes the sum of those V − 1 edge weights.Go Quick Reference — Kruskal & Prim
Kruskal
sort.Slice(edges, func(i, j int) bool {
return edges[i].w < edges[j].w
})
for _, e := range edges {
if dsu.Union(e.u, e.v) {
cost += e.w
used++
}
}Best fit for edge lists and sparse graphs. DSU rejects cycles.
Prim
heap.Push(&pq, Edge{w: 0, to: start})
for pq.Len() > 0 {
e := heap.Pop(&pq).(Edge)
if visited[e.to] { continue }
visited[e.to] = true
cost += e.w
// push edges from e.to
}Best fit for adjacency lists or matrices. Visited nodes reject cycles.
MST Patterns
Cut property
The cheapest edge crossing a cut is safe for some MST.
Cycle property
The heaviest edge on a cycle is never required when it is uniquely heaviest.
Disconnected graph
No spanning tree exists; algorithms produce a minimum spanning forest.
Pre-connected components
Union free existing connections before processing paid edges.
Virtual node trick
Represent wells or optional facilities as edges from one synthetic node.
Critical edges
Removing a critical edge raises MST cost or disconnects the graph.
MST → connect every vertex
Accepted edges → exactly V − 1
Cycle → reject
Kruskal → sorted edges + DSU
Prim → frontier heap + visitedComplexity
Kruskal: O(E log E) time, O(V) DSU space
Heap Prim: O(E log V) time, O(V + E) space
Matrix Prim: O(V²) time, O(V) extra spaceCommon Interview Mistakes
• Confusing MST with shortest path
• Forgetting an MST needs V − 1 accepted edges
• Allowing a cycle
• Not checking that all vertices became connected
• Using Dijkstra to minimize total network cost
• Adding Prim cost for an already visited node
• Using DSU without sorting in Kruskal
• Assuming the MST is unique
Interview Rules
Connect all nodes cheaply → MST
Edges accepted → V − 1
Cycle → reject
Edge list → Kruskal + DSU
Adjacency list → Prim + heap
Disconnected → spanning forest
Shortest source route → not MSTProduction Thinking
Infrastructure → design minimum-cost fiber, road, power, or pipeline networks.
Virtual node → model build-vs-connect choices such as wells versus pipes.
Production warning → a pure MST has no redundant backup path when an edge fails.
Remember This
MST → cheapest total network
Tree edges → V − 1
No cycles → mandatory
Kruskal → edge list + DSU
Prim → adjacency + heap
Disconnected → spanning forest
MST path ≠ shortest path💡 Golden Rule: Repeatedly choose a safe cheapest connection until every node is connected without a cycle.