binomial queues - department of computer science and ... · binomial queue size a binomial queue h...

24
Section 6.8 Binomial Queues 1

Upload: others

Post on 05-Aug-2020

13 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Binomial Queues - Department of Computer Science and ... · Binomial Queue Size A binomial queue H with N nodes has O(log N) binomial trees let k be the largest integer such that

Section 6.8

Binomial Queues

1

Page 2: Binomial Queues - Department of Computer Science and ... · Binomial Queue Size A binomial queue H with N nodes has O(log N) binomial trees let k be the largest integer such that

Heap Operations: Merge

2

✤ Given two binary heaps H1 and H2, produce a new heap H’ combining H1 and H2

✤ Binary heaps take Θ(n1 + n2) time to merge

✤ i.e. they can never merge in better than linear time

✤ We can do better, however

✤ Merge in O(log N) time

✤ this comes at the expensive of a slight performance hit on our other operations

Page 3: Binomial Queues - Department of Computer Science and ... · Binomial Queue Size A binomial queue H with N nodes has O(log N) binomial trees let k be the largest integer such that

Binomial Trees

3

✤ Binomial trees are recursive defined

✤ Start with one node

✤ This is a binomial tree of height 0

✤ To form a tree of height k, attach two trees of height k - 1 together

✤ Attach one as a child of the root of the other

B0 B1 B2

B3

B0 B1 B2

B3

Page 4: Binomial Queues - Department of Computer Science and ... · Binomial Queue Size A binomial queue H with N nodes has O(log N) binomial trees let k be the largest integer such that

B4

4

B4

Page 5: Binomial Queues - Department of Computer Science and ... · Binomial Queue Size A binomial queue H with N nodes has O(log N) binomial trees let k be the largest integer such that

Binomial Tree Size

5

✤ A binomial tree of height k has 2k nodes

✤ Conversely, a binomial tree with n nodes has log2(n) height

✤ The number of nodes at level d of a tree with height k is the binomial coefficient:

kd

!

"#

$

%&=

k!d!(k − d)!

B3

Page 6: Binomial Queues - Department of Computer Science and ... · Binomial Queue Size A binomial queue H with N nodes has O(log N) binomial trees let k be the largest integer such that

Binomial Queues

6

✤ Binomial Heaps/Binomial Queues

✤ use a forest of binomial trees

✤ use each binomial tree {0,1} times

✤ impose heap ordering on each binomial tree

✤ no relationship between the roots of each tree

Page 7: Binomial Queues - Department of Computer Science and ... · Binomial Queue Size A binomial queue H with N nodes has O(log N) binomial trees let k be the largest integer such that

Binomial Queues

7

16

18

12

21 24

65

H1:

Page 8: Binomial Queues - Department of Computer Science and ... · Binomial Queue Size A binomial queue H with N nodes has O(log N) binomial trees let k be the largest integer such that

Binomial Queue Size

✤ A binomial queue H with N nodes has O(log N) binomial trees

✤ let k be the largest integer such that 2k ≤ N

✤ observe that k ≤ log2(N)

✤ N can be written as the sum of unique powers of 2, the largest of which is 2k

✤ this sum uses each power of 2 {0,1} times

✤ the sum has at most k + 1 terms in it

✤ each term corresponds to a binomial tree of 2n nodes in the forest of H8

Page 9: Binomial Queues - Department of Computer Science and ... · Binomial Queue Size A binomial queue H with N nodes has O(log N) binomial trees let k be the largest integer such that

Merge

✤ “Add” corresponding trees from the two forests

✤ For k from 0 to maxheight

✤ If neither queue has a Bk, skip

✤ If only 1, leave it

✤ If two, attach the larger priority root as a child of the other,producing a tree of height k + 1

✤ If three, pick two to merge, leave 19

16

18

12

21 24

65

H1:

14

26

23

51 24

65

H2:13

O(log N)!

Page 10: Binomial Queues - Department of Computer Science and ... · Binomial Queue Size A binomial queue H with N nodes has O(log N) binomial trees let k be the largest integer such that

After Merging H1 and H2

10

12

21 24

65

14

26 16

18

23

51 24

65

13

Page 11: Binomial Queues - Department of Computer Science and ... · Binomial Queue Size A binomial queue H with N nodes has O(log N) binomial trees let k be the largest integer such that

Insertion

11

✤ To insert a node X into a binomial queue H:

✤ Observe that a single node is a binomial tree of height 0

✤ So treat X as a binomial queue

✤ Merge X and H

✤ Merge operation takes log(N) time

✤ Therefore so does insert

Page 12: Binomial Queues - Department of Computer Science and ... · Binomial Queue Size A binomial queue H with N nodes has O(log N) binomial trees let k be the largest integer such that

insert(1)

12

1

Page 13: Binomial Queues - Department of Computer Science and ... · Binomial Queue Size A binomial queue H with N nodes has O(log N) binomial trees let k be the largest integer such that

insert(2)

13

1

2

Page 14: Binomial Queues - Department of Computer Science and ... · Binomial Queue Size A binomial queue H with N nodes has O(log N) binomial trees let k be the largest integer such that

insert(3)

14

1

2

3

Page 15: Binomial Queues - Department of Computer Science and ... · Binomial Queue Size A binomial queue H with N nodes has O(log N) binomial trees let k be the largest integer such that

insert(4)

15

1

2 3

4

Page 16: Binomial Queues - Department of Computer Science and ... · Binomial Queue Size A binomial queue H with N nodes has O(log N) binomial trees let k be the largest integer such that

insert(5)

16

5 1

2 3

4

Page 17: Binomial Queues - Department of Computer Science and ... · Binomial Queue Size A binomial queue H with N nodes has O(log N) binomial trees let k be the largest integer such that

insert(6)

17

5

6

1

2 3

4

Page 18: Binomial Queues - Department of Computer Science and ... · Binomial Queue Size A binomial queue H with N nodes has O(log N) binomial trees let k be the largest integer such that

insert(7)

18

5

6

1

2 3

4

7

Page 19: Binomial Queues - Department of Computer Science and ... · Binomial Queue Size A binomial queue H with N nodes has O(log N) binomial trees let k be the largest integer such that

deleteMin

19

✤ To deleteMin from a binomial queue H

✤ Find the binomial tree with the smallest root, let this be Bk

✤ Remove Bk from H, leaving the rest of the trees to form queue H’

✤ Delete (and return to user) the root of Bk

✤ this leaves us with the children of Bk’s root,which are binomial trees of size B0, B1, ..., Bk-1

✤ then let the trees B0, B1, ..., Bk-1 form a new binomial queue H’’

✤ Merge H’ and H’’ to repair the tree

Also O(log N)!

Page 20: Binomial Queues - Department of Computer Science and ... · Binomial Queue Size A binomial queue H with N nodes has O(log N) binomial trees let k be the largest integer such that

deleteMin

20

12

21 24

65

14

26 16

18

23

51 24

65

13

Page 21: Binomial Queues - Department of Computer Science and ... · Binomial Queue Size A binomial queue H with N nodes has O(log N) binomial trees let k be the largest integer such that

deleteMin

21

12

21 24

65

14

26 16

18

21 24

65

14

26 16

18

H'' :

Page 22: Binomial Queues - Department of Computer Science and ... · Binomial Queue Size A binomial queue H with N nodes has O(log N) binomial trees let k be the largest integer such that

deleteMin

22

23

51 24

65

H' :

14

65

14

26 16

18

H'' :21

13

Page 23: Binomial Queues - Department of Computer Science and ... · Binomial Queue Size A binomial queue H with N nodes has O(log N) binomial trees let k be the largest integer such that

deleteMin

23

13

21 24

65

14

26 16

18

23

51 24

65

Page 24: Binomial Queues - Department of Computer Science and ... · Binomial Queue Size A binomial queue H with N nodes has O(log N) binomial trees let k be the largest integer such that

Non-Standard Operations

24

✤ percolateUp

✤ identical to binary heap

✤ decreaseKey

✤ percolateUp as far as root of binomial tree

✤ delete (an arbitrary node)

✤ decreaseKey to -∞, then deleteMin