CSE 2123: Collections: Priority Queues. Jeremy Morris

Similar documents
Programming II (CS300)

Programming II (CS300)

The heap is essentially an array-based binary tree with either the biggest or smallest element at the root.

CSCI 136 Data Structures & Advanced Programming. Lecture 22 Fall 2018 Instructor: Bills

Advanced Java Concepts Unit 3: Stacks and Queues

Heaps. Heaps. A heap is a complete binary tree.

Priority Queues and Binary Heaps

PRIORITY QUEUES AND HEAPS

3. Priority Queues. ADT Stack : LIFO. ADT Queue : FIFO. ADT Priority Queue : pick the element with the lowest (or highest) priority.

Trees & Tree-Based Data Structures. Part 4: Heaps. Definition. Example. Properties. Example Min-Heap. Definition

INFO1x05 Tutorial 6. Exercise 1: Heaps and Priority Queues

PRIORITY QUEUES AND HEAPS

Sorting and Searching

Priority queues. Priority queues. Priority queue operations

Priority Queues. Lecture15: Heaps. Priority Queue ADT. Sequence based Priority Queue

Algorithms and Data Structures

Sorting and Searching

The smallest element is the first one removed. (You could also define a largest-first-out priority queue)

Priority queues. Priority queues. Priority queue operations

CS2 Algorithms and Data Structures Note 6

CS2 Algorithms and Data Structures Note 6

Computing Science 115 Final Examination April 23, 2002 Section: B2 BASU. Please put student id on last page. Instructions:

9. Heap : Priority Queue

PRIORITY QUEUES AND HEAPS

You must include this cover sheet. Either type up the assignment using theory5.tex, or print out this PDF.

Lecture 13: AVL Trees and Binary Heaps

CSE332: Data Abstractions Lecture 4: Priority Queues; Heaps. James Fogarty Winter 2012

CSE 241 Class 17. Jeremy Buhler. October 28, Ordered collections supported both, plus total ordering operations (pred and succ)

HEAPS: IMPLEMENTING EFFICIENT PRIORITY QUEUES

Stacks, Queues, and Priority Queues. Inf 2B: Heaps and Priority Queues. The PriorityQueue ADT

Exam Datastrukturer. DIT960 / DIT961, VT-18 Göteborgs Universitet, CSE

CSE 2123 Recursion. Jeremy Morris

CSE 214 Computer Science II Heaps and Priority Queues

Priority Queues and Huffman Encoding

Priority Queues and Huffman Encoding

EXAMINATIONS 2016 TRIMESTER 2

EXAMINATIONS 2015 COMP103 INTRODUCTION TO DATA STRUCTURES AND ALGORITHMS

Thus, it is reasonable to compare binary search trees and binary heaps as is shown in Table 1.

Computer Science 210 Data Structures Siena College Fall Topic Notes: Priority Queues and Heaps

Priority Queues and Binary Heaps. See Chapter 21 of the text, pages

Operations. Priority Queues & Heaps. Prio-Q as Array. Niave Solution: Prio-Q As Array. Prio-Q as Sorted Array. Prio-Q as Sorted Array

Name CPTR246 Spring '17 (100 total points) Exam 3

CSE 332 Autumn 2013: Midterm Exam (closed book, closed notes, no calculators)

Data Structures and Algorithms

CS210 (161) with Dr. Basit Qureshi Final Exam Weight 40%

CSC 321: Data Structures. Fall 2016

CSC 321: Data Structures. Fall 2017

Priority Queues Heaps Heapsort

Chapter 6 Heapsort 1

CS 61B Midterm 2 Guerrilla Section Spring 2018 March 17, 2018

Priority Queues and Binary Heaps. See Chapter 21 of the text, pages

COMP 250 Fall priority queues, heaps 1 Nov. 9, 2018

Priority Queues 1 / 15

CSCI 200 Lab 11 A Heap-Based Priority Queue

Heaps, stacks, queues

Data Structures Lecture 7

CE 221 Data Structures and Algorithms

COMP : Trees. COMP20012 Trees 219

Abstract vs concrete data structures HEAPS AND PRIORITY QUEUES. Abstract vs concrete data structures. Concrete Data Types. Concrete data structures

Heaps 2. Recall Priority Queue ADT. Heaps 3/19/14

Review: Trees Binary Search Trees Sets in Java Collections API CS1102S: Data Structures and Algorithms 05 A: Trees II

- Alan Perlis. Topic 24 Heaps

EXAMINATIONS 2017 TRIMESTER 2

CS350: Data Structures Heaps and Priority Queues

Data Structures. Giri Narasimhan Office: ECS 254A Phone: x-3748

BINARY HEAP cs2420 Introduction to Algorithms and Data Structures Spring 2015

CSE373: Data Structures & Algorithms Lecture 9: Priority Queues and Binary Heaps. Linda Shapiro Spring 2016

Priority Queues, Binary Heaps, and Heapsort

How much space does this routine use in the worst case for a given n? public static void use_space(int n) { int b; int [] A;

CSE 332: Data Structures & Parallelism Lecture 3: Priority Queues. Ruth Anderson Winter 2019

Heaps Goodrich, Tamassia. Heaps 1

Lecture 23: Priority Queues 10:00 AM, Mar 19, 2018

" Which data Structure to extend to create a heap? " Two binary tree extensions are needed in a heap. n it is a binary tree (a complete one)

CS106X Programming Abstractions in C++ Dr. Cynthia Bailey Lee

Containers: Priority Queues. Jordi Cortadella and Jordi Petit Department of Computer Science

Containers: Priority Queues

Describe how to implement deque ADT using two stacks as the only instance variables. What are the running times of the methods

Priority Queues Heaps Heapsort

Priority Queue. How to implement a Priority Queue? queue. priority queue

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Priority Queues / Heaps Date: 9/27/17

Summer Final Exam Review Session August 5, 2009

Heap sort. Carlos Moreno uwaterloo.ca EIT

CSE100. Advanced Data Structures. Lecture 12. (Based on Paul Kube course materials)

CompSci 201 Tree Traversals & Heaps

This is a set of practice questions for the final for CS16. The actual exam will consist of problems that are quite similar to those you have

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

Data Structures and Algorithms

Heaps and Priority Queues

COMP Data Structures

CSE 373: Data Structures and Algorithms

CSE332 Summer 2010: Final Exam

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

Binary Heaps in Dynamic Arrays

Properties of a heap (represented by an array A)

U N I V E R S I T Y O F W E L L I N G T O N EXAMINATIONS 2018 TRIMESTER 2 COMP 103 PRACTICE EXAM

CSCI-1200 Data Structures Fall 2018 Lecture 23 Priority Queues II

1.00 Lecture 26. Data Structures: Introduction Stacks. Reading for next time: Big Java: Data Structures

ext Total Score /20 /20 /15 /20 /25 /5 Grader

CMSC 341 Lecture 10 Binary Search Trees

CS 240 Fall Mike Lam, Professor. Priority Queues and Heaps

Transcription:

CSE 2123: Collections: Priority Queues Jeremy Morris 1

Collections Priority Queue Recall: A queue is a specific type of collection Keeps elements in a particular order We ve seen two examples FIFO queues Stacks (LIFO queues) What if we want to keep things in a particular order that isn t based on when they entered the queue? Keep elements in sorted order using a priority 2

Collections Priority Queue A priority queue uses queue operations like a FIFO queue or a stack But when remove is called, the element with the most urgent priority is returned Instead of the element put first into the queue or last into the queue For the Java implementation of a priority queue, lower values have a more urgent priority Something with a priority of 1 will be removed before something with a priority of 10 This of this as a ranked list of items the #1 football team has a higher priority than the #10 team 3

Collections Priority Queue Priority queue processing relies on a few standard methods (that should be familiar): Add an object to the queue: void add(e obj) Retrieve (and remove) the item with the lowest score from the queue E remove() Examine the next item in the queue without removing it: E peek() Test to see if the queue is empty: boolean isempty() 4

Collections Priority Queue Java uses the PriorityQueue class to implement a priority queue A priority queue of Integers: PriorityQueue<Integer> intqueue = new PriorityQueue<Integer>(); A priority queue of Strings: PriorityQueue<String> strqueue = new PriorityQueue<String>(); 5

Collections Priority Queues What do we use for rankings (priorities)? Numeric types (Integer, Double) are in the obvious order (ascending by value) For boolean values, false comes before true Characters are ordered by their numeric (Unicode) value Strings are ordered lexicographically Similar to how words are ordered in the dictionary See pages 92-93 in the text or the Java API documentation for the compareto method of String for details 6

Collections Priority Queues What do we use for rankings (priorities) for other types? Java s priority queue implementation requires that we implement the Comparable interface for anything we want to put into it We must write a compareto method for any classes we want to use with a PriorityQueue As with TreeSets and TreeHash 7

Example Priority Queue public static void main(string[] args) { // TODO Auto-generated method stub PriorityQueue<Integer> myqueue = new PriorityQueue<Integer>(); myqueue.add(10); myqueue.add(5); myqueue.add(22); System.out.println("QUEUE: "+myqueue); } while (!myqueue.isempty()) { int head = myqueue.remove(); System.out.print("HEAD: "+head+" "); System.out.println("QUEUE: "+myqueue); } 8

Collections Priority Queue (Heaps) Priority queues are often implemented using a data structure known as a heap This is how the PriorityQueue class is implemented in Java A heap is another form of binary tree It is NOT a binary search tree Instead we want to keep our element with the most urgent priority (smallest value) as the root node of the heap 9

Heaps must satisfy a heap ordering property Heaps can either be min heaps or max heaps, determined by their ordering property For Java s PriorityQueue, the min heap property is used (and that will be the one we discuss here) The min heap property states that the value of each node is always greater than or equal to the value of its parent That ensures that the node with the minimum value will be at the root 10

Suppose we have the elements {1, 3, 7, 10, 19, 22} stored in a heap The heap constructed might look like the tree to the right 3 1 7 The heap creates a complete binary tree filling in nodes across each level 10 19 22 11

Suppose we have the elements {1, 3, 7, 10, 19, 22} stored in a heap The heap constructed might look like the tree to the right 3 1 7 The heap creates a complete binary tree filling in nodes across each level 10 19 22 Now suppose we remove our first element (our most urgent value in the min node) 12

Now suppose we remove our first element (our most urgent value in the min node) We need a new root node 1 3 22 7 How can we preserve our min heap property? When the root node is removed, we automatically swap in the bottom node of the priority queue 10 19 13

When the root node is removed, we automatically swap in the bottom node of the priority queue 1 3 22 7 The min heap property is violated, so our remove is not finished yet Next we need fix our tree so that the heap property is maintained 10 19 The root is swapped with its child that has the smallest value 14

The root is swapped with its child that has the smallest value 1 3 We re still not done our heap property is still violated 22 7 So our node is again swapped with its child that has the smallest value 10 19 15

We re still not done our heap property is still violated 1 3 So our node is again swapped with its child that has the smallest value 10 7 At this point we re finished We have our value taken off the priority queue 22 19 The min heap property is no longer violated The root node is our minimum value and is in position to be removed next 16

What about if we need to add something to our heap? 3 This operates like remove in reverse Instead of moving a bad root node down the heap, we ll move a good node up the heap 10 7 We stop when we reach the spot in the heap where our node should be 22 19 Let s add the value of 2 to our heap 17

Let s add the value of 2 to our heap 3 The 2 is added to the bottom of the heap We re in violation of our min heap property 10 7 So we swap the child with its parent 22 19 2 18

We re in violation of our min heap property 3 So we swap the child with its parent We re still in violation of our min heap property, so we keep going 10 2 We swap our inserted node with its new parent 22 19 7 19

We swap our inserted node with its new parent 2 And now we re done Our min heap property is satisfied 10 3 No more swaps are needed 22 19 7 20

Notice that our min heap property is maintained just by applying a few simple rules Always insert at the bottom of the heap When we insert, check to see if the parent of our new node is larger If it is, swap them and repeat until the parent is smaller or our new node is the root node Remove nodes from the top of the heap When we remove, move the bottom node up to the root Check to see if our new root node is larger than its children If it is, swap it with the smallest of its children and repeat until all children are larger or we hit the bottom of the heap 21

Using these rules we can build a heap Suppose we have the elements {7, 10, 3, 22, 1, 19} to store in a heap We ll add them in that order First we add the 7 Nothing in the heap yet It has no parents The heap property is satisfied 7 22

Using these rules we can build a heap Suppose we have the elements {7, 10, 3, 22, 1, 19} to store in a heap We ll add them in that order First we add the 7 Next the 10 Add it to the bottom Compare to its parent The heap property is satisfied 10 7 23

{7, 10, 3, 22, 1, 19} Next the 3 Add it to the bottom Compare to its parent The heap property is NOT satisfied Swap it with its parent 7 10 3 24

{7, 10, 3, 22, 1, 19} Next the 3 Add it to the bottom Compare to its parent The heap property is NOT satisfied Swap it with its parent Now the heap property is satisfied 3 10 7 25

{7, 10, 3, 22, 1, 19} Next the 22 Add it to the bottom Compare to its parent The heap property is satisfied 22 3 10 7 26

{7, 10, 3, 22, 1, 19} Next the 1 Add it to the bottom Compare to its parent The heap property is NOT satisfied Swap it with its parent 22 1 3 10 7 27

{7, 10, 3, 22, 1, 19} Next the 1 Add it to the bottom Compare to its parent The heap property is NOT satisfied Swap it with its parent The heap property is STILL NOT satisfied Swap it with its new parent 22 10 3 1 7 28

{7, 10, 3, 22, 1, 19} Next the 1 Add it to the bottom Compare to its parent The heap property is NOT satisfied Swap it with its parent The heap property is STILL NOT satisfied Swap it with its new parent Now the heap property is satisfied 22 10 1 3 7 29

{7, 10, 3, 22, 1, 19} Next the 19 Add it to the bottom Compare to its parent The heap property is satisfied 1 3 7 22 10 19 30

Your Turn - Heaps Provide the heap that would result if the following integers were added to the tree in the given order: [ 8, 3, 23, 15, 1, 5, 42, 64, 36 ] Provide heap that would result if the following characters were added in the given order: [ r, q, z, a, c, l, x, w, b ] 31

Collections Priority Queue So when is it good to use a PriorityQueue in Java? With the heap implementation, finding the smallest value is constant time It s always at the top of the heap Removing the minimum element or adding a new element is at worst log n time We may need to make up to log n swaps to maintain our heap property Finding an arbitrary element in the heap is at worst linear time Worst case we have to examine every node! 32

Collections Priority Queue So when is it good to use a PriorityQueue in Java? When we restrict ourselves to queue operations Working only in priority order Don t need to access elements in the middle of the pack 33

Example 2 Priority Queue public static void main(string[] args) { // TODO Auto-generated method stub PriorityQueue<String> myqueue = new PriorityQueue<String>(); myqueue.add( Marley, Bob ); myqueue.add( Cool, Joe ); myqueue.add( Brown, Charles ); System.out.println("QUEUE: "+myqueue); } while (!myqueue.isempty()) { String head = myqueue.remove(); System.out.print("HEAD: "+head+" "); System.out.println("QUEUE: "+myqueue); } 34