Top Banner
CSE 326: Data Structures Binomial Queues Ben Lerner Summer 2007
44

CSE 326: Data Structures Binomial Queues Ben Lerner Summer 2007.

Dec 22, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CSE 326: Data Structures Binomial Queues Ben Lerner Summer 2007.

CSE 326: Data Structures Binomial Queues

Ben Lerner

Summer 2007

Page 2: CSE 326: Data Structures Binomial Queues Ben Lerner Summer 2007.

2

Adminstrivia

• Due today: Homework #1

• Released today: Homework #2

Page 3: CSE 326: Data Structures Binomial Queues Ben Lerner Summer 2007.

3

BuildHeap: Floyd’s Method

5 11 3 10 6 9 4 8 1 7 212

Add elements arbitrarily to form a complete tree.Pretend it’s a heap and fix the heap-order property!

27184

96103

115

12

Page 4: CSE 326: Data Structures Binomial Queues Ben Lerner Summer 2007.

4

Buildheap pseudocode

private void buildHeap() {

for ( int i = currentSize/2; i > 0; i-- )

percolateDown( i );

}

runtime:

Page 5: CSE 326: Data Structures Binomial Queues Ben Lerner Summer 2007.

5

BuildHeap: Floyd’s Method

67184

92103

115

12

671084

9213

115

12

1171084

9613

25

12

1171084

9653

21

12

Page 6: CSE 326: Data Structures Binomial Queues Ben Lerner Summer 2007.

6

Finally…

11710812

9654

23

1

runtime:

Page 7: CSE 326: Data Structures Binomial Queues Ben Lerner Summer 2007.

7

Facts about HeapsObservations:• finding a child/parent index is a multiply/divide by two• operations jump widely through the heap• each percolate step looks at only two new nodes• inserts are at least as common as deleteMins

Realities:• division/multiplication by powers of two are equally fast• looking at only two new pieces of data: bad for cache!• with huge data sets, disk accesses dominate

Page 8: CSE 326: Data Structures Binomial Queues Ben Lerner Summer 2007.

8

CPU – 1 cycle

Cache – ~10 cycles

Memory – ~200 cycles

Disk – ~200,000 cycles

Cycles to access:

Page 9: CSE 326: Data Structures Binomial Queues Ben Lerner Summer 2007.

9

4

9654

23

1

8 1012

7

11

A Solution: d-Heaps• Each node has d children

• Still representable by array

• Good choices for d:– (choose a power of two for

efficiency)– fit one set of children in a

cache line– fit one set of children on a

memory page/disk block

3 7 2 8 5 12 11 10 6 9112

Page 10: CSE 326: Data Structures Binomial Queues Ben Lerner Summer 2007.

10

Operations on d-Heap

• Insert : runtime =

• deleteMin: runtime =

Does this help insert or deleteMin more?

Page 11: CSE 326: Data Structures Binomial Queues Ben Lerner Summer 2007.

11

One More Operation

• Merge two heaps. Ideas?

Page 12: CSE 326: Data Structures Binomial Queues Ben Lerner Summer 2007.

12

New Operation: Merge

• Given two heaps, merge them into one– First attempt: aren’t two heaps “just like” one heap

after deleteMin? • NO. Violates the completeness property

– Second attempt: • Foreach element in smaller heap, insert into larger one• Runtime?

– Third attempt:• Concatenate the arrays of the two heaps, then run buildHeap• Runtime?

Page 13: CSE 326: Data Structures Binomial Queues Ben Lerner Summer 2007.

13

Merging heaps

• Binary Heap is a special purpose hot rod– FindMin, DeleteMin and Insert only– does not support fast merges of two heaps

• For some applications, the items arrive in prioritized clumps, rather than individually

• Is there somewhere in the heap design that we can give up a little performance so that we can gain faster merge capability?

Page 14: CSE 326: Data Structures Binomial Queues Ben Lerner Summer 2007.

14

Binomial Queues

• Binomial Queues are designed to be merged quickly with one another

• Using pointer-based design we can merge large numbers of nodes at once by simply pruning and grafting tree structures

• More overhead than Binary Heap, but the flexibility is needed for improved merging speed

Page 15: CSE 326: Data Structures Binomial Queues Ben Lerner Summer 2007.

15

Worst Case Run Times

Insert

FindMin

DeleteMin

Merge

(log N)

(1)

(N)

(log N)

Binary Heap

(log N)

O(log N)

O(log N)

(log N)

Binomial Queue

Page 16: CSE 326: Data Structures Binomial Queues Ben Lerner Summer 2007.

16

Binomial Queues

• Binomial queues give up (1) FindMin performance in order to provide O(log N) merge performance

• A binomial queue is a collection (or forest) of heap-ordered trees– Not just one tree, but a collection of trees – each tree has a defined structure and capacity– each tree has the familiar heap-order property

Page 17: CSE 326: Data Structures Binomial Queues Ben Lerner Summer 2007.

17

Binomial Queue with 5 Trees

B0B1B2B3B4

depth

number of elements

4

24 = 16

3

23 = 8

2

22 = 4

1

21 = 2

0

20 = 1

Page 18: CSE 326: Data Structures Binomial Queues Ben Lerner Summer 2007.

18

Structure Property

• Each tree contains two copies of the previous tree– the second copy is attached at

the root of the first copy

• The number of nodes in a tree of depth d is exactly 2d

B0B1B2

depth

number of elements

2

22 = 4

1

21 = 2

0

20 = 1

Page 19: CSE 326: Data Structures Binomial Queues Ben Lerner Summer 2007.

19

Powers of 2

• Any number N can be represented in base 2– A base 2 value identifies the powers of 2

that are to be included20 = 110

21 = 210

22 = 410

23 = 810

Hex16 Decimal10

1 1 3 3

1 0 0 4 4

1 0 1 5 5

Page 20: CSE 326: Data Structures Binomial Queues Ben Lerner Summer 2007.

20

Numbers of nodes

• Any number of entries in the binomial queue can be stored in a forest of binomial trees

• Each tree holds the number of nodes appropriate to its depth, ie 2d nodes

• So the structure of a forest of binomial trees can be characterized with a single binary number– 1002 1·22 + 0·21 + 0·20 = 4 nodes

Page 21: CSE 326: Data Structures Binomial Queues Ben Lerner Summer 2007.

Structure Examples

4

8

N=210=102 21 = 2

94

8

21 = 2 20 = 1

94

85

7

22 = 4 21 = 2 20 = 1

4

85

7

22 = 4

N=310=112

N=410=1002

N=510=1012

22 = 4

22 = 4

20 = 1 20 = 121 = 2

Page 22: CSE 326: Data Structures Binomial Queues Ben Lerner Summer 2007.

22

What is a merge?

• There is a direct correlation between– the number of nodes in the tree– the representation of that number in base 2– and the actual structure of the tree

• When we merge two queues, the number of nodes in the new queue is the sum of N1+N2

• We can use that fact to help see how fast merges can be accomplished

Page 23: CSE 326: Data Structures Binomial Queues Ben Lerner Summer 2007.

4

8

N=210=102 21 = 2

94

8

21 = 2 20 = 1N=310=112

22 = 4

22 = 4

20 = 1

N=110=12 21 = 222 = 4 20 = 1

9

Example 1.

Merge BQ.1 and BQ.2

Easy Case.

There are no comparisons and there is no restructuring.

BQ.1

+ BQ.2

= BQ.3

Page 24: CSE 326: Data Structures Binomial Queues Ben Lerner Summer 2007.

4

6

N=210=102 21 = 2

21 = 2 20 = 1N=410=1002

22 = 4

22 = 4

20 = 1

N=210=102 21 = 222 = 4 20 = 1

Example 2.

Merge BQ.1 and BQ.2

This is an add with a carry out.

It is accomplished with one comparison and one pointer change: O(1)

BQ.1

+ BQ.2

= BQ.3

1

3

1

34

6

Page 25: CSE 326: Data Structures Binomial Queues Ben Lerner Summer 2007.

4

6

N=310=112 21 = 2

21 = 2 20 = 1N=210=102

22 = 4

22 = 4

20 = 1

N=310=112 21 = 222 = 4 20 = 1Example 3.

Merge BQ.1 and BQ.2

Part 1 - Form the carry.

BQ.1

+ BQ.2

= carry

1

3

7

8

7

8

Page 26: CSE 326: Data Structures Binomial Queues Ben Lerner Summer 2007.

4

6

N=310=112 21 = 2

21 = 2 20 = 1N=610=1102

22 = 4

22 = 4

20 = 1

N=310=112 21 = 222 = 4 20 = 1

Example 3.

Part 2 - Add the existing values and the carry.

+ BQ.1

+ BQ.2

= BQ.3

1

3

7

8

7

8

21 = 2 20 = 1N=210=102 22 = 4

carry

7

8

1

34

6

Page 27: CSE 326: Data Structures Binomial Queues Ben Lerner Summer 2007.

27

Merge Algorithm• Just like binary addition algorithm• Assume trees X0,…,Xn and Y0,…,Yn are binomial queues

– Xi and Yi are of type Bi or null

C0 := null; //initial carry is null//for i = 0 to n do combine Xi,Yi, and Ci to form Zi and new Ci+1

Zn+1 := Cn+1

Page 28: CSE 326: Data Structures Binomial Queues Ben Lerner Summer 2007.

28

Exercise

94

8

21 = 2 20 = 1

12

107

12

22 = 4 21 = 2 20 = 1N=310=112 N=510=101222 = 4

13

15

Page 29: CSE 326: Data Structures Binomial Queues Ben Lerner Summer 2007.

29

O(log N) time to Merge

• For N keys there are at most log2 N trees in a binomial forest.

• Each merge operation only looks at the root of each tree.

• Total time to merge is O(log N).

Page 30: CSE 326: Data Structures Binomial Queues Ben Lerner Summer 2007.

30

Insert

• Create a single node queue B0 with the new item and merge with existing queue

• O(log N) time

Page 31: CSE 326: Data Structures Binomial Queues Ben Lerner Summer 2007.

31

DeleteMin

1. Assume we have a binomial forest X0,…,Xm

2. Find tree Xk with the smallest root

3. Remove Xk from the queue

4. Remove root of Xk (return this value)

– This yields a binomial forest Y0, Y1, …,Yk-1.

5. Merge this new queue with remainder of the original (from step 3)

• Total time = O(log N)

Page 32: CSE 326: Data Structures Binomial Queues Ben Lerner Summer 2007.

32

Implementation• Binomial forest as an array of multiway trees

– FirstChild, Sibling pointers– Sibling pointers act like a linked list

0 1 2 3 4 5 6 7

52

9

1

107

12

4

813

15

52

9

1

4 7 10

1213 8

15

Page 33: CSE 326: Data Structures Binomial Queues Ben Lerner Summer 2007.

33

DeleteMin Example

1And return this

0 1 2 3 4 5 6 7

5 2

9

1

4 7 10

1213 8

15

0 1 2 3 4 5 6 7

5

10

2

4 7 9

1213 8

15

Page 34: CSE 326: Data Structures Binomial Queues Ben Lerner Summer 2007.

34

DeleteMin step 1: remove min

1Return this

0 1 2 3 4 5 6 7

5 2

9

1

0 1 2 3 4 5 6 7

5 2

94 7 10

1213 8

15 4 7 10

1213 8

15

Keep these

Page 35: CSE 326: Data Structures Binomial Queues Ben Lerner Summer 2007.

35

0 1 2 3 4 5 6 7New forest

Old forest

10 7

12

4

13 8

15

107

12

4

13 8

15

DeleteMin step 2: new forest0 1 2 3 4 5 6 7

5 2

9

0 1 2 3 4 5 6 7

5 2

9

Page 36: CSE 326: Data Structures Binomial Queues Ben Lerner Summer 2007.

36

0 1 2 3 4 5 6 7

5 2

9

0 1 2 3 4 5 6 7

Merge5

100 1 2 3 4 5 6 7

4710

12 13 8

15

2

4 7 9

1213 8

15

DeleteMin step 3: merge forests

Page 37: CSE 326: Data Structures Binomial Queues Ben Lerner Summer 2007.

37

Merge in detail0 1 2 3 4 5 6 7

5 2

9

0 1 2 3 4 5 6 7

5

100 1 2 3 4 5 6 7

4710

12 13 8

15

2

4 7 9

1213 8

15

Page 38: CSE 326: Data Structures Binomial Queues Ben Lerner Summer 2007.

38

Merge step 1: 1-trees

5

5

1010

0 1 2 3

0 1 2 3Carry out

Page 39: CSE 326: Data Structures Binomial Queues Ben Lerner Summer 2007.

39

97

Merge step 2: 2-trees

2

9

0 1 2 3 4 5 6 7

5

10

2

9

12

5

10

0 1 2 3

0 1 2 3Carry out

Carry in

Merging two treesis just a stack push!

7

12

Page 40: CSE 326: Data Structures Binomial Queues Ben Lerner Summer 2007.

40

Merge step 3: 4-trees0 1 2 3

0 1 2 3 4 5 6 7

0 1 2 3

4

13 8

15

7 9

12

2

4

13 8

15

2

7 9

12

Carry in

2 Carry out

Page 41: CSE 326: Data Structures Binomial Queues Ben Lerner Summer 2007.

41

Merge step 4: 8-trees0 1 2 3

0 1 2 3 4 5 6 7

0 1 2 3

2

4 7 9

1213 8

15

Carry in

2

4 7 9

1213 8

15

Page 42: CSE 326: Data Structures Binomial Queues Ben Lerner Summer 2007.

42

Why Binomial?

B0B1B2B3B4

tree depth d

nodes at depth k

4

1, 4, 6, 4, 1

3

1, 3, 3, 1

2

1, 2, 1

1

1, 1

0

1

!)!(

!

kkd

d

k

d

Page 43: CSE 326: Data Structures Binomial Queues Ben Lerner Summer 2007.

43

Other Priority Queues

• Leftist Heaps – O(log N) time for insert, deletemin, merge

• Skew Heaps– O(log N) amortized time for insert, deletemin,

merge

• Calendar Queues– O(1) average time for insert and deletemin– Assuming insertions are “random”

Page 44: CSE 326: Data Structures Binomial Queues Ben Lerner Summer 2007.

44

Exercise Solution

94

8

12

107

12

13

15+

1

9

2

107

12

4

813

15