mediumGraphUnion FindGraphs

Kruskal's MST

mediumTime: O(E log E)Space: O(V)

Signals to notice

build MST by sorting all edgesadd edges that don't create cyclesunion-find for cycle detection

Brute force first

Try all possible edge subsets that form a spanning tree — exponential. That direct path helps you understand the question, but it tends to treat every possibility as brand new instead of learning from earlier steps.

The key insight

Kruskal's: sort all edges by weight. Process edges cheapest first — add an edge if it connects two different components (Union-Find check). Stop after V-1 edges. Instead of recomputing the world every time, you preserve just enough context to let the next decision become obvious.

What must stay true

The cheapest edge that connects two different components is always safe to add to the MST (cut property). Union-Find efficiently checks and merges components. As long as that statement keeps holding, you can trust the steps built on top of it.

Easy way to go wrong

Not stopping after V-1 edges — a spanning tree of V vertices has exactly V-1 edges. Processing more is wasted work. Most mistakes here are not about syntax; they come from losing track of what your state, pointer, or structure is supposed to mean.

Graphs Pattern