Disjoint Sts Data Strutur (Chap. 21) A disjoint-st is a olltion ={S 1, S 2,, S k } o distint dynami sts. Eah st is idntiid by a mmbr o th st, alld rprsntativ. Disjoint st oprations: MAKE-SET(x): rat a nw st with only x. assum x is not alrady in som othr st. UNION(x,y): ombin th two sts ontaining x and y into on nw st. A nw rprsntativ is sltd. FIND-SET(x): rturn th rprsntativ o th st ontaining x. Linkd-List Implmntation Eah st as a linkd-list, with had and tail, and ah nod ontains valu, nxt nod pointr and bak-to-rprsntativ pointr. Exampl: MAKE-SET osts O(1): just rat a singl lmnt list. FIND-SET osts O(1): just rturn bak-torprsntativ pointr. Multipl Oprations Linkd-lists or two sts Suppos multipl oprations: n: #MAKE-SET oprations (xutd at bginning). m: #MAKE-SET, UNION, FIND-SET oprations. m n, #UNION opration is at most n-1. St {,h,} had tail St {, g} had tail h g UNION o two Sts had g h tail An Appliation o Disjoint-St Dtrmin th onntd omponnts o an undirtd graph. CONNECTED-COMPONENTS(G) 1. or ah vrtx v V[G] 2. do MAKE-SET(v) 3. or ah dg (u,v) E[G] 4. do i FIND-SET(u) FIND-SET(v) 5. thn UNION(u,v) SAME-COMPONENT(u,v) 1. i FIND-SET(u)=FIND-SET(v) 2. thn rturn TRUE 3. ls rturn FALSE UNION Implmntation A simpl implmntation: UNION(x,y) just appnds x to th nd o y, updats all bak-to-rprsntativ pointrs in x to th had o y. Eah UNION taks tim linar in th x s lngth. Suppos n MAKE-SET(x i ) oprations (O(1) ah) ollowd by n-1 UNION UNION(x 1, x 2 ), O(1), UNION(x 2, x 3 ), O(2),.. UNION(x n-1, x n ), O(n-1) Th UNIONs ost 1+2+ +n-1=θ(n 2 ) So 2n-1 oprations ost Θ(n 2 ), avrag Θ(n) ah. Not good!! How to solv it??? 1
Wightd-Union Huristi Instad appnding x to y, appnding th shortr list to th longr list. Assoiatd a lngth with ah list, whih indiats how many lmnts in th list. Rsult: a squn o m MAKE-SET, UNION, FIND-SET oprations, n o whih ar MAKE-SET oprations, th running tim is O(m+nlg n). Why??? Hints: Count th numbr o updats to bak-to-rprsntativ pointr or any x in a st o n lmnts. Considr that ah tim, th UNION will at last doubl th lngth o unitd st, it will tak at most lg n UNIONS to unit n lmnts. So ah x s bak-to-rprsntativ pointr an b updatd at most lg n tims. Union by Rank & Path Comprssion Union by Rank: Eah nod is assoiatd with a rank, whih is th uppr bound on th hight o th nod (i.., th hight o subtr rootd at th nod), thn whn UNION, lt th root with smallr rank point to th root with largr rank. Path Comprssion: usd in FIND-SET(x) opration, mak ah nod in th path rom x to th root dirtly point to th root. Thus rdu th tr hight. Disjoint-st Implmntation: Forsts Path Comprssion Rootd trs, ah tr is a st, root is th rprsntativ. Eah nod points to its parnt. Root points to itsl. d d h d St {,h,} St {,d} h d UNION Straightorward Solution Thr oprations MAKE-SET(x): rat a tr ontaining x. O(1) FIND-SET(x): ollow th hain o parnt pointrs until to th root. O(hight o x s tr) UNION(x,y): lt th root o on tr point to th root o th othr. O(1) It is possibl that n-1 UNIONs rsults in a tr o hight n-1. (just a linar hain o n nods). So n FIND-SET oprations will ost O(n 2 ). Algorithm or Disjoint-St Forst UNION(x,y) 1. LINK(FIND-SET(x),FIND-SET(y)) MAKE-SET(x) 1. p[x] x 2. rank[x] 0 LINK(x,y) 1. i rank[x]>rank[y] 2. thn p[y] x 3. ls p[x] y 4. i rank[x]=rank[y] 5. thn rank[y]++ FIND-SET(x) 1. i x p[x] 2. thn p[x] FIND-SET(p[x]) 3. rturn p[x] Worst as running tim or m MAKE-SET, UNION, FIND-SET oprations is: O(mα(n)) whr α(n) 4. So narly linar in m. 2
Analysis o Union by Rank with Path Comprssion (by amortizd analysis) Disuss th ollowing: A vry quikly growing untion and its vry slowly growing invrs Proprtis o Ranks Proving tim bound o O(mα(n)) whr α(n) is a vry slowly growing untion. How Quik A k (j) Inras Lt us s A k (1): or k=0,1,2,3,4. A 0 (1)=1+1=2 A 1 (1)=2.1+1=3 A 2 (1)=2 1+1 (1+1)-1=7 A 3 (1)=A 2 (1+1) (1)=A 2 (2) (1)=A 2 (A 2 (1))=A 2 (7)=2 7+1 (7+1)- 1=2 8.8-1=2047 A 4 (1)=A 32 (1)=A 3 (A 3 (1)) =A 3 (2047)=A 2 (2048) (2047) >> A 2 (2047) =2 2048.2048-1 >2 2048 =(2 4 ) 512 =(16) 512 >>10 80. (stimatd numbr o atoms in univrs) A vry quikly growing untion and its invrs For intgrs k 0 and j 1, din A k (j): A k (j)= j+1 i k=0 A k-1 (j+1) (j) i k 1 Whr A k-10 (j)=j, A k-1 (i) (j)= A k-1 (A k-1 (i-1) (j)) or i 1. k is alld th lvl o th untion and i in th abov is alld itrations. A k (j) stritly inras with both j and k. Lt us s how quik th inras is!! Invrs o A k (n):α(n) α(n)=min{k: A k (1) n} (so, A α(n) (1) n ) α(n)= 0 or 0 n 2 1 n =3 2 or 4 n 7 3 or 8 n 2047 4 or 2048 n A 4 (1). Extrmly slow inrasing untion. α(n) 4 or all pratial purposs. Quiknss o Funtion A k (j) s Inras Lmma 21.2 (Pag 510): For any intgr j, A 1 (j) =2j+1. Proo: By indution on i, prov A 0i (j) =j+i. So A 1 (j)= A 0 (j+1) (j) =j+(j+1)=2j+1. Lmma 21.3 (Pag 510): For any intgr j, A 2 (j) =2 j+1 (j+1)-1. Proo: By indution on i, prov A 1i (j) =2 i (j+1)-1 A 2 (j)= A 1 (j+1) (j) = 2 j+1 (j+1)-1. O(mα(n)) bound: Proprty o Ranks Lmma 21.4 (pag 511): For all nods x, rank[x] rank[p[x]], with strit inquality i x p[x]. Corollary 21.5 (pag 511): As w ollow th path rom any nod to th root, th nod ranks stritly inras. Lmma 21.6 (pag 512): Evry nod had rank at most n-1. Proo: rank bgins with 0, inras possibly with only LINK oprations, whih is at most n-1 tim. In at, at most log(n). 3
O(mα(n)) bound proo Using amortizd analysis (Chap. 17) Using LINK instad UNION (vry UNION is don by two FIND-SETs and on LINK) Lmma 21.7 (pag 512): Suppos onvrting a squn S' o m' MAKE-SET, UNION, and FIND-SET oprations into a squn S o m MAKE-SET, LINK, FIND-SET by turning UNION to two FIND-SETs and on LINK, thn i S runs in O(mα(n)), thn S' runs in O(m'α(n)). Proo: baus o m' m 3m', thus m=o(m'). Rlations among rank[p[x]], lvl(x) and itr(x) Sin rank[p[x]] monotonially inras ovr tim, in ordr or itr(x) to dras, lvl(x) must inras. Or say anothr way, as long as lvl(x) rmains unhangd, itr(x) must ithr inras or rmains unhangd. Potntial Funtion For ah nod x, assign a potntial untion φ q (x) atr q oprations. Thn potntial or ntir orst, Φ q = x φ q (x) Φ 0 =0 at th bginning. Φ q will nvr b ngativ. φ q (x)= α(n) rank[x] i x is a root or rank[x]=0. [α(n)-lvl(x)] rank[x]-itr(x) othrwis. Proprtis or Potntial Funtion φ q (x) Lmma 21.8 (pag 514): For vry nod x, and or all q, 0 φ q (x) α(n) rank[x] Proo: i x is a root or rank[x]=0, thn orrt by dinition. Suppos x is not a root and rank[x]>0, φ q (x)= [α(n)-lvl(x)] rank[x]-itr(x) (α(n)-(α(n)-1)) rank[x]-rank[x] =rank[x]-rank[x]=0. φ q (x)= [α(n)-lvl(x)] rank[x]-itr(x) [α(n)-0] rank[x]-1= α(n) rank[x]-1<α(n) rank[x] lvl(x) and itr(x) lvl(x)=max{k: rank[p[x]] A k (rank[x])} 0 Lvl(x)<α(n), sin rank[p[x]] rank[x]+1=a 0 (rank[x]) and A α(n) (rank[x]) A α(n) (1) n > rank[p[x]]. itr(x)=max{i: rank[p[x]] A lvl(x) (i) (rank[x])} 1 itr(x) rank[x], sin rank[p[x]] A lvl(x) (rank[x])=a lvl(x) (1) (rank[x]) and A lvl(x) (rank[x]+1) (rank[x])=a lvl(x)+1 (rank[x])>rank[p[x]]. Potntial Changs o Oprations Lmma 21.9 (pag 515): Lt x b a nod that is not a root, and suppos qth opration is ithr LINK or FIND-SET. Thn atr th qth opration, φ q (x) φ q-1 (x). Morovr, i rank[x] 1 and ithr lvl[x] or itr(x) hangs du to th qth opration, thn φ q (x) φ q-1 (x)-1. Proo: x not root rank[x] not hang n not hang α(n) not hang. I rank[x]=0, thn φ q (x) = φ q-1 (x)=0. suppos rank[x]>0. I lvl(x) not hang, I itr(x) not hang, φ q (x) = φ q-1 (x), sin all kp sam I itr(x) inras, thn at las by 1, φ q (x) will dras at last 1. I lvl(x) inrass (at last by 1), thn (α(n)-lvl(x)) rank[x] drops at last by rank[x]. Suppos itr(x) drops, thn, th drop is at most rank[x]-1. so φ q (x) will drop at last rank[x]-(rank[x]-1)=1. Thus φ q (x) φ q-1 (x)-1. 4
Amortizd Costs o Oprations Lmma 21.10 (pag 515): Th amortizd ost o ah MAKE-SET opration is O(1). Proo: rat a singl nod x with rank 0, so φ q (x) =0. no othr hang to th orst, so Φ q= Φ q-1. Th lt is th atual ost, whih is O(1). Proo o Lmma 21.12 (ont.) Proo that at las max(0,s-(α(n)+2)) nods on th ind path hav thir potntial dras by at last 1. Lt x b a nod on th ind path: rank[x]>0, ollowd somwhr by y that is not a root, and lvl(y)=lvl(x) just bor FIND-SET. At most α(n)+2 nods do not satisy: 1th nod, root nod, th last nod w or whih lvl(w)=0,1,, α(n)-1. Thus at last max(0,s-(α(n)+2)) nods satisy. Lt us ix x, show x s potntial drass by at last 1. Amortizd Costs o Oprations (ont.) Lmma 21.11 (pag 515): Th amortizd ost o ah LINK opration is O(α(n)). Proo: (LINK(x,y) maks y th parnt o x). Atual ost or LINK opration is O(1). Considring potntial hang: Thr kinds o nods: x, y, and th old hildrn o y. By Lmma 21.9, th potntial o y s old hildrn not inras. For x (hangd to non-root rom a root), φ q (x)= [α(n)-lvl(x)] rank[x]- itr(x) [α(n)-0] rank[x]-1= α(n) rank[x]-1<α(n) rank[x]=φ q-1 (x). For y, rank[y] may stay sam or inras by 1, so φ q (y)=α(n) rank[y]=φ q- 1(y) or φ q-1 (y)+α(n). Thus th potntial inras du to th LINK opration is at most α(n). Thus th amortizd ost is O(1)+O(α(n))=O(α(n)) Proo o Lmma 21.12 (ont.) Lt k=lvl(x)=lvl(y), Just prior to path omprssion ausd by FIND-SET, w hav rank[p[x]] A k (itr(x)) (rank[x]) (by itr(x) s d.) rank[p[y]] A k (rank[y]) (by lvl(y) s d.) rank[y] rank[p[x]] (sin y ollows x somwhr). Lt i=itr(x) bor path omprssion, w hav rank[p[y]] A k (rank[y]) A k (rank[p[x]]) (sin A k (j) is stritly inrasing) A k (A k (itr(x)) (rank[x])) =A k (i+1) (rank[x]) Atr path omprssion, rank[p[x]]=rank[p[y]], whih not dras, and rank[x] not hang, so rank[p[x]] A k (i+1) (rank[x]). Whih mans that ithr itr(x) inrass (to at last i+1), or lvl(x) to inras. Thus by Lmma 21.9, φ q (x) φ q-1 (x)-1. that is x s potntial drass by at last 1. As a rsult, w prov th lmma 21.12. Amortizd Costs o Oprations (ont.) Lmma 21.12 (pag 516): Th amortizd ost o ah FIND-SET opration is O(α(n)). Proo: suppos thr ar s nods in th ind path. Th atual ost o FIND-SET is O(s). Root s potntial dos not hang and no othr nod s potntial inrass (by Lmma 21.9). At last max(0,s-(α(n)+2)) nods on th ind path hav thir potntial dras by at last 1. Thus th amortizd ost is at most O(s)-(s-(α(n)+2)) =O(α(n)). Uppr bound or Disjoint-sts Thorm 21.13 (pag 517): A squn o m MAKE-SET, UNION, FIND- SET oprations, n o whih ar MAKE-SET oprations, an b prormd on a disjoint-st orst with union by rank and path omprssion in worst ast tim O(mα(n)). 5
Summary Disjoint st Thr oprations Dirnt implmntations and dirnt osts Forst implmntation: Union by rank and path omprssion Proprtis: rank, lvl, itr. Amortizd analysis o th oprations: Potntial untion. A k (j) union: A k (j)= j+1 i k=0 A (j+1) k-1 (j) i k 1 Whr A k-10 (j)=j, A (i) k-1 (j)= A k-1 (A (i-1) k-1 (j)) or i 1. k is alld th lvl o th untion and i in th abov is alld itrations. α(n)=min{k: A k (1) n} A typial xampl using Disjoint St Kruskal's algorithm (Minimum Spanning Tr) sort th dgs o G in inrasing ordr by lngth kp a subgraph S o G, initially mpty or ah dg in sortd ordr i th ndpoints o ar disonntd in S add to S rturn S Not: grdy algorithm Analysis: Th tsting whthr two ndpoints ar disonntd looks lik it should b slow (linar tim pr itration, or O(mn) total). in at, onstant tim. 6