Design and Analysis of Algorithms - - PowerPoint PPT Presentation

design and analysis of algorithms
SMART_READER_LITE
LIVE PREVIEW

Design and Analysis of Algorithms - - PowerPoint PPT Presentation

Design and Analysis of Algorithms


slide-1
SLIDE 1

Design and Analysis of Algorithms

. . ก ก ก 2542

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

ก ก กกก

Advanced Data Structures

Fibonacci Heaps

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

Outline

Lazy Union + Cascading Cut Decrease Key Analysis

slide-2
SLIDE 2

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

Lazy Bino. Heap vs. Fibonacci

  • Make-Heap
  • Insert
  • Minimum
  • Extract-Min
  • Union
  • Decrease-Key
  • Delete

: π( 1 ) : π( 1 ) : π( 1 ) : ν( log n ) * : π( 1 ) : π( log n ) : π( log n ) : π( 1 ) : π( 1 ) : π( 1 ) : ν( log n ) * : π( 1 ) : π( 1 ) * : O( log n ) *

* amortized cost

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

Fibonacci Heaps

Lazy union : π(1) Cascading cut in Decrease-Key

achieve π(1) amortized time in Decrease-Key maintain O(log n) amortized time in Extract-Min

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

Fibonacci Heaps : Simple Cut

2 5 21 9 32 20 11 13 40 18 H

Fib-Heap-Decrease-Key( H, x, 4 )

x

20

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

Fibonacci Heaps : Simple Cut

5 9 32 20 4 13 40 18 H 2 21 20

slide-3
SLIDE 3

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

Fibonacci Heaps : Simple Cut

20 5 9 32 20 4 13 40 18 H

Decrease-Key : Simple Cut is π(1). More than one tree of the same rank. Trees are not necessarily binomial trees

2 21

Extract-Min is no longer amortized O( log n ).

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

Simple Cut Doesnt Work

5 9 32 20 11 13 40 18 H

Fib-Heap-Delete( H, x ) { Fib-Heap-Decrease-Key( H, x, -inf ) Fib-Heap-Extract-Min( H ) }

x

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

Simple Cut Dosent Work

5 9 32 20 11 13 40

Λ⊗

H

Fib-Heap-Delete( H, x ) { Fib-Heap-Decrease-Key( H, x, -inf ) Fib-Heap-Extract-Min( H ) }

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

Simple Cut Dosent Work

5 9 32 20 11 13 40 H

Fib-Heap-Delete( H, x ) { Fib-Heap-Decrease-Key( H, x, -inf ) Fib-Heap-Extract-Min( H ) }

slide-4
SLIDE 4

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

Simple Cut Dosent Work

5 9 32 20 11 13 40 H

Fib-Heap-Delete( H, x ) { Fib-Heap-Decrease-Key( H, x, -inf ) Fib-Heap-Extract-Min( H ) }

The rank of the root is no longer O( log n )

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

Lazy Binomial Heap : Analysis

Invarient : one baht credited at each root node for future consolidating task.

H

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

Lazy Binomial Heap : Analysis

Insert : amortized cost = 2

  • ne for list insertion + one stored at the root

H

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

Lazy Binomial Heap : Analysis

Extract-Min : amortized cost = O( log n )

H

slide-5
SLIDE 5

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

Lazy Binomial Heap : Analysis

Extract-Min : amortized cost = O( log n )

Delete minimum root using the credit stored at the root H

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

Lazy Binomial Heap : Analysis

Extract-Min : amortized cost = O( log n )

consolidate O( log n ) children

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

Simple Cut Doesnt Work

delete root consolidate

Extract-Min

costs π( n ) rank is π( n ) To maintain O( log n ) Extract-Min, node rank must be O( log n )

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

Cascading Cut

Guarantees any node rank to be O( log n ) Extract-Min : O( log n ) Decrease-Key : π( 1 )

Amortized cost

slide-6
SLIDE 6

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

Marking Nodes

Mark a (nonroot) node the first time that it loses a child (because of a simple cut). If a marked node loses another child, then cut it from its parent causing cascading cut

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

2 20 5 12 21 9 32 20 11 13 40 18 30 H

Cascading Cuts

Fib-Heap-Decrease-Key( H, x, 7 )

7 4 19 31 12 22 33 45 28

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

2 20 5 12 21 9 32 20 7 13 40 18 30 H

Cascading Cuts

Fib-Heap-Decrease-Key( H, x, 8 )

8 4 19 31 12 22 33 45 28

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

2 20 5 12 21 9 32 8 7 13 40 18 30 H

Cascading Cuts

Fib-Heap-Decrease-Key( H, x, 11 )

11 4 19 31 12 22 33 45 28

slide-7
SLIDE 7

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

2 20 5 12 21 9 32 8 7 11 40 18 30 H

Cascading Cuts

4 19 31 12 22 33 45 28

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

2 20 5 12 21 9 32 8 7 11 40 18 30 H

Cascading Cuts

4 19 31 12 22 33 45 28

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

2 20 5 12 21 9 32 8 7 11 40 18 30 H

Cascading Cuts

4 19 31 12 22 33 45 28

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

Cascading Cuts : Two Issues

Why is Decrease-Key Θ(1) ? Why is any node rank O( log n ) ?

i.e., Extract-Min is O( log n )

slide-8
SLIDE 8

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

Decrease-Key is Θ(1)

Let w be a cut node

2 A 1

w

The amortized cost of Decrease-Key is 4

two is credited at ws parent node

  • ne is for a simple cut at node w

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

Decrease-Key is Θ(1)

Let w be a cut node

2 A 1

w

The amortized cost of Decrease-Key is 4

two is credited at ws parent node

  • ne is for a simple cut at node w
  • ne is credited at w after being

a new root

1 ...

One baht at each root node will be used during consolidation.

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

Decrease-Key is Θ(1)

A B C 1

x y z

amortized cost : actual cost : Cut( x )

2

+2 (2

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

Decrease-Key is Θ(1)

A B C 2 1

x y z

amortized cost : (2+1 actual cost : (1 Cut( x )

slide-9
SLIDE 9

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

Decrease-Key is Θ(1)

B C 2 1

y z

amortized cost : (2+1+1 actual cost : (1 Cut( x ) +1

A 0x 1

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

Decrease-Key is Θ(1)

B C 2 1

y z

amortized cost : (2+1+1)+ actual cost : (1)+ Cut( y )

A 1x

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

Decrease-Key is Θ(1)

B C 2 1

y z

amortized cost : (2+1+1)+(2+ actual cost : (1)+ Cut( y )

A 1x

+2

4

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

Decrease-Key is Θ(1)

B C 4 1

y z

amortized cost : (2+1+1)+(2+1 actual cost : (1)+(1 Cut( y )

A 1x

slide-10
SLIDE 10

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

Decrease-Key is Θ(1)

B C 4 1

y z

amortized cost : (2+1+1)+(2+1+1 actual cost : (1)+(1 Cut( y )

A 1x

+1

1

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

Decrease-Key is Θ(1)

B C 1 4 1

y z

amortized cost : (2+1+1)+(2+1+1) actual cost : (1)+(1

A 1x

Cut( y ) -> Cut( z ) +2

2 2

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

Decrease-Key is Θ(1)

B C 1 1 2 1

y z

amortized cost : (2+1+1)+(2+1+1) actual cost : (1)+(1+1)

A 1x

Cut( y ) -> Cut( z )

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

Money at Marked Nodes

Four bahts are used to cut node w whose parent is node v

two bahts are transferred to v

  • ne baht is for the cut
  • ne baht is stored on w for future consolidation

2

w v

slide-11
SLIDE 11

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

Money at Marked Nodes

If node v loses only one child, it has two bahts

  • ne baht is for future cut (caused by cascading cut)

the other baht is for future consolidation

2 v

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

Money at Marked Nodes

If node v loses two children, it has four bahts

two bahts are tranferred to its parent (may cause further cut).

  • ne baht is for cutting the node

another baht is for future consolidation

4 1

v

2 2 1

v

1 2 1

v

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

x v1 v2 v3 vi-1... ... vi

Ranks of Children

Rank of v1≥ 0 Rank of x is i Rank of v2≥ 0 Rank of v3≥ 1 Rank of vi-1≥ i-3 Rank of vi ≥ i-2

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

x

Ranks of Chlidren

Rank of v1≥ 0

slide-12
SLIDE 12

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

x

Ranks of Chlidren

Rank of v1≥ 0

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

x

Ranks of Chlidren

Rank of v1≥ 0

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

x v1 v2 v3 vi-1... ... vi

Ranks of Chlidren

Rank of v1≥ 0 Rank of x is i Rank of v2≥ 0 Rank of v3≥ 1 Rank of vi-1≥ i-3 Rank of vi ≥ i-2

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

Ranks of Chlidren

x Rank of x ≥ i-1 vi Rank of vi ≥ i-1

a node could have lost at most one child

i-2

slide-13
SLIDE 13

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

Ranks of Children

The ith youngest child of any node x has rank of at least i-2 Let Sr the size of the smallest tree whose root has rank r

  • Sr ≥ Fr+2

Fibonacci Number

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

Fibonacci Numbers

Fk = Fk-1+ Fk-2 k ≥ 2, F0 = 0, F1 = 1 (Fibonacci #) Σ

i = 0 k

Fk+2 = 1 + Fi

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

x v1 v2 v3 vr-1... ... vr

Sr ≥ Fr+2

Rank of v1≥ 0 Rank of x is r Rank of v2≥ 0 Rank of v3≥ 1 Rank of vr-1≥ r-3 Rank of vr ≥ r-2

≥ S0 ≥ S0 ≥ S1 ≥ Sr-3 ≥ Sr-2

Σ

i = 0 r-2

1 + S0 + Si

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

Sr ≥ Fr+2

− =

+ + =

2

1

r i i r

S S S

− =

+ =

2

2

r i i

S

− = +

+ ≥

2 2

2

r i i

F

=

+ =

r j j

F

2

2

=

+ =

r j j

F 1

2 +

=

r

F

S0 = 1 Si ≥ Fi+2 j = i+2 F0 = 0, F1 = 1 Σ

j = 0 k

Fk+2 = 1 + Fj

slide-14
SLIDE 14

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

rank = O(log n)

Let Sr the size of the smallest tree whose root has rank r, Sr ≥ Fr+2

≥ φ r

n ≥ Sr ≥ φ r The max. degree (rank) of any node in an n-node Fibonacci heap is O( log n ) Extract-Min : Consolidation takes O( log n ) amortized time.

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

Conclusion

Lazy Union + Cascading Cut

Insert is Θ(1) Union is Θ(1) Decrease-Key is Θ(1) : amortized cost Extract-Min is O( log n ) : amortized cost