Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Union-find. 2 Maintain a collection of disjoint sets under the following two operations S 3 = Union(S 1,S 2 ) Find(x) : returns the set containing x.

Similar presentations


Presentation on theme: "1 Union-find. 2 Maintain a collection of disjoint sets under the following two operations S 3 = Union(S 1,S 2 ) Find(x) : returns the set containing x."— Presentation transcript:

1 1 Union-find

2 2 Maintain a collection of disjoint sets under the following two operations S 3 = Union(S 1,S 2 ) Find(x) : returns the set containing x

3 3 Union-find We assume there are n fixed elements We start with n sets each containing a single element Each element has a pointer to its representation in the set containing it

4 4 S1 = {e1} S2 = {e2} S3={e3} S4={e4} …… S1 = {e1} A= {e2,e3} S4={e4} …… A = Union(S3,S4) Find(e2)  A B = Union(A,S7) S1 = {e1} B= {e2,e3,e7} S4={e4} ……

5 5 Why ? Suppose we want to maintain an equivalence relation: y z t x b v s a y ≡ z

6 6 y, z t x b v s a y ≡ s

7 7 y, z, s t x b v a y ≡ s

8 8 Queries Equivalent?(y,a)

9 9 Can solve this with union-find Each equivalence class is a set y ≡ s  union(find(y),find(s)) Equivalent?(y,a)  return yes if find(y) = find(a)

10 10 e2 Representation Represent each set by a tree, each node represents an item, the root also represents the set e3e7 B

11 11 Concretely e3 e7 e2 B

12 12 Find(e10) e6 e9 e11 e8e10 C

13 13 Find(e10) e6 e9 e11 e8e10 Find(x) while (x.parent ≠null) do x ← x.parent return (x) C

14 14 D=Union(B,C) B e2 e3 e7 C e6 e9 e11 e8e10

15 15 D=Union(B,C) B e2 e3 e7 C e6 e9 e11 e8e10

16 16 D=Union(B,C) B e2 e3 e7 D e6 e9 e11 e8e10

17 17 D=Union(B,C) B e2 e3 e7 C e6 e9 e11 e8e10

18 18 D=Union(B,C) B e2 e3 e7 C e6 e9 e11 e8e10

19 19 D=Union(B,C) D e2 e3 e7 C e6 e9 e11 e8e10

20 20 Link by size For the find’s sake its better to hang the smaller on the larger

21 21 D=Union(B,C) B e2 e3 e7 C e6 e9 e11 e8e10 5 3 If (C.size > B.size) then B.Parent ← C; C.size = C.size + B.size return (C) Else C.parent ← B; B.size = B.size + C.size return (B)

22 22 D=Union(B,C) B e2 e3 e7 C e6 e9 e11 e8e10 8 3 If (C.size > B.size) then B.Parent ← C; C.size = C.size + B.size return (C) Else C.parent ← B; B.size = B.size + C.size return (B) D

23 23 Analysis Union: O(1) time Find: O(log(n)) time The depth of a tree is at most log(n)

24 24 Proof By induction on the sequence of operations: For every x Depth(x) ≤ log |T(x)| x

25 25 Path compression


Download ppt "1 Union-find. 2 Maintain a collection of disjoint sets under the following two operations S 3 = Union(S 1,S 2 ) Find(x) : returns the set containing x."

Similar presentations


Ads by Google