Disjoint Union / Find Equivalence Relations Equivalence Classes
Total Page:16
File Type:pdf, Size:1020Kb
Equivalence Relations Disjoint Union / Find • A relation R is defined on set S if for CSE 326 every pair of elements a, b∈S, a R b is Data Structures either true or false. Unit 13 • An equivalence relation is a relation R that satisfies the 3 properties: › Reflexive: a R a for all a∈S Reading: Chapter 8 › Symmetric: a R b iff b R a; for all a,b∈S › Transitive: a R b and b R c implies a R c 2 Dynamic Equivalence Equivalence Classes Problem • Given an equivalence relation R, decide • Starting with each element in a singleton set, whether a pair of elements a,b∈S is and an equivalence relation, build the equivalence classes such that a R b. • Requires two operations: • The equivalence class of an element a › Find the equivalence class (set) of a given is the subset of S of all elements element › Union of two sets related to a. • It is a dynamic (on-line) problem because the • Equivalence classes are disjoint sets sets change during the operations and Find must be able to cope! 3 4 Disjoint Union - Find Union • Maintain a set of disjoint sets. • Union(x,y) – take the union of two sets › {3,5,7} , {4,2,8}, {9}, {1,6} named x and y • Each set has a unique name, one of its › {3,5,7} , {4,2,8}, {9}, {1,6} members › Union(5,1) › {3,5,7} , {4,2,8}, {9}, {1,6} {3,5,7,1,6}, {4,2,8}, {9}, 5 6 Find An Application • Find(x) – return the name of the set • Build a random maze by erasing edges. containing x. › {3,5,7,1,6}, {4,2,8}, {9}, › Find(1) = 5 › Find(4) = 8 7 8 An Application (ct’d) An Application (ct’d) • Pick Start and End • Repeatedly pick random edges to delete. Start Start End End 9 10 Desired Properties A Cycle (we don’t want that) • None of the boundary is deleted • Every cell is reachable from every other Start cell. • There are no cycles – no cell can reach itself by a path unless it retraces some part of the path. End 11 12 Good Solution : A Hidden A Good Solution Tree Start Start End End 13 14 Number the Cells Basic Algorithm We have disjoint sets S ={ {1}, {2}, {3}, {4},… {36} } each cell is unto itself. We have all possible edges E ={ (1,2), (1,7), (2,8), (2,3), … } 60 edges total. • S = set of sets of connected cells • E = set of edges Start 1 2 3 4 5 6 While there is more than one set in S pick a random edge (x,y) 7 8 9 10 11 12 u := Find(x); v := Find(y); if u ≠ v then 13 14 15 16 17 18 Union(u,v) //knock down the wall between the cells (cells in 19 20 21 22 23 24 . Remove (x,y) from E //the same set are connected) 25 26 27 28 29 30 • If u=v there is already a path between x and y • All remaining members of E form the maze 31 32 33 34 35 36 End 15 16 Example Step Example S S Pick (8,14) S {1,2,7,8,9,13,19} {1,2,7,8,9,13,19,14,20 26,27} {1,2,7,8,9,13,19} {3} Find(8) = 7 {3} {3} {4} Find(14) = 20 {4} Start 1 2 3 4 5 6 {4} {5} {5} {5} {6} {6} 7 8 9 10 11 12 {6} Union(7,20) {10} {10} {10} 13 14 15 16 17 18 {11,17} {11,17} {11,17} {12} {12} {12} 19 20 21 22 23 24 {14,20,26,27} {15,16,21} {14,20,26,27} {15,16,21} . 25 26 27 28 29 30 {15,16,21} . End . {22,23,24,29,39,32 31 32 33 34 35 36 . {22,23,24,29,39,32 33,34,35,36} {22,23,24,29,30,32 33,34,35,36} 33,34,35,36} 17 18 Example Example at the End Pick (19,20) S S {1,2,7,8,9,13,19 {1,2,3,4,5,6,7,… 36} 14,20,26,27} Start 1 2 3 4 5 6 {3} Start 1 2 3 4 5 6 {4} E 7 8 9 10 11 12 {5} 7 8 9 10 11 12 {6} 13 14 15 16 17 18 13 14 15 16 17 18 {10} 19 20 21 22 23 24 {11,17} 19 20 21 22 23 24 {12} 25 26 27 28 29 30 {15,16,21} 25 26 27 28 29 30 . 31 32 33 34 35 36 End . 31 32 33 34 35 36 End {22,23,24,29,39,32 33,34,35,36} 19 20 Up-Tree for DU/F Find Operation Initial state 1 2 3 4 5 6 7 • Find(x) follow x to the root and return the root Intermediate 1 3 7 1 3 7 state 2 5 4 2 5 4 Roots are the names of each set. 6 Find(6) = 7 6 21 22 Union Operation Simple Implementation • Union(i,j) - assuming i and j roots, point i • Array of indices (Up[i] is parent of i) Up [x] = 0 means to j. 1 2 3 4 5 6 7 x is a root. up 0 1 0 7 7 5 0 Union(1,7) 1 3 7 1 3 7 2 5 4 2 5 4 6 6 23 24 Union Find • Design Find operator Union(up[] : integer array, x,y : integer) : { //precondition: x and y are roots// › Recursive version Up[x] := y › Iterative version } Find(up[] : integer array, x : integer) : integer { //precondition: x is in the range 1 to size// Constant Time! ??? } 25 26 A Bad Case Weighted Union 1 2 3 … n • Weighted Union (weight = number of nodes) Union(1,2) … › Always point the smaller tree to the root of the 2 3 n larger tree 1 Union(2,3) W-Union(1,7) 3 … n : : 1 3 7 2 2 1 4 n Union(n-1,n) 1 2 5 4 3 2 Find(1) n steps!! 6 1 27 28 Example Again Analysis of Weighted Union 1 2 3 … n • With weighted union an up-tree of height h Union(1,2) has weight at least 2h. 2 3 … n • Proof by induction Union(2,3) 1 › Basis: h = 0. The up-tree has one node, 20 = 1 2 … n : : › Inductive step: Assume true for all h’ < h. 1 3 Union(n-1,n) h-1 T W(T1) > W(T2) > 2 2 Minimum weight Weighted Induction h-1 1 3 … n up-tree of height h union hypothesis Find(1) constant time T1 T formed by 2 W(T) > 2h-1 + 2h-1 = 2h weighted unions 29 30 Worst Case for Weighted Analysis of Weighted Union Union n/2 Weighted Unions • Let T be an up-tree of weight n formed by weighted union. Let h be its height. • n > 2h n/4 Weighted Unions • log2 n > h • Find(x) in tree T takes O(log n) time. • Can we do better? 31 32 Example of Worst Cast (cont’) Elegant Array Implementation After n -1 = n/2 + n/4 + …+ 1 Weighted Unions 1 3 7 2 1 4 2 5 4 log2n 6 1 2 3 4 5 6 7 0 1 0 7 7 5 0 Can save the extra Find up space by storing the If there are n = 2k nodes then the longest weight 2 1 4 complement of weight path from leaf to root has length k. in the space reserved for the root 33 34 Weighted Union Path Compression W-Union(i,j : index){ • On a Find operation point all the nodes on the //i and j are roots// wi := weight[i]; search path directly to the root. wj := weight[j]; 1 7 1 7 if wi < wj then up[i] := j; weight[j] := wi + wj; 2 5 4 PC-Find(3) 2 3 6 5 4 else up[j] :=i; weight[i] := wi +wj; 6 8 9 10 8 9 } 3 10 35 36 Self-Adjustment Works Path Compression Find PC-Find(i : index) { r := i; while up[r] ≠ 0 do //find root// r := up[r]; if i ≠ r then //compress path// PC-Find(x) k := up[i]; while k ≠ r do up[i] := r; i := k; x k := up[k] return(r) } 37 38 Disjoint Union / Find Example with Weighted Union and PC • Worst case time complexity for a W- 7 1 Union is O(1) and for a PC-Find is O(log n). 2 5 4 • Time complexity for m ≥ n operations on 6 8 9 n elements is O(m log* n) where log* n is a very slow growing function. i 3 10 › log * n < 7 for all reasonable n. Essentially constant time per operation! 39 40 Amortized Complexity Find Solutions Recursive • For disjoint union / find with weighted Find(up[] : integer array, x : integer) : integer { union and path compression. //precondition: x is in the range 1 to size// if up[x] = 0 then return x › average time per operation is essentially a else return Find(up,up[x]); } constant. Iterative › worst case time for a PC-Find is O(log n). Find(up[] : integer array, x : integer) : integer { //precondition: x is in the range 1 to size// • An individual operation can be costly, while up[x] ≠ 0 do but over time the average cost per x := up[x]; return x; operation is not.