Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 33

UNIT-2

Disjoint Sets and Union/find operations

Dr.B.Srinu

Dept of Computer Science & Engineering


Today’s Outline
 Disjoint Set Union/Find ADT
 Up-trees
 Weighted Union
 Path Compression(Collapsing Find)

Disjoint Sets 2
Disjoint Set
 Sets and Disjoint Set :
 Disjoint Set Union:
 Considering a set S={1,2,3…10} (when n=10),
 then elements can be partitioned into three disjoint
sets
 s1={1,7,8,9}, s2={2,5,10} and s3={3,4,6}.

 Disjoint Set: if then Si and Sj


Si  S j  
are called as disjoint sets

Disjoint Sets 3
Disjoint Set Union/Find

 1. Disjoint Set union:


 If Si and Sj are two disjoint sets, then their
union Si U Sj = all the elements x such that x
is in Si or Sj.
Thus S1 U S2 ={1,7,8,9,2,5,10}.
 2. Find(i):
 Given the element I, find the set containing i.
Thus, 4 is in set S3, 9 is in S1.

Disjoint Sets 4
Disjoint Set Union/Find ADT
Union/Find ADT operations
 union find(4)
 find {1,4,8} {6}
 create
 destroy 8 {7} {2,3,6}
{5,9,10}
{2,3}
union(2,6)
Disjoint set equivalence property: every element of a DS U/F
structure belongs to exactly one set
Dynamic equivalence property: the set of an element can
change after execution of a union

Disjoint Sets CSE 326 Autumn 2001 5


Example a 3 b 10 c
Construct the maze on the right
2 1 6
Initial state (set names underlined):
{a}{b}{c}{d}{e}{f}{g}{h}{i} d 4 e 7 f

Maze constructor (outside agent) 11 9 8


traverses edges in random order and
decides whether to union
g 12 h 5 i

Disjoint Sets CSE 326 Autumn 2001 7


Example, First Step
{a}{b}{c}{d}{e}{f}{g}{h}{i} a 3 b 10 c

2 1 6
find(b)  b
find(e)  e
d 4 e 7 f
find(b)  find(e) so:
add 1 to E 11 9 8
union(b, e)
g 12 h 5 i
{a}{b,e}{c}{d}{f}{g}{h}{i}
Order of edges in blue

Disjoint Sets CSE 326 Autumn 2001 8


Up-Tree Intuition
Finding the representative member of a set is
somewhat like the opposite of finding whether a
given item exists in a set.

So, instead of using trees with pointers from


each node to its children; let’s use trees with a
pointer from each node to its parent.

Disjoint Sets CSE 326 Autumn 2001 9


Union-Find
Up-Tree Data Structure
 Each subset is an up-tree
with its root as its
representative member
 All members of a given a c g h
set are nodes in that
set’s up-tree d b f i
 Hash table maps input
data to the node
associated with that data e

Up-trees are not necessarily binary!


Disjoint Sets CSE 326 Autumn 2001 10
Find
find(f)
find(e)
a b 10 c
a c g h

d e 7 f
d b f i 11 9 8
g 12 h i
e

Just traverse to the root!


runtime:
Disjoint Sets CSE 326 Autumn 2001 11
Union
union(a,c)

a b 10 c
a c g h

d e f
d b f i 11 9 8
g 12 h i
e

runtime: Just hang one root from the other!

Disjoint Sets CSE 326 Autumn 2001 12


a 3 b 10 c

Example (1/11)
2 1 6
d 4 e 7 f
11 9 8
union(b,e) g 12 h 5 i

a b c d e f g h i

a b c d f g h i

e
a 3 b 10 c

Example (2/11)
2 6
d 4 e 7 f
11 9 8
union(a,d) g 12 h 5 i

a b c d f g h i
e

a b c f g h i
d e
a 3 b 10 c

Example (3/11)
6
d 4 e 7 f
11 9 8
union(a,b) g 12 h 5 i

a b c f g h i
d e

a c f g h i
b d
e
a b 10 c

Example (4/11)
6
d 4 e 7 f
11 9 8
find(d) = find(e) g 12 h 5 i
No union!

a c f g h i
b d
e

While we’re finding e,


could we do anything else?
a b 10 c

Example (5/11)
6
d e 7 f
11 9 8
union(h,i) g 12 h 5 i

a c f g h i a c f g h

b d b d i

e e
a b 10 c

Example (6/11)
6
d e 7 f
11 9 8
union(c,f) g 12 h i

a c f g h a c g h

b d i b d f i

e e
a b 10 c

Example (7/11) d e 7 f
find(e)
find(f) 11 9 8
union(a,c) g 12 h i

a c g h c g h

b d f i a f i

e b d

Could we do a
better job on this union? e
a b 10 c

Example (8/11) d e f
find(f)
find(i) 11 9 8
union(c,h) g 12 h i

c g h c g

a f i a f h

b d b d i

e e
a b 10 c

Example (9/11) d e f
find(e) = find(h) and find(b) = find(c) 11 9
So, no unions for either of these. g 12 h i

c g

a f h

b d i

e
a b c

Example (10/11) d e f
find(d)
find(g) 11
union(c, g) g 12 h i

c g g

c
a f h
a f h
b d i
b d i

e e
a b c

Example (11/11) d e f
find(g) = find(h)
So, no union.
And, we’re done! g 12 h i

g a b c

c d e f

a f h
g h i
b d i
Ooh… scary!
e Such a hard maze!
Disjoint set data structure
 A forest of up-trees
can easily be stored a c g h
in an array.
 Also, if the node b d f i
names are integers or
characters, we can
use a very simple, e
perfect hash.
Nifty storage trick!
0 (a) 1 (b) 2 (c) 3 (d) 4 (e) 5 (f) 6 (g) 7 (h) 8 (i)
up-index: -1 0 -1 0 1 2 -1 -1 7

Disjoint Sets CSE 326 Autumn 2001 24


Implementation
 Simple Find Algorithm Simple Algorithm for Union:
 Algorithm Find(i) Algorithm Union(i,j)
 { {
//replace the disjoint sets
 j:=i; with roots i and j, I not equal
 while(p[j]>0) do to j by their union Integer i,j;
 j:=p[j];
 return j; P[j] :=i;
}
 }

runtime: O(depth) or … runtime: O(1)

Disjoint Sets CSE 326 Autumn 2001 25


Degenerate Tree:

The time taken for n-1 unions is O(n).

runtime: O(depth) or … runtime: O(1)

Disjoint Sets CSE 326 Autumn 2001 26


Room for Improvement:
Weighted Union
 Always makes the root of the larger tree the new root
 Often cuts down on height of the new up-tree

a c g h c g h a g h

b d f i a f i b d c i

e b d e f
Could we do a e
better job on this union?
Weighted union!

Disjoint Sets CSE 326 Autumn 2001 27


Weighted Union Code
typedef ID int;
ID union(ID x, ID y) {
assert(up[x] == -1);
assert(up[y] == -1);

if (weight[x] > weight[y]) {


up[y] = x;
weight[x] += weight[y]; new runtime of union:
} else {
up[x] = y;
weight[y] += weight[x];
}
} new runtime of find:

Disjoint Sets CSE 326 Autumn 2001 28


Weighted Union Find Analysis
 Finds with weighted union are O(max up-tree height)
 But, an up-tree of height h with weighted union must
have at least 2h nodes
Base case: h = 0, tree has 20 = 1 node
Induction hypothesis: assume true for h < h

A merge can only increase tree height by


one over the smaller tree. So, a tree of
  2max height  n and height h-1 was merged with a larger tree to
form the new tree. Each tree then has  2h-1
max height  log n nodes by the induction hypotheses for a
 So, find takes O(log n) total of at least 2h nodes. QED.

Disjoint Sets CSE 326 Autumn 2001 29


Room for Improvement:
Path Compression(collapsing find)
 Points everything along the path of a find to the root
 Reduces the height of the entire access path to 1

a c f g h i a c f g h i
b d b d
e e
While we’re finding e,
could we do anything else?
Path compression!

Disjoint Sets CSE 326 Autumn 2001 30


Path Compression Example
find(e)

c g c g

a f h a f h
b e
b d
d
i
i e

Disjoint Sets CSE 326 Autumn 2001 31


Path Compression(collapsing find) Code
typedef ID int;
ID find(Object x) {
assert(hTable.contains(x));
ID xID = hTable[x];
ID hold = xID;
while(up[xID] != -1) {
xID = up[xID];
}
while(up[hold] != -1) {
temp = up[hold];
up[hold] = xID;
hold = temp; runtime:
}
return xID;
}

Disjoint Sets CSE 326 Autumn 2001 32


Complexity of
Weighted Union + Path Compression
Ackermann created a function A(x, y) which grows very fast!
Inverse Ackermann function (x, y) grows very slooooowwwly …
Single-variable inverse Ackermann function is called log* n
How fast does log n grow? log n = 4 for n = 16
Let log(k) n = log (log (log … (log n)))

Then, let log* n = minimum k such that log(k) n  1


How fast does log* n grow? log* n = 4 for n = 65536
log* n = 5 for n = 265536 (20,000 digit number!)

How fast does (x, y) grow? Even sloooowwwer than log* n


(x, y) = 4 for n far larger than the number of atoms in the
universe (2300)

Disjoint Sets CSE 326 Autumn 2001 33


Complexity of
Weighted Union + Path Compression
 Tarjan proved that m weighted union and find
operations on a set of n elements have worst case
complexity O(m(m, n))

 This is essentially amortized constant time

 In some practical cases, weighted union or path


compression or both are unnecessary because trees
do not naturally get very deep.

Disjoint Sets CSE 326 Autumn 2001 34

You might also like