← All Tools

Kruskal's Minimum Spanning Tree Visualizer

Kruskal's algorithm sorts every edge by weight, then walks the list adding each edge to the growing forest — but only if its two endpoints belong to different components. Cycle-checking is done in near-constant time using a union-find (disjoint-set) structure with path compression and union by rank. After |V|−1 accepted edges the forest becomes a single spanning tree of minimum total weight. Watch the DSU roots merge and the sorted edge list get consumed step by step.

Run stats

Vertices |V|0
Edges |E|0
Edges considered0
Edges accepted0
Edges rejected (cycle)0
Union operations0
MST weight0
Components remaining0

Disjoint-set forest (components)

Sorted edge list

Trace

accepted (MST)   current   rejected (cycle)

Why Kruskal is correct — the cut property

Every time Kruskal accepts an edge e = (u, v), that edge is the lightest one crossing the cut that separates u's component from every other vertex. The cut property says: for any cut, the minimum-weight edge crossing it belongs to some MST. So accepting e can never be a mistake. Sorted-edge order plus union-find gives us O(|E| log |E|) — dominated by the sort — and the DSU operations amortise to nearly O(|E| α(|V|)), where α is the inverse Ackermann function (effectively constant for any conceivable input).

When the graph is disconnected, Kruskal still terminates cleanly — you get a minimum spanning forest, one MST per connected component. That's why real-world tools like tc traffic-control classes, network-latency clustering, and cluster hierarchical-linkage (single-linkage clustering is literally MST plus cut) can use Kruskal on graphs without a global connectivity guarantee.

Kruskal vs Prim

  Kruskal Prim
Grows bylightest global edge that doesn't cyclelightest edge leaving current tree
Core data structureunion-find (DSU)min-priority queue
ComplexityO(|E| log |E|)O((|V|+|E|) log |V|)
Better onsparse graphs / edge-list inputdense graphs / adjacency-matrix input
Disconnected graphgives minimum spanning forest naturallyneeds a wrapper loop, one Prim per component