3.5.15Graphs

Disjoint Set Union (Union-Find) — path compression + union by rank → α(n)

2,018 words9 min readdifficulty · medium4 backlinks

WHAT is it?


WHY do the two optimizations exist?


HOW: deriving the structure from scratch

Step 1 — find with path compression

Step 2 — union by rank

Step 3 — combine → α(n)

Figure — Disjoint Set Union (Union-Find) — path compression + union by rank → α(n)

Worked example (full trace)


Common mistakes


Recall Feynman: explain to a 12-year-old

Every kid belongs to a team, and each team has one captain. To find your captain you ask "who's your boss?" and keep going up until someone says "I'm the boss" — that's the captain. Trick 1 (path compression): on the way up, everyone you pass writes down the captain's name directly, so next time they answer instantly. Trick 2 (union by rank): when two teams merge, the smaller team's captain salutes the bigger team's captain, so chains never get long. Do both and asking "who's your captain?" is basically free — like, never more than 4 questions even for billions of kids.


Flashcards

What does parent[x] == x signify in DSU?
x is the root / representative of its set.
What is path compression?
During find, repoint visited nodes directly (or via grandparent) toward the root so future finds are faster.
What is union by rank?
Hang the tree with smaller rank under the one with larger rank; on a tie, attach either way and increment the winner's rank.
Why is rank only an upper bound, not true height?
Path compression flattens trees, so real height ≤ stored rank; we never update rank afterward.
Minimum number of nodes in a tree of rank r?
2r2^r (proved by induction: a rank rises only when two equal-rank roots merge).
Max rank / height with union by rank alone?
log2n\le \log_2 n, since a rank-r tree has 2r\ge 2^r nodes.
Amortized time per op with both optimizations?
O(α(n))O(\alpha(n)), inverse Ackermann ≤ 4 in practice.
Why early-return when find(x) == find(y) in union?
They're already in the same set; merging would corrupt parent pointers / rank.
Is union by size equivalent to union by rank?
Yes, both give O(α(n))O(\alpha(n)) with path compression; size hangs fewer-node tree under larger.
What does α(n) ≤ 4 mean practically?
For any realistic n, each operation costs effectively constant time.

Connections

  • Kruskal's Minimum Spanning Tree — uses DSU to detect cycles when adding edges.
  • Connected Components — DSU answers "same component?" online.
  • Trees and Forests — each set is a rooted tree; the collection is a forest.
  • Amortized Analysis — α(n) bound comes from a potential-function argument.
  • Ackermann Function — its inverse defines the complexity.
  • Graph Cycle Detection — union creating same-root ⇒ cycle in undirected graph.

Concept Map

stores sets as

root is

supports

supports

compares roots for

can build

makes find

fixed by

fixed by

flattens during

hangs shorter under taller in

uses

together give

together give

is

Disjoint Set Union

Rooted Trees

Representative

find x

union x y

same x y

Naive Union

Height n Chain

O of n per op

Path Compression

Union by Rank

rank as height bound

alpha n

Inverse Ackermann ≤ 4

Hinglish (regional understanding)

Intuition Hinglish mein samjho

DSU yaani Union-Find ka kaam simple hai: tumhare paas kuch groups hain, aur har group ka ek captain (root) hota hai. find(x) poochta hai "tumhara captain kaun hai?" aur union(x,y) do groups ko jodta hai. Pure structure ko ek parent[] array se store karte hain, jahan root apne aap ko hi point karta hai (parent[root]==root).

Bina optimization ke yeh ek lambi chain ban sakti hai — phir har find mein O(n)O(n) steps lagte hain, bahut slow. Isko theek karne ke do tricks hain. Pehla, union by rank: jab do trees merge karo, toh chhoti (kam height wali) tree ko badi tree ke neeche lagao. Isse height kabhi badhti nahi, sirf tie hone par 1 badhti hai. Isse proof nikalta hai ki rank rr wale tree mein kam se kam 2r2^r nodes hote hain, matlab height log2n\le \log_2 n.

Doosra trick path compression: jab find chalao, toh raste mein jitne nodes aaye sabko seedha root pe point kara do. Agli baar wahi find ek hi hop mein ho jayega. Dono tricks saath lagao toh per-operation cost gir kar α(n) (inverse Ackermann) ho jaati hai — jo practically 4 se kabhi zyada nahi hoti, yaani basically constant time.

Yeh matter isliye karta hai kyunki DSU Kruskal MST, connected components, aur cycle detection jaise problems ka backbone hai. Interview aur competitive programming mein yeh ek must-know hai. Yaad rakhne ka mantra: "Find pe compress karo, Union mein rank dekho."

Go deeper — visual, from zero

Test yourself — Graphs

Connections