Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Unit 7: Disjoint Sets

․Course contents:
⎯ Data structures for disjoint sets
․Reading:
⎯ Chapter 21

Unit 7 Y.-W. Chang 1


Disjoint Sets
․A disjoint-set data structure maintains a collection
S = {S1, S2, …, Sk} of disjoint dynamic sets.
․Each set is identified by a representative, which is
some member of the set.
․Operations supported:
⎯ Make-Set(x): Sx = { x }

⎯ Union(x, y): Sx ∪ Sy

⎯ Find-Set(x): representative of the set containing x

Unit 7 Y.-W. Chang 2


Application: Connected Components

Connected-Components(G) Same-Component(u,v)
1. for each vertex v ∈ V[G] /* check if u, v are in the same
2. Make-Set(v); set */
3. for each edge (u, v) ∈ E[G] 1. if Find-Set(u) = Find-Set(v)
4. if Find-Set(u) ≠ Find-Set(v) 2. return TRUE;
5. Union(u, v); 3. return FALSE;

Unit 7 Y.-W. Chang 3


Linked-List Representation of Disjoint Sets

․n: # of Make-Set operations.


․m: total # of Make-Set, Find-Set, Union operations.
․Operations supported:
⎯ Make-Set: O(1) time (θ(n) for n Make-Set operations).
⎯ Find-Set: O(1) time.
⎯ Union??
Unit 7 Y.-W. Chang 4
Time Complexity
․ n: # of Make-Set operations.
․ m: total # of Make-Set, Find-Set, Union operations.
․ Each naive Union takes linear time.
∑ i = θ(q2) for q -1 union operations, where q = m - n + 1.
q −1

i =1
⎯ Total time for n Make-Set and q -1 Union operations: O(n+q2) = O(m2)
⇒ amortized time of an operation is O(m) time.
․ Weighted-union heuristic: Append the smaller list onto the longer.
․ Theorem: Using the weighted-union heuristic, a sequence of m
Make-Set, Union, and Find-Set operations, with n Make-Set
operations, takes O(m + nlgn) time.

Unit 7 Y.-W. Chang 5


Rooted-Tree Representation of Disjoint Sets
․ Disjoint-set forest: sets are represented by rooted trees.
․ Operations:
⎯ Make-Set: create a tree with only one node.
⎯ Find-Set (find path): traverse parent pointers until tree root.
⎯ Union: point one root to the other.
․ Two heuristics to speed up the disjoint-set data structure:
⎯ Union by rank: Point the root of the smaller-sized tree to that of the
larger one (approximation: upper bound of height).
⎯ Path compression: Make each node on the find path point directly to
the root during Find-Set.

Unit 7 Y.-W. Chang 6


Pseudocode: Disjoint-set Forests
․ rank[x]: upper bound on the height of x.

Make-Set(x)
1. p[x] ← x;
2. rank[x] ← 0. Link(x,y)
1. if rank[x] > rank[y]
Union(x,y) 2. p[y] ← x;
1. Link(Find-Set(x), Find-Set(y)). 3. else p[x] ← y;
4. if rank[x] = rank[y]
Find-Set(x) 5. rank[y] ← rank[y]+1.
1. if x ≠ p[x]
2. p[x] ← Find-Set(p[x]);
3. return p[x].

Unit 7 Y.-W. Chang 7


Time Complexity
․ Runtime by using both union by rank and path compression.
⎯ O(m α(m, n)): Tarjan, 1975, where α(m, n) is “inverse” of Ackermann's
function A:

α (m, n) = min{i ≥ 1: A(i, ⎢⎣ m / n ⎥⎦ ) > lg n}


α(m, n) ≤ 4 for all practical cases.
⎯ A slightly weaker bound O(m lg*n): Hopcroft & Ullman, 1973.
․ Runtime: O(m) for all practical applications.

Unit 7 Y.-W. Chang 8

You might also like