Pattern #62
Kruskal's Algorithm
Sorted-edge MST construction, Union Find cycle prevention, component merging, virtual nodes, critical edges, and Go templates.
Must Solve
15 core 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.Redundant Connection — DSU Relationmedium
- 6.Find Critical and Pseudo-Critical Edges in MSTmedium
- 7.Minimum Spanning Tree from Edge Listhard
- 8.Minimum Cost to Connect Componentshard
- 9.Minimum Spanning Foresthard
- 10.Maximum Spanning Tree — Varianthard
- 11.MST With Pre-Connected Nodeshard
- 12.MST With Virtual Nodehard
- 13.Minimum Cost to Connect Islandshard
- 14.Edge Classification in MSThard
- 15.Second Minimum Spanning Tree — Advancedhard
Also Important
- 16.Earliest Acquaintance / Connectivity by Sorted Eventsmedium
- 17.Minimum Cost Network Constructionmedium
- 18.Offline Connectivity Queriesmedium
- 19.Manhattan MST — Advancedmedium
- 20.Dynamic MST — Advancedmedium
How to Think
Minimum Spanning TreeKruskalSort edges by weightUnion FindSort every edge by weight
↓
Find roots of both endpoints
↓
Different roots? Union + take edge
Same root? Reject cycle
↓
Stop after V − 1 accepted edgesGo Quick Reference — Kruskal + DSU
sort.Slice(edges, func(i, j int) bool {
return edges[i].weight < edges[j].weight
})
cost, used := 0, 0
for _, edge := range edges {
if dsu.Union(edge.u, edge.v) {
cost += edge.weight
used++
if used == n-1 { break }
}
}
if used != n-1 { return -1 } // disconnected
return costUnion must compare representative roots. Path compression plus union by size makes DSU operations almost constant amortized time.
DSU & Kruskal Variants
Pre-connected nodes
Union all free existing connections before scanning paid edges.
Virtual node
Turn wells or optional facilities into normal weighted edges from node 0.
Minimum spanning forest
If disconnected input is allowed, keep the cheapest tree for each component.
Maximum spanning tree
Sort edges descending; the DSU rule remains identical.
Critical edge
Exclude it and recompute; higher cost or disconnection means critical.
Pseudo-critical edge
Force it first; equal optimal total means it can belong to an MST.
Sort edges → cheapest first
Different roots → accept + Union
Same root → reject cycle
Accepted edge → add cost
V − 1 accepted → MST completeComplexity
Sort edges: O(E log E)
DSU work: O(E α(V))
Total: O(E log E)
Space: O(V + E)Common Interview Mistakes
• Forgetting to sort edges
• Taking every cheap edge without a cycle check
• Comparing parent values instead of Find roots
• Adding weight when Union fails
• Not verifying V − 1 accepted edges
• Using Kruskal for a shortest-path question
• Treating directed edges as a normal MST
• Assuming equal weights imply a unique MST
Interview Rules
Edge-list MST → Kruskal
First step → sort by weight
Cycle check → Find(u) == Find(v)
Safe edge → Union succeeds
Cost update → only after successful Union
Completion → V − 1 edges
Disconnected → forest or failureProduction Thinking
Network rollout → sort candidate links by build cost and merge isolated regions.
Offline connectivity → sort events by time or threshold, then answer queries with DSU.
Production warning → a generated complete graph may require O(V²) edges before sorting.
Remember This
Kruskal → edge-list MST
First → sort edges
Cycle test → Find roots
Accept → successful Union
Cost → add only accepted edge
Stop → V − 1 edges
Time → O(E log E)💡 Golden Rule: Process edges from cheapest to most expensive and accept one only when it merges two different components.