3.5.15 · HinglishGraphs

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

1,878 words9 min readRead in English

3.5.15 · Coding › Graphs


YEH HAI KYA?


DO OPTIMIZATIONS KYUN HAIN?


KAISE: Structure scratch se derive karna

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: 12-saal ke bachche ko samjhao

Har bachcha ek team ka hissa hai, aur har team ka ek captain hota hai. Apna captain dhundhne ke liye tum poochho "tera boss kaun hai?" aur tab tak upar jaate raho jab tak koi nahi bole "main boss hoon" — wahi captain hai. Trick 1 (path compression): upar jaate waqt, jitne log milte hain woh seedha captain ka naam likh lete hain, toh agli baar instantly jawaab dete hain. Trick 2 (union by rank): jab do teams merge hon, choti team ka captain badi team ke captain ko salute karta hai, toh chains kabhi lambi nahi hoti. Dono karo aur "tera captain kaun hai?" practically free ho jaata hai — matlab, billions of kids ke liye bhi kabhi 4 se zyada sawaal nahi.


Flashcards

DSU mein parent[x] == x ka kya matlab hai?
x apne set ka root / representative hai.
Path compression kya hai?
find ke dauran, visited nodes ko seedha (ya grandparent ke zariye) root ki taraf repoint karo taaki future finds fast hon.
Union by rank kya hai?
Chote rank wale tree ko bade rank wale ke neeche lagao; tie pe kisi bhi taraf attach karo aur winner ka rank increment karo.
Rank sirf upper bound kyun hai, true height nahi?
Path compression trees ko flatten karta hai, toh real height ≤ stored rank hoti hai; hum rank baad mein update nahi karte.
Rank r wale tree mein minimum kitne nodes hote hain?
(induction se proved: rank tabhi badhta hai jab do equal-rank roots merge hote hain).
Sirf union by rank se max rank / height kitna hoga?
, kyunki rank-r tree mein nodes hote hain.
Dono optimizations ke saath amortized time per op?
, inverse Ackermann jo practice mein ≤ 4 hai.
Union mein find(x) == find(y) hone par early-return kyun karte hain?
Woh already ek hi set mein hain; merge karna parent pointers / rank corrupt kar dega.
Union by size aur union by rank equivalent hain?
Haan, dono path compression ke saath dete hain; size chote-node tree ko bade ke neeche laata hai.
Practically α(n) ≤ 4 ka matlab kya hai?
Kisi bhi realistic n ke liye, har operation effectively constant time leta hai.

Connections

  • Kruskal's Minimum Spanning Tree — edges add karte waqt cycles detect karne ke liye DSU use karta hai.
  • Connected Components — DSU online "same component?" answer karta hai.
  • Trees and Forests — har set ek rooted tree hai; collection ek forest hai.
  • Amortized Analysis — α(n) bound ek potential-function argument se aata hai.
  • Ackermann Function — uska inverse complexity define karta hai.
  • Graph Cycle Detection — union mein same root banna ⇒ undirected graph mein cycle.

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