Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE 326 Union Find Richard Anderson Text Chapter 8 7-29-2005 CSE 326.

Similar presentations


Presentation on theme: "CSE 326 Union Find Richard Anderson Text Chapter 8 7-29-2005 CSE 326."— Presentation transcript:

1 CSE 326 Union Find Richard Anderson Text Chapter 8 CSE 326

2 Union Find Problem Fixed collection of items Disjoint subsets
Find(x) – which subset is x in? Union(x,y) – merge the sets containing x and y What run times do you expect for implementations of Union and Find? CSE 326

3 Applications of Union Find
Building connected components foreach edge (e = (v1, v2)){ if (Find(v1) != Find(v2)) Union(v1, v2); } CSE 326

4 Equivalence Classes Fixed collection of items partitioned into equivalence classes Equivalence classes identified by one of their members {1, 3, 5}, {2, 8}, {4}, {6, 9, 10, 11}, {7} CSE 326

5 Draw an in-tree representation of the following equivalence relation
{1,11}, {2, 4, 6, 8, 10, 12}, {3, 5, 7, 9}, {13}, {14}, {15} CSE 326

6 Array representation Store parents in the array P 3 8 3 4 3 9 7 8 9 9
6 CSE 326

7 Basic Find operation Write the code for Find(x) using the Array P[ ]
CSE 326

8 Union Union(x, y){ x1 = Find(x); y1 = Find(y); P[x1] = y1; } 7-29-2005
CSE 326

9 Unions create long chains
CSE 326

10 Weighted Union W[x]: number of descendents of x (written wt(x))
Union(x, y){ x1 = Find(x); y1 = Find(y); if (W[x1] > W[y1]){ P[y1] = x1; W[x1] = W[x1] + W[y1]; } else { P[x1] = y1; W[y1] = W[x1] + W[y1]; W[x]: number of descendents of x (written wt(x)) Weighted union: smaller tree points to the bigger tree CSE 326

11 Binomial Trees, B0, B1, …, Bk B0 Bk = Union(Bk-1, Bk-1) Bk-1 Bk-1 Bk
CSE 326

12 Binomial Trees Draw B4 What is the height of Bk?
What is the weight of Bk? CSE 326

13 Theorem: Weight balanced unions guarantee O(log n) depth
Show wt(T) >= 2ht(T) Show this is an invariant maintained by Union and Find operations Base case – true for singleton node Trivial case – invariant for Find operations Interesting case: T = Union(T1, T2) CSE 326

14 wt(T1) >= 2ht(T1), wt(T2) >= 2ht(T2) Show wt(T) >= 2ht(T)
Assume wt(T2) >= wt(T1) Idea, consider cases: ht(T1) >= ht(T2) ht(T1) < ht(T2) T1 T T2 CSE 326

15 Case 1 ht(T1) >= ht(T2) T Show 2ht(T) <= wt(T)
2ht(T) = 2ht(T1)+1 <= 2wt(T1) <= wt(T) T1 T T2 Show 2ht(T) <= wt(T) Hint: what is ht(T)? Recall: wt(T2) >= wt(T1) 2ht(T1) <= wt(T1) 2ht(T2) <= wt(T2) CSE 326

16 Case 2: ht(T1) < ht(T2) T Show 2ht(T) <= wt(T)
2ht(T) = 2ht(T2) <= wt(T2) <= wt(T) T1 T T2 Show 2ht(T) <= wt(T) Hint: What is ht(T)? Recall: wt(T2) >= wt(T1) 2ht(T1) <= wt(T1) 2ht(T2) <= wt(T2) CSE 326

17 Path Compression Long chains are only expensive if they are traversed multiple times Idea: Compress chains when they are traversed CSE 326

18 Path Compression Algorithms
Find(x) { y = x; while (P[x] != x) x = P[x]; while (P[y] != x){ t = y; P[y] = x; y = t; } return x; Find(x){ while (P[x] != x) x = P[x] = P[P[x]]; return x; } CSE 326

19 Amortized Complexity Cost of n intermixed Union-Find operations
Basic algorithm O(n2) Weighted union O(n log n) Path Compression O(n log n) Weighted union + Path Compression O(a(n) n) CSE 326


Download ppt "CSE 326 Union Find Richard Anderson Text Chapter 8 7-29-2005 CSE 326."

Similar presentations


Ads by Google