Loading algorithms…
Loading visualizer…
Greedy minimum spanning tree using Union-Find for cycle detection — O(E log E).
Kruskal's algorithm builds a Minimum Spanning Tree (MST) by greedily adding the lightest edge that does not form a cycle. A spanning tree connects every node with the fewest possible edges (V−1), and "minimum" means the sum of edge weights is as small as possible.
The naive "check if adding this edge creates a cycle" test is expensive. Union-Find (also called Disjoint Set Union) keeps each node's current component and supports two operations in near-constant time: find(node) and union(a, b). With path compression and union by rank, both run in amortized O(α(n)) time, where α is the inverse Ackermann function (effectively a constant for any practical input).
💡 Cut property: For any cut of the graph (a partition of nodes into two non-empty sets), the minimum-weight edge crossing that cut belongs to some MST. Kruskal repeatedly exploits this cut property by always adding the lightest edge that bridges two distinct components.
Drag nodes to rearrange layout · Step through edges sorted by weight · Watch Union-Find track each component