Sets and disjoint sets union123

1,099
-1

Published on

Published in: Education
1 Comment
3 Likes
Statistics
Notes
No Downloads
Views
Total Views
1,099
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
0
Comments
1
Likes
3
Embeds 0
No embeds

No notes for slide

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

×