Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 9: Union-Find Algorithms The Design and Analysis of Algorithms.

Similar presentations


Presentation on theme: "Chapter 9: Union-Find Algorithms The Design and Analysis of Algorithms."— Presentation transcript:

1 Chapter 9: Union-Find Algorithms The Design and Analysis of Algorithms

2 2 Union-Find Algorithms Basic Idea Quick Find Quick Union Path Compression

3 3 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.

4 4 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 S x and S y containing x and y, and adds it to the collection. S x and S y are deleted from the collection

5 5 Subset’s representative Use one element from each of the disjoint subsets in a collection Two principal implementations  Quick find  Quick union

6 6 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

7 7

8 8 Find(x) operation To find the representative of an element we simply check the array with the representatives –  (1)

9 9 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

10 10 Union(x,y) Efficiency  (N) worst time for one union operation  (N 2 ) for union (2,1), union (3,2), … union (n, n-1) union-by size: attach the smaller set to the larger set  worst case is still  (N), average case for a sequence of N-1 union operations is O(NlogN)

11 11 Quick Union Update only the representative of one of the sets (the smaller set) 123 645 12 3645 12 364 5 Given the subsets {1,6}, {2, 4}, {3, 5}, after union(2,5) we obtain: In Quick Find R = 1, 2, 2, 2, 2, 1 In Quick Union R = 1, 2, 2, 2, 3, 1 O(N + m logN)

12 12 Path Compression Make every element encountered during the find to point to the root of the tree. Efficiency: slightly worse than linear.


Download ppt "Chapter 9: Union-Find Algorithms The Design and Analysis of Algorithms."

Similar presentations


Ads by Google