# 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.

## Presentation on theme: "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."— Presentation transcript:

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.

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.

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

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 248 5 28 4 5 8 42

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? Val:15755772 Idx:12345678 7 36 5 24 8 1

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? Val:15755772 Idx:12345678

Union Operation Given two values, we must 1 st 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. 5 241 1 5 24 8 8 5 24 8 17 36

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: Val:15751772 Idx:12345678 Val:55755772 Idx:12345678 Val:15755772 Idx:12345678

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 5 24 8 1 52 4 8 Path Compression Val:15751772 Idx:12345678 Val:15751771 Idx:12345678 Val:11751772 Idx:12345678

Similar presentations