Priority queues Priority queue ADT Binary heap Heap insertion, - - PowerPoint PPT Presentation

priority queues
SMART_READER_LITE
LIVE PREVIEW

Priority queues Priority queue ADT Binary heap Heap insertion, - - PowerPoint PPT Presentation

Priority queues Priority queue ADT Binary heap Heap insertion, removal March 16, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 1 Priority queues? Suppose Geoff has made a to-do list (with priority values): 6 MT2 regrades 2 Vacuum


slide-1
SLIDE 1

Priority queues

Priority queue ADT Binary heap Heap insertion, removal

March 16, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 1

slide-2
SLIDE 2

Priority queues?

  • Suppose Geoff has made a to-do list (with priority values):

6 – MT2 regrades 2 – Vacuum home 8 – Renew car insurance 1 – Sleep 9 – Extinguish computer on fire 2 – Eat 8 – Lecture prep 1 – Bathe

  • We are interested in quickly finding the task with the highest

priority

March 16, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 2

slide-3
SLIDE 3

Priority queue ADT

  • A collection organised so as to allow fast access to and

removal of the largest (or smallest) element

– Prioritisation is a weaker condition than ordering – Order of insertion is irrelevant – Element with the highest priority (whatever that means) is the first element to be removed

  • Not really a queue: not FIFO!

March 16, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 3

slide-4
SLIDE 4

Priority queue ADT

  • Priority queue operations

– create – destroy – insert – removeMin (or removeMax) – isEmpty

March 16, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 4

  • Priority queue property

– For two elements 𝑦 and 𝑧 in the queue, if 𝑦 has a higher priority value than 𝑧, 𝑦 will be removed before 𝑧 – Note that in most definitions, a single priority queue structure supports

  • nly removeMin, or only removeMax, and not both

G(9) D(100) G(9) C(3) F(7) E(5) A(4) B(6) insert D(100) removeMax

slide-5
SLIDE 5

Priority queue properties

  • A priority queue is an ADT that maintains a multiset of items

– vs set: a multiset allows duplicate entries

  • Two or more distinct items in a priority queue may have the

same priority

  • If all items have the same priority, will it behave FIFO like a

queue?

– not necessarily! Due to implementation details

March 16, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 5

slide-6
SLIDE 6

Priority queue applications

  • Hold jobs for a printer in order of size
  • Manage limited resources such as bandwidth on a transmission

line from a network router

  • Sort numbers
  • Anything greedy: an algorithm that makes the "locally best

choice" at each step

March 16, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 6

slide-7
SLIDE 7

Data structures for priority queues

  • Worst case complexities

March 16, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 7

Structure insert removeMin Unordered array 𝑃 1 𝑃 𝑜 Ordered array 𝑃 𝑜 𝑃 𝑜 Unordered list 𝑃 1 𝑃 𝑜 Ordered list 𝑃 𝑜 𝑃 1 Binary search tree 𝑃 𝑜 𝑃 𝑜 AVL tree 𝑃 log 𝑜 𝑃 log 𝑜 Binary heap 𝑃 log 𝑜 𝑃 log 𝑜

Heap has asymptotically same performance as AVL tree, but MUCH simpler to implement

slide-8
SLIDE 8

Binary heap

  • A heap is binary tree with two properties
  • Heaps are complete

– All levels, except the bottom, must be completely filled in – The leaves on the bottom level are as far to the left as possible

  • Heaps are partially ordered

– For a max heap – the value of a node is at least as large as its children’s values – For a min heap – the value of a node is no greater than its children’s values

March 16, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 8

A complete, partially-ordered binary tree

slide-9
SLIDE 9

Review: complete binary trees

March 16, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 9

complete binary trees incomplete binary trees

slide-10
SLIDE 10

Partially ordered tree

  • max heap example

March 16, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 10

98 86 41 13 65 32 29 9 10 44 23 21 32 Heaps are not fully ordered – an in-order traversal would result in: 9, 13, 10, 86, 44, 65, 23, 98, 21, 32, 32, 41, 29

slide-11
SLIDE 11

Duplicate priority values

  • It is important to realise that two binary heaps can contain the

same data, but items may appear in different positions in the structure

March 16, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 11

Min heap examples

2 5 7 5 7 8 2 5 5 7 7 8

slide-12
SLIDE 12

Heap implementation

  • Heaps can be implemented

using arrays

  • There is a natural method of

indexing tree nodes

– Index nodes from top to bottom and left to right as shown on the right – Because heaps are complete binary trees there can be no gaps in the array (except index 0)

March 16, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 12

Using an array

1 2 3 4 5 6 7 1 2 3 4 5 6 .. .

slide-13
SLIDE 13

Referencing nodes

  • To move around in the tree, it will be necessary to find the index
  • f the parents of a node

– or the children of a node

  • The array is indexed from 1 to 𝑜
  • Each level’s nodes are indexed from:
  • 2𝑚𝑓𝑤𝑓𝑚 to 2𝑚𝑓𝑤𝑓𝑚+1 − 1 (where the root is level 0)

– The children of a node 𝑗, are the array elements indexed at 2𝑗 and 2𝑗 + 1 – The parent of a node 𝑗, is the array element indexed at 𝑗 2

March 16, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 13

slide-14
SLIDE 14

Array heap example

March 16, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 14

98 86 41 13 65 32 29 9 10 44 23 21 32 1 2 3 4 5 6 7 8 9 10 11 12 13 Heap Underlying array 98 86 41 13 65 32 29 9 10 1 2 3 4 5 6 7 44 23 21 32 10 11 12 13 8 9 value index

slide-15
SLIDE 15

Heap implementation

March 16, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 15

class MinHeap { private: int size; // number of stored elements int capacity; // maximum capacity of array int* arr; // array in dynamic memory public: ... }; MinHeap::MinHeap(int initcapacity) { size = 0; capacity = initcapacity; arr = new int[capacity+1]; }

slide-16
SLIDE 16

Heap insertion

  • On insertion the heap properties have to be maintained

– A heap is a complete binary tree and – A partially ordered binary tree

  • The insertion algorithm first ensures that the tree is complete

– new item is the first available (left-most) leaf on the bottom level – i.e. the first free element in the underlying array

  • Fix the partial ordering

– Compare the new value to its parent – Swap them if the new value is greater than the parent – Repeat until this is not the case

  • Referred to as heapify up, percolate up, or trickle up, bubble up, etc.

March 16, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 16

slide-17
SLIDE 17

Heap insertion example

  • max heap

March 16, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 17

98 86 41 13 65 32 29 9 10 44 23 21 32 98 86 41 13 65 32 29 9 10 1 2 3 4 5 6 7 44 23 21 32 10 11 12 13 8 9 value index 14 Insert 81

slide-18
SLIDE 18

Heap insertion example

  • max heap

March 16, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 18

98 86 41 13 65 32 29 9 10 44 23 21 32 98 86 41 13 65 32 29 9 10 1 2 3 4 5 6 7 44 23 21 32 10 11 12 13 8 9 value index 81 14 Insert 81 81 81 29 parent: 14 2 = 7 81 29

slide-19
SLIDE 19

Heap insertion example

  • max heap

March 16, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 19

98 86 41 13 65 32 81 9 10 44 23 21 32 98 86 41 13 65 32 81 9 10 1 2 3 4 5 6 7 44 23 21 32 10 11 12 13 8 9 value index 29 14 Insert 81 29 81 41 parent: 7 2 = 3 81 41 81 is less than 98 so finished

slide-20
SLIDE 20

Heap insertion complexity

  • Item is inserted at the bottom level in the first available space

– this can be tracked using the heap size attribute – 𝑃 1 access using array index

  • Repeated heapify-up operations. How many?

– each heapify-up operation moves the inserted value up one level in the tree – Upper limit on the number of levels in a complete tree? 𝑃 log 𝑜

  • Heap insertion has worst-case performance of 𝑃 log 𝑜

March 16, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 20

slide-21
SLIDE 21

Building a heap (an heap?)

  • A heap can be constructed by repeatedly inserting items into

an empty heap. Complexity?

– 𝑃 log 𝑜 per item, 𝑃 𝑜 items – 𝑃 𝑜 log 𝑜 total

  • More about this next class

March 16, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 21

slide-22
SLIDE 22

Removing the priority item

  • Heap properties must be satisfied after removal
  • Make a temporary copy of the root’s data
  • Similarly to the insertion algorithm, first ensure that the heap

remains complete

– Replace the root node with the right-most leaf – i.e. the highest (occupied) index in the array

  • Swap the new root with its largest valued child until the

partially ordered property holds

– i.e. heapifyDown

  • Return the copied root’s data

March 16, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 22

slide-23
SLIDE 23

Heap removal example

March 16, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 23

98 86 41 13 65 32 29 9 10 44 23 21 17 removeMax 98 86 41 13 65 32 29 9 10 1 2 3 4 5 6 7 44 23 21 17 10 11 12 13 8 9 value index

slide-24
SLIDE 24

Heap removal example

March 16, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 24

17 98 86 41 13 65 32 29 9 10 44 23 21 removeMax 17 13 98 86 41 13 65 32 29 9 10 1 2 3 4 5 6 7 44 23 21 10 11 12 8 9 value index Replace root with rightmost leaf 17 17

slide-25
SLIDE 25

Heap removal example

March 16, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 25

17 86 41 13 65 32 29 9 10 44 23 21 removeMax 13 17 86 41 13 65 32 29 9 10 1 2 3 4 5 6 7 44 23 21 10 11 12 8 9 value index Swap with largest child ? ? 17 86 children of root: 2 ∙ 1, 2 ∙ 1 + 1 = 2, 3 86 17

slide-26
SLIDE 26

Heap removal example

March 16, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 26

86 17 41 13 65 32 29 9 10 44 23 21 removeMax 13 86 17 41 13 65 32 29 9 10 1 2 3 4 5 6 7 44 23 21 10 11 12 8 9 value index Swap with largest child ? ? children of 2: 2 ∙ 2, 2 ∙ 2 + 1 = 4, 5 65 17 65 17

slide-27
SLIDE 27

Heap removal example

March 16, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 27

86 65 41 13 17 32 29 9 10 44 23 21 removeMax 13 86 65 41 13 17 32 29 9 10 1 2 3 4 5 6 7 44 23 21 10 11 12 8 9 value index Swap with largest child ? ? children of 5: 2 ∗ 5, 2 ∗ 5 + 1 = 10, 11 17 44 17 44

slide-28
SLIDE 28

Complexity of removeMin / removeMax

  • Analysis is similar to insertion
  • Replace root with last element – 𝑃 1
  • Repeated heapify-down operations starting from root level

– each heapify-down moves one level closer to the bottom of the tree – How many levels? 𝑃 log 𝑜

  • Removing the priority item from a heap is also 𝑃 log 𝑜 in the

worst case

March 16, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 28

slide-29
SLIDE 29

Array implementation and insertion

  • Note that like other array-based structures, we have a limited

capacity at creation time

– What to do when the array is full? Expand it!

  • Since array indices correspond exactly to node positions in the

tree, and nodes should remain in their original positions after expanding the array, we can simply copy into the same indices

March 16, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 29

3 27 9 31 46 12 1 2 3 4 5 6 value index 3 27 9 31 46 12 1 2 3 4 5 6 7 10 11 12 8 9 value index 62

slide-30
SLIDE 30

Exercise

  • Insert the following sequence of items into an initially empty

max heap: 34, 76, 11, 32, 73, 9, 50, 65, 41, 27, 3, 88

  • Draw the underlying array after all insertions have completed
  • Insert the same sequence of items into an initially empty min

heap and draw the array after all insertions have been completed.

  • Implement recursive and iterative implementations:

– void MinHeap::heapifyUp(int index);

March 16, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 30

slide-31
SLIDE 31

Readings for this lesson

  • Carrano & Henry

– Chapter 13.3 (ADT priority queue) – Chapter 17.1 – 17.3 (Heap)

  • Next class:

– Carrano & Henry, Chapter 17.4 (Heapsort)

March 16, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 31