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. 1.Min Cost to Connect All Points
    medium
  2. 2.Connecting Cities With Minimum Cost
    medium
  3. 3.Optimize Water Distribution in a Village
    medium
  4. 4.Minimum Cost to Connect All Nodes
    medium
  5. 5.Redundant Connection — DSU Relation
    medium
  6. 6.Find Critical and Pseudo-Critical Edges in MST
    medium
  7. 7.Minimum Spanning Tree from Edge List
    hard
  8. 8.Minimum Cost to Connect Components
    hard
  9. 9.Minimum Spanning Forest
    hard
  10. 10.Maximum Spanning Tree — Variant
    hard
  11. 11.MST With Pre-Connected Nodes
    hard
  12. 12.MST With Virtual Node
    hard
  13. 13.Minimum Cost to Connect Islands
    hard
  14. 14.Edge Classification in MST
    hard
  15. 15.Second Minimum Spanning Tree — Advanced
    hard

Also Important

  1. 16.Earliest Acquaintance / Connectivity by Sorted Events
    medium
  2. 17.Minimum Cost Network Construction
    medium
  3. 18.Offline Connectivity Queries
    medium
  4. 19.Manhattan MST — Advanced
    medium
  5. 20.Dynamic MST — Advanced
    medium

How to Think

Connect all nodes cheaply?Minimum Spanning Tree
Input is mainly edges?Kruskal
What comes first?Sort edges by weight
How are cycles avoided?Union Find
Sort every edge by weight
↓
Find roots of both endpoints
↓
Different roots? Union + take edge
Same root? Reject cycle
↓
Stop after V − 1 accepted edges

Go 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 cost

Union 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.

Visual Memory Rule
Sort edges → cheapest first
Different roots → accept + Union
Same root → reject cycle
Accepted edge → add cost
V − 1 accepted → MST complete

Complexity

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 failure

Production 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.