Disjoint Set Union (DSU): Union by Rank & Path Compression
“Partition representation, tree rank heuristics, path compression mechanics, and the inverse Ackermann bound O(alpha(n)).”
1.The Disjoint Set ADT & Representation
§ 5.1.A Disjoint Set Union (DSU) data structure maintains a collection S = {S_1, S_2, ..., S_k} of disjoint dynamic sets.
Each set is identified by a canonical representative (the root of a tree).
Core Operations:
1. Make-Set(x): Initializes {x}.
2. Find-Set(x): Returns the representative of the set containing x.
3. Union(x, y): Merges the sets containing x and y.
2.The Two Synergistic Optimizations
§ 5.2.1. Union by Rank:
Each node stores a rank (an upper bound on its height).
When unioning two sets, make the root with smaller rank point to the root with larger rank. If both ranks are equal, pick either root and increment its rank by 1.
This guarantees that tree depth never exceeds O(log n).
2. Path Compression:
During Find-Set(x), update the parent pointer of every node visited along the path to point directly to the root.
Combined Complexity:
Combining Union by Rank with Path Compression yields an amortized time of O(alpha(n)) per operation, where alpha(n) is the extremely slow-growing Inverse Ackermann function.
3. Worked Problem: Rank Invariant & Union Sequence
Rank increases if and only if both roots being unioned have identical rank.
Problem: Elements {1, 2, 3, 4, 5, 6, 7, 8} are initialized using Make-Set (rank 0 for all). The following sequence of Union operations is performed using Union by Rank: Union(1, 2), Union(3, 4), Union(5, 6), Union(7, 8), Union(1, 3), Union(5, 7), Union(1, 5). What is the final rank of the root node?
Step 1: Pairwise unions at rank 0:
- Union(1, 2): rank(1)=0, rank(2)=0. Equal! Root = 1, rank(1) = 0 + 1 = 1.
- Union(3, 4): rank(3)=0, rank(4)=0. Equal! Root = 3, rank(3) = 1.
- Union(5, 6): rank(5)=0, rank(6)=0. Equal! Root = 5, rank(5) = 1.
- Union(7, 8): rank(7)=0, rank(8)=0. Equal! Root = 7, rank(7) = 1.
Step 2: Second-level unions at rank 1:
- Union(1, 3): rank(1)=1, rank(3)=1. Equal! Root = 1, rank(1) = 1 + 1 = 2.
- Union(5, 7): rank(5)=1, rank(7)=1. Equal! Root = 5, rank(5) = 1 + 1 = 2.
Step 3: Final union at rank 2:
- Union(1, 5): rank(1)=2, rank(5)=2. Equal! Root = 1, rank(1) = 2 + 1 = 3.
Final Answer: The final root has a rank of 3.
Worked example: union by rank keeps trees short
A tree's rank increases only when two trees of equal rank are joined.
A tree of rank r has at least 2^r elements, by induction: joining two rank r − 1 trees doubles the minimum size.
2^r ≤ 16, so r ≤ 4, and height equals rank without path compression.
Practice: test yourself
Original practice questions in the GATE style, with worked explanations. Not past-paper questions.
Ten elements start as singletons. After union(1,2), union(3,4), union(2,4), union(5,6) and union(1,3), how many sets are there?
With union by rank and path compression, the amortised cost of an operation is:
Kruskal's algorithm uses union-find to:
Primary Academic References & Exam Sources
Every concept in Notes From a B.Tech Brain is synthesized from authoritative academic texts, seminal peer-reviewed research, verified exam archives, and renowned university lectures.
Curated for clarity, rigorous numerical solving, and semester excellence:
Visual thinkers who want to see each algorithm step animated clearly.
GATE-targeted coverage with step-by-step GATE PYQ walkthroughs.