0% found this document useful (0 votes)
51 views6 pages

Algorithms Theory 09 - Union-Find Data Structures

Union-find data structures can be used to maintain disjoint sets and support operations like making new sets from single elements, finding which set an element belongs to, and uniting two sets. They use representatives to identify each set, where the representative is a member of the set. Several optimizations can improve the performance of union-find structures, like using weighted union to append smaller sets to larger ones, maintaining set sizes during unions, and compressing paths during finds. With these optimizations, union-find operations run in near-linear time on average.

Uploaded by

Tony Leng
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views6 pages

Algorithms Theory 09 - Union-Find Data Structures

Union-find data structures can be used to maintain disjoint sets and support operations like making new sets from single elements, finding which set an element belongs to, and uniting two sets. They use representatives to identify each set, where the representative is a member of the set. Several optimizations can improve the performance of union-find structures, like using weighted union to append smaller sets to larger ones, maintaining set sizes during unions, and compressing paths during finds. With these optimizations, union-find operations run in near-linear time on average.

Uploaded by

Tony Leng
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Union-find data structures

Problem: Maintain a collection of disjoint sets while supporting the following operations: e.make-set(): Creates a new set whose only member is e.

Algorithms Theory 09 Union-Find Data Structures

e.find-set(): Prof. Dr. S. Albers

Returns the set Mi containing e.

union(Mi , Mj ): Unites the sets Mi and Mj into a new set.

Winter term 07/08

Winter term 07/08

Union-find data structures

Union-find data structures


Operations using representatives: e.make-set(): Creates a new set whose only member is e. The representative is e. e.find-set(): Returns the name of the representative of the set containing e. e.union(f): Unites the sets Me and Mf that contain e and f into a new set M and returns a member of Me Mf as the new representative of M. The sets Me and Mf are then destroyed.

Representation of set Mi : Mi is identified by a representative, which is some member of Mi.

Winter term 07/08

Winter term 07/08

Observations
If n is the number of make-set operations and m the total number of make-set, find-set and union operations, then m >= n after at most (n 1) union operations, only one set remains in the collection

Application: Connected components


Input: graph G = (V,E) Output: collection of the connected components of G Algorithm: Connected-Components for all v in V do v.make-set() for all (u,v) in E do if u.find-set() v.find-set() then u.union(v) Same-Component (u,v): if u.findset() = v.findset() then return true

Winter term 07/08

Winter term 07/08

Linked-list representation

Linked-list representation
b.union(d)

a x.make-set() x.find-set() x.union(y) e b f a g h d

Winter term 07/08

Winter term 07/08

Bad sequence of operations


e1.make-set() e2.make-set() en.make-set() e2.union(e1) e3.union(e2) en.union(en-1)
...
Winter term 07/08

Improvement
Weighted-union heuristic Always append the smaller list to the longer list. (Maintain the length of a list as a parameter). Theorem Using the weighted-union heuristic, the running time of a sequence of m make-set, find-set, and union operations, n of which are make-set() operations, is O(m + n log n). Proof Consider element e. Number of times es pointer to the representative is updated: log n

e1 e2 en e2 e3 en e1 e2 e1 e2 e3 en e1 e2 en-1 e1
..... e1

The longer list is always appended to the shorter list! Pointer updates for the i-th operation ei.union(ei-1): Running time of 2n -1 operations:
9

Disjoint-set forests
c h b e f d g s i v r u x y a.make-set() y.find-set() d.union(e): Make the representative of one set (e.g. f) the parent of the representative of the other set.
Winter term 07/08 11

...

en-1 .... e1

Winter term 07/08

10

Example
m = total number of operations ( 2n )

for i = 1 to n do ei.make-set( ) for i = 2 to n do ei.union(ei -1) for i = 1 to f do e1.find-set( ) n-th step

running time of f find-set operations:

O(f * n)

Winter term 07/08

12

Union by size
additional variable: e.size = (# nodes in the subtree rooted at e) e.make-set() 1 e.parent = e 2 e.size = 1 e.union(f) 1 link(e.find-set( ), f.find-set( ))

Union by size
link(e,f) 1 if e.size f.size 2 then f.parent = e 3 e.size = e.size + f.size 4 else /* e.size < f.size */ 5 e.parent = f 6 f.size = e.size + f.size

Winter term 07/08

13

Winter term 07/08

14

Union by size
Theorem The method union-by-size maintains the following invariant: A tree of height h contains at least 2h nodes. Proof h1 T1 h2
1

Union by size
Case 1: The height of the new tree is equal to the height of T1.

g 1 + g 2 g1 2 h

Case 2: The new tree T has a greater height. height of T: h2 + 1 T2

g = g1 + g 2 2h + 2h = 2h +1
2 2 2

g1 = T1 2h

g 2 = T2 2h

Consequence The running time of a find-set operation is O( log n ), where n is the number of make-set operations.

T1 T2
Winter term 07/08 15 Winter term 07/08 16

Path compression during find-set operations

Analysis of the running time


m total number of operations, f of which are find-set operations and n of which are make-set operations at most n 1 union operations Union by size: O(n + f log n) find-set operation with path compression: If f < n, (n + f log n) If f n, (f log1 +f/n n)

e.find-set() 1 if e e.parent 2 then e.parent = e.parent.find-set( ) 3 return e.parent


Winter term 07/08 17

Winter term 07/08

18

Analysis of the running time


Theorem (Union by size with path compression) Using the combined union-by-size and path-compression heuristic, the running time of m disjoint-set operations on n elements is (m * (m,n) ), where (m,n) is the inverse of Ackermanns function.

Ackermanns function and its inverse


Ackermanns function A(1,j) = 2j A(i,1) = A(i 1,2) A(i,j) = A(i 1, A(i, j - 1)) inverse of Ackermanns function for j 1 for i 2 for i,j 2

(m, n) = min{i 1 A(i, m / n) > log n}

Winter term 07/08

19

Winter term 07/08

20

Ackermanns function and its inverse


A(i, m / n ) A(i,1) A(2,1) = A(1,2 ) = 2 2 = 4 A(3,1) = A(2,2 ) = A(1, A(2,1)) = 2 4 = 16 A(4,1) = A(3,2 ) = A(2, A(3,1)) = A(2,16 ) 2 2 = 2 65536
22 2

(m, n ) 4, for n satisfying log n < 2 65536

Winter term 07/08

21

You might also like