CS2351 Data Structures Tutorial 3: Data Structures or Disjoint Sets 1
Aout this lecture Data Structure or Disjoint Sets Support Union and Find operations Various Methods: 1. Union y Size 2. Union y Rank 3. Union y Rank + Path Compression 2
Maintainin Disjoint Set In some applications, especially in alorithms relatin to raphs, we oten have a set o elements, and want to maintain a dynamic partition o them I.e., the partition chanes over time Our taret corresponds to maintainin dynamic disjoint sets o the elements 3
Maintainin Disjoint Set Let Σ = { S 1, S 2,, S k } e a collection o dynamic disjoint sets o the elements Let x and y e any two elements We want to support: Make-Set(x): create a set containin x Find(x) : return which set x elons Union(x,y) : mere the sets containin x and containin y into one 4
Example Application: Findin Connected Components Step 0: Bein with the input raph a e h c d 5
Example Application: Findin Connected Components Step 1: Make-Set(v) or each vertex v a e h c d current Σ: { {a}, {}, {c}, {d}, {e}, {}, {}, {h} } 6
Example Application: Findin Connected Components Step 2: Visit each ede (u,v), perorm Union(u,v) a e h c d current Σ: { {a}, {}, {c}, {d}, {e}, {}, {}, {h} } 7
Step 2: Visit (a,) a e h ede visited c d current Σ: { {a,}, {c}, {d}, {e}, {}, {}, {h} } Step 2: Visit (c,d) a e h c d current Σ: { {a,}, {c,d}, {e}, {}, {}, {h} } 8
Step 2: Visit (e,) a e h ede visited c d current Σ: { {a,}, {c,d}, {e,}, {}, {h} } Step 2: Visit (,c) a e h c d current Σ: { {a,,c,d}, {e,}, {}, {h} } 9
Step 2: Visit (,) a e h ede visited c d current Σ: { {a,,c,d}, {e,,}, {h} } Step 2: Visit (,d) a e h c d current Σ: { {a,,c,d}, {e,,}, {h} } 10
Example Application: Findin Connected Components Ater Step 2 (when all edes visited) : Each Disjoint Set Connected Component a e h c d current Σ: { {a,,c,d}, {e,,}, {h} } 11
Remarks To acilitate Find(x), each set usually chooses one o its element as a representative Find(x) returns the representative element o the set where x elons To check i x and y elon to the same set, we can just check i Find(x) == Find(y) 12
Disjoint-Set Forest One popular method to maintain disjoint sets is y a orest Each set a separate rooted tree Representative root o tree 13
Example Current dynamic sets : { {a,,c,d}, {e,,}, {h} } h a c d e 14
Disjoint-Set Forest To perorm Union(x,y), we join the trees containin x and containin y, y linkin their roots E.. Union(,h) in previous example ives: h e 15
Disjoint-Set Forest Let H max = max heiht o all trees In the worst-case: Make-Set : Find or Union : Θ(1) time Ο(H max ) time m operations on n elements : worst-case Θ(mn) time 16
Union By Size Let us apply a union-y-size heuristic : To perorm Union, we link root o the smaller tree to root o the larer tree H max = Ο(lo n) (how to prove??) m operations : Θ(m lo n) time 17
Union By Rank A similar heuristic is called union-y-rank Each node keeps track o its rank an upper ound on the heiht o the node In a sinle-node tree (created y Make-Set) rank o root = 0 To perorm Union, we link root with smaller rank to root with larer rank 18
Union By Rank Rank needs not e very accurate as lon as it always ives an upper ound o heiht is enouh When Union is perormed, only the rank o the roots may chane : I oth roots have same rank rank o new root increases y 1 Else, no chane 19
Example o Union y Rank Beore Union Ater Union(c,) 1 1 2 0 0 0 0 0 a c d e 1 0 0 0 a c d 0 e 0? = rank 20
Union By Rank Let H max = max heiht o all trees H max = Ο(lo n) (how to prove??) m operations : Θ(m lo n) time So, union y rank is no etter than union y size, ut 21
Path Compression The closer a node to its root, the aster the Find or Union operation When we perorm Find(x), we will need to ind the root o the tree containin x will access every ancestor o x why don t we make all these ancestors o x closer to the root now? ( Because no increase in asymptotic perormance!!! ) 22
Example o Path Compression Beore Find(x) Ater Find(x) w v u x w v u x 23
Union y Rank + Path Compression I Union(x,y) is always perormed y irst Find(x), Find(y), and then linkin the roots, then y cominin union-y-rank (at Union) and path compression (at Find and Union) : m operations: Θ(m α(n)) time Inverse Ackermann (in practice, at most 4) 24
Findin Connected Components Recall: To ind connected components o a raph G with n vertices and m edes there are n Make-Set and m Find or Union operations Which scheme or dynamic disjoint sets ives the est runnin time (theoretically)? Ans. Depends on m (why?) 25