0% found this document useful (0 votes)
43 views16 pages

Disjoint Sets

Uploaded by

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

Disjoint Sets

Uploaded by

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

DISJOINT SETS

Dr. K. Shahu Chatrapati


Disjoint Set Representation
• We assume that the elements of the sets are the numbers 1,2,3,…,n.
• We assume that the sets being represented are pair wise disjoint.
• For example, n=10, S1={0,6,7,8}, S2={1,4,9} S3={2,3,5}.
• Figure 1 shows the possible representation for these sets.
• In this representation, each set is represented as a tree. For each set,
nodes from the children are linked to the parent rather than from the
parent to the children.

Figure 1 Possible tree representation of


sets
Disjoint Set Union Operation
 Disjoint set union
– To obtain union of two sets, set the parent field of one of the
roots to the other root as shown in Figure 2.
– This can be accomplished easily if, with each set name, we
keep a pointer to the root of the tree representing that set.

Figure 2 Possible Representations of S1 ᵁ S2


Disjoint Set Find Operation
 Find(i). Given the element i, find the set containing i.
– If each root has a pointer to the set name, then to determine
Find(i), we can follow parent links to the root of its tree and
use the pointer to the set name.
– The data representation for S1, S2, and S3 is given in Figure 3.

Figure 3 Data Representation for S1, S2, and S3


Array Representation of Disjoint Sets
• We can represent the tree nodes using an array p[1:n], where n
is the maximum number of elements.
• The ith element of this array represents the tree node that
contains element i. This array element gives the parent pointer
of the corresponding tree node.
• Root nodes have a parent of -1.
• Figure 4. shows this representation of S1, S2, and S3 of Figure
1.

Figure 4 Array representation of S1, S2, and S3


Simple Algorithms for Union and Find
• In presenting the union
and find algorithms, we
ignore the set names and
identify sets just by the
roots of the trees
representing them.
• The operation of Find(i)
now becomes:
Determining the root of
the tree containing
element i.
• The function Union(i,j)
Algorithm 1 Simple Algorithms for union
requires two trees with
and find
roots i and j be joined.
Performance of Simple Union and Find
 Although algorithms in Algorithm 1 are very easy to state,

 For instance, if we start with 𝑞 elements each in a set of its


their performance characteristics are not very good.

𝑆𝑖 = ሼ𝑖ሽ,1 ≤ 𝑖 ≤ 1), then


configuration consists of a forest with 𝑞 nodes, and
own (i.e., the initial

𝑝 = 0,1 ≤ 𝑖 ≤ 𝑞.
 Now let us process the following sequence of union-find

𝑈𝑛𝑖𝑜𝑛ሺ 1,2ሻ ,𝑈𝑛𝑖𝑜𝑛ሺ 2,3ሻ ,…,𝑈𝑛𝑖𝑜𝑛(𝑛 − 1,𝑛)


operations:

𝐹𝑖𝑛𝑑ሺ 1ሻ ,𝐹𝑖𝑛𝑑ሺ 2ሻ ,…,𝐹𝑖𝑛𝑑(𝑛)

 Since the time taken for a union is constant, the 𝑛 −


 This sequence results in the degenerate tree of Figure 5

1 unions can be processed in time 𝑂ሺ 𝑛ሻ .


 However, each find requires following a sequence of parent

time required to process a find for an element at level 𝑖 of a


pointers from the element to be found to the root. Since the

tree is 𝑂ሺ 𝑖ሻ , The total time needed to process the 𝑛 finds


𝑂(σ𝑛 𝑖=1 𝑖) = 𝑂ሺ 𝑛 ሻ .
2
Figure 5 Degenerate
tree
Weighting Rule for Union(i,j)
• We can improve the performance of our union and find algorithms by
avoiding the creation of degenerate trees.
• To accomplish this, we make use of 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 number in the tree with root j, then make j the parent of i;
otherwise make i the parent of.
• When we use the weighting rule to perform the sequence of set unions
given before, we obtain the trees of Figure 6.
• To implement the weighting rule, we need to know how many nodes are
there in every tree. To do this, we maintain a count field in the root of every
tree. If i is a root node, count[i] equals the number of nodes in that tree.
• Since all nodes other than roots of trees have a positive number in the p
field, we can maintain the count in the p field of the roots as a negative
number.
• Using this convention, we obtain Algorithm 2.
Figure 6 Trees obtained using the weighting rule

Algorithm 2 Union algorithm with weighting rule


Example 1

Consider the behavior of Weighted Union on

𝑝ሾ𝑖ሿ = −𝑐𝑜𝑢𝑛𝑡ሾ𝑖ሿ =
the following sequence of unions starting from

1,1 ≤ 𝑖 ≤ 𝑖 = 𝑛;
the initial configuration

𝑈𝑛𝑖𝑜𝑛ሺ 1,2ሻ ,𝑈𝑛𝑖𝑜𝑛ሺ 3,4ሻ ,𝑈𝑛𝑖𝑜𝑛ሺ 5,6ሻ ,


𝑈𝑛𝑖𝑜𝑛ሺ 7,8ሻ ,𝑈𝑛𝑖𝑜𝑛ሺ 1,3ሻ ,
𝑈𝑛𝑖𝑜𝑛ሺ 5,7ሻ ,𝑈𝑛𝑖𝑜𝑛ሺ 1,5ሻ

height of each tree with 𝑚 nodes is log


‫ ہ‬2𝑚 +
The trees of Figure 7 are obtained, where the

1.
‫ۂ‬
… Example 1

Figure 7 Trees achieving worst-case bound


Collapsing Rule

• The time required to perform a union using


Weighting rule has increased somewhat but is still
bounded by a constant (that is O(1)).
• The height of the tree with m nodes is log2m+1.
Consequently the time to process a find is O(log
m) if there are m elements in a tree.
• Find can be improved using the collapsing rule.
• 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.
Find Algorithm with Collapsing Rule
Example 2
• Consider the tree created by Weighted Union on the
sequence of unions of Example 1. Now process the
following eight finds:
Find(7), Find(7),…,Find(7)
• If simple find is used, each Find(7) requires going up three
parent link fields for a total of 24 moves to process all eight
finds.
• When collapsing find is used, the first Find(7) requires
going up three links and then resetting two links. (note: even
though only two parent links need to be reset, collapsing
find will reset three: the parent of 5 is reset to 1)
• Each of the remaining seven finds requires going up only
one link field. The total cost is now only 13 moves
Example 2

Figure 8 Tree resulting after Find(7)

• The use of collapsing rule roughly doubles the time for an individual
find. However it reduces the worst-case over a sequence of finds.

You might also like