design and analysis of algorithmssomchai/2110427/... · design and analysis of algorithms. . ˘ˇ...

14
Design and Analysis of Algorithms . . 2542 http://www.cp.eng.chula.ac.th/faculty/spj Advanced Data Structures Disjoint Set http://www.cp.eng.chula.ac.th/faculty/spj Outline Make-Set, Union, Find-Set Disjoint-Set Forests Union by Rank Path Compression Analysis http://www.cp.eng.chula.ac.th/faculty/spj Disjoint Sets : ADT • Make-Set( x ) • Union( s1, s2 ) • Find-Set( x ) : π( 1 ) : π( 1 ) : ν( log * n ) amortized cost http://www.cp.eng.chula.ac.th/faculty/spj Disjoint-Set Forests 1 2 3 4 8 6 7 5 9 {1, 2, 9, 4, 6} set #1 {3} set #3 {5, 7, 8} set #8

Upload: others

Post on 05-Aug-2020

21 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Design and Analysis of Algorithmssomchai/2110427/... · Design and Analysis of Algorithms. . ˘ˇ ˇ ˇ ˘ˆ˙ ˇ˝ ˆ˛ ˚˜ !˛" ˇ # 2542

Design and Analysis of Algorithms

. .

!"#2542

http://www.cp.eng.chula.ac.th/faculty/spj

!"## $%&'(()#* &$(+!$ ,*"&'(-"*%##++(&%# .# .$'/(0 1$/-1%#%

Advanced Data Structures

Disjoint Set

http://www.cp.eng.chula.ac.th/faculty/spj

Outline

Make-Set, Union, Find-Set

Disjoint-Set Forests

Union by Rank

Path Compression

Analysis

http://www.cp.eng.chula.ac.th/faculty/spj

Disjoint Sets : ADT

• Make-Set( x )

• Union( s1, s2 )

• Find-Set( x )

: π( 1 )

: π( 1 )

: ν( log * n ) amortized cost

http://www.cp.eng.chula.ac.th/faculty/spj

Disjoint-Set Forests

1

2

3

4

8

6

7 59

1, 2, 9, 4, 6

set #1

3

set #3

5, 7, 8

set #8

Page 2: Design and Analysis of Algorithmssomchai/2110427/... · Design and Analysis of Algorithms. . ˘ˇ ˇ ˇ ˘ˆ˙ ˇ˝ ˆ˛ ˚˜ !˛" ˇ # 2542

http://www.cp.eng.chula.ac.th/faculty/spj

Disjoint-Set : Find-Set

1

2 4

6

9

1, 2, 9, 4, 6set #1

Find-Set( 6 ) = Find-Set( 2 )

= Find-Set( 1 )= 1

π( h )

http://www.cp.eng.chula.ac.th/faculty/spj

Disjoint Set : Union

31

2 4

6

9

8

7 5

Union( 1, 8 )

http://www.cp.eng.chula.ac.th/faculty/spj

Disjoint Set : Union

3

1

2 4

6

9

8

7 5

Union( 1, 8 )

http://www.cp.eng.chula.ac.th/faculty/spj

Disjoint Set : Two Heuristics

Union : Smart Union

Find-Set : Path Compression

http://www.cp.eng.chula.ac.th/faculty/spj

Disjoint Set : Smart Union

Union by height

Union by size

http://www.cp.eng.chula.ac.th/faculty/spj

Smart Union1

2 4

6

9

8

7 53 1011

1

2 4

6

9 8

7 53 1011

1

2 4

6

9

8

7 53 1011

Union by heightUnion by size

Page 3: Design and Analysis of Algorithmssomchai/2110427/... · Design and Analysis of Algorithms. . ˘ˇ ˇ ˇ ˘ˆ˙ ˇ˝ ˆ˛ ˚˜ !˛" ˇ # 2542

http://www.cp.eng.chula.ac.th/faculty/spj

Smart Union : Worst-Case

http://www.cp.eng.chula.ac.th/faculty/spj

Smart Union : Worst-Case

http://www.cp.eng.chula.ac.th/faculty/spj

Smart Union : Worst-Case

http://www.cp.eng.chula.ac.th/faculty/spj

Smart Union : Worst-Case

http://www.cp.eng.chula.ac.th/faculty/spj

Smart Union : Worst-Case

http://www.cp.eng.chula.ac.th/faculty/spj

Smart Union : Worst-Case

Page 4: Design and Analysis of Algorithmssomchai/2110427/... · Design and Analysis of Algorithms. . ˘ˇ ˇ ˇ ˘ˆ˙ ˇ˝ ˆ˛ ˚˜ !˛" ˇ # 2542

http://www.cp.eng.chula.ac.th/faculty/spj

Smart Union : Worst-Case

http://www.cp.eng.chula.ac.th/faculty/spj

Smart Union : Worst-Case

http://www.cp.eng.chula.ac.th/faculty/spj

Smart Union : Worst-Case

http://www.cp.eng.chula.ac.th/faculty/spj

Smart Union : Worst-Case

http://www.cp.eng.chula.ac.th/faculty/spj

Smart Union : Worst-Case

http://www.cp.eng.chula.ac.th/faculty/spj

Smart Union : Worst-Case

Page 5: Design and Analysis of Algorithmssomchai/2110427/... · Design and Analysis of Algorithms. . ˘ˇ ˇ ˇ ˘ˆ˙ ˇ˝ ˆ˛ ˚˜ !˛" ˇ # 2542

http://www.cp.eng.chula.ac.th/faculty/spj

Smart Union : Worst-Case

http://www.cp.eng.chula.ac.th/faculty/spj

Smart Union : Worst-Case

http://www.cp.eng.chula.ac.th/faculty/spj

Smart Union : Worst-Case

http://www.cp.eng.chula.ac.th/faculty/spj

Smart Union : Worst-Case

height = π( log n )Union : π(1), Find-Set : π( log n )

http://www.cp.eng.chula.ac.th/faculty/spj

Path Compression

1 2

Find-Set( 1 ) = Find-Set( 2 ) = Find-Set( 3 ) = 7

7

p[3] = 7

3

http://www.cp.eng.chula.ac.th/faculty/spj

Path Compression

1 2

Find-Set( 1 ) = Find-Set( 2 ) = Find-Set( 3 ) = 7

7

3

p[2] = p[3] = 7

Page 6: Design and Analysis of Algorithmssomchai/2110427/... · Design and Analysis of Algorithms. . ˘ˇ ˇ ˇ ˘ˆ˙ ˇ˝ ˆ˛ ˚˜ !˛" ˇ # 2542

http://www.cp.eng.chula.ac.th/faculty/spj

Path Compression

1 2

Find-Set( 1 ) = Find-Set( 2 ) = Find-Set( 3 ) = 7

7

3

p[1] = p[2] = p[3] = 7

http://www.cp.eng.chula.ac.th/faculty/spj

Union by Rank

With path compression

Union by height : inefficient to recompute heights

Union by size : perfectly compatible

Union by rank

rank = estimated height

no height updates during path compression

http://www.cp.eng.chula.ac.th/faculty/spj

Make-Set

Make-Set( x ) p[x] = x rank[x] = 0

http://www.cp.eng.chula.ac.th/faculty/spj

Union by Rank

Union( s1, s2 ) if rank[s1] > rank[s2] p[s2] = s1 else p[s1] = s2 if rank[s1] = rank[s2] rank[s2]++

http://www.cp.eng.chula.ac.th/faculty/spj

Union by Rank

5

4

y

x

6

5x

y

Union(x, y)

Union(x, y)

5

4x

y

rank[x] < rank[y]

5

5

y

x

rank[x] = rank[y]

http://www.cp.eng.chula.ac.th/faculty/spj

Find-Set

Find-Set( x ) if x = p[x] return x p[x] = Find-Set( p[x] ) return p[x]

No rank is updated during path compression

Page 7: Design and Analysis of Algorithmssomchai/2110427/... · Design and Analysis of Algorithms. . ˘ˇ ˇ ˇ ˘ˆ˙ ˇ˝ ˆ˛ ˚˜ !˛" ˇ # 2542

http://www.cp.eng.chula.ac.th/faculty/spj

Union by Rank & Path Compression

n elements (n Make-Sets, at most n-1 Unions)

m Make-Set, Union, and Find-Set operations

m = Ω( n )

worst case = Ο( m α(m, n) )

α(m, n) is the inverse of Ackermann s function

a very very very slow function

http://www.cp.eng.chula.ac.th/faculty/spj

Ackermanns Function and Inverse

A( 1, j ) = 2 j for j ≥ 1

A( i, 1 ) = A( i-1, 2 ) for i ≥ 2

A( i, j ) = A( i-1, A(i, j-1) ) for i, j ≥ 2

α( m, n ) = min i ≥ 1 : A( i, m/n ) > lg n

http://www.cp.eng.chula.ac.th/faculty/spj

Ackermanns Function

162

22

2

22

2

22

162

22

2

22

162

22

22

2

22

22

232

22

42

22

12

22

32

42

1 2 3 4

1

2

3

ji

http://www.cp.eng.chula.ac.th/faculty/spj

Analysis

n elements (n Make-Sets, at most n-1 Unions)

m Make-Set, Union, and Find-Set operations

m = Ω( n )

union by rank, path compression

worst case = O( m α(m, n) ) = O( m log * n )

proof this12lg*

2

2

+=

n

nm

http://www.cp.eng.chula.ac.th/faculty/spj

Properties of Ranks : #1

For all tree roots x, size( x ) ≥ 2rank( x )

% Basis : rank = 0, 20 = 1 = only one root node

% Induction : Let+s Union( a, b )

rank is increased by 1 only if rank(a) = rank(b)

size( a ∪ b ) = size( a ) + size( b )

≥ 2rank[ a ] + 2rank[ b ]

= 2rank[ a ]+1 = 2rank[ a ∪ b ]

http://www.cp.eng.chula.ac.th/faculty/spj

Properties of Ranks : #2

The ranks of the nodes on a path from a leaf to a

root increase monotonically

0

1 0

2

0

1 0

3

0

1 0

2

0

1 0

4

x

y

z

0

1

0

2

0

1 0

3

0

1 0

2

0

1 0

4

x y z

Find-Set( x )

Page 8: Design and Analysis of Algorithmssomchai/2110427/... · Design and Analysis of Algorithms. . ˘ˇ ˇ ˇ ˘ˆ˙ ˇ˝ ˆ˛ ˚˜ !˛" ˇ # 2542

http://www.cp.eng.chula.ac.th/faculty/spj

Properties of Ranks : #3

The number of nodes of rank r is at most n/2r

each node of rank r is the root of a subtree of at least

2r nodes (property #1)

all subtrees are disjoint

∴ there are at most n / 2r disjoint subtrees of rank r

there are at most n / 2r nodes of rank r

http://www.cp.eng.chula.ac.th/faculty/spj

Properties of Ranks

For all tree roots x, size(x) ≥ 2rank(x)

The ranks of the nodes on a path from a leaf to a

root increase monotonically

The number of nodes of rank r is at most n/2r

http://www.cp.eng.chula.ac.th/faculty/spj

Analysis

Actual cost of Find-Set( x ) is the number of nodes

of the path from x to the root

Accounting method : deposit 1 baht for every node

on the path from x to the root

For m operations, how many bahts did we spend ?

http://www.cp.eng.chula.ac.th/faculty/spj

Rank Grouping

Partition ranks into groups

group 0 contains only elements of rank 0

rank r goes to group G(r)

the largest rank in any group g is F(g), F = G -1

the number of ranks in any group g > 0 is F(g)-F(g-1)

http://www.cp.eng.chula.ac.th/faculty/spj

Rank Grouping : Example

Group Rank

0 0

1 1

2 2, 3, 4

3 5, 6, ..., 9

4 10, 11, , 16

i (i-1)2+1, , i2

F( g ) = g2

G( r ) = r0.5

http://www.cp.eng.chula.ac.th/faculty/spj

centralaccount

Money Deposit

8

7

5

4

2

1

0

x

Find-Set( x )

+1

+1

+1

9

+1 +1 +1Ran

k #

s ar

e m

onot

onic

ally

incr

easi

ng

group 0

group 1

group 2

+1

+1

+1

Page 9: Design and Analysis of Algorithmssomchai/2110427/... · Design and Analysis of Algorithms. . ˘ˇ ˇ ˇ ˘ˆ˙ ˇ˝ ˆ˛ ˚˜ !˛" ˇ # 2542

http://www.cp.eng.chula.ac.th/faculty/spj

Money Deposit

87542

1

0

x

After path compression99

+1 +1 +1

+1 +1 +1

centralaccount

All of the deposited nodes

along the access path get

a new parent of higher

rank.

http://www.cp.eng.chula.ac.th/faculty/spj

Money Deposit Rules

Deposit one baht at the central account if

v is the root

the parent of v is the root

the parent of v is in a different group from v

Otherwise deposit one baht into the node

http://www.cp.eng.chula.ac.th/faculty/spj

Central Account

After m operations, the total deposit at the central

account is at most m( G(n) + 2 )

Each operation deposits at most ( G(n) + 2 ) bahts.

http://www.cp.eng.chula.ac.th/faculty/spj

8

5

0

9

Central Account Deposit

7

4

2

1

3 groups : G(n) = 2

8

9

+2 bahts

5

0

G(n) bahts

Find-Set( x )x

http://www.cp.eng.chula.ac.th/faculty/spj

Central Account

Each operation deposits at most ( G(n) + 2 ) bahts.

The total deposit at the central account, after m

operations, is at most m( G(n) + 2 )

http://www.cp.eng.chula.ac.th/faculty/spj

Node Accounts

Let node v be in group g.

How many bahts can be deposited into node v ?

If one baht is deposited into a node v, v will get a

new parent of higher rank.

Page 10: Design and Analysis of Algorithmssomchai/2110427/... · Design and Analysis of Algorithms. . ˘ˇ ˇ ˇ ˘ˆ˙ ˇ˝ ˆ˛ ˚˜ !˛" ˇ # 2542

http://www.cp.eng.chula.ac.th/faculty/spj

Money Deposit

8

7

5

4

2

1

0

x

Find-Set( x )9

http://www.cp.eng.chula.ac.th/faculty/spj

Money Deposit

8

7

5

4

2

1

0

x

Find-Set( x )9

+1

+1

+1

http://www.cp.eng.chula.ac.th/faculty/spj

Money Deposit

87542

1

0

x

After path compression99

+1 +1 +1

All of the deposited nodes

along the access path get

a new parent of higher

rank.

http://www.cp.eng.chula.ac.th/faculty/spj

Node Accounts

Let node v be in group g.

How many bahts can be deposited into node v ?

If one baht is deposited into a node v, v will get a

new parent of higher rank.

How many times can v be moved before its parent

get pushed out of group g ?

Answer : the number of ranks in group g

= F(g) - F(g-1) - 1

( F(g) is the largest rank # in group g )

http://www.cp.eng.chula.ac.th/faculty/spj

Node Account Deposit : Example

0 0 0 0 0 00 0 0 0 0 0 0 0 0 0

http://www.cp.eng.chula.ac.th/faculty/spj

Node Account Deposit : Example

0

1 0 0 0 00 0 0 0 0 0 0 0 0 0

Page 11: Design and Analysis of Algorithmssomchai/2110427/... · Design and Analysis of Algorithms. . ˘ˇ ˇ ˇ ˘ˆ˙ ˇ˝ ˆ˛ ˚˜ !˛" ˇ # 2542

http://www.cp.eng.chula.ac.th/faculty/spj

Node Account Deposit : Example

0

1 0 00 0 0 0 0 0 0 0 0 0

0

1

http://www.cp.eng.chula.ac.th/faculty/spj

Node Account Deposit : Example

0

1 0 0 0 0 0 0 0 0 0 0

0

1

0

1

http://www.cp.eng.chula.ac.th/faculty/spj

Node Account Deposit : Example

0

1 0 0 0 0 0 0 0 0

0

1

0

1

0

1

http://www.cp.eng.chula.ac.th/faculty/spj

Node Account Deposit : Example

0

1 0 0 0 0 0 0

0

1

0

1

0

1

0

1

http://www.cp.eng.chula.ac.th/faculty/spj

Node Account Deposit : Example

0

1 0 0 0 0

0

1

0

1

0

1

0

1

0

1

http://www.cp.eng.chula.ac.th/faculty/spj

Node Account Deposit : Example

0

1 0 0

0

1

0

1

0

1

0

1

0

1

0

1

Page 12: Design and Analysis of Algorithmssomchai/2110427/... · Design and Analysis of Algorithms. . ˘ˇ ˇ ˇ ˘ˆ˙ ˇ˝ ˆ˛ ˚˜ !˛" ˇ # 2542

http://www.cp.eng.chula.ac.th/faculty/spj

Node Account Deposit : Example

0

1

0

1

0

1

0

1

0

1

0

1

0

1

0

1

http://www.cp.eng.chula.ac.th/faculty/spj

Node Account Deposit : Example

0

1 0

2

0

1

0

1

0

1

0

1

0

1

0

1

http://www.cp.eng.chula.ac.th/faculty/spj

Node Account Deposit : Example

0

1 0

2

0

1 0

2

0

1

0

1

0

1

0

1

http://www.cp.eng.chula.ac.th/faculty/spj

Node Account Deposit : Example

0

1 0

2

0

1 0

2

0

1 0

2

0

1

0

1

http://www.cp.eng.chula.ac.th/faculty/spj

Node Account Deposit : Example

0

2

0

1 0

2

0

1 0

2

0

1 0

2

0

1

http://www.cp.eng.chula.ac.th/faculty/spj

Node Account Deposit : Example

0

2

0

1 0

3

0

1 0

2

0

1 0

2

0

0

1

Page 13: Design and Analysis of Algorithmssomchai/2110427/... · Design and Analysis of Algorithms. . ˘ˇ ˇ ˇ ˘ˆ˙ ˇ˝ ˆ˛ ˚˜ !˛" ˇ # 2542

http://www.cp.eng.chula.ac.th/faculty/spj

Node Account Deposit : Example

0

2

0

1 0

3

0

1 0

2

0

1 0

2

+1

0

1

http://www.cp.eng.chula.ac.th/faculty/spj

Node Account Deposit : Example

0

2

0

1 0

3

0

1 0

2

0

1 0

3

+1

0

1

http://www.cp.eng.chula.ac.th/faculty/spj

Node Account Deposit : Example

0

2

0

1 0

3

0

1 0

2

0

1 0

4

+1

0

1

http://www.cp.eng.chula.ac.th/faculty/spj

Node Account Deposit : Example

0

2

0

1 0

3

0

1 0

2

0

1 0

4

+2

0

1

Group Rank

0 0

1 1, 2, 3

2 4, 5, ..., 8

Any node in group 1 can be deposited

at most 3 -1 = 2 bahts.

http://www.cp.eng.chula.ac.th/faculty/spj

The Number of Nodes in a Group

∑+−=

)(

1)1( 2)(

gF

gFrr

ngV

∑∞

+−=

1)1( 2

1

gFrr

n

∑∞

=

+−=

01)1(

2

1

2 ssgF

n

1)1(2

2

+−=

gF

n

)1(2

=gF

n

The number of nodes, V(g),

in group g > 0 is at most

n/2F(g-1)

http://www.cp.eng.chula.ac.th/faculty/spj

Total Node Deposit

The maximum number of bahts deposited to all

nodes in group g is at most n F(g) / 2F(g-1)

the number of nodes in group g > 0 is at most n/2F(g-1)

each node gets at most F(g)-F(g-1)-1 bahts

F(g) - F(g-1) - 1 < F(g)

The total node deposit is at most Σ n F(g) / 2F(g-1)g=1

G(n)

Page 14: Design and Analysis of Algorithmssomchai/2110427/... · Design and Analysis of Algorithms. . ˘ˇ ˇ ˇ ˘ˆ˙ ˇ˝ ˆ˛ ˚˜ !˛" ˇ # 2542

http://www.cp.eng.chula.ac.th/faculty/spj

Total Deposit

m ( G(n)+2 ) + n Σ F(g) / 2F(g-1)g=1

G(n)

Central account deposit Total node deposit

What are G(n) and F(g) ?

http://www.cp.eng.chula.ac.th/faculty/spj

Total Deposit

m ( G(n)+2 ) + n Σ F(g) / 2F(g-1)g=1

G(n)

Let F( g ) = g, G( r ) = r

m ( n + 2 ) + n Σ g / 2(g-1)g=1

n

< m ( n + 2 ) + 2n

= O( m n )

Very loose upper bound, worse than O( m log n )

http://www.cp.eng.chula.ac.th/faculty/spj

Total Deposit

m ( G(n)+2 ) + n Σ F(g) / 2F(g-1)g=1

G(n)

Let F( g ) = 2F(g-1), G( r ) = 1 + lg* n

m ( 1+lg*n + 2 ) + n Σ 2(g-1) / 2(g-1)g=1

1+lg*n

= m ( 2+lg*n ) + n( 1+lg*n )

= O( m log* n ) m = Ω(n)

http://www.cp.eng.chula.ac.th/faculty/spj

Conclusion

n elements (n Make-Sets, at most n-1 Unions)

m Make-Set, Union, and Find-Set operations

m = Ω( n )

union by rank, path compression

worst case = O( m log* n ) : almost linear

amortized cost = O( log* n ) : almost constant