0% found this document useful (0 votes)
15 views26 pages

DAA - Unit 2 (Disjoint Sets)

The document discusses disjoint sets and their representation using tree and parent array structures, along with the Union and Find algorithms. It highlights the performance of these algorithms, particularly focusing on improving efficiency through techniques like the weighting rule and collapsing find. The document also presents various problems and examples to illustrate the concepts and performance comparisons of different union and find methods.

Uploaded by

kolluriravi901
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views26 pages

DAA - Unit 2 (Disjoint Sets)

The document discusses disjoint sets and their representation using tree and parent array structures, along with the Union and Find algorithms. It highlights the performance of these algorithms, particularly focusing on improving efficiency through techniques like the weighting rule and collapsing find. The document also presents various problems and examples to illustrate the concepts and performance comparisons of different union and find methods.

Uploaded by

kolluriravi901
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

UNIT 2: DISJOINT SETS

CO2:Illustrating Divide and Conquer Design


Paradigm algorithms.
Disjoint Set Representation
Operations:
▪ Union Algorithm
▪ Find Algorithm
▪ Dr. K Madhavi
Disjoint Sets
Disjoint Set definition: The two sets Si and Sj , i ≠ j are
disjoint if there is no element that is in both Si and Sj .
● For Ex: S1 = {1,7,8,9}, S2 = {2,5,10} and S3 = {3,4,6} are
three pair wise disjoint sets.
Disjoint Set Representation:
1. Tree representation
2. Parent array
3. Data representation
Disjoint Set Representation
Disjoint Set Representation: for sets S1 = {1,7,8,9} , S2 = {2,5,10} , S3 = {3,4,6}

Tree representation:
make one element of the set
as the parent others as
children and the links are
from children to parent.

Parent array representation:


Array element represents the
parent pointer of the
corresponding tree node.,
root nodes have a parent of -1

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

Note: sets S1 = {1,7,8,9} , S2 = {2,5,10} , S3 = {3,4,6}


Find operation is used to determine the set of the element. To accomplish this , it
should traverse from the element to the root (using tree or parent array representation)
and then use the data representation to find the set name.
For Ex: Find(9): p[9] is 1→ p[1] is -1, so 1 is the root element, root 1` is pointing to the
set name S1
Union Operation
Sets: S1 = {1,7,8,9} , S2 = {2,5,10} , S3 = {3,4,6}
S1 U S2 = {1,2,5,7,8,9,10}, In tree representation, make one of
the tree as sub tree of another.
After performing union
Union and Find Algorithms

For the algorithm Union( i , j ), we Algorithm Find( i ) starts at i and


pass in two trees with roots i and j. traverse until it reaches a node
The first tree becomes the sub tree with parent value -1.
of the second, p[ i ] = j, where p is
a parent array.
Problem1 on Union and Find algorithms

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

Union(n-1,n): Degenerate tree


p[ n-1 ] = n (Straight line tree)
Problem1 on Union and Find Cont…
After performing the following sequence of operations:

Parent array is generated, and the tree is the tree representation of


parent array.

Now perform next sequence of operations:


operation Find(1) Find(2) Find(n)

Traverse: (until p[ i ]=-1 i.e. P[1] →2 P[2] →3 P[n] is -1


traversing a sequence of P[2] →3 P[3] →4
parent pointers from the
element to be found to the p[n-1]-->n p[n-1]-->n
root) P[n] is -1 P[n] is -1

No. of levels traversed n n-1 1


(or no. of comparisons) to
reach root node.
Performance of Union Algorithm
In the following sequence,n-1 union operations are performed on n
elements, A degenerate tree is generated.

The time taken for a union


operation is constant, i.e., O(1)

(n-1) union operations are


processed in time O(n-1). i.e., O(n)
Performance of Find Algorithm
Perform the following find operations on the tree generated by n-1 union operations
No. of
element level levels
(i) no traversed

1 n n

2 n-1 n-1

Each find operation requires traversing


sequence of parent pointers from the element
to be found to the root node. n-1 2 2

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.

To avoid degenerate trees, we use weighting rule for Union(i , j).


Any algorithm that completes in constant time is the best algorithm. So, we cannot
improve performance of SimpleUnion algorithm.

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.

Weighting rule for Union(i , j):


If the number of nodes in the tree with root i is less than the number of nodes in the tree
with root j, then make parent of i as j i.e., p[ i ] = j, otherwise make parent of j as i i.e., p[ j ]
= i.
Problem 2 : Weighted Union Algorithm
Start with n elements in a set of its own (that is, Si ={ i }, 1≤ i ≤ n), then the initial
configuration consists of a forest with n trees, and p[i] = -1, 1≤ i ≤ n.

Perform the sequence of union operations using weighting rule:


Union(1,2) , Union(1,3) , … , Union(1,n)

parent array tree

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 // p[ i ] := -count[ i ] i 1 2 3 4 ... n


// p[ j ] := -count[ j ]
// in comment p -1 -1 -1 -1 ... -1

Union(1,2) temp := p[1]+p[2] // -2


if(p[1] > p[2]) // false i 1 2 3 4 ... n
else
p -2 1 -1 -1 ... -1
p[2] := 1; p[1] := -2;

Union(1,3) temp := p[1]+p[3] // -3


i 1 2 3 4 ... n
if(p[1] > p[2]) // false
else p -3 1 1 -1 ... -1
p[3] := 1; p[1] := -3;

... ... ... ...

Union(1,n) temp := p[1]+p[n] // -n


i 1 2 3 4 ... n
if(p[1] > p[n]) // false
else p -n 1 1` 1 ... 1
p[n]:=1; p[1]:= -n;
Comparing performances of
SimpleUnion, WeightedUnion Algorithms

● Number of steps to process a Union operation using


SimpleUnion is 1 (constant no. of steps).
Ex :SimpleUnion(1,2)
p[1]:=2 // →step 1
● Number of steps to process a Union operation using
WeightedUnion is 4 (constant no. of steps).
Ex: WeightedUnion(1, 2)
temp := p[1]+p[2] //-2 →step 1
if( p[1] > p[2]) // false →step 2

else // no step count, condition is checked for if


The time required to perform a WeightedUnion
p[2]:=1; //→step 3 operation is increased when compared to
p[1]:=-2 // →step 4 SimpleUnion but still bounded by a constant.
Note: Irrespective of the values of i and j, both the
i.e., O(1)
algorithms are taking constant amount of time to
Prob 2: Case 1 - Performance of Find algorithm after WeightedUnion
Height of the tree is 2. Therefore the time required
to process a find operation is O(2) which is a
constant i.e., O(1). (refer note)

For n Find operations it takes 2n steps. So the time


required is O(2n). i.e, O(n).

Performance of Find algorithm is increased from


O(n2) to O(n) after applying weighting rule to Union
algorithm.

But, getting two level trees is the best case. So, we


have to analyze for worst case.

Note : The time required to process a Find for an


element at level i of a tree is O(i).Time complexity
for n Find operations is O(n2)
Problem 3: Weighted Union Algorithm

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

Initial //p[ i ] := -count [ I ]


// p[ j ] := -count [ j ]
comment

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(5,6) temp:= p[5]+p[6] // -2


if(p[5] > p[6]) // false
else
p[6] := 5; p[5] := -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

After performing Union(1,2),


Union(3,4), Union(5,6), Union(7,8)

temp :=p[1] + p[3] // -4


if(p[1] > p[3]) // false
Union(1,3) else
p[3]:=1; p[1]:=-4

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)

If there are n-1 union operations and n find operations to be


processed in a sequence then the time taken to process is
O(n + n logn) i.e., O(n logn)

Height of the tree is decreased due to weighting rule, so the


performance of Find algorithm is increased.(from O(n2) to
O(n logn)

● For n Find operations


before applying weighting rule is O(n2)
After applying weighting rule
Best case is O(n) → case 1
Worst case is O(nlogn) → case 2

Further improvement is possible by modifying Find


algorithm using collapsing rule.

Note : the time required to process a find for an element at


level i of a tree is O(i).
Performance comparison of SimpleFind

Time complexity of SimpleFind after O(n2)


SimpleUnion for n find operations
(problem1)
Best case O(n)
Time complexity of (problem2)
SimpleFind after (case1)
WeightedUnion for n
find operations Worst case O(nlogn)
(problem3)
(case2)
Collapsing Find Algorithm

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)

You might also like