Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Upcoming SlideShare
×

# Sets and disjoint sets union123

3,074 views

Published on

Published in: Education
• Full Name
Comment goes here.

Are you sure you want to Yes No
Your message goes here
• nice, slick, and simple... liked the design.

Are you sure you want to  Yes  No
Your message goes here

### Sets and disjoint sets union123

1. 1. Sets And Disjoint Sets Union By: Ankita Goyal
2. 2. Definition <ul><li>Given a  set  of elements, it is often useful to break them up or  partition  them into a number of  separate, nonoverlapping sets . A  disjoint-set data structure  is a  data structure  that keeps track of such a partitioning. </li></ul>
3. 3. Operation we can perform: <ul><li>A  union-find algorithm  is an algorithm that performs two useful operations on such a data structure: </li></ul><ul><li>Find : Determine which set a particular element is in. Also useful for determining if two elements are in the same set. </li></ul><ul><li>Union : Combine or merge two sets into a single set. </li></ul>
4. 4. Possible representations of sets <ul><li>S1 S2 S3 </li></ul>1 7 8 9 5 2 10 3 4 6
5. 5. Possible Representations of S1 U S2 1 7 9 8 2 5 10 7 10 2 1 5 9 8 S1 ∪ S2 S1 ∪ S2
6. 6. Data representation for s1, s2 and s3 1 6 3 5 4 2 10 7 8 9 S1 S2 S3
7. 7. Array representation of S1, S2 and S3 i [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] p -1 5 -1 3 -1 3 1 1 1 5
8. 8. Simple algorithms for union and find <ul><li>Algorithm SimpleUnion(i,j) </li></ul><ul><li>{ </li></ul><ul><li>P[i]:=j; </li></ul><ul><li>Algorithm SimpleFind(i) </li></ul><ul><li>{ </li></ul><ul><li>While(p[i]>=0) do i:=p[i]; </li></ul><ul><li>Return i; </li></ul><ul><li>} </li></ul>
9. 9. Weighting rule for Union(i,j) <ul><li>If the number of nodes in the tree with root i is less than the number in the tree with root j, then make j the parent of i; otherwise make i the parent of j. </li></ul>
10. 10. Trees obtained using the weighted rule <ul><li>Initial union(1,2) union(1,3) </li></ul><ul><li>Union(1,4) union(1,n) </li></ul>1 2 n 3 2 1 5 4 n n 3 2 1 4 n 4 3 2 1 2 n 3 1
11. 11. Union algorithm with weighting rule <ul><li>Algorithm WeightedUnion(i,j) </li></ul><ul><li>// Union sets with roots I and j, i != j, using the </li></ul><ul><li>// weighting rule. p[i]= -count[i] and p[j]= -count[j] </li></ul><ul><li>{ </li></ul><ul><li>temp:=p[i]+p[j]; </li></ul><ul><li>if (p[i]>p[j]) then </li></ul><ul><li>{ // i has fewer nodes. </li></ul><ul><li>p[i]:=j; p[j]:=temp; </li></ul><ul><li>} </li></ul><ul><li>else </li></ul><ul><li>{ // j has fewer or equal nodes. </li></ul><ul><li>P[j]:=i; p[i]:=temp; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
12. 12. Thank You