Download presentation
Presentation is loading. Please wait.
1
Priority Queues Chapters 10 & 26
2
Outline Review of Stacks and Queues Priority Queues Visualization at
priority queue operations the “heap” data structure Visualization at /Heap.html
3
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
4
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
5
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
6
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
7
Removing from Priority Queues
Elements come out smallest to largest remove() 3 remove() 4 remove() 11 remove() 13 remove() 14 remove() 16 14 16 16
8
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>>
9
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
10
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)
11
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
12
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
13
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
14
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
15
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
16
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
17
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
18
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
19
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
20
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
21
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); }
22
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
23
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>
24
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()); }
25
Implementing a PQ Seems like an ordered list would be good: adding
removing 16 4 16 14 16 16
26
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
27
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
28
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
29
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
30
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
31
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
32
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
33
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
34
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
35
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 5 7 12 81 8 24 17 Note: array index starts at 1
36
Exercise Store the following heap into an array 1 2 5 8 3 19 12 15 14
20
37
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?
38
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
39
Insertion Example Insert 6 into this heap N = 8 5 1 2 3 4 5 6 7 7 12 5
7 12 5 7 12 81 8 24 17 81 8 24 17
40
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 7 12 5 7 12 81 8 24 17 ? 81 8 24 17 6
41
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 7 12 5 7 12 81 8 24 17 81 6 8 24 17 81
42
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 6 6 12 5 7 6 12 7 8 24 17 81 7 8 24 17 81
43
Exercise Continuing the example: insert 14 insert 7 insert 4 5 6 12 7
8 24 17 5 6 12 7 8 24 17 81 81
44
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
45
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 4 5 12 7 6 24 17 81 14 8 7
46
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 7 5 12 7 6 24 17 81 14 8 7
47
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 5 7 12 7 6 24 17 81 14 8 7
48
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 5 6 12 7 7 24 17 81 14 8 7
49
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 5 6 12 7 7 24 17 81 14 8 7
50
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
51
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
52
Deleting from the Heap Set temp to A[1]
Percolate Down // from previous slide Reduce the size of the heap by 1 Return temp
53
Exercise Delete from this heap Delete again from this heap And again 5
6 12 7 17 24 24 81 14 18 5 6 12 7 17 24 24 81 14 18
54
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, …
55
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
56
Questions
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.