Data Structures for Disjoint Sets

Data Structures for Disjoint Sets
S = { S1 , S2 , S3 ,… , Sn }
Each set is an “equiv. Class”
Class”
identified by a rep.
Make_Set(x)
Make_Set(x) … Create the
singleton set S x = x with
x as rep.
{ }
Union(x,y)
Union(x,y) … Create
destroy S & S
x
Sx ∪ S y
Collection of disjoint sets
S1
S2
S3
Data Structures for Disjoint Sets
Parameters
• n = # of MakeMake-Set ops
Sn
• m = # of all ops
{Make_Set,
Make_Set, Union, Find_Set}
Find_Set}
• # Unions
with set rep and
•
m≥n
≤ n−1
y
Find_Set(x)
Find_Set(x) Return pointer to
set rep of
Data Structures for Disjoint Sets
Data Structures for Disjoint Sets
Data Structures for Disjoint Sets
Data Structures for Disjoint Sets
1
Data Structures for Disjoint Sets
Data Structures for Disjoint Sets
n −1
1 + 2 + 3 + … + ( n − 1) = ∑ j =
j =1
WeightedWeighted-Union Heuristic
( n − 1)n
= O( n2 )
2
DisjointDisjoint-Set Forest Representation
• Each rep has a LengthLength-ofof-List field
• Use this to append smaller sets to larger
ones
Theorem.
Theorem. If the LinkedLinked-List representation
of sets and if the weightedweighted-union heuristic
are used, and if a sequence of m
{Make_Set,
Make_Set, Union, Find_Set}
Find_Set} ops are used
(with n MakeMake-Set ops),
ops), then the
computational time taken is
O ( m + n lg n )
A Problem
Union
Data Structures for Disjoint Sets
2
Data Structures for Disjoint Sets
Heuristics to Improve Running Time
• Union by Rank
• Path Compression
Each node has a field containing Rank,
Rank, which
is an upper bound on the height of the node
3