data structures for disjoint set union find data
play

Data Structures for Disjoint Set Union-Find Data Structure Disjoint - PowerPoint PPT Presentation

Data Structures for Disjoint Set Union-Find Data Structure Disjoint Set Data Structure Disjoint Set Data Structure Storing a family of sets S = S 1 , S 2 , . . . , S k with i j S i S j = . Each set S i is


  1. Data Structures for Disjoint Set

  2. Union-Find Data Structure

  3. Disjoint Set Data Structure Disjoint Set Data Structure � � ◮ Storing a family of sets S = S 1 , S 2 , . . . , S k with i � j → S i ∩ S j = ∅ . ◮ Each set S i is identified by a representative s i ∈ S i . ◮ Three operations: Make-Set , Union , Find-Set Make-Set ( x ) ◮ Creates a new set { x } . (Clearly, x is representative of the set.) ◮ x cannot be in any other set already. Union ( x , y ) ◮ Merges the sets containing x and y into one set. Find-Set ( x ) ◮ Finds the representative of the set containing x . 3 / 17

  4. Implementation Idea ◮ Represent each set S i as rooted tree (i. e., S is a forest). ◮ Root of tree is representative � � Example: S = { b , c , e , h } , { d , f , д } c f h e d g b 4 / 17

  5. Implementation — Find-Set Find-Set ◮ Follow pointers to root. Example: Find ( b ) = c c f h e d g b 5 / 17

  6. Implementation — Find-Set 1 Procedure Find-Set ( x ) While par ( x ) � x 2 Let x : = par ( x ) . 3 Return x 4 6 / 17

  7. Implementation — Union Union ( x , y ) ◮ Find the representatives r x and r y of x and y (i. e., find roots of trees). ◮ Make r x parent of r y Example: Union( b , д ) c f h e d g b 7 / 17

  8. Implementation — Union 1 Procedure Union ( x , y ) Set par ( Find-Set ( x )) : = Find-Set ( y ) 2 8 / 17

  9. Implementation Questions ◮ What is the worst-case runtime for these operations? ◮ Can we improve the runtime? 9 / 17

  10. Implementation Questions ◮ What is the worst-case runtime for these operations? ◮ Can we improve the runtime? Example ◮ Assume that we perform Union ( 1 , 2 ) , Union ( 1 , 3 ) , Union ( 1 , 4 ) , . . . , Union ( 1 , n ) . ◮ Then, the runtime is in O ( n 2 ) . 9 / 17

  11. Improving Find-Set Observation ◮ If we use Find-Set multiple times on the same element, we have to search for the root each time again. Idea ◮ Update the parent pointer when calling Find-Set such that it points on the root. 1 Procedure Find-Set( x ) If par ( x ) � x Then 2 Set par ( x ) : = Find-Set ( par ( x )) 3 Return par ( x ) 4 10 / 17

  12. Improving Union Idea: Union by Rank ◮ Keep track of height of a tree. Number is denoted as rank of a vertex. ◮ Make root of smaller tree child of root of larger tree. 1 Procedure Make-Set( x ) par ( x ) : = x 2 rank ( x ) : = 0 3 Observation ◮ We only need to keep track of the rank of the root. 11 / 17

  13. Improving Union 1 Procedure Union ( x , y ) Let x : = Find-Set ( x ) . 2 Let y : = Find-Set ( y ) . 3 If rank ( x ) > rank ( y ) Then 4 Set par ( y ) : = x . 5 6 Else Set par ( x ) : = y . 7 If rank ( x ) = rank ( y ) Then 8 Set rank ( y ) : = rank ( y ) + 1 . 9 12 / 17

  14. Runtime Assume our sets contain n elements in total. Runtime ◮ Worst case for single operation: O ( log n ) (Why?) ◮ Worst case for m operations: O ( m · α ( n )) Thus, O ( α ( n )) amortised runtime per operation. α -Function ◮ Inverse Ackermann function ◮ α ( atoms in the universe ) ≤ 4 ◮ Grows extremely slow. However, it is strictly speaking not constant. 13 / 17

  15. Partition Refinement

  16. Partition Re fi nement Union-Find ◮ Start with a partition P = { S 1 , S 2 , . . . , S k } of a set S ◮ Step by step join two sets S i and S j together. � � ◮ Union( i , j ) : P : = P \ { S i , S j } ∪ { S i ∪ S j } Partition Re fi nement ◮ Start with a partition P = { S 1 , S 2 , . . . , S k } of a set S (often P = {S} ) ◮ Step by step, based on a set X ⊆ S , split subsets S i into S i \ X and S i ∩ X . ◮ Refine( X ) : P : = { S \ X , S ∩ X | S ∈ P } 15 / 17

  17. Implementation – Data Structure Data Structure ◮ Set S is an array storing all its elements. ◮ Partition P is a (doubly-linked) list of subsets S i ◮ Subset S i is represented by two integers which describe the interval (i. e., the first and last index) of S i in the array S S 1 S 2 S 3 0 | 2 3 | 5 6 | 7 P s 1 s 2 s 3 s 4 s 5 s 6 s 7 s 8 S 0 1 2 3 4 5 6 7 16 / 17

  18. Implementation – Re fi nement Re fi nement ◮ Flag all elements s i ∈ X . S 1 S 2 S 3 0 | 2 3 | 5 6 | 7 P s 1 s 2 s 3 s 4 s 5 s 6 s 7 s 8 S 0 1 2 3 4 5 6 7 17 / 17

  19. Implementation – Re fi nement Re fi nement ◮ Flag all elements s i ∈ X . ◮ For each subset S i ◮ Reorder such that fl agged elements are in front. S 1 S 2 S 3 0 | 2 3 | 5 6 | 7 P s 1 s 3 s 2 s 4 s 5 s 4 s 7 s 8 S 0 1 2 3 4 5 6 7 17 / 17

  20. Implementation – Re fi nement Re fi nement ◮ Flag all elements s i ∈ X . ◮ For each subset S i ◮ Reorder such that fl agged elements are in front. ◮ Split S i into two sets containing only fl agged or non- fl agged elements. S 1 S 2 S 3 S 4 S 5 0 | 1 2 | 2 3 | 4 5 | 5 6 | 7 P s 1 s 3 s 2 s 4 s 5 s 4 s 7 s 8 S 0 1 2 3 4 5 6 7 17 / 17

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend