Priority Queues Chapters 10 & 26.

Slides:



Advertisements
Similar presentations
COL 106 Shweta Agrawal and Amit Kumar
Advertisements

PRIORITY QUEUES AND HEAPS Lecture 19 CS2110 Spring
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.
Queues and Priority Queues
Chapter 13 Queues and Priority Queues CS Data Structures Mehmet H Gunes Modified from authors’ slides.
Binary Heaps CSE 373 Data Structures Lecture 11. 2/5/03Binary Heaps - Lecture 112 Readings Reading ›Sections
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.
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:
1 HEAPS & PRIORITY QUEUES Array and Tree implementations.
Queues and Priority Queues
9/17/20151 Chapter 12 - Heaps. 9/17/20152 Introduction ► Heaps are largely about priority queues. ► They are an alternative data structure to implementing.
Priority Queues and Binary Heaps Chapter Trees Some animals are more equal than others A queue is a FIFO data structure the first element.
Chapter 11 Heap. Overview ● The heap is a special type of binary tree. ● It may be used either as a priority queue or as a tool for sorting.
Data Structures Week 8 Further Data Structures The story so far  Saw some fundamental operations as well as advanced operations on arrays, stacks, and.
1 Heaps and Priority Queues Starring: Min Heap Co-Starring: Max Heap.
Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority.
CSE373: Data Structures & Algorithms Lecture 6: Priority Queues Dan Grossman Fall 2013.
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.
PRIORITY QUEUES AND HEAPS Slides of Ken Birman, Cornell University.
Queues & Priority Queues Radix Sort Heap Sort. Outline n Queues queue operationsqueue operations algorithms with queuesalgorithms with queues radix sortradix.
CSE373: Data Structures & Algorithms Lecture 6: Priority Queues Kevin Quinn Fall 2015.
Binary heaps and Heapsort. Priority Queues n The smallest element is removed item = remove();item = remove(); Priority Queue Minimum.
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”
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part R3. Priority Queues.
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.
CompSci 100e 8.1 Scoreboard l What else might we want to do with a data structure? AlgorithmInsertionDeletionSearch Unsorted Vector/array Sorted vector/array.
CSE332: Data Abstractions Lecture 4: Priority Queues Dan Grossman Spring 2012.
1 Priority Queues (Heaps)  Sections 6.1 to Priority Queues  Regular queues which supports –First In, First Out –Enqueue(): add a new element.
Priority Queues and Heaps Tom Przybylinski. Maps ● We have (key,value) pairs, called entries ● We want to store and find/remove arbitrary entries (random.
CSE373: Data Structures & Algorithms Priority Queues
CSE373: Data Structures & Algorithms
Chapter 11 Heap.
Priority Queues (Heaps)
October 30th – Priority QUeues
Hashing Exercises.
Chapter 13 Queues and Priority Queues
Heap Chapter 9 Objectives Upon completion you will be able to:
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Heaps and Priority Queue
Chapter 8 – Binary Search Tree
Data Structures & Algorithms Priority Queues & HeapSort
i206: Lecture 14: Heaps, Graphs intro.
CSE332: Data Abstractions Lecture 4: Priority Queues
ITEC 2620M Introduction to Data Structures
Priority Queues.
Heaps Chapter 10 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates two.
Ch. 8 Priority Queues And Heaps
Heaps Chapter 10 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates two.
Heaps Chapter 11 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates.
A Robust Data Structure
Cs212: Data Structures Computer Science Department Lecture 7: Queues.
Data Structures and Analysis (COMP 410)
Sorting "There's nothing in your head the sorting hat can't see. So try me on and I will tell you where you ought to be." -The Sorting Hat, Harry Potter.
Heaps Chapter 11 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates.
A Heap Implementation Chapter 26 Adapted from Pearson Education, Inc.
Priority Queues & Heaps
CSE 12 – Basic Data Structures
Visit for more Learning Resources
Priority Queues.
CSE 373 Priority queue implementation; Intro to heaps
Priority Queues.
Priority Queues CSE 373 Data Structures.
Priority Queues & Heaps
CSCS-200 Data Structure and Algorithms
Data Structures and Algorithm Analysis Priority Queues (Heaps)
Heaps Chapter 10 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates two.
Heaps.
Presentation transcript:

Priority Queues Chapters 10 & 26

Outline Review of Stacks and Queues Priority Queues Visualization at priority queue operations the “heap” data structure Visualization at www.cs.usfca.edu/~galles/JavascriptVisual /Heap.html

Stack and Queue ADTs Stack access point: the top push an item onto the top look at the top item pull an item off the top Queue access points: front and back add an item at the back look at front item remove front item

Priority Queues Priority queue is a queue where the “most important” items get removed first “most important” = “highest priority” Most important is usually the smallest priority #1 priority #2 … priority # 1,000,004

Priority Queue Operations Priority queues must have these operations: add (push, insert) .add(item) remove highest priority item (pop) .remove() Typically have more operations…. look at highest priority item .peek() check if empty .isEmpty() get size .size() make empty .clear() Note: same as queues

Adding to Priority Queues Like Queues, only see one element unlike Queues, see smallest element, not oldest add(16) add(4) add(13) add(14) add(3) add(11) 16 4 16 4 13 16 4 13 14 16 3 4 13 14 16 3 4 11 13 14 16

Removing from Priority Queues Elements come out smallest to largest remove()  3 remove()  4 remove()  11 remove()  13 remove()  14 remove()  16 3 4 11 13 14 16 4 11 13 14 16 11 13 14 16 13 14 16 14 16 16

Priority Queue Restriction Need to know how to sort elements in PQ elements must know how to sort themselves: PQ<Integer> pqi; PQ<String> pqs; Class must implement Comparable public interface PQ<T extends Comparable<T>> actually, only need some supertype to be… public interface PQ<T extends Comparable<? super T>>

Review of Generic Types Included in <angle brackets> after name PQ<T>  PQ can hold any reference type PQ<T extends Comparable<T>>  PQ can only hold a type T that implements Comparable<T> PQ<T extends Comparable<? super T>>  PQ can hold a type T that implements Comparable<S> where S is a supertype of T

T extends Comparable<? super T> For example: public class Person implements Comparable<Person> where Person::compareTo sorts people by name public class Student extends Person Student is a Person, so Student implements Comparable<Person> Students are sorted by name PQ<Person> is OK (Persons sorted by name) PQ<Student> is OK (Students sorted by name)

Priority Queue Uses Airport baggage check with separate “Executive Class” check-in could be done as two queues Hospital emergency room may put a patient back in the PQ if more urgent case arrives during processing Asynchronous event simulation

Asynchronous Events Simulation of timed events Event record event type + time it occurs implements Comparable<Event> Keep events in a PQ get next event from the PQ (minimum element) update the clock with the time generate any new events & insert into the PQ

Bank Simulation Customers arrive at random Multiple tellers available served in the order they arrive (they queue!) Multiple tellers available serve customers as they become available some customers take more time than others Simulate with randomness random arrival times, random service times

Bank Simulation Events Event: arrive Time: 3 Customers arrive at random serve in order of arrival event arrival: record time of arrival Served by first available teller service takes random amount of time event departure: record time service will end Handle events in the order they happen priority queue sorted on time of event Event: arrive Time: 4 Event: arrive Time: 12 Event: depart Time: 8

Bank Simulation Parameters New arrival every 1 to 10 minutes for an hour can schedule these at start of simulation Each service takes 1 to 10 minutes can’t schedule until after they go to the teller! events need to be added during the simulation Calculate average & maximum wait time

arrival(4), arrival(7), arrival(8), arrival(17), … Arrival Schedule eventPQ arrival(4), arrival(7), arrival(8), arrival(17), … lineUpQ time numTellersFree 2 customers totalWaitingTime maxWaitingTime

First Event: Arrival @ time = 4 eventPQ arrival(7), arrival(8), departure(10), arrival(17), … arrival(7), arrival(8), arrival(17), arrival(18), … arrival(4), arrival(7), arrival(8), arrival(17), … lineUpQ time numTellersFree 4 1 2 customers totalWaitingTime maxWaitingTime 1

Next Event: Arrival @ 7 eventPQ arrival(8), departure(10), departure(11), … arrival(8), departure(10), arrival(17), arrival(18), … arrival(7), arrival(8), departure(10), arrival(17), … lineUpQ time numTellersFree 7 1 customers totalWaitingTime maxWaitingTime 2 1

Next Event: Arrival @ 8 eventPQ arrival(8), departure(10), departure(11), … departure(10), departure(11), arrival(17), … lineUpQ arrival(8) time numTellersFree 8 customers totalWaitingTime maxWaitingTime 2

Next Event: Departure @ 10 eventPQ departure(11), arrival(17), departure(17), … departure(11), arrival(17), arrival(18), … departure(10), departure(11), arrival(17), … lineUpQ arrival(8) time numTellersFree 10 customers totalWaitingTime maxWaitingTime 3 2 2 2

General Event Simulation P.Q. of events some events added at start some events added as go along loop until P.Q. is empty addStartingEvents(pq); while (!pq.isEmpty()) { Event e = pq.remove(); processEvent(e, pq); }

Priority Queues in Java java.util.PriorityQueue is a class PriorityQueue<Event> eventPQ = new PriorityQueue<>(); It implements the Queue interface(!) but of course removes items in the PQ way Bank simulation also need a Queue Queue<Event> lineUpQ = new ArrayDeque<>(); but only because people need to line up

PriorityQueues in Java Can use a Comparator when create the PQ PQ will use that comparator to sort the elements PriorityQueue<Integer> pq1, pq2; pq1 = new PriorityQueue<>(); pq2 = new PriorityQueue<>((x, y) -> y – x); pq1 removes smaller elements first pq2 removes larger elements first Need a Comparator unless is Comparable public class Event implements Comparable<Event>

Exercise Show the output of the following code: PriorityQueue<String> words = new PriorityQueue<>( (x, y) -> x.length() – y.length()); words.add("These"); words.add("are"); words.add("some"); words.add("elements."); while (!words.isEmpty()) { System.out.println(words.remove()); }

Implementing a PQ Seems like an ordered list would be good: adding removing 16 3 4 11 13 14 16 4 16 4 11 13 14 16 4 13 16 11 13 14 16 4 13 14 16 13 14 16 3 4 13 14 16 14 16 3 4 11 13 14 16 16

Implementing a PQ Turns out ordered list not so good it works it is slow I will explain this later in the term a lot of work to keep elements totally ordered they don’t need to be totally ordered only need to be able to find smallest quickly

A Heap Representation Keep elements semi-sorted by importance most important element at the front other elements “spread outˮ behind the first each element has two larger elements behind it elements near front are smaller elements near back are bigger remove  smaller elements shuffle forward add  push ahead until reach smaller element

A “Heap” of Elements Most important: 1st row Each element can have 2 more behind it (below in this diagram) may only have one, or maybe even none behind me  less important (larger) than me

Heap Order Property Highest priority item at root 5 Highest priority item at root here that’s the 5 Left & right children also heap ordered highest priority in that sub-tree at its root here: 7 on left & 12 on right Note: 5’s children are not next two smallest they could have been, but aren’t necessarily 7 12 81 8 24 17

Duplicate Entries Heaps allow duplicates A highest priority item appears at root others of same priority will be top in their sub-tree duplicates may also appear in separate sub-trees 1 1 5 8 1 8 14 15 14 14

Adding to a Heap New elements start at back of heap last row, as far left as possible may need to start a new row look at element just ahead decide whether to push up

Adding to a Heap New elements start at back of heap last row, as far left as possible may need to start a new row look at element just ahead decide whether to push up if smaller, move ahead element ahead drops back

Adding to a Heap New elements start at back of heap last row, as far left as possible may need to start a new row look at element just ahead decide whether to push up if smaller, move ahead element ahead drops back repeat until in proper position element ahead/above is smaller

New Element Joins the Heap Insert requires adding a new element to the structure & making it back into a heap small (high priority) items must “percolate up” stop when parent is smaller… or reach root 2 4 8 9 8 7 13 11 9 8 20 18 23 15 14 12 16 20 8

Storing Heaps in Arrays 5 5 Heap can be stored compactly in an array do a “level order” traversal of the tree into the array Left child of i is in 2i Right child of i is in 2i+1 Parent of i is in i/2 Leave location 0 empty! 7 7 12 12 81 81 8 8 24 24 17 17 1 2 3 4 5 6 7 5 7 12 81 8 24 17 Note: array index starts at 1

Exercise Store the following heap into an array 1 2 5 8 3 19 12 15 14 20

Exercise Draw the complete “heap” represented by the following array: [2, 4, 9, 7, 13, 11, 20, 18, 23, 15, 14, 12, 16, 19] Is it actually a heap? if not, can you make it into a heap?

Insert item into Heap Let N be the next available position in the array While N > 1 & A[N/2] > item set A[N] to A[N/2] set N to N/2 Set A[N] to the new item Add 1 to size of heap

Insertion Example Insert 6 into this heap N = 8 5 1 2 3 4 5 6 7 7 12 5 1 2 3 4 5 6 7 7 12 5 7 12 81 8 24 17 81 8 24 17

Insertion Example Insert 6 into this heap N  8 A[N/2] = A[4] = 81 5 Insert 6 into this heap N  8 A[N/2] = A[4] = 81 81 > 6  percolate up 1 2 3 4 5 6 7 8 7 12 5 7 12 81 8 24 17 ? 81 8 24 17 6

Insertion Example Insert 6 into this heap N = 4 A[N/2] = A[2] = 7 5 Insert 6 into this heap N = 4 A[N/2] = A[2] = 7 7 > 6  percolate up 1 2 3 4 5 6 7 8 7 12 5 7 12 81 8 24 17 81 6 8 24 17 81

Insertion Example Insert 6 into this heap N = 2 A[N/2] = A[1] = 5 5  6  percolating done Set A[N] = A[2] to 6 1 2 3 4 5 6 7 8 6 6 12 5 7 6 12 7 8 24 17 81 7 8 24 17 81

Exercise Continuing the example: insert 14 insert 7 insert 4 5 6 12 7 8 24 17 1 2 3 4 5 6 7 8 5 6 12 7 8 24 17 81 81

Removing from a Heap Front element leaves heap How shall we fill the empty space? NOT by pushing forward into the open space that might ruin our nice heap Instead we put the last element in front(!) Then elements start pushing forward starting with the smaller of the people in the second row of the heap

Deletion from Heap Remove the root Promote last item to root 4 4 Remove the root Promote last item to root Let it percolate down 5 12 7 6 24 17 81 14 8 7 1 2 3 4 5 6 7 8 9 10 11 4 5 12 7 6 24 17 81 14 8 7

Deletion from Heap Remove the root Promote last item to root 7 Remove the root Promote last item to root Let it percolate down choose smaller of two children 5 12 7 6 24 17 81 14 8 1 2 3 4 5 6 7 8 9 10 7 5 12 7 6 24 17 81 14 8 7

Deletion from Heap Remove the root Promote last item to root 5 Remove the root Promote last item to root Let it percolate down choose smaller of two children 7 12 7 6 24 17 81 14 8 1 2 3 4 5 6 7 8 9 10 5 7 12 7 6 24 17 81 14 8 7

Deletion from Heap Remove the root Promote last item to root 5 Remove the root Promote last item to root Let it percolate down choose smaller of two children (if it has two children) 6 12 7 7 24 17 81 14 8 1 2 3 4 5 6 7 8 9 10 5 6 12 7 7 24 17 81 14 8 7

Deletion from Heap Remove the root Promote last item to root 5 Remove the root Promote last item to root Let it percolate down choose smaller of two children (if it has two children) 6 12 7 7 24 17 81 14 8 1 2 3 4 5 6 7 8 9 10 5 6 12 7 7 24 17 81 14 8 7

Percolating Down Check number of children if none – then we are done percolating if one – it is the smallest child if two – choose whichever is smaller If smaller child is smaller than percolator move the child up continue percolating from the child’s position

NOTE: P is the position we are deleting from Percolating Down Let P be the last position in the heap Let N be 1 If 2N<P (* has surviving children *) If 2N+1=P (* one child *), let S be 2N Else let S be (A[2N]<A[2N+1]) ? 2N : 2N+1 If (A[P]>A[S]) let A[N] be A[S], N be S & go to step 3 Let A[N] be A[P] NOTE: P is the position we are deleting from

Deleting from the Heap Set temp to A[1] Percolate Down // from previous slide Reduce the size of the heap by 1 Return temp

Exercise Delete from this heap Delete again from this heap And again 5 6 12 7 17 24 24 81 14 18 1 2 3 4 5 6 7 8 9 10 5 6 12 7 17 24 24 81 14 18

Our Heap is Now a PQ remove method always gets smallest item and leaves next smallest item in its place add method inserts new item anywhere but moves it up to front if it’s smallest We can add convenience methods look at front, check if empty, make empty, …

Notes on the Implementation Code for heap is more complicated than code for an ordered list would be many more lines harder to understand But heap is faster than ordered list we will prove this later in the term

Questions