Download presentation
Presentation is loading. Please wait.
Published byMyron Davidson Modified over 9 years ago
1
Set Representation S 1 ={0, 6, 7, 8}, S 2 ={1, 4, 9}, S 3 ={2, 3, 5} Two operations considered here Disjoint set union S 1 S 2 ={0,6,7,8,1,4,9} Find(i): Find the set containing the element i. 3 S 3, 8 S 1 0 6 7 8 1 4 9 2 3 5 S i S j =
2
Disjoint Set Union 1 4 9 0 6 7 8 1 4 9 0 6 7 8 Possible representation for S 1 union S 2 Make one of trees a subtree of the other S1 union S2 S2 union S1 OR
3
0 67 8 4 1 9 2 3 5 *Figure 5.41:Data Representation of S 1 S 2 and S 3 (p.240)
4
Array Representation for Set int find1(int i) { for (; parent[i]>=0; i=parent[i]); return i; } void union1(int i, int j) { parent[i]= j; }
5
n-1 n-2 0 *Figure 5.43:Degenerate tree (p.242) union operation O(n) n-1 find operation O(n 2 ) union(0,1), find(0) union(1,2), find(0). union(n-2,n-1),find(0) degenerate tree
6
*Figure 5.44:Trees obtained using the weighting rule(p.243) weighting rule for union(i,j): if # of nodes in i < # in j then j the parent of i
7
Modified Union Operation void union2(int i, int j) { int temp = parent[i]+parent[j]; if (parent[i]>parent[j]) { parent[i]=j; parent[j]=temp; } else { parent[j]=i; parent[i]=temp; } If the number of nodes in tree i is less than the number in tree j, then make j the parent of i; otherwise make i the parent of j. Keep a count in the root of tree i has fewer nodes. j has fewer nodes
8
Figure 5.45:Trees achieving worst case bound (p.245) log 2 8 +1
9
Modified Find(i) Operation int find2(int i) { int root, trail, lead; for (root=i; parent[root]>=0; root=parent[root]); for (trail=i; trail!=root; trail=lead) { lead = parent[trail]; parent[trail]= root; } return root: } If j is a node on the path from i to its root then make j a child of the root
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.