binomial heaps jyh-shing roger jang ( 張智星 ) csie dept, national taiwan university

27
Binomial Heaps Jyh-Shing Roger Jang ( 張張張 ) CSIE Dept, National Taiwan University

Upload: noah-clarke

Post on 22-Dec-2015

225 views

Category:

Documents


1 download

TRANSCRIPT

Binomial Heaps

Jyh-Shing Roger Jang (張智星 )CSIE Dept, National Taiwan University

2

Memory Allocation in Classes

The binary heap is good for simple operations such as insertion, deletion, and min extraction, but it is bad for union operation.

Union operation requires building up a new heap from scratch using the elements from the old heaps, which is expensive

Binomial heaps support union operations more efficiently.

3

Binomial Trees

A binomial heap is a collection of binomial trees A binomial tree Bk is an ordered tree defined recursively

B0 consists of a single node Bk consists of two binomial tree Bk-1, with root of one is the

leftmost child of the root of the other

4

Properties of Binomial Trees

For a binomial tree Bk There are 2k nodes Height = k Degree of root = k There are exactly nodes at depth i for i = 0, 1, …, k The root has a k subtrees, with each subtree being Bi with i =

k-1, k-2, …, 0. Deleting root yields binomial trees Bk-1, Bk-2, …, B0 .

Proof byinduction on k

i

k

5

Binomial Trees: Why It’s Named So

6

Proof of the “Binomial” Property

7

Properties of Binomial Trees

8

Binomial Heap

Binomial heap by Vuillemin, 1978 Composed of a sequence of binomial trees

Each tree is min-heap ordered 0 or 1 binomial tree of order k

9

Binomial Heap: Implementation

Common implementation of binomial heaps Represent trees via left-child, right-sibling pointers

Three links per node: parent, left, right Roots of trees connected with singly linked list

Degrees of trees strictly decreasing from left to right

10

Binomial Heap: Properties

Properties of N-node binomial heap Min key contained in roots of Bk-1, Bk-2, …, B0

Contains Bi if bi=1 for binary representation bk-1bk-2…b2b0 . At most binomial trees Height ≤

1log2 N

N2log

11

Binomial Heap: Proof of Properties

Properties of N-node binomial heap Assume its binary representation is bk-1bk-2…b1b0, with bk-1 ≠ 0

At most binomial trees Height = k-1 ≤

1log1log

1log1log

122

2222

22

22

1

0211

NkN

NkN

N

Nkk

kkk

1log2 N

N2log

111…1100…0

12

Binomial Heap: Union of Same-order Binomial Trees

Create Binomial Tree H that is union of H’ and H’’ Easy if H’ and H’’ are of the same order (degree)

Connect roots of H’ and H’’, with smaller key to be root of H Choose smaller Min key contained in roots of Bk-1, Bk-2, …, B0

Keep min-heap order

13

Binomial Heap: Union

Union of two binomial heaps Reference:

http://www.cs.princeton.edu/~wayne/cs423/lectures/heaps.ppt

Just like adding two binary numbers

14

Binomial Heap: Union

55

45 32

30

24

23 22

50

48 31 17

448 29 10

6

37

3 18

41

3328

15

25

7 12

+

15

Binomial Heap: Union

55

45 32

30

24

23 22

50

48 31 17

448 29 10

6

37

3

41

3328

15

25

7

+

12

18

18

12

16

55

45 32

30

24

23 22

50

48 31 17

448 29 10

6

37

3

41

3328

15

25

7

+

12

18

25

377

3

18

12

18

12

17

55

45 32

30

24

23 22

50

48 31 17

448 29 10

6

37

3

41

3328

15

25

7

12

+

18

25

377

3

41

28 33 25

3715 7

3

18

12

18

12

18

55

45 32

30

24

23 22

50

48 31 17

448 29 10

6

37

3

41

3328

15

25

7

+

18

12

41

28 33 25

3715 7

3

12

18

25

377

3

41

28 33 25

3715 7

3

18

12

19

55

45 32

30

24

23 22

50

48 31 17

448 29 10

6

37

3

41

3328

15

25

7

+

18

12

41

28 33 25

3715 7

3

12

18

25

377

3

41

28 33 25

3715 7

3

55

45 32

30

24

23 22

50

48 31 17

448 29 10

6

18

12

20

Binomial Heap: Union

Create heap H that is union of heaps H' and H''. Analogous to binary addition.

Running time. O(log N) Proportional to number of trees in root lists

001 1

100 1+

011 1

11

1

1

0

1

19 + 7 = 26

21

3

37

6 18

55

45 32

30

24

23 22

50

48 31 17

448 29 10

H

Binomial Heap: Delete Min

Delete node with minimum key in binomial heap H. Find root x with min key in root list of H, and delete H' broken binomial trees H Union(H', H)

Running time. O(log N)

Comparison: O(1) for finding minif we always keep a pointer to the min

22

Binomial Heap: Delete Min

Delete node with minimum key in binomial heap H. Find root x with min key in root list of H, and delete H' broken binomial trees H Union(H', H)

Running time. O(log N)

55

45 32

30

24

23 22

50

48 31 17

37

6 18

448 29 10

H

H'

23

3

37

6 18

55

x 32

30

24

23 22

50

48 31 17

448 29 10

H

Binomial Heap: Decrease Key

Decrease key of node x in binomial heap H. Suppose x is in binomial tree Bk.

Bubble node x up the tree if x is too small.

Running time. O(log N) Proportional to depth of node x log2 N .

depth = 3

24

Binomial Heap: Delete

Delete node x in binomial heap H. Decrease key of x to -. Bubble up to the root. Delete min.

Running time. O(log N)

25

Binomial Heap: Insert

Insert a new node x into binomial heap H. H' MakeHeap(x) H Union(H', H)

Running time. O(log N)

3

37

6 18

55

45 32

30

24

23 22

50

48 31 17

448 29 10

H

x

H'

26

Binomial Heap: Sequence of Inserts

Insert a new node x into binomial heap H. If N = .......0, then only 1 steps. If N = ......01, then only 2 steps. If N = .....011, then only 3 steps. If N = ....0111, then only 4 steps.

Inserting 1 item can take (log N) time. If N = 11...111, then log2 N steps.

But, inserting sequence of N items takes O(N) time! (N/2)(1) + (N/4)(2) + (N/8)(3) + . . . 2N Amortized analysis. Basis for getting most operations

down to constant time.

50

48 31 17

4429 10

3

37

6 x

221

22

2 11

NN

N

nn

Nn

27

Priority Queues

make-heap

Operation

insert

find-min

delete-min

union

decrease-key

delete

1

Binary

log N

1

log N

M*log(M+N)

log N

log N

1

Binomial

log N

1

log N

log N

log N

log N

1

Fibonacci *

1

1

log N

1

1

log N

1

Relaxed

1

1

log N

1

1

log N

1

Linked List

1

N

N

1

1

N

is-empty 1 1 1 11

Heaps

just did this

With extra book keeping

M is the size of the smaller heap