1 / 10

Disjoint Sets

Disjoint Sets. CS2 -- 9/8/2010. Disjoint Sets. A disjoint set contains a set of sets such that in each set, an element is designated as a marker for the set. A simple Disjoint Set: {1}, {2}, {3}, {4}, {5} There is only one marker for each of these sets. The element itself.

jaxon
Télécharger la présentation

Disjoint Sets

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Disjoint Sets CS2 -- 9/8/2010

  2. Disjoint Sets • A disjoint set contains a set of sets such that in each set, an element is designated as a marker for the set. • A simple Disjoint Set: {1}, {2}, {3}, {4}, {5} • There is only one marker for each of these sets. • The element itself.

  3. Disjoint Sets • Given the original Disjoint Set: {1}, {2}, {3}, {4}, {5} • Union(1,3) would make our structure look like: {1,3}, {2}, {4}, {5} • we can choose 1 or 3 as the marker for the set. Let’s choose 1. • Union(1,4): {1,3,4}, {2}, {5} • Union(2,5): {1,3,4}, {2, 5} • where we choose 2 as the marker.

  4. Disjoint Sets • Given the last Disjoint Set: {1,3,4}, {2, 5} • We can do a findsetoperation. • findset(3) should return 1, since 1 is the marked element in the set.

  5. Disjoint Set Implementation • A set with disjoint sets can be represented in several ways. • Given {2,4,5,8} • with 5 as the marked element. • Here are a few ways it could be stored: 5 5 5 2 4 8 2 8 8 4 2 4

  6. Disjoint Set Implementation • We can also store a disjoint set in an array. • Given: {2,4,5,8} , {1} , {3,6,7} • Could be stored as: • The 5 stored in array[2], signifies that 5 is 2’s parent. • The 2 in array[8] signifies that 2 is 8’s parent, etc. • The 5 in array[5] signifies that 5 is a marker for its set. • Based on this storage scheme, how could we implement the initial makeset algorithm and how could we implement a findset algorithm? 7 1 5 2 4 3 6 8

  7. Disjoint Set Implementation • We can also store a disjoint set in an array. • Given: {2,4,5,8} , {1} , {3,6,7} • Could be stored as: • The 5 stored in array[2], signifies that 5 is 2’s parent. • The 2 in array[8] signifies that 2 is 8’s parent, etc. • The 5 in array[5] signifies that 5 is a marker for its set. • Based on this storage scheme, how could we implement the initial makeset algorithm and how could we implement a findset algorithm?

  8. Union Operation • Given two values, we must 1st find the markers for those two values, then merge those two trees into one. • Given the Disjoint Set from before: • If we perform union(5,1)we could do either of the following: • We prefer the right one, since it minimizes the height of the tree. So we should probably keep track of the height of our tree to do our merges efficiently. 1 5 7 2 4 3 6 8 1 5 5 2 4 1 2 4 8 8

  9. Union Operation • Minimizing the height of the tree • We choose which tree to merge with based on which tree has a small height. • If they are equal we are forced to add 1 to the height of the new tree. • Given the Disjoint set from before: • We have 2 options: Option 1: Option 2:

  10. Path Compression • One last enhancement! Every time we are forced to do a findset operation, we can directly connect each node on the path from the original node to the root. • First, we find the root of this tree which is 1. • Then you go through the path again, starting at 8, changing the parent of each of the nodes on that path to 1. • Then you take the 2 that was previously stored in index 8, and then change the value in that index to 1: 1 1 Path Compression 5 2 8 5 2 4 4 8

More Related