Data Structures Disjoint Sets
Data Structures Disjoint Sets
Make_Set(x)
Make_Set(x) … Create the Sn • m = # of all ops
{ }
singleton set S x = x with {Make_Set,
Make_Set, Union, Find_Set}
Find_Set}
x as rep.
• # Unions ≤ n−1
Union(x,y)
Union(x,y) … Create Sx ∪ S y with set rep and
• m≥n
destroy S & S
x 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
( n − 1)n
1 + 2 + 3 + … + ( n − 1) = ∑ j = = O( n2 )
j =1 2
Weighted-
Weighted-Union Heuristic Disjoint-
Disjoint-Set Forest Representation
• Each rep has a Length-
Length-of-
of-List field
• Use this to append smaller sets to larger
ones
Theorem.
Theorem. If the Linked-
Linked-List representation
of sets and if the weighted-
weighted-union heuristic
are used, and if a sequence of m
{Make_Set,
Make_Set, Union, Find_Set}
Find_Set} ops are used
(with n Make-
Make-Set ops),
ops), then the
computational time taken is Union
O ( m + n lg n )
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