Disjoint-Set Data Structure — Definition, Formula & Example
A disjoint-set data structure, also called union-find, keeps track of elements split into non-overlapping groups. It answers two questions quickly: which group an element is in, and what happens when two groups are merged.
A disjoint-set data structure maintains a partition of a finite set into pairwise disjoint subsets. Each subset is identified by one of its members, called its representative. It supports three operations. creates the singleton . returns the representative of the subset containing . replaces the subsets containing and with their union.
Key Formula
Where:
- = Total worst-case time for a sequence of operations, using both union by rank and path compression
- = Number of MakeSet, Find, and Union operations performed
- = Number of elements (MakeSet operations)
- = Inverse Ackermann function. It grows so slowly that it is at most 4 for any input size used in practice.
How It Works
Each group is stored as a rooted tree. Every element points to a parent, and the root of each tree is the group's representative, so its parent is itself. Find follows parent pointers up to the root. Union finds both roots and makes one root the child of the other. Two heuristics keep the trees shallow. Union by size (or rank) attaches the smaller tree under the larger one. Path compression makes every node visited during a Find point directly to the root.
Worked Example
Problem: Six computers, labeled 1 through 6, start unconnected. Cables are added in this order: Union(1,2), Union(3,4), Union(2,4), Union(5,6). Use union by size, and on a tie let the first argument's root stay the root. Can computer 4 reach computer 5? How many separate networks remain?
Step 1: Start with parent[i] = i and size 1 for each computer. Union(1,2) makes 1 the parent of 2. Union(3,4) makes 3 the parent of 4. Union(5,6), the last cable, will do the same for 6 under 5, and it does not affect the other trees. Each new tree has size 2.
Step 2: For Union(2,4), first find the roots: Find(2) = 1 and Find(4) = 3. Both trees have size 2, a tie, so root 3 goes under root 1. The merged tree has size 4.
Step 3: Find(4) follows 4 → 3 → 1, so its root is 1. Path compression then sets parent[4] = 1. Find(5) returns 5. The roots differ, so 4 and 5 are in different groups. Before compression, the tallest tree had height 2, which is within the union-by-size bound for a tree of 4 nodes.
Answer: Computer 4 cannot reach computer 5. Each of the 4 cables joined two different networks, so 6 − 4 = 2 networks remain: {1, 2, 3, 4} and {5, 6}.
Why It Matters
Kruskal's algorithm for minimum spanning trees uses union-find to reject any edge whose endpoints already share a root, which would create a cycle. The same structure labels connected regions in image processing and runs percolation simulations. It is a standard topic in college data structures and algorithms courses.
Common Mistakes
Mistake: Testing whether two elements are in the same set by comparing their parents, as in parent[x] == parent[y].
Correction: Elements in the same tree can have different parents. In the example, parent[2] = 1 and parent[4] = 3, yet 2 and 4 are in the same set. Always compare roots: Find(x) == Find(y).
