Chapter 21 Data Structures for Disjoint Sets Lee, Hsiu-Hui Ack: This presentation is based on the...

15
Chapter 21 Data Structures for Disjoint Sets Lee, Hsiu-Hui Ack: This presentation is based on the lecture slides fro m Prof. Tsai, Shi-Chun as well as various materials from t he web. .

description

chap21Hsiu-Hui Lee3 Connected Component: an application ab cd ef g h i j (a)

Transcript of Chapter 21 Data Structures for Disjoint Sets Lee, Hsiu-Hui Ack: This presentation is based on the...

Page 1: Chapter 21 Data Structures for Disjoint Sets Lee, Hsiu-Hui Ack: This presentation is based on the lecture slides from Prof. Tsai, Shi-Chun as well as various.

Chapter 21

Data Structures for Disjoint Sets Lee, Hsiu-Hui

Ack: This presentation is based on the lecture slides from Prof. Tsai, Shi-Chun as well as various materials from t

he web..

Page 2: Chapter 21 Data Structures for Disjoint Sets Lee, Hsiu-Hui Ack: This presentation is based on the lecture slides from Prof. Tsai, Shi-Chun as well as various.

20080111 chap21 Hsiu-Hui Lee 2

Disjoint-set Data Structure• To maintain a collection of disjoint dynamic sets

S = {S1, S2, …, Sk}.

Each set Si is identified by a representative x=rep[Si].

• Operations:MAKE-SET(x): creates a new set whose only member is x.

with rep[{x}] = x (for any x ∉ Si for all i).

UNION(x, y): replaces sets Sx, Sy with Sx ∪ Sy in S for any x, y in distinct sets Sx, Sy .

FIND-SET(x): returns representative of set Sx containing x.

Page 3: Chapter 21 Data Structures for Disjoint Sets Lee, Hsiu-Hui Ack: This presentation is based on the lecture slides from Prof. Tsai, Shi-Chun as well as various.

20080111 chap21 Hsiu-Hui Lee 3

Connected Component: an application

a b

c d

e f

g

h

i

j

(a)

Page 4: Chapter 21 Data Structures for Disjoint Sets Lee, Hsiu-Hui Ack: This presentation is based on the lecture slides from Prof. Tsai, Shi-Chun as well as various.

20080111 chap21 Hsiu-Hui Lee 4

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)

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

Page 5: Chapter 21 Data Structures for Disjoint Sets Lee, Hsiu-Hui Ack: This presentation is based on the lecture slides from Prof. Tsai, Shi-Chun as well as various.

20080111 chap21 Hsiu-Hui Lee 5

Linked-list representation of disjoint sets

The first object in each linked list serve as its set’s representative

Page 6: Chapter 21 Data Structures for Disjoint Sets Lee, Hsiu-Hui Ack: This presentation is based on the lecture slides from Prof. Tsai, Shi-Chun as well as various.

20080111 chap21 Hsiu-Hui Lee 6

MAKE-SET(x) : O(1)FIND-SET(x) : O(1)UNION(x, y) : by appending x’s list onto the end of y’s list

UNION (e, g)

Page 7: Chapter 21 Data Structures for Disjoint Sets Lee, Hsiu-Hui Ack: This presentation is based on the lecture slides from Prof. Tsai, Shi-Chun as well as various.

20080111 chap21 Hsiu-Hui Lee 7

A simple implementation of union

m: # of operation = 2n-1Amortized time : Θ (n)

Θ(n)

Θ (1+2+...+n)=Θ (n2)

Page 8: Chapter 21 Data Structures for Disjoint Sets Lee, Hsiu-Hui Ack: This presentation is based on the lecture slides from Prof. Tsai, Shi-Chun as well as various.

20080111 chap21 Hsiu-Hui Lee 8

A weight-union heuristic

• In simple implementation of union, we may be appending a longer list onto a shorter list

• Weighted-union heuristicTo append the smaller list onto the longer

Page 9: Chapter 21 Data Structures for Disjoint Sets Lee, Hsiu-Hui Ack: This presentation is based on the lecture slides from Prof. Tsai, Shi-Chun as well as various.

20080111 chap21 Hsiu-Hui Lee 9

Theorem 21.1• Using the linked-list representation of disjoint

sets and the weight-union heuristic, a sequence of m MAKE-SET, UNION, and FIND-SET operations, n of which are MAKE-SET operations, takes O( m + n lg n) time.

Page 10: Chapter 21 Data Structures for Disjoint Sets Lee, Hsiu-Hui Ack: This presentation is based on the lecture slides from Prof. Tsai, Shi-Chun as well as various.

20080111 chap21 Hsiu-Hui Lee 10

Disjoint-set forests• We represent sets by rooted trees.• Each node contains one member and each tree

represents one set.• In a disjoint-set forest, each member points to

its parent.

UNION (e, g)

Page 11: Chapter 21 Data Structures for Disjoint Sets Lee, Hsiu-Hui Ack: This presentation is based on the lecture slides from Prof. Tsai, Shi-Chun as well as various.

20080111 chap21 Hsiu-Hui Lee 11

Heuristics to improve the running time• Union by rank

The root with smaller rank is made to point to the root with larger rank during a UNION operation

• Path compressionDuring FIND-SET, to make each node on the

find path point directly to the root.

Page 12: Chapter 21 Data Structures for Disjoint Sets Lee, Hsiu-Hui Ack: This presentation is based on the lecture slides from Prof. Tsai, Shi-Chun as well as various.

20080111 chap21 Hsiu-Hui Lee 12

Before FIND-SET(a) After FIND-SET(a)

Page 13: Chapter 21 Data Structures for Disjoint Sets Lee, Hsiu-Hui Ack: This presentation is based on the lecture slides from Prof. Tsai, Shi-Chun as well as various.

20080111 chap21 Hsiu-Hui Lee 13

Pseudo code for disjoint-set forestsMAKE-SET(x)1 p[x] x2 rank[x] 0

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

path compression

Page 14: Chapter 21 Data Structures for Disjoint Sets Lee, Hsiu-Hui Ack: This presentation is based on the lecture slides from Prof. Tsai, Shi-Chun as well as various.

20080111 chap21 Hsiu-Hui Lee 14

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

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

union by rank

Page 15: Chapter 21 Data Structures for Disjoint Sets Lee, Hsiu-Hui Ack: This presentation is based on the lecture slides from Prof. Tsai, Shi-Chun as well as various.

20080111 chap21 Hsiu-Hui Lee 15

Effect of the heuristics

• Either union by rank or path compression improves the running time.O(m lg n) --- union by rank Θ(n + f ‧ (1+log2+f/nn)) --- path comprsssion

• The improvement is greater when the two heuristics are used together.O(m (n)) --- both