Disjoint Sets Data Structure ! A dynamic collection S = {S 1 ,S 2 ,…,S k } of disjoint sets. ! Each set S i is identified by a Data Structures for representative member. Disjoint Sets ! Operations: – Make-Set(x): create a new set in S, whose only member is x (assuming x is not already in one of the sets). – Union(x, y): replace the two sets S x and S y that contain x and y, by their union (assuming they are disjoint). – Find-Set(x): find and return the representative of the set containing x. 1 2 Application: connected Example components ! Given a graph G=(V,E) compute its partitioning into connected components. ! S = {{a},{b},{c},{d},{e},{f},{g},{h},{i},{j}} Connected-Components(G=(V,E)): ! S = {{a},{b,d},{c},{e},{f},{g},{h},{i},{j}} for each vertex v in V Make-Set(v); ! S = {{a},{b,d},{c},{e,g},{f},{h},{i},{j}} for each edge e in E if (Find-Set(u) != Find-Set(v)) ! S = {{a,c},{b,d},{e,g},{f},{h},{i},{j}} Union(u, v); ! S = {{a,c},{b,d},{e,g},{f},{h,i},{j}} Same-Component(u, v): ! S = {{a,c,b,d},{e,g},{f},{h,i},{j}} return (Find-Set(u) == Find-Set(v)); ! S = {{a,c,b,d},{e,f,g},{h,i},{j}} ! S = {{a,c,b,d},{e,f,g},{h,i},{j}} 3 4
A linked lists Example representation ! S={S 1 , S 2 }, S 1 ={c,h,e,b}, S 2 ={f,g,d} ! Each set in the collection is represented by a linked list. ! First element in each list is the representative of the set. ! Each element holds a pointer to the representative. ! Make-Set and Find-Set take O(1) time. ! The result of Union(e,g): ! Union of two sets S 1 and S 2 takes time O(min(|S 1 |,|S 2 |)) using the weighted-union heuristic . 5 6 Analysis Disjoint-Set Forests ! Amortized analysis ! Each set is a represented by a tree, and the representative is the root. ! Theorem (22.1): Using the linked-list representation of disjoint sets and the ! Each element points to its parent in weighted-union heuristic, a sequence the tree (the root points to itself). of m MakeSet, Union, and FindSet operations, n of which are MakeSet ( ) operations, takes O m + n lg n time. ! Proof idea: show that the pointer to ! How long do the different operations representative cannot be updated take? more than times. lg n – Make-Set – Find-Set – Union 7 8
Example Heuristics ! S={S 1 , S 2 }, S 1 ={c,h,e,b}, S 2 ={f,g,d} ! Union by rank: always make the root of the smaller tree become the child of the root of the larger tree. ! Path compression: during a Find-Set operation, make each node on the find path point directly to the root. ! The result of Union(e,g): 9 10 Pseudocode Path Compression makeSet(x) { x.parent = x; x.rank = 0; ! Before: } findSet(x) { if (x != x.parent) x.parent = findSet(x.parent); return x.parent; } Union(x, y) { Link(findSet(x), findSet(y)); } ! After performing Find-Set(a): Link(x, y) { if (x.rank > y.rank) y.parent = x; else { x.parent = y; if (x.rank == y.rank) y.rank++; } 11 12 }
Analysis Analysis ! A very quickly growing function: ! Theorem: A sequence of m MakeSet, Union, and FindSet operations, n of 1 if 0 j + k = which are FindSet operations, can be = ( ) A j performed on a disjoint-set forest with k ( j + 1) ( ) A j if k ≥ 1 union by rank and path compression in k − 1 ( ) worst case time O m α ( ) n ! 80 ! For example, A 4 (1) 10 ! For all practical purposes this time is linear in m, so each operation has constant amortized cost. ! A very slowly growing inverse: { } ( ) α ( ) n = min k A : 1 ≥ n k ! For all practical purposes: α ( ) n ≤ 4 13 14
Recommend
More recommend