Disjoint Set Union Simulator
Disjoint Set Union—also called Union-Find—maintains a collection of non-overlapping sets. It answers which component an element belongs to and efficiently merges two components using path compression and union by rank.
Make SetO(1)
FindAmortized O(α(n))
UnionAmortized O(α(n))
OptimizationsRank + compression
Union-Find operations
Example Union-Find forest loaded. Choose an operation.
Elements0
Components0
Maximum depth0
Parent rewrites0
Find path
—
Last operation
—
Current components
Parent, rank, and size table
| Element | Parent | Representative | Rank | Component size |
|---|
Optimized operations
find(x):
if parent[x] != x:
parent[x] = find(parent[x])
return parent[x]
union(a, b):
rootA = find(a)
rootB = find(b)
attach lower-rank root
below higher-rank root
if ranks are equal:
choose one root
increase its rankUnion-Find concepts
Disjoint components
Every element belongs to exactly one component. Each component is represented by a rooted parent tree.
Path compression
Find rewrites parent links along the search path to point directly to the representative, flattening future searches.
Union by rank
The lower-rank root is attached below the higher-rank root. Equal ranks require one rank increment.
Typical applications
Union-Find is used in Kruskal's minimum spanning tree algorithm, connectivity queries, image segmentation, and dynamic grouping.
Terminology
| Term | Meaning |
|---|---|
| Representative | The root element that identifies a component. |
| Parent | The next element followed toward the representative. |
| Rank | An upper-bound estimate of tree height used for merging. |
| Component size | The number of elements represented by a root. |
| α(n) | The inverse Ackermann function, which grows extremely slowly. |