0% found this document useful (0 votes)
19 views7 pages

Ada U2 Notes

lecture notes

Uploaded by

Ajay Bhoopal
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)
19 views7 pages

Ada U2 Notes

lecture notes

Uploaded by

Ajay Bhoopal
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/ 7

IT603PC: ALGORITHM DESIGN AND ANALYSIS

UNIT-II
Disjoint Sets: Disjoint set operations, union and find algorithms
Disjoint Sets: If Si and Sj, i≠j are two sets, then there is no element that is
in both Si and Sj.. For example: n=10 elements can be partitioned into three
disjoint sets,

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

Tree representation of sets:

Disjoint set Operations:

1. Disjoint set Union(i,j)


2. Find(i)

Disjoint set Union:

Means Combination of two disjoint sets elements.

From the above example S1 U S2 ={1,7,8,9,5,2,10 }

For S1 U S2 tree representation, simply make one of the tree is a subtree of


the other.
Find: Given element i, find the set containing i.

Form above example:

Find(4)->S3

Find(1)->S1

Find(10)->S2

Data representation of sets:

Tress can be accomplished easily if, with each set name, we keep a pointer
to the root of the tree representing that set.

For presenting the union and find algorithms, we ignore the set names and
identify sets just by the roots of the trees representing them.

For example: if we determine that element ‘i’ is in a tree with root ‘j’ has a
pointer to entry ‘k’ in the set name table, then the set name is just name[k]

For unite (adding or combine) to a particular set we use FindPointer


function.

Example: If you wish to unite to Si and Sj then we wish to unite the tree
with roots

FindPointer (Si) and FindPointer (Sj)

FindPointer is a function that takes a set name and determines the root of
the tree that represents it.

For determining operations:

Find(i)-1st determine the root of the tree and find its pointer to entry in
setname table.
Union(i, j)-Means union of two trees whose roots are i and j.

If set contains numbers 1 through n, we represents tree node

P[1:n].

n-Maximum number of elements.

Each node represent in array

i 1 2 3 4 5 6 7 8 9 10
P -1 5 -1 3 -1 3 1 1 1 5

Algorithm

Algorithm Simpleunion(i, j)

P[i]:=j; // Accomplishes the union

Find(i) by following the indices, starting at i until we reach a node with


parent value -1.

Example: Find(6) start at 6 and then moves to 6’s parent. Since P[3] is
negative, we reached the root.

Algorithm SimpleFind(i)

while(P[i]≥0) do i:=P[i];

return i;

If n numbers of roots are there then the above algorithms are not useful for
union and find.

For union of n trees-> Union(1,2), Union(2,3), Union(3,4),…..Union(n-1,n).

For Find i in n trees-> Find(1), Find(2),….Find(n).

Time taken for the union (simple union) is -> O(1) (constant).

For the n-1 unions -> O(n).


Time taken for the find for an element at level i of a tree is -> O(i).

For n finds -> O(n2).

To improve the performance of our union and find algorithms by avoiding


the creation of degenerate trees. For this we use a weighting rule for union(i,
j)

Weighting rule for Union(i, j):

If the number of nodes in the tree with root ‘i’ is less than the tree with root
‘j’, then make ‘j’ the parent of ‘i’; otherwise make ‘i’ the parent of ‘j’.

Algorithm for weightedUnion(i, j)

Algorithm WeightedUnion(i,j)

//Union sets with roots i and j, i≠j

// The weighting rule, p[i]= -count[i] and p[j]= -count[j].

temp := p[i]+p[j];

if (p[i]>p[j]) then

{ // i has fewer nodes.

P[i]:=j;

P[j]:=temp;

}
else

{ // j has fewer or equal nodes.

P[j] := i;

P[i] := temp;

For implementing the weighting rule, we need to know how many nodes
there are in every tree.
For this we maintain a count field in the root of every tree.
i->root node
count[i]->number of nodes in the tree.
Time required for this above algorithm is O(1) + time for remaining
unchanged is determined by using Lemma.

Lemma:- Let T be a tree with m nodes created as a result of a sequence


of unions each performed using WeightedUnion. The height of T is no
greater than |log2 m|+1.

Collapsing rule: If ‘j’ is a node on the path from ‘i’ to its root and p[i]≠root[i],
then set p[j] to root[i].

Algorithm for Collapsing find.

Algorithm CollapsingFind(i)

//Find the root of the tree containing element i.

//collapsing rule to collapse all nodes form i to the root.

r;=i;

while(p[r]>0) do r := p[r]; //Find the root.

While(i ≠ r) do // Collapse nodes from i to root r.

s:=p[i];

p[i]:=r;

i:=s;
}

return r;

Collapsing find algorithm is used to perform find operation on the tree


created by WeightedUnion.

For example: Tree created by using WeightedUnion

Now process the following eight finds: Find(8), Find(8),……….Find(8)

If SimpleFind is used, each Find(8) requires going up three parent link fields
for a total of 24 moves to process all eight finds.

When CollapsingFind is used the first Find(8) requires going up three links
and then resetting two links. Total 13 moves requires for process all eight
finds.

You might also like