Chapter 13 Priority Queues. 2 Priority queue A stack is first in, last out A queue is first in, first out A priority queue is least-in-first-out The “smallest”

Slides:



Advertisements
Similar presentations
COL 106 Shweta Agrawal and Amit Kumar
Advertisements

Heaps1 Part-D2 Heaps Heaps2 Recall Priority Queue ADT (§ 7.1.3) A priority queue stores a collection of entries Each entry is a pair (key, value)
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture24.
Priority Queues. 2 Priority queue A stack is first in, last out A queue is first in, first out A priority queue is least-first-out The “smallest” element.
Sorting Gordon College 13.1 Some O(n2) Sorting Schemes
Priority Queues. Container of elements where each element has an associated key A key is an attribute that can identify rank or weight of an element Examples.
Heapsort. 2 Why study Heapsort? It is a well-known, traditional sorting algorithm you will be expected to know Heapsort is always O(n log n) Quicksort.
Priority Queues. Priority queue A stack is first in, last out A queue is first in, first out A priority queue is least-first-out –The “smallest” element.
Priority Queues. Priority queue A stack is first in, last out A queue is first in, first out A priority queue is least-first-out –The “smallest” element.
Priority Queues. Container of elements where each element has an associated key A key is an attribute that can identify rank or weight of an element Examples.
Priority Queues1 Part-D1 Priority Queues. Priority Queues2 Priority Queue ADT (§ 7.1.3) A priority queue stores a collection of entries Each entry is.
Sorting Chapter 13 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved
Maps A map is an object that maps keys to values Each key can map to at most one value, and a map cannot contain duplicate keys KeyValue Map Examples Dictionaries:
Heapsort Based off slides by: David Matuszek
1 HEAPS & PRIORITY QUEUES Array and Tree implementations.
Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07.
1 Hash Tables  a hash table is an array of size Tsize  has index positions 0.. Tsize-1  two types of hash tables  open hash table  array element type.
PRIORITY QUEUES (HEAPS). Queues are a standard mechanism for ordering tasks on a first-come, first-served basis However, some tasks may be more important.
ADT Table and Heap Ellen Walker CPSC 201 Data Structures Hiram College.
Heapsort CSC Why study Heapsort? It is a well-known, traditional sorting algorithm you will be expected to know Heapsort is always O(n log n)
The Binary Heap. Binary Heap Looks similar to a binary search tree BUT all the values stored in the subtree rooted at a node are greater than or equal.
Adapted from instructor resource slides Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All.
1 Heaps and Priority Queues Starring: Min Heap Co-Starring: Max Heap.
Priority Queues Dr. David Matuszek
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Sorting.
Sorting Dr. Yingwu Zhu. Heaps A heap is a binary tree with properties: 1. It is complete Each level of tree completely filled Except possibly bottom level.
Chapter 2: Basic Data Structures. Spring 2003CS 3152 Basic Data Structures Stacks Queues Vectors, Linked Lists Trees (Including Balanced Trees) Priority.
COSC2007 Data Structures II Chapter 12 Tables & Priority Queues III.
Priority Queues and Heaps. October 2004John Edgar2  A queue should implement at least the first two of these operations:  insert – insert item at the.
1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap.
Data Structures Chapter 6. Data Structure A data structure is a representation of data and the operations allowed on that data. Examples: 1.Array 2.Record.
CPSC 252 Binary Heaps Page 1 Binary Heaps A complete binary tree is a binary tree that satisfies the following properties: - every level, except possibly.
PRIORITY QUEUES AND HEAPS Slides of Ken Birman, Cornell University.
Heapsort. What is a “heap”? Definitions of heap: 1.A large area of memory from which the programmer can allocate blocks as needed, and deallocate them.
1 Heaps A heap is a binary tree. A heap is best implemented in sequential representation (using an array). Two important uses of heaps are: –(i) efficient.
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part R3. Priority Queues.
Sorting Dr. Yingwu Zhu. Heaps A heap is a binary tree with properties: 1. It is complete Each level of tree completely filled Except possibly bottom level.
HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.
FALL 2005CENG 213 Data Structures1 Priority Queues (Heaps) Reference: Chapter 7.
AVL Trees and Heaps. AVL Trees So far balancing the tree was done globally Basically every node was involved in the balance operation Tree balancing can.
Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin, and Skylight.
CS 367 Introduction to Data Structures Lecture 8.
Heaps © 2010 Goodrich, Tamassia. Heaps2 Priority Queue ADT  A priority queue (PQ) stores a collection of entries  Typically, an entry is a.
Properties: -The value in each node is greater than all values in the node’s subtrees -Complete tree! (fills up from left to right) Max Heap.
2 Binary Heaps What if we’re mostly concerned with finding the most relevant data?  A binary heap is a binary tree (2 or fewer subtrees for each node)
Course: Programming II - Abstract Data Types HeapsSlide Number 1 The ADT Heap So far we have seen the following sorting types : 1) Linked List sort by.
1 Priority Queues (Heaps). 2 Priority Queues Many applications require that we process records with keys in order, but not necessarily in full sorted.
Heaps and Priority Queues What is a heap? A heap is a binary tree storing keys at its internal nodes and satisfying the following properties:
Heaps (8.3) CSE 2011 Winter May 2018.
Heaps 8/2/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser,
Heap Sort Example Qamar Abbas.
Description Given a linear collection of items x1, x2, x3,….,xn
original list {67, 33,49, 21, 25, 94} pass { } {67 94}
Chapter 8 – Binary Search Tree
Priority Queues.
Tree Representation Heap.
CS Data Structures Chapter 17 Heaps Mehmet H Gunes
Heaps © 2014 Goodrich, Tamassia, Goldwasser Heaps Heaps
A Heap Implementation Chapter 26 Adapted from Pearson Education, Inc.
Priority Queues & Heaps
Adapted from instructor resource slides
Sorting Dr. Yingwu Zhu.
Priority Queues.
Priority Queues.
Heaps By JJ Shepherd.
Heaps and priority queues
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Instructor: Dr. Michael Geiger Spring 2017 Lecture 30: Sorting & heaps
CS210- Lecture 13 June 28, 2005 Agenda Heaps Complete Binary Tree
Presentation transcript:

Chapter 13 Priority Queues

2 Priority queue A stack is first in, last out A queue is first in, first out A priority queue is least-in-first-out The “smallest” element is the first one removed (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 Remove any “smallest” element (don’t care which) Remove the first one added

3 A priority queue ADT Here is one possible ADT: PriorityQueue() : a constructor void add(Comparable o) : inserts o into the priority queue Comparable removeLeast() : removes and returns the least element Comparable getLeast() : returns (but does not remove) the least element boolean isEmpty() : returns true iff empty int size() : returns the number of elements void clear() : discards all elements

4 Evaluating implementations When we choose a data structure, it is important to look at usage patterns 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”

5 Array implementations A priority queue could be implemented as an unsorted array (with a count of elements) 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) 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

6 Linked list implementations A priority queue could be implemented as an unsorted linked list Adding an element would take O(1) time (why?) Removing an element would take O(n) time (why?) A priority queue could be implemented as a sorted linked list 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 Again, these are inefficient implementations

7 Binary tree implementations A priority queue could be represented as a (not necessarily balanced) binary search tree 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 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

8 Heap implementation A priority queue can be implemented as a heap In order to do this, we have to define the heap property 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 Heapsort: Blue node has the MAX heap property Priority queue: Blue node has the MIN heap property

9 Array representation of a heap Left child of node i is 2*i + 1, right child is 2*i + 2 Unless the computation yields a value larger than lastIndex, in which case there is no such child Parent of node i is (i – 1)/2 Unless i == lastIndex = 5

10 Using the heap To add an element: Increase lastIndex and put the new value there Reheap the newly added node This is called up-heap bubbling or percolating up Up-heap bubbling requires O(log n) time To remove an element: Remove the element at location 0 Move the element at location lastIndex to location 0, and decrement lastIndex Reheap the new root node (the one now at location 0 ) 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

11 Comments 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 Simple implementations 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

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

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved 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 Heaps Which of the following are MAX heaps? A B C

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved 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 i th node in i th location of array (vector)

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved 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 Implementing a Heap (note array starts at 1 here) In an array implementation children of i th node are at myArray[2*i] and myArray[2*i+1] Parent of the i th node is at mayArray[i/2]

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

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

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved 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 myArray[c] < myArray[c + 1] Increment c by 1 b. If myArray[r] < myArray[c] i. Swap myArray[r] and myArray[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 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

Percolate Up for 0-based array PercolateUp(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

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved 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 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 myArray[r ], … myArray[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 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 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 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 Heap Algorithms in STL Found in the library –make_heap() heapify –push_heap() insert –pop_heap() delete –sort_heap() heapsort Note program which illustrates these operations, Fig. 13.1Fig. 13.1

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved 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 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

OSsim.java Simulates a (very slow!) "operating system" Each minute one task is processed Each minute 0, 1 or 2 tasks arrive –placed in PriorityQueue 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

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

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

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

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

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