DATA STRUCTURES/Student Engineering Notebook
Page 10·notes
HASHING, COLLISION DYNAMICS & DISJOINT SETS—~32 MIN READ

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

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

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.

WORKED EXAM ANALYSIS · STEP-BY-STEP DERIVATION

3. Worked Problem: Rank Invariant & Union Sequence

⚠️ Pitfall Warning:

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 NUMERICAL · STUDENT DERIVATION✎ Key Takeaway: Add path compression and each operation costs O(α(n)) amortised, where α is the inverse Ackermann function: effectively constant.

Worked example: union by rank keeps trees short

Problem Statement:
Using union by rank only (no path compression), what is the greatest possible height of a tree containing 16 elements?
Step 1: What rank means

A tree's rank increases only when two trees of equal rank are joined.

Step 2: The size argument

A tree of rank r has at least 2^r elements, by induction: joining two rank r − 1 trees doubles the minimum size.

Step 3: Apply it to n = 16

2^r ≤ 16, so r ≤ 4, and height equals rank without path compression.

r ≤ log₂ 16
↳ Result: 4
Final Answer:Height at most 4 (= log₂ 16)
STUDENT PRACTICE · CONCEPT DRILL

Practice: test yourself

Original practice questions in the GATE style, with worked explanations. Not past-paper questions.

Problem 1 · GATE-LEVEL

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?

Problem 2 · TRICKY

With union by rank and path compression, the amortised cost of an operation is:

Problem 3 · FOUNDATION

Kruskal's algorithm uses union-find to:

SOURCE TRANSPARENCY & CITATIONS

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 University Lecture Series
Introduction to Algorithms (4th Edition) ·
LECTURE SERIES
RECOMMENDED YOUTUBE EDUCATORS & FULL-COURSE PLAYLISTS

Curated for clarity, rigorous numerical solving, and semester excellence:

Abdul Bari · Data Structures & Algorithms↗Instructor: Abdul Bari · Covers: Trees, graphs, hashing, sorting — with visual algorithm animations

Visual thinkers who want to see each algorithm step animated clearly.

Open YouTube↗
Gate Smashers · Data Structures Full Course↗Instructor: Varun Singla · Covers: Arrays, linked lists, trees, BST, AVL, heaps, hashing — GATE numericals

GATE-targeted coverage with step-by-step GATE PYQ walkthroughs.

Open YouTube↗