26
1 Priority Queues (Heaps) Sections 6.1 to 6.5

1 Priority Queues (Heaps) Sections 6.1 to 6.5. 2 The Priority Queue ADT DeleteMin –log N time Insert –log N time Other operations –FindMin Constant

Embed Size (px)

Citation preview

Page 1: 1 Priority Queues (Heaps)  Sections 6.1 to 6.5. 2 The Priority Queue ADT  DeleteMin –log N time  Insert –log N time  Other operations –FindMin  Constant

1

Priority Queues (Heaps) Sections 6.1 to 6.5

Page 2: 1 Priority Queues (Heaps)  Sections 6.1 to 6.5. 2 The Priority Queue ADT  DeleteMin –log N time  Insert –log N time  Other operations –FindMin  Constant

2

The Priority Queue ADT

DeleteMin – log N time

Insert– log N time

Other operations– FindMin

Constant time

– Initialize N time

Page 3: 1 Priority Queues (Heaps)  Sections 6.1 to 6.5. 2 The Priority Queue ADT  DeleteMin –log N time  Insert –log N time  Other operations –FindMin  Constant

3

Applications of Priority Queues

Any event/job management that assign priority to events/jobs

In Operating Systems– Scheduling jobs

In Simulators– Scheduling the next event (smallest event time)

Page 4: 1 Priority Queues (Heaps)  Sections 6.1 to 6.5. 2 The Priority Queue ADT  DeleteMin –log N time  Insert –log N time  Other operations –FindMin  Constant

4

Priority Queue Implementation

Implemented as adaptor class around– Linked lists

O(N) worst-case time on either insert() or deleteMin()

– Binary Search Trees O(log(N)) average time on insert() and delete() Overkill: all elements are sorted

– However, we only need the minimum element

– Heaps This is what we’ll study and use to implement Priority

Queues O(logN) worst case for both insertion and delete

operations

Page 5: 1 Priority Queues (Heaps)  Sections 6.1 to 6.5. 2 The Priority Queue ADT  DeleteMin –log N time  Insert –log N time  Other operations –FindMin  Constant

5

Partially Ordered Trees

A partially ordered tree (POT) is a tree T such that:– There is an order relation <= defined for the

vertices of T– For any vertex p and any child c of p, p <= c

Consequences:– The smallest element in a POT is the root– No conclusion can be drawn about the order of

children

Page 6: 1 Priority Queues (Heaps)  Sections 6.1 to 6.5. 2 The Priority Queue ADT  DeleteMin –log N time  Insert –log N time  Other operations –FindMin  Constant

6

Binary Heaps

A binary heap is a partially ordered complete binary tree – The tree is completely filled on all levels except possibly the

lowest.

In a more general d-Heap– A parent node can have d children

We simply refer to binary heaps as heaps

0

3 2

4 5

root

Page 7: 1 Priority Queues (Heaps)  Sections 6.1 to 6.5. 2 The Priority Queue ADT  DeleteMin –log N time  Insert –log N time  Other operations –FindMin  Constant

7

Vector Representation of Complete Binary Tree

Storing elements in vector in level-order– Parent of v[k] = v[k/2]– Left child of v[k] = v[2*k]– Right child of v[k] = v[2*k + 1]

R

l r

ll lr rrrl

root

rrrllrllrlR

7654321

Page 8: 1 Priority Queues (Heaps)  Sections 6.1 to 6.5. 2 The Priority Queue ADT  DeleteMin –log N time  Insert –log N time  Other operations –FindMin  Constant

8

Heap example

Parent of v[k] = v[k/2] Left child of v[k] = v[2*k] Right child of v[k] = v[2*k + 1]

Page 9: 1 Priority Queues (Heaps)  Sections 6.1 to 6.5. 2 The Priority Queue ADT  DeleteMin –log N time  Insert –log N time  Other operations –FindMin  Constant

9

Examples

Which one is a heap?

Page 10: 1 Priority Queues (Heaps)  Sections 6.1 to 6.5. 2 The Priority Queue ADT  DeleteMin –log N time  Insert –log N time  Other operations –FindMin  Constant

10

Implementation of Priority Queue (heap)

Page 11: 1 Priority Queues (Heaps)  Sections 6.1 to 6.5. 2 The Priority Queue ADT  DeleteMin –log N time  Insert –log N time  Other operations –FindMin  Constant

11

Insertion Example: insert(14)

14

14

14

Page 12: 1 Priority Queues (Heaps)  Sections 6.1 to 6.5. 2 The Priority Queue ADT  DeleteMin –log N time  Insert –log N time  Other operations –FindMin  Constant

12

Basic Heap Operations: insert(x)

Maintain the complete binary tree property and fix any problem with the partially ordered tree property– Create a leaf at the end– Repeat

Locate parent if POT not satisfied

– Swap with parent else

– Stop

– Insert x into its final location

Page 13: 1 Priority Queues (Heaps)  Sections 6.1 to 6.5. 2 The Priority Queue ADT  DeleteMin –log N time  Insert –log N time  Other operations –FindMin  Constant

13

Implementation of insert

Page 14: 1 Priority Queues (Heaps)  Sections 6.1 to 6.5. 2 The Priority Queue ADT  DeleteMin –log N time  Insert –log N time  Other operations –FindMin  Constant

14

deleteMin() example

31

13 14 16 19 21 19 68 65 26 32 31

Page 15: 1 Priority Queues (Heaps)  Sections 6.1 to 6.5. 2 The Priority Queue ADT  DeleteMin –log N time  Insert –log N time  Other operations –FindMin  Constant

15

deleteMin() Example (Cont’d)

31

31

31

Page 16: 1 Priority Queues (Heaps)  Sections 6.1 to 6.5. 2 The Priority Queue ADT  DeleteMin –log N time  Insert –log N time  Other operations –FindMin  Constant

16

Basic Heap Operations: deleteMin()

Replace root with the last leaf ( last element in the array representation – This maintains the complete binary tree property

but may violate the partially ordered tree property Repeat

– Find the smaller child of the “hole”– If POT not satisfied

Swap hole and smaller child

– else Stop

Page 17: 1 Priority Queues (Heaps)  Sections 6.1 to 6.5. 2 The Priority Queue ADT  DeleteMin –log N time  Insert –log N time  Other operations –FindMin  Constant

17

Implementation of deleteMin()

Page 18: 1 Priority Queues (Heaps)  Sections 6.1 to 6.5. 2 The Priority Queue ADT  DeleteMin –log N time  Insert –log N time  Other operations –FindMin  Constant

18

Implementation of deleteMin()

Page 19: 1 Priority Queues (Heaps)  Sections 6.1 to 6.5. 2 The Priority Queue ADT  DeleteMin –log N time  Insert –log N time  Other operations –FindMin  Constant

19

Constructor

Construct heap from a collection of item How to?

– Naïve methods– Insert() each element– Worst-case time: O(N(logN))– We show an approach taking O(N) worst-case

Basic idea– First insert all elements into the tree without

worrying about POT– Then, adjust the tree to satisfy POT, starting from

the bottom

Page 20: 1 Priority Queues (Heaps)  Sections 6.1 to 6.5. 2 The Priority Queue ADT  DeleteMin –log N time  Insert –log N time  Other operations –FindMin  Constant

20

Constructor

Page 21: 1 Priority Queues (Heaps)  Sections 6.1 to 6.5. 2 The Priority Queue ADT  DeleteMin –log N time  Insert –log N time  Other operations –FindMin  Constant

21

Example

percolateDown(7)

percolateDown(6) percolateDown(5)

Page 22: 1 Priority Queues (Heaps)  Sections 6.1 to 6.5. 2 The Priority Queue ADT  DeleteMin –log N time  Insert –log N time  Other operations –FindMin  Constant

22

percolateDown(1)

percolateDown(4) percolateDown(3)

percolateDown(2)

Page 23: 1 Priority Queues (Heaps)  Sections 6.1 to 6.5. 2 The Priority Queue ADT  DeleteMin –log N time  Insert –log N time  Other operations –FindMin  Constant

23

Complexity Analysis Consider a tree of height h with 2h-1 nodes

– Time = 1•(h) + 2•(h-1) + 4•(h-2) + ... + 2h-1•1– = i=1

h 2h-i i = 2h i=1h i/2i

– = 2h O(1) = O(2h) = O(N) Proof for i=1

h i/2i = O(1)– i/2i ≤ ∫i-1

i (x+1)/2x dx i=1

h i/2i ≤ i=1∞ i/2i ≤ ∫0

∞ (x+1)/2x dx Note: ∫u dv = uv - ∫v du, with dv = 2-x and u = x and ∫2-x dx =

-2-x/ln 2

– ∫0∞ (x+1)/2x dx = -x 2-x/ln 2|0∞ + 1/ln 2 ∫0

∞ 2-x dx + ∫0∞ 2-

x dx – = -2-x/ln2 2|0∞ - 2-x/ln 2|0∞ = (1 + 1/ln 2)/ln 2 = O(1)

Page 24: 1 Priority Queues (Heaps)  Sections 6.1 to 6.5. 2 The Priority Queue ADT  DeleteMin –log N time  Insert –log N time  Other operations –FindMin  Constant

24

Alternate Proof

Prove i=1h i/2i = O(1)

– i=0∞ xi = 1/(1-x) when |x| < 1

– Differentiating both sides with respect to x we get– i=0

∞ i xi-1 = 1/(1-x)2

– So, i=0∞ i xi = x/(1-x)2

– Substituting x = 1/2 above gives i=0

∞ i 2-i = 0.5/(1-0.5)2 = 2 = O(1)

Page 25: 1 Priority Queues (Heaps)  Sections 6.1 to 6.5. 2 The Priority Queue ADT  DeleteMin –log N time  Insert –log N time  Other operations –FindMin  Constant

25

C++ STL Priority Queues

priority_queue class template– Implements deleteMax instead of deleteMin in default– MaxHeap instead of MinHeap

Template– Item type– container type (default vector)– comparator (default less)

Associative queue operations– Void push(t)– void pop()– T& top()– void clear()– bool empty()

Page 26: 1 Priority Queues (Heaps)  Sections 6.1 to 6.5. 2 The Priority Queue ADT  DeleteMin –log N time  Insert –log N time  Other operations –FindMin  Constant

26