GraphsAnimated
Union-Find (Disjoint Set Union)
Track groups with parent pointers, path compression and union by rank.
Time
O(α(n)) per op
Extra space
O(n)
Use it for
Connectivity, Kruskal, clustering.
How it works
- Each element starts in its own group; parent[i] points to the group’s representative (itself at first).
- find(x) follows parents up to the representative. Path compression makes later finds faster by pointing everything straight at the root.
- union(a, b) joins the two groups by making one root the parent of the other (attach the smaller tree under the larger: "union by size").
- Operations take almost constant time, O(α(n)).
Watch it run
Loading animation…