Disjoint components
Every element belongs to exactly one component. Each component is represented by a rooted parent tree.
Disjoint Set Union—also called Union-Find—maintains a collection of non-overlapping sets. It efficiently answers which component an element belongs to and merges two components.
Example Union-Find forest loaded. Choose an operation.
| Element | Parent | Representative | Rank | Component size |
|---|
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
and increase its rankEvery element belongs to exactly one component. Each component is represented by a rooted parent tree.
Find rewrites every parent along the search path to point directly to the representative, flattening future searches.
The shorter estimated tree is attached below the taller one. This prevents unnecessary depth growth.
Union-Find is used in Kruskal's minimum spanning tree algorithm, connectivity queries, image segmentation, and dynamic grouping.
| 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. |