260 likes | 329 Views
Union-find. Union-find. 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. Union-find. We assume there are n fixed elements We start with n sets each containing a single element
E N D
Union-find • Maintain a collection of disjoint sets under the following two operations • S3 =Union(S1,S2) • Find(x) : returns the set containing x
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
S1 = {e1} S2 = {e2} S3={e3} S4={e4} …… A = Union(S3,S4) S1 = {e1} A= {e2,e3} S4={e4} …… Find(e2) A B = Union(A,S7) S1 = {e1} B= {e2,e3,e7} S4={e4} ……
Why ? • Suppose we want to maintain an equivalence relation: b y v z s t a x y ≡ z
y, z b v s t a x y ≡ s
y, z, s b v t a x y ≡ s
Queries • Equivalent?(y,a)
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)
Representation • Represent each set by a tree, each node represents an item, the root also represents the set B e2 e7 e3
Concretely B e2 e7 e3
Find(e10) C e6 e9 e11 e10 e8
Find(e10) C e6 e9 e11 e10 e8 Find(x) while (x.parent ≠null) do x ← x.parent return (x)
D=Union(B,C) e6 C e9 e2 B e11 e7 e3 e10 e8
D=Union(B,C) e6 C e9 e2 B e11 e7 e3 e10 e8
D=Union(B,C) e6 D e9 e2 B e11 e7 e3 e10 e8
D=Union(B,C) e2 B e6 C e7 e3 e9 e11 e10 e8
D=Union(B,C) e2 B e6 C e7 e3 e9 e11 e10 e8
D=Union(B,C) e2 D e6 C e7 e3 e9 e11 e10 e8
Link by size • For the find’s sake its better to hang the smaller on the larger
D=Union(B,C) e6 5 C e2 3 B e9 e11 e7 e3 e10 e8 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=Union(B,C) D e6 8 C e2 3 B e9 e11 e7 e3 e10 e8 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)
Analysis • Union: O(1) time • Find: O(log(n)) time • The depth of a tree is at most log(n)
Proof By induction on the sequence of operations: For every x Depth(x) ≤ log |T(x)| x