← chapter

Minimum spanning trees: Kruskal

Same MST as Prim, opposite strategy. Sort edges cheapest-first; add each that doesn't make a cycle.

Merge a forest, don't grow a tree

Watch the forest merge

Node color = which tree it's in. Orange = accepted (colors merge) · dashed red = rejected (cycle). Seven colors coalesce into one → total 15 (same as Prim).

Kruskal's cost is the sort

250K edges: full Kruskal 133ms, but union-find pass alone <2ms. The sort is 98%.

Union-find is (almost) free

Takeaway

An algorithm = a strategy (cut property) + a data structure (union-find). The data structure's quality IS the algorithm's cost. Next: strongly connected components — back to DFS, where the tier began.