DAA - Unit 2 (Disjoint Sets)
DAA - Unit 2 (Disjoint Sets)
Tree representation:
make one element of the set
as the parent others as
children and the links are
from children to parent.
Data representation:
Each root has a pointer to the
set name, to determine which
set an element is currently in,
we follow parent links to the
root of the tree to set name.
Operations on Disjoint sets
Start with q elements in a set of its own ( i.e. Si ={ i }, 1 ≤ i ≤ q), then the
initial configuration consists of a forest with q nodes, and
p[ i ] = -1, 1 ≤ i ≤ q.
Problem1 on Union and Find Cont…
Operation Parent array representation Tree representation
Initial
Union(1,2):
p[ 1 ] = 2
Union(2,3):
p[ 2 ] = 3
1 n n
2 n-1 n-1
n 1 1
Since the time required to process a find for
an element at level i of a tree is O(i).
The total time needed to
Note: i is level no and i in O(i) is no. of levels process n finds is
traversed.
Improving performance of Union and Find Algorithm
Performance of union and find algorithms can be improved by avoiding degenerate trees.
Here we are trying to improve performance of Find algorithm which is O(i) for a Find
operation, where “i” is number of levels traversed.
By decreasing the number of levels of a tree while doing Union operation we can improve
performance of Find algorithm. To decrease number of levels, weighting rule is used.
i 1 2 3 4 ... n
Initial
p -1 -1 -1 -1 ... -1
Note: All nodes other than the roots of trees have a positive number in the
parent array.
Here, -1 indicates it is a root node and the number of nodes in the tree is 1.
Problem 2: Weighted Union Cont…
Operation WeightedUnion(i,j) Parent array tree
Initial i 1 2 3 4 5 6 7 8
p -1 -1 -1 -1 -1 -1 -1 -1
Problem 3: Weighted Union Cont…
Operation WeightedUnion( i, j ) parent array tree
Union(1,2) temp:=p[1]+p[2] // -2
if(p[1] > p[2]) // false
else
p[2] := 1; p[1] := -2
Union(3,4) temp:=p[3]+p[4] // -2
if(p[3] > p[4]) // false
else
p[4] := 3; p[3] := -2
Union(7,8) temp:=p[7]+p[8] // -2
if(p[7] > p[8]) //false
else
p[8]:=7; p[7]:=-2
Problem 3: Weighted Union Cont…
Operation WeightedUnion( i , j ) Parent array tree
temp:=p[5] + p[7] // -4
if(p[5] > p[7]) // false
Union(5,7) else
p[7] := 5; p[5] := -4
temp:=p[1]+p[5] // -8
if(p[1] > p[5]) // false
Union(1,5) else
p[5] := 1; p[1] := -8
Prob 3: Case 2- Performance analysis of find operation
Height of the tree with m nodes is
Therefore the time required to process a find operation is
O(log m) (refer note)
Input tree
Output tree
Problem 4: CollapsingFind Algorithm
Consider the tree created by WeightedUnion on the sequence of problem 3.
Now process the following eight finds
Find(8), Find(8), . . . , Find(8)
Algorithm operation CollapsingFind
Algorithm r:=8;
while( p[r] > 0 ) do r := p[r]; // 3 links
CollapsingFind(i) // p[ 8 ] >0 r := p[ 8 ] := 7;
{ // p[ 7 ] > 0 r := p[ 7 ] := 5;
r:= i; // p[ 5 ] > 0 r := p[ 5 ] := 1;
// Find root node, Find(8) // p[ 1 ] > 0 false root is 1
while( i ≠ r) do // 8 ≠ 1 // 3 links
// with SimpleFind
s := p[ i ]; p[i] := r; i := s; Note: To find the root of 8,
while( p[r]>0 ) do r:=p[r]; // s:=p[ 8 ]; p[8]:=1; i:=7; i ≠ r 7≠1
//collapsing nodes //s:=p[ 7 ]; p[7]:=1; i:=5 i ≠ r 5≠1 going up 3 links and resetting
while( i ≠ r ) do // s:=p[ 5 ]; p[5]:=1; i:=1 i ≠ r false to root node 3 links. total:6 links
{
s := p[i]; r := 8; Note: To find the root of 8,
p[i] := r; Find(8) while( p[r] > 0 ) do r := p[r] // 1
going up only one link.
i := s; link
// p[8] > 0 r := p[8] := 1 Remaining Find(8) oprations
} // p[1] > 0 // false will take same i.e., only 1 link.
} // while loop is repeated once
// to reset 8 to root node1 - 1 link
Problem 4: CollapsingFind for first Find(8) (6 links)
Input Output
Parent array
Tree
Performance of CollapsingFind
Consider the tree created by WeightedUnion on sequence of Problem 3.
Now process the following eight finds
Find(8), Find(8), . . . , Find(8)