Chapter 13 Priority Queues Priority queue n n

  • Slides: 36
Download presentation
Chapter 13 Priority Queues

Chapter 13 Priority Queues

Priority queue n n n A stack is first in, last out A queue

Priority queue n n n A stack is first in, last out A queue is first in, first out A priority queue is least-in-first-out n The “smallest” element is the first one removed n n n (You could also define a largest-in-first-out priority queue) The definition of “smallest” is up to the programmer (for example, you might define it by implementing Comparator or Comparable) If there are several “smallest” elements, the implementer must decide which to remove first n n Remove any “smallest” element (don’t care which) Remove the first one added 2

A priority queue ADT n Here is one possible ADT: n n n n

A priority queue ADT n Here is one possible ADT: n n n n Priority. Queue(): a constructor void add(Comparable o): inserts o into the priority queue Comparable remove. Least(): removes and returns the least element Comparable get. Least(): returns (but does not remove) the least element boolean is. Empty(): returns true iff empty int size(): returns the number of elements void clear(): discards all elements 3

Evaluating implementations n When we choose a data structure, it is important to look

Evaluating implementations n When we choose a data structure, it is important to look at usage patterns n n If we load an array once and do thousands of searches on it, we want to make searching fast—so we would probably sort the array If we load a huge array and expect to do only a few searches, we probably don’t want to spend time sorting the array For almost all uses of a queue (including a priority queue), we eventually remove everything that we add Hence, when we analyze a priority queue, neither “add” nor “remove” is more important—we need to look at the timing for “add + remove” 4

Array implementations n A priority queue could be implemented as an unsorted array (with

Array implementations n A priority queue could be implemented as an unsorted array (with a count of elements) n n n Adding an element would take O(1) time (why? ) Removing an element would take O(n) time (why? ) Hence, adding and removing an element takes O(n) time This is an inefficient representation A priority queue could be implemented as a sorted array (again, with a count of elements) n n Adding an element would take O(n) time (why? ) Removing an element would take O(1) time (why? ) Hence, adding and removing an element takes O(n) time Again, this is inefficient 5

Linked list implementations n A priority queue could be implemented as an unsorted linked

Linked list implementations n A priority queue could be implemented as an unsorted linked list n n n A priority queue could be implemented as a sorted linked list n n n Adding an element would take O(1) time (why? ) Removing an element would take O(n) time (why? ) Adding an element would take O(n) time (why? ) Removing an element would take O(1) time (why? ) As with array representations, adding and removing an element takes O(n) time n Again, these are inefficient implementations 6

Binary tree implementations n A priority queue could be represented as a (not necessarily

Binary tree implementations n A priority queue could be represented as a (not necessarily balanced) binary search tree n n n Insertion times would range from O(log n) to O(n) (why? ) Removal times would range from O(log n) to O(n) (why? ) A priority queue could be represented as a balanced binary search tree n n n Insertion and removal could destroy the balance We need an algorithm to rebalance the binary tree Good rebalancing algorithms require only O(log n) time, but are complicated 7

Heap implementation n n A priority queue can be implemented as a heap In

Heap implementation n n A priority queue can be implemented as a heap In order to do this, we have to define the heap property n n In Heapsort, a node has the heap property if it is at least as large as its children (for a MAX heap) For a priority queue, we will define a node to have the heap property if it is as least as small as its children (since we are using smaller numbers to represent higher priorities) – i. e. a MIN heap 3 12 8 3 Heapsort: Blue node has the MAX heap property 8 12 Priority queue: Blue node has the MIN heap property 8

Array representation of a heap 3 12 18 0 6 14 1 2 8

Array representation of a heap 3 12 18 0 6 14 1 2 8 3 4 last. Index = 5 5 6 7 8 9 10 11 12 3 12 6 18 14 8 n Left child of node i is 2*i + 1, right child is 2*i + 2 n n Unless the computation yields a value larger than last. Index, in which case there is no such child Parent of node i is (i – 1)/2 n Unless i == 0 9

Using the heap n To add an element: n n Increase last. Index and

Using the heap n To add an element: n n Increase last. Index and put the new value there Reheap the newly added node n n n This is called up-heap bubbling or percolating up Up-heap bubbling requires O(log n) time To remove an element: n n Remove the element at location 0 Move the element at location last. Index to location 0, and decrement last. Index n Reheap the new root node (the one now at location 0) n n n This is called down-heap bubbling or percolating down Down-heap bubbling requires O(log n) time Thus, it requires O(log n) time to add and remove an element 10

Comments n n A priority queue is a data structure that is designed to

Comments n n A priority queue is a data structure that is designed to return elements in order of priority Efficiency is usually measured as the sum of the time it takes to add and to remove an element n n n Simplementations take O(n) time Heap implementations take O(log n) time Balanced binary tree implementations take O(log n) time Binary tree implementations, without regard to balance, can take O(n) (linear) time Thus, for any sort of heavy-duty use, heap or balanced binary tree implementations are better 11

Java 5 java. util. Priority. Queue n Java 5 finally has a Priority. Queue

Java 5 java. util. Priority. Queue n Java 5 finally has a Priority. Queue class, based on heaps n n n n Priority. Queue<E> queue = new Priority. Queue<E>(); boolean add(E o) boolean remove(Object o) boolean offer(E o) E peek() boolean poll() void clear() int size() 12

Heaps A heap is a binary tree with properties: 1. It is complete •

Heaps A heap is a binary tree with properties: 1. It is complete • • Each level of tree completely filled Except possibly bottom level (nodes in left most positions) 2. It satisfies heap-order property • Data in each node >= data in children Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0 -13 -140909 -3 13

Heaps • Which of the following are MAX heaps? A B Nyhoff, ADTs, Data

Heaps • Which of the following are MAX heaps? A B Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0 -13 -140909 -3 C 14

Implementing a Heap • Use an array or vector • Number the nodes from

Implementing a Heap • Use an array or vector • Number the nodes from top to bottom – Number nodes on each row from left to right • Store data in ith node in ith location of array (vector) Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0 -13 -140909 -3 15

Implementing a Heap • Note the placement of the nodes in the array (note

Implementing a Heap • Note the placement of the nodes in the array (note the array cells start at 1 not 0, unlike our implementation) Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0 -13 -140909 -3 16

Implementing a Heap (note array starts at 1 here) • In an array implementation

Implementing a Heap (note array starts at 1 here) • In an array implementation children of ith node are at my. Array[2*i] and my. Array[2*i+1] • Parent of the ith node is at may. Array[i/2] Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0 -13 -140909 -3 17

Basic Heap Operations • Constructor – Set my. Size to 0, allocate array •

Basic Heap Operations • Constructor – Set my. Size to 0, allocate array • Empty – Check value of my. Size • Retrieve max item – Return root of the binary tree, my. Array[1] Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0 -13 -140909 -3 18

Basic Heap Operations • Delete max item – Max item is the root, replace

Basic Heap Operations • Delete max item – Max item is the root, replace with last node in tree Result called a semiheap – Then interchange root with larger of two children – Continue this with the resulting sub-tree(s) Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0 -13 -140909 -3 19

Percolate Down Algorithm (for an array starting at 1—your handout has pseudocode for 0

Percolate Down Algorithm (for an array starting at 1—your handout has pseudocode for 0 based array) 1. Set c = 2 * r 2. While r <= n do following a. If c < n and my. Array[c] < my. Array[c + 1] Increment c by 1 b. If my. Array[r] < my. Array[c] i. Swap my. Array[r] and my. Array[c] ii. set r = c iii. Set c = 2 * c else Terminate repetition End while Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0 -13 -140909 -3 20

Basic Heap Operations • Insert an item – Amounts to a percolate up routine

Basic Heap Operations • Insert an item – Amounts to a percolate up routine – Place new item at end of array – Interchange with parent so long as it is greater than its parent Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0 -13 -140909 -3 21

Percolate Up for 0 -based array • • Percolate. Up(int leaf) Set p =

Percolate Up for 0 -based array • • Percolate. Up(int leaf) Set p = parent index of leaf Set value = data at leaf index While leaf > 0 AND value < parent value – Change the leaf data to parent’s data – Set leaf index = parent index – Set p = new parent index of leaf • Set data at final leaf position to value Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0 -13 -140909 -3 22

Heapsort • Given a list of numbers in an array – Stored in a

Heapsort • Given a list of numbers in an array – Stored in a complete binary tree • Convert to a heap – Begin at last node not a leaf – Apply percolated down to this subtree – Continue Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0 -13 -140909 -3 23

Heapsort • Algorithm for converting a complete binary tree to a heap – called

Heapsort • Algorithm for converting a complete binary tree to a heap – called "heapify" For r = n/2 down to 1: Apply percolate_down to the subtree in my. Array[r] , … my. Array[n] End for • Puts largest element at root Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0 -13 -140909 -3 24

Heapsort • Now swap element 1 (root of tree) with last element – This

Heapsort • Now swap element 1 (root of tree) with last element – This puts largest element in correct location • Use percolate down on remaining sublist – Converts from semi-heap to heap Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0 -13 -140909 -3 25

Heapsort • Again swap root with rightmost leaf • Continue this process with shrinking

Heapsort • Again swap root with rightmost leaf • Continue this process with shrinking sublist Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0 -13 -140909 -3 26

Heapsort Algorithm 1. Consider x as a complete binary tree, use heapify to convert

Heapsort Algorithm 1. Consider x as a complete binary tree, use heapify to convert this tree to a heap 2. for i = n down to 2: a. Interchange x[1] and x[i] (puts largest element at end) b. Apply percolate_down to convert binary tree corresponding to sublist in x[1]. . x[i-1] Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0 -13 -140909 -3 27

Heap Algorithms in STL • Found in the <algorithm> library – make_heap() heapify –

Heap Algorithms in STL • Found in the <algorithm> library – make_heap() heapify – push_heap() insert – pop_heap() delete – sort_heap() heapsort • Note program which illustrates these operations, Fig. 13. 1 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0 -13 -140909 -3 28

Priority Queue • A collection of data elements – Items stored in order by

Priority Queue • A collection of data elements – Items stored in order by priority – Higher priority items removed ahead of lower • Operations – – – – Constructor Insert Find, remove smallest/largest (priority) element Replace Change priority Delete an item Join two priority queues into a larger one Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0 -13 -140909 -3 29

Priority Queue • Implementation possibilities – As a list (array, vector, linked list) –

Priority Queue • Implementation possibilities – As a list (array, vector, linked list) – As an ordered list – Best is to use a heap Basic operations have O(log 2 n) time • Java priority queue class uses heap Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0 -13 -140909 -3 30

OSsim. java • Simulates a (very slow!) "operating system" • Each minute one task

OSsim. java • Simulates a (very slow!) "operating system" • Each minute one task is processed • Each minute 0, 1 or 2 tasks arrive – placed in Priority. Queue<Task> PQ – to be processed in a future minute Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0 -13 -140909 -3 31

OSsim Animation • Trace Table Minute Task Dequeue'd Wait Time num Arrivals Tasks Enqueue'd

OSsim Animation • Trace Table Minute Task Dequeue'd Wait Time num Arrivals Tasks Enqueue'd 1 - 2 new Task(0, 1) new Task(1, 1) - Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0 -13 -140909 -3 PQ (0, 1) (1, 1) 32

OSsim Animation • Trace Table Minute Task Dequeue'd Wait Time num Arrivals Tasks Enqueue'd

OSsim Animation • Trace Table Minute Task Dequeue'd Wait Time num Arrivals Tasks Enqueue'd 1 - - 2 2 (0, 1) 1 2 new Task(0, 1) new Task(1, 1) new Task(0, 2) Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0 -13 -140909 -3 PQ (0, 2) (1, 1) 33

OSsim Animation • Trace Table Minute Task Dequeue'd Wait Time num Arrivals Tasks Enqueue'd

OSsim Animation • Trace Table Minute Task Dequeue'd Wait Time num Arrivals Tasks Enqueue'd 1 - - 2 2 (0, 1) 1 2 3 (0, 2) 1 1 new Task(0, 1) new Task(1, 1) new Task(0, 2) new Task(1, 3) Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0 -13 -140909 -3 PQ (0, 2) (1, 1) (1, 3) 34

OSsim Animation • Trace Table Minute Task Dequeue'd Wait Time num Arrivals Tasks Enqueue'd

OSsim Animation • Trace Table Minute Task Dequeue'd Wait Time num Arrivals Tasks Enqueue'd 1 - - 2 2 (0, 1) 1 2 3 (0, 2) 1 1 new Task(0, 1) new Task(1, 1) new Task(0, 2) new Task(1, 3) 4 (0, 2) 1 0 - Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0 -13 -140909 -3 PQ (1, 1) (1, 3) 35

OSsim Animation • Trace Table PQ (1, 3) Minute Task Dequeue'd Wait Time num

OSsim Animation • Trace Table PQ (1, 3) Minute Task Dequeue'd Wait Time num Arrivals Tasks Enqueue'd 1 - - 2 2 (0, 1) 1 2 3 (0, 2) 1 1 new Task(0, 1) new Task(1, 1) new Task(0, 2) new Task(1, 3) 4 (0, 2) 1 0 - 5 (1, 1) 4 0 - Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0 -13 -140909 -3 36