Professional Documents
Culture Documents
Disjoint Set
Disjoint Set
Date:22.07.2014
A partition of a set is a collection of subsets such that Union of all these subsets is the set itself
Any two subsets are mutually disjoint.
S = {1,2,3,4}, A = {1,2}, B = {3,4}, C = {2,3,4}, D = {4}
Is A, B a partition of S?
Yes
Is A, C partition of S?
No
Is A, D a partition of S?
No
In presenting the union and find algorithms, we ignore the set names and identify sets just by the
roots of the trees representing them. This simplifies the discussion. Since set set elements are
numbered 1 through n, we represent the tree nodes using an array p[1:n], where n is maximum
number of elements.
[1]
-1
[2]
5
[3]
-1
[4]
3
[5]
-1
[6]
3
[7]
1
[8]
1
[9]
1
[10]
5
Now using the above algorithm, let us process the following sequence of union-find operations:
Union(1,2), Union(2,3), .... Union(n-1,n)
Find(1), Find(2), ...., Find(n).
This sequence results the following degenerate tree. Since the time taken for a
union is constant, the (n-1) unions can be processed in time O(n). 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 process the n finds is
Consider the behaviour of WeightedUnion on the following sequence of unions starting from the initial
configuration p[i]=-count[i]= -1, 8 I 1 =n;
Union(1, 2), Union(3, 4), Union(5, 6), Union(7, 8), Union(1, 3), Union(5, 7), Union(1, 5)
We will get the the following tree as given below:
Consider we process eight finds in the beside mentioned tree.
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. We
can think it in different way:
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].