Mathwords logoMathwords

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 SS into pairwise disjoint subsets. Each subset is identified by one of its members, called its representative. It supports three operations. MakeSet(x)\text{MakeSet}(x) creates the singleton {x}\{x\}. Find(x)\text{Find}(x) returns the representative of the subset containing xx. Union(x,y)\text{Union}(x, y) replaces the subsets containing xx and yy with their union.

Key Formula

T(m,n)=O(m α(n))T(m, n) = O\big(m\,\alpha(n)\big)
Where:
  • T(m,n)T(m, n) = Total worst-case time for a sequence of operations, using both union by rank and path compression
  • mm = Number of MakeSet, Find, and Union operations performed
  • nn = Number of elements (MakeSet operations)
  • α(n)\alpha(n) = 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.
parent[2]=1,parent[4]=3,parent[6]=5\text{parent}[2] = 1,\quad \text{parent}[4] = 3,\quad \text{parent}[6] = 5
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.
parent[3]=1,size[1]=2+2=4\text{parent}[3] = 1,\quad \text{size}[1] = 2 + 2 = 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 ⌊log⁡24⌋=2\lfloor \log_2 4 \rfloor = 2 for a tree of 4 nodes.
Find(4)=1≠5=Find(5)\text{Find}(4) = 1 \neq 5 = \text{Find}(5)
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).

Related Terms

  • Disjoint Sets — Defines the non-overlapping groups this structure stores
  • Set — Basic object that each tree in the structure represents
  • Discrete Mathematics — Field where partitions and graph connectivity are studied
  • Kruskal's Algorithm — Uses union-find to detect cycles efficiently