PPT

advertisement
The Design and Analysis of Algorithms
Chapter 9:
Union-Find Algorithms
Union-Find Algorithms

Basic Idea

Quick Find

Quick Union

Path Compression
2
Basic Idea

Initialization: each disjoint subset
is one-element subset, containing
a different element of S.

Union-find operation: acts on the
collection of n one-element
subsets to give larger subsets.
3
Abstract data type for the finite set

makeset(x) - creates an one-element set {x}
find(x) - returns a subset containing x

union(x,y) - constructs the union of disjoint

subsets Sx and Sy containing x and y, and
adds it to the collection.
Sx and Sy are deleted from the collection
4
Subset’s representative

Use one element from each of the
disjoint subsets in a collection

Two principal implementations
 Quick
find
 Quick union
5
Quick Find
Record the representatives for each element in an
array
R[k] = k if k is a representative of some set
R[k] = m if k is in a set with representative m
 The sets are stored in lists whose heads are in an
array heads[1..N]
 If k is a representative of a set, then heads[k]
contains a reference to a list with the elements
of that set
 If k is not a representative of a set, heads[k] is
null

6
7
Find(x) operation
 To
find the representative of an
element we simply check the array
with the representatives – (1)
8
Union(x,y) operation



A  find(x)
B  find(y)
Attach the one of the lists to the
other:



A[last].next  B[first]
A[last]  B[last]
Update the representatives of the
attached set
9
Union(x,y) Efficiency
(N) worst time for one union operation
 (N2) for union (2,1), union (3,2), …
union (n, n-1)
 union-by size: attach the smaller set to the
larger set

case is still (N), average case for a
sequence of N-1 union operations is O(NlogN)
 worst
10

Update only the
representative of one of the
sets (the smaller set)

1
2
3
6
4
5
Quick Union
O(N + m logN)
Given the subsets {1,6}, {2, 4}, {3, 5}, after
union(2,5) we obtain:
1
1
6
2
3
4
6
5
2
3
4
5
In Quick Find
In Quick Union
R = 1, 2, 2, 2, 2, 1
R = 1, 2, 2, 2, 3, 1
11
Path Compression

Make every element encountered during the find to
point to the root of the tree.
Efficiency: slightly worse than linear.
12
Download