34
21. Data Structures for Disjoint Sets Heejin Park College of Information and Communications Hanyang University

21. Data Structures for Disjoint Sets Heejin Park College of Information and Communications Hanyang University

Embed Size (px)

DESCRIPTION

3 Disjoint sets  Two sets A and B are disjoint if A ∩ B = {}. Ex> A = {1, 2}, B = {3, 4}  Sets S 1, S 2, …, S k are disjoint if every two distinct sets S i and S j are disjoint. Ex> S 1 = {1, 2, 3}, S 2 = {4, 8}, S 3 ={5,7}

Citation preview

Page 1: 21. Data Structures for Disjoint Sets Heejin Park College of Information and Communications Hanyang University

21. Data Structures for Disjoint Sets

Heejin ParkCollege of Information and Communications

Hanyang University

Page 2: 21. Data Structures for Disjoint Sets Heejin Park College of Information and Communications Hanyang University

2

Contents

Disjoint-sets

Disjoint-set operations

An application of disjoint-set data structures

Disjoint-set data structures

Page 3: 21. Data Structures for Disjoint Sets Heejin Park College of Information and Communications Hanyang University

3

Disjoint sets

Disjoint sets Two sets A and B are disjoint if A ∩ B = {}.

Ex> A = {1, 2}, B = {3, 4}

Sets S1, S2, …, Sk are disjoint

if every two distinct sets Si and Sj are disjoint.

Ex> S1 = {1, 2, 3}, S2 = {4, 8}, S3={5,7}

Page 4: 21. Data Structures for Disjoint Sets Heejin Park College of Information and Communications Hanyang University

4

Disjoint sets

A collection of disjoint sets A set of disjoint sets is called a collection of disjoint sets.

Ex> {{1, 2, 3}, {4, 8}, {5,7}}

Each set in a collection has a representative member and the set is identified by the member.

Ex> {{1, 2, 3}, {4, 8}, {5,7}}

Page 5: 21. Data Structures for Disjoint Sets Heejin Park College of Information and Communications Hanyang University

5

Disjoint sets

A collection of dynamic disjoint sets Dynamic: Sets are changing.

New sets are created. {{1, 2, 3}, {4, 8}, {5,7}} {{1, 2, 3}, {4, 8}, {5,7}, {9}}

Two sets are united. {{1, 2, 3}, {4, 8}, {5,7} } {{1, 2, 3}, {4, 8, 5, 7}}

Page 6: 21. Data Structures for Disjoint Sets Heejin Park College of Information and Communications Hanyang University

6

Disjoint-set operations

Disjoint-set operations

MAKE-SET(x)

UNION(x, y)

FIND-SET(x)

Page 7: 21. Data Structures for Disjoint Sets Heejin Park College of Information and Communications Hanyang University

7

Disjoint-set operations

MAKE-SET(x) Given a member x, generate a set for x. MAKE-SET(9)

{{1, 2, 3}, {4, 8}, {5,7}} {{1, 2, 3}, {4, 8}, {5,7}, {9} }

Page 8: 21. Data Structures for Disjoint Sets Heejin Park College of Information and Communications Hanyang University

8

Disjoint-set operations

UNION(x, y) Given two members x and y, unite the set containing x and an

other set containing y. UNION(1,4) {{1, 2, 3}, {4, 8}, {5,7}} {{1, 2, 3, 4, 8}, {5,7}}

FIND-SET(x) Find the representative of the set containing x. FIND-SET(5): 7

Page 9: 21. Data Structures for Disjoint Sets Heejin Park College of Information and Communications Hanyang University

9

Disjoint-set data structures

Problem Developing data structures to maintain a collection of dyna

mic disjoint sets supporting disjoint-set operations, which are MAKE-SET(x), UNION(x,y), FIND-SET(x).

Page 10: 21. Data Structures for Disjoint Sets Heejin Park College of Information and Communications Hanyang University

10

Parameters for running time analysis #Total operations: m #MAKE-SET ops: n #UNION ops: u #FIND-SET ops: f m = n + u + f

Disjoint-set data structures

Page 11: 21. Data Structures for Disjoint Sets Heejin Park College of Information and Communications Hanyang University

11

u ≤ n – 1 n is the number of sets are generated by MAKE-SET ops. Each UNION op reduces the number of sets by 1. So, after n-1 UNION ops, we have only 1 set and then we

cannot do UNION op more.

Assumption The first n operations are MAKE-SET operations.

Disjoint-set data structures

Page 12: 21. Data Structures for Disjoint Sets Heejin Park College of Information and Communications Hanyang University

12

Contents

Disjoint-sets

Disjoint-set operations

An application of disjoint-set data structures

Disjoint-set data structures

Page 13: 21. Data Structures for Disjoint Sets Heejin Park College of Information and Communications Hanyang University

13

Computing connected components (CC) Static graph

Depth-first search: Θ(V+E)

Dynamic graph Depth-first search is inefficient. Maintaining a disjoint-set data structure is more efficient.

Application

Page 14: 21. Data Structures for Disjoint Sets Heejin Park College of Information and Communications Hanyang University

14

Connected component computation

a b

c d

e f

g

h j

i

{{a,b,c,d}, {e,f,g}, {h,i}, {j}} {{a,b,c,d}, {e,f,g}, {h, i, j}}

Depth first search: Θ(V + E) Disjoint-set data structures: UNION(h,j)

Page 15: 21. Data Structures for Disjoint Sets Heejin Park College of Information and Communications Hanyang University

15

Computing CC using disjoint set operations

CONNECTED-COMPONENTS(G)1 for each vertex v∈V[G]2 do MAKE-SET(v)3 for each edge (u, v) ∈ E[G]4 do if FIND-SET(u) ≠ FIND-SET(v)5 then UNION(u, v)

Connected component computation

Page 16: 21. Data Structures for Disjoint Sets Heejin Park College of Information and Communications Hanyang University

16

a b

c d

e f

g

h j

i

Connected component computation

Initial sets {a} {b} {c} {d} {e} {f} {g} {h} {i} {j}

(b,d) {a} {b,d} {c} {e} {f} {g} {h} {i} {j}

(e,g) {a} {b,d} {c} {e,g} {f} {h} {i} {j}

(a,c) {a,c} {b,d} {e,g} {f} {h} {i} {j}

Page 17: 21. Data Structures for Disjoint Sets Heejin Park College of Information and Communications Hanyang University

17

a b

c d

e f

g

h j

i

Connected component computation

(a,c) {a,c} {b,d} {e,g} {f} {h} {i} {j}

(h,i) {a,c} {b,d} {e,g} {f} {h,i} {j}

(a,b) {a,b,c,d} {e,g} {f} {h,i} {j}

(e,f) {a,b,c,d} {e,f,g} {h,i} {j}

(b,c) {a,b,c,d} {e,f,g} {h,i} {j}

Page 18: 21. Data Structures for Disjoint Sets Heejin Park College of Information and Communications Hanyang University

18

SAME-COMPONENT(u, v)1 if FIND-SET(u) = FIND-SET(v)2 then return TRUE3 else return FALSE

Connected component computation

Page 19: 21. Data Structures for Disjoint Sets Heejin Park College of Information and Communications Hanyang University

19

Contents

Disjoint-sets

Disjoint-set operations

An application of disjoint-set data structures

Disjoint-set data structures

Page 20: 21. Data Structures for Disjoint Sets Heejin Park College of Information and Communications Hanyang University

20

Disjoint-set data structures

Linked-list representation

Forest representation

Disjoint-set data structures

Page 21: 21. Data Structures for Disjoint Sets Heejin Park College of Information and Communications Hanyang University

21

Linked-list representation

Each set is represented by a linked list.

Members of a disjoint set are objects in a linked list.

The first object in the linked list is the representative.

All objects have pointers to the representative.

Linked-list representation

Page 22: 21. Data Structures for Disjoint Sets Heejin Park College of Information and Communications Hanyang University

22

Linked-list representation

{{b,c,e,h}, {d,f,g}}: Two linked lists are needed.

chead

tail

h e b

fhead g d

tail

Page 23: 21. Data Structures for Disjoint Sets Heejin Park College of Information and Communications Hanyang University

23

MAKE-SET(x) Θ(1)

FIND-SET(x) Θ(1)

Linked-list representation

xhead

tail

Page 24: 21. Data Structures for Disjoint Sets Heejin Park College of Information and Communications Hanyang University

24

Linked-list representation

bhead c e

tail

fhead d

tail

bhead c e

tail

f d

UNION(x,y): Attaching a linked list to the other

Page 25: 21. Data Structures for Disjoint Sets Heejin Park College of Information and Communications Hanyang University

25

UNION(x,y): Attaching a linked list to the other

Θ(m2) time where m2 is the number of objects in the linked list being attached. Changing tail pointer & linking two linked lists: Θ(1) Changing pointers to the representative: Θ(m2)

Linked-list representation

bhead c e

tail

f d

Page 26: 21. Data Structures for Disjoint Sets Heejin Park College of Information and Communications Hanyang University

26

Running time for m (= n + f + u) operations Simple implementation of union

O(n+f+u2) time O(m+n2) time Because u < n

A weighted-union heuristic O(n+f+ulgu) time O(m+mlgm) time

Linked-list representation

Page 27: 21. Data Structures for Disjoint Sets Heejin Park College of Information and Communications Hanyang University

27

Forest representation

Each set is represented by a tree.

Each member points to its parent.

The root of each tree is the rep.

Forest representation

{{b,c,e,h}, {f,d,g}}

c

h e

b

f

d

g

Page 28: 21. Data Structures for Disjoint Sets Heejin Park College of Information and Communications Hanyang University

28

MAKE-SET(x) 1 p[x] ← x

FIND-SET(x)1 if x = p[x]2 then return x3 else FIND-SET(p[x])4 return

Forest representation

Page 29: 21. Data Structures for Disjoint Sets Heejin Park College of Information and Communications Hanyang University

29

Union by rank Idea: Attach the shorter tree to the higher tree. Each node maintains a rank,which is an upper bound on the

height of the node. Compare the ranks of the two roots and attach the tree whos

e root’s rank is smaller to the other.

Forest representation

Page 30: 21. Data Structures for Disjoint Sets Heejin Park College of Information and Communications Hanyang University

30

c

h e

b

f

d

g

c

h e

b

d

g

f

Forest representation

Page 31: 21. Data Structures for Disjoint Sets Heejin Park College of Information and Communications Hanyang University

31

MAKE-SET(x) 1 p[x] ← x2 rank[x] ← 0

UNION(x, y)1 LINK(FIND-SET(x),

FIND-SET(y))

LINK(x, y)1 if rank[x] > rank[y]2 then p[y] ← x3 else p[x] ← y4 if rank[x] = rank[y]5 then rank[y] ← rank[y] + 1

Forest representation

Page 32: 21. Data Structures for Disjoint Sets Heejin Park College of Information and Communications Hanyang University

32

Path compression Change the parent to the root during FIND-SET(x).

Forest representation

FIND-SET(x)1 if x ≠ p[x]2 then p[x] ← FIND-SET(p[x])3 return p[x]

Page 33: 21. Data Structures for Disjoint Sets Heejin Park College of Information and Communications Hanyang University

33

f

e

d

c

b

a

f

edcba

Forest representation

Page 34: 21. Data Structures for Disjoint Sets Heejin Park College of Information and Communications Hanyang University

34

Worst case running time : O(m α(n))

α(n) ≤ 4 :for all practical situations.

Forest representation