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

Binomial Heaps

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

Outline

Priority Queue : ADT Binary Heap Binomial Trees Binomial Heaps Operations

slide-2
SLIDE 2

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

Priority Queue : ADT

  • Make-Heap()
  • Insert( H, x )
  • Minimum( H )
  • Extract-Min( H )
  • Union( H1, H2 )
  • Decrease-Key( H, x, k )
  • Delete( H, x )

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

Binary Heap

4 10 30 14 20 43 100 17

π( log n )

4 10 30 14 20 43 100 17

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

Binary Heap : Insert

4 10 30 14 20 43 100 17 4 10 30 14 20 43 100 17 7 7 Bubble up

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

Binary Heap : Insert

4 10 30 14 20 43 100 17 4 10 30 14 20 43 100 17 7 7

π( log n )

slide-3
SLIDE 3

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

Binary Heap : Extract-Min

4 10 30 14 20 43 100 17 4 10 30 14 20 43 100 17 7 7

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

Binary Heap : Extract-Min

4 10 30 14 20 43 100 17 4 10 30 14 20 43 100 17 7 7

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

Binary Heap : Extract-Min

10 30 14 20 43 100 17 14 10 30 20 43 100 17 7 7 Percolate down

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

Binary Heap : Extract-Min

10 30 14 20 43 100 17 14 10 30 20 43 100 17 7 7

π( log n )

slide-4
SLIDE 4

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

Binary Heap : Union

10 30 14 20 43 100 17 14 10 30 20 43 100 17 7 7 50 23 4 50 23 4

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

Binary Heap : Union

10 30 14 20 43 100 17 14 10 30 20 43 100 17 7 7 50 23 4 50 23 4

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

Binary Heap : Union

10 30 14 20 43 100 17 7 50 23 4

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

Binary Heap : Union

10 30 14 43 100 17 7 50 23 4 20

slide-5
SLIDE 5

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

Binary Heap : Union

10 30 14 20 43 100 17 7 50 23 4

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

Binary Heap : Union

10 30 4 20 43 100 17 7 50 23 14

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

Binary Heap : Union

10 30 4 20 43 100 17 7 50 23 14

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

Binary Heap : Union

4 30 10 20 43 100 17 7 50 23 14

slide-6
SLIDE 6

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

Binary Heap : Union

7 30 10 20 43 100 17 4 50 23 14

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

Binary Heap : Union : Analysis

n

21

n

22

n

23

n

2h x 1 x 2 x 3 x h

) ( ) ( 2

lg

n O h O n

n h h

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

Binary Heap & Binomial Heap

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

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

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

Binomial Trees

B0 B1 B2 B3 B4 Bk-1 Bk-1 Bk

slide-7
SLIDE 7

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

Properties of Binomial Trees

B4

4 = 4C1 6 = 4C2 4 = 4C3 1 = 4C4

Binomial Coef.

24 1 = 4C0 # nodes

The height of B4 is 4 Root of B4 has degree 4

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

Properties of Binomial Trees

For the binomial tree Bk

there are 2k nodes the height of the tree is k there are exactly kCi nodes at depth i the root has degree k

The max. degree of any node in an n-node binomial tree is lg n

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

Binomial Heaps

A list of binomial trees satisfying these properties

each binomial tree is heap-ordered (the root of a tree contains the smallest key in the tree) there is at most one Bk for any given k in the heap (an n-node binomial heap consists of at most 1 + lg n binomial trees)

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

Heap-Ordered Property

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

slide-8
SLIDE 8

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

Structural Property

B0 B2 B3

H = ( B3 , B2 , B0 ) has 23 + 22 + 20 = 1310= 11012 nodes

The binary representation of 1310

has 4 bits

The binary representation of n has (1 + lg n) bits An n-node binomial heap has at most (1 + lg n) binomial trees

B3 B2 B0

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

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

Binomial Heaps: Minimum

20 20 2

π( #trees )= O( 1 + lg n ) = O( log n )

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

Binomial Heap : Union

2 20 5 12 21 9 32 20 11 13 40 18 30 H1 8 10 31 12 44 H2

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

Binomial Heap : Union

2 20 5 12 21 9 32 20 11 13 40 18 30 H1 8 10 31 12 44 H2

slide-9
SLIDE 9

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

20 10

Binomial Heap : Union

2 5 12 21 9 32 20 11 13 40 18 30 H1 8 31 12 44 H2

B1 B2 B2

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

Binomial Heap : Union

2 20 5 12 21 9 32 20 11 13 40 18 30 H1 8 10 31 12 44 H2

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

Binomial Heap : Union

2 20 5 12 21 9 32 20 11 13 40 18 30 H1 10 8 31 12 44 H2

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

Binomial Heap : Union

2 20 5 12 21 9 32 20 11 13 40 18 30 H1 10 H2

B3 B3

8 31 12 44

slide-10
SLIDE 10

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

Binomial Heap : Union

20 5 9 32 20 11 13 40 18 H1 10 H2 2 12 21 30 8 31 12 44

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

Binomial Heap : Union

20 5 9 32 20 11 13 40 18 H1 10 H2 2 12 21 30 8 31 12 44

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

Binomial Heap : Union

20 H1 10 5 9 32 20 11 13 40 18 2 12 21 30 8 31 12 44

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

Binomial Heap Union : Analysis

...

H1

n1 nodes

≤ 1+lg n1 roots

...

H2

n2 nodes

≤ 1+lg n2 roots

Binomial_Heap_Union

...

H

n = n1+ n2 nodes

≤ 2+lg n1+ lg n2< 2+ lg n+ lg n = O( log n )

slide-11
SLIDE 11

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

Binomial Heap Union : Analysis

13 nodes 5 nodes

Binomial_Heap_Union

18 nodes

H1

B0 B2 B3

H2

B0 B2

H

B1 B4 1 1 0 1

1 0 1 1 0 0 1 0

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

Binomial Heap Union : Analysis

T1 : Union H1 and H2 of size n1 and n2 , respectively. T2 : Carry-ripple adding numbers n1 and n2 in binary

  • T1 = O( T2 )

H1

B0 B2 B3

1 1 0 1 Let n = n1 + n2 ⇒

1 + lg n bits

T2 = Θ( 1 + lg n ) = Θ( log n )

  • T1 = O( log n )

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

Binomial Heap : Insert

H*

...

H

...

H

O( log n )

Binomial_Heap_Union Binomial_Heap_Insert

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

Building Binomial Heap

Build an n-node binomial heap by n successive insertions An insertion costs O( log n ) n insertions cost O( n log n ) Correct but not tight !

slide-12
SLIDE 12

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

Building Binomial Heap

H

0 0 0

b0 b1 b2

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

Building Binomial Heap

H

1 0 0

b0 b1 b2

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

Building Binomial Heap

H

0 1 0

b0 b1 b2

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

Building Binomial Heap

H

1 1 0

b0 b1 b2

slide-13
SLIDE 13

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

Building Binomial Heap

H

0 0 1

b0 b1 b2

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

Building Binomial Heap

H

1 0 1

b0 b1 b2

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

Building Binomial Heap

H

0 1 1

b0 b1 b2

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

Building Binomial Heap

H

1 1 1

b0 b1 b2

slide-14
SLIDE 14

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

Building Binomial Heap

Calling binary counter Increments n times cost O( n ) in worst case (remember ?) Building an n-node binomial heap using n successive insertions is O( n ) time complexity Later we will show that the amortized time of

Binomial-Heap-Insert is Θ(1)

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

Binomial Heap : Extract Min.

20 H 10 5 9 32 20 11 13 40 18 2 12 21 30 8 31 12 44

Minimum : O( log n )

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

Binomial Heap : Extract Min.

20 H 10 5 9 32 20 11 13 40 18 12 21 30 8 31 12 44 H*

Delete min. root : Θ(1) the root of Bk has degree k k is Θ( log n ) worst case Θ( log n ) children Reverse list : Θ( log n )

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

Binomial Heap : Extract Min.

20 H 10 20 5 9 32 11 13 40 18 12 21 30 H* 8 31 12 44 21 8 31 12 44 20 10 12 30 5 9 32 20 11 13 40 18 H

Union : O( log n )

slide-15
SLIDE 15

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

Binomial Heap : Extract Min.

Minimum : O( log n ) Delete min root : Θ(1) Reverse list : Θ( log n ) Union heaps : O( log n ) Extract Min : Θ( log n )

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

Binomial Heap : Decrease Key.

20 H 10 5 9 32 20 11 13 40 18 2 12 21 30 8 31 12 44

Binomial-Heap-Decrease-Key( H, x, 3 )

x

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

Binomial Heap : Decrease Key.

20 H 10 5 9 32 20 13 40 18 2 12 21 30 8 31 12 44

3

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

Binomial Heap : Decrease Key.

20 H 10 5 9 32 20 13 40 18 2 12 21 30 8 31 12 44

3

slide-16
SLIDE 16

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

Binomial Heap : Decrease Key.

20 H 10 5 9 32 20 13 40 18 2 12 21 30 8 31 12 44

3

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

Binomial Heap : Decrease Key.

20 H 10 5 9 32 20 13 40 18 2 12 21 30 8 31 12 44

3

The height of Bk is k k is Θ( log n ) worst case Decrease Key : Θ( log n )

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

Binomial Heap : Delete

Binomial-Heap-Delete( H, x ) { Binomial-Heap-Decrease-Key( H, x, -∞ ) Binomial-Heap-Extract-Min( H ) }

Θ( log n )

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

Amortized Analysis : Insert

Cost : remove and create trees Each insertion creates only one new trees, but can remove zero or more trees If actual cost is c

a Bc-1 is created, but all B0 , B1 ,..., Bc-2 are removed a net increase of (2- c) trees

Let the potential function be the number of trees ai = ci + Φi - Φi-1 = ci + (2 - ci) = 2 = Θ(1)

slide-17
SLIDE 17

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

Quiz #1

20 H 10 5 9 32 20 13 40 18 2 12 21 30 8 31 12 44

3 How do we actually represent the binomial heaps ? ( CLR p.404 )

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

Quiz #2

20 H 10 5 9 32 20 13 40 18 2 12 21 30 8 31 12 44

3 What are the pros and cons of reverse-ordered binomial heaps ?

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

Quiz #3

What do we have to modify in order to make

Binomial-Heap-Minimum to be Θ(1) ?

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

Conclusion

The binomial heap is a mergeable heap Heap-Union : O( log n ) Insert : Θ(1) amortized