Queues & Priority QueuesQueues & Priority Queues
Radix SortRadix SortHeap SortHeap Sort
OutlineOutline
QueuesQueues• queue operationsqueue operations• algorithms with queuesalgorithms with queues• radix sortradix sort
Priority QueuesPriority Queues• priority queue operationspriority queue operations• the “heap” data structurethe “heap” data structure• heap sortheap sort
Queue OperationsQueue Operations
Queues require these operationsQueues require these operations• add at back (add at back (enqueueenqueue, , putput, , insertinsert)) .add(item).add(item)• delete from front (delete from front (dequeuedequeue, , getget)) .remove().remove()
Typically have some othersTypically have some others• check if emptycheck if empty .isEmpty().isEmpty()• inspect front element (inspect front element (getFrontgetFront, , peekpeek)) .front().front()• empty it outempty it out .clear().clear()• get sizeget size .size().size()
A QueueA Queue
Arranged left to rightArranged left to right• front at the leftfront at the left
Old items are Old items are removedremoved or or dequeued dequeued at frontat front• myQueue.remove()myQueue.remove()
New items are New items are inserted inserted or or enqueued enqueued at backat back• myQueue.add(99)myQueue.add(99)
17 24 81 12 5 7 17 12 8
Queue
Front
Back
A QueueA Queue
Arranged left to rightArranged left to right• front at the leftfront at the left
Old items are Old items are removedremoved or or dequeued dequeued at frontat front• myQueue.remove()myQueue.remove()
New items are New items are inserted inserted or or enqueued enqueued at backat back• myQueue.add(99)myQueue.add(99)
24 81 12 5 7 17 12 8
Queue
Front
Back
A QueueA Queue
Arranged left to rightArranged left to right• front at the leftfront at the left
Old items are Old items are removedremoved or or dequeued dequeued at frontat front• myQueue.remove()myQueue.remove()
New items are New items are inserted inserted or or enqueued enqueued at backat back• myQueue.add(99)myQueue.add(99)
24 81 12 5 7 17 12 8 99
Queue
Front
Back
Real World QueuesReal World Queues
Any line-upAny line-up• bank, ticket window, amusement ridebank, ticket window, amusement ride• (except – people might give up & leave)(except – people might give up & leave)
Assembly lineAssembly line• chassis inserted at one endchassis inserted at one end• completed vehicle comes off the othercompleted vehicle comes off the other
FILO and LILOFILO and LILO
Stack operations all at one endStack operations all at one end Last element in the Last element in the stackstack is the first one out is the first one out
• Last In, First Out = LIFOLast In, First Out = LIFO• AKA FILO = First In, Last OutAKA FILO = First In, Last Out
Last element in a Last element in a queuequeue is last one out is last one out• Last In, Last Out = LILOLast In, Last Out = LILO• AKA FIFO = First In, First OutAKA FIFO = First In, First Out
ExerciseExercise
Draw the Draw the queuesqueues that result from the that result from the following operationsfollowing operations
Start with empty each timeStart with empty each time• add A, add B, add C, remove, removeadd A, add B, add C, remove, remove• add 1, add 2, remove, add 3, add 4add 1, add 2, remove, add 3, add 4
Exceptional CircumstancesExceptional Circumstances
Similar to stacks:Similar to stacks:• try to remove/get front of empty queuetry to remove/get front of empty queue• try to add to a full queuetry to add to a full queue• try to add a nulltry to add a null
Solutions similar as well:Solutions similar as well:• return null / throw an exceptionreturn null / throw an exception• return false / throw an exceptionreturn false / throw an exception• two versions of remove and fronttwo versions of remove and front
Using QueuesUsing Queues
File serverFile server Railway sidingsRailway sidings Wire routing (shortest path)Wire routing (shortest path) Image component labelingImage component labeling Radix SortRadix Sort
Wire Routing ProblemWire Routing Problem
Need to connect two Need to connect two locations on a devicelocations on a device• can’t go over existing can’t go over existing
componentscomponents• want shortest pathwant shortest path
00000065456001110654311001110543S1F0000011143450001111051110001110001110000000011110000000000000000011110000000011110000000000000000
Wire Routing ProblemWire Routing Problem
Represent device by 0-1 Represent device by 0-1 matrixmatrix• 0 = free space0 = free space• 1 = component1 = component
Need start & end pointsNeed start & end points• locations in matrixlocations in matrix• (2, 8) and (2, 10)(2, 8) and (2, 10)
000000000000000000000000011100000110011100000110011100000100011100000100000011100000000011100000001111001110001111001110001110001110001110001110000000011110000000011110000000000000000000000000000011110000000011110000000011110000000011110000000000000000000000000000
Wire Routing ProblemWire Routing Problem
Two Pass MethodTwo Pass Methodto RouteWire(matrix, start, end)to RouteWire(matrix, start, end)
// find distances from start// find distances from startLabelDistances(matrix, start, end)LabelDistances(matrix, start, end)
// find path from end back to start// find path from end back to startFindRouteBack(matrix, start, end)FindRouteBack(matrix, start, end)
Distance PassDistance Pass
add start cell, labeled 2add start cell, labeled 2• [(2, 8)][(2, 8)]
remove start cell (=2), add remove start cell (=2), add its neighbours, labeled 3its neighbours, labeled 3• [(1, 8), (3, 8), (2, 7)][(1, 8), (3, 8), (2, 7)]
remove next cell (=3), add remove next cell (=3), add itsits neighbours, labeled 4 neighbours, labeled 4• [(3, 8), (2, 7), (0, 8), (1, 7)][(3, 8), (2, 7), (0, 8), (1, 7)]
0000000000000000000000000011111100000000001111000011111100000000ss11ee00000000001111110000000000000011111111000011111100000011111100000011111100000000000000001111111100000000000000000000000000000000001111111100000000000000001111111100000000000000000000000000000000
00000000000000000000000000111111000000000011110000111111000000002211ee00000000001111110000000000000011111111000011111100000011111100000011111100000000000000001111111100000000000000000000000000000000001111111100000000000000001111111100000000000000000000000000000000
0123456789ab0123456789a
00000000000000000000000000111111000000003311110000111111000000332211ee00000000001111110033000000000011111111000011111100000011111100000011111100000000000000001111111100000000000000000000000000000000001111111100000000000000001111111100000000000000000000000000000000
00000000000000004400000000111111000000443311110000111111000000323211ee00000000001111110033000000000011111111000011111100000011111100000011111100000000000000001111111100000000000000000000000000000000001111111100000000000000001111111100000000000000000000000000000000
Distance PassDistance Pass
Repeat until you reach goalRepeat until you reach goal• or until queue is emptyor until queue is empty
Remove (3, 10), labeled 5, Remove (3, 10), labeled 5, label its neighbours 6label its neighbours 6• [(4, 7), (2, 5), (0, 10), …][(4, 7), (2, 5), (0, 10), …]• add (2, 10) = endadd (2, 10) = end• can stop nowcan stop now
000000000000654566545600001111110065436543111100001111110054325432116600000000001111114344345500000011111111005511111100000011111100000011111100000000000000001111111100000000000000000000000000000000001111111100000000000000001111111100000000000000000000000000000000
0123456789ab0123456789a
Label Distances CodeLabel Distances Code
to LabelDistances(matrix, start, end)to LabelDistances(matrix, start, end)matrix[start] matrix[start] 2; 2;q.add(start);q.add(start);while (!q.empty())while (!q.empty())
if (q.front() = end)if (q.front() = end) break;break;LabelNeighbours(q.top(), matrix);LabelNeighbours(q.top(), matrix);q.remove();q.remove();
if (q.empty())if (q.empty())throw exception(“No path found”);throw exception(“No path found”);
Doesn’t stop as early as it could
Path FindingPath Finding
Start at goalStart at goal Pick a neighbour labeled Pick a neighbour labeled
one less than youone less than you Repeat until reach startRepeat until reach start
• in this example a single path in this example a single path
000000000000654566545600001111110065436543111100001111110054325432116600000000001111114345434500000011111111005511111100000011111100000011111100000000000000001111111100000000000000000000000000000000001111111100000000000000001111111100000000000000000000000000000000
Finding Path BackFinding Path Back
to FindRouteBack(matrix, start, end)to FindRouteBack(matrix, start, end)current current end; end;while (current != start)while (current != start)
label label matrix[current]; matrix[current];matrix[current] matrix[current] 1; 1; // now used// now usedcurrent current FindNeighbour(matrix, current, FindNeighbour(matrix, current,
label–1);label–1);matrix[start] matrix[start] 1; 1; // start used, too// start used, too
ExercisesExercises
Show how the queue develops for the rest Show how the queue develops for the rest of the example aboveof the example above• assume always look north, east, south, westassume always look north, east, south, west• assume stop as soon as the end cell labeledassume stop as soon as the end cell labeled
To think aboutTo think about• how could we rewrite the code above to stop as how could we rewrite the code above to stop as
soon as the end cell gets labeled?soon as the end cell gets labeled?
Radix SortRadix Sort
An array of numbers to sortAn array of numbers to sort• [75, 73, 72, 64, 115, 31, 232, 105][75, 73, 72, 64, 115, 31, 232, 105]
A List of 10 queuesA List of 10 queuesList<Queue<Integer>> radix = …;List<Queue<Integer>> radix = …;
0 1 2 3 4 5 6 7 8 9
Radix SortRadix Sort
Use 1s digit to choose queue to put them inUse 1s digit to choose queue to put them in• [7[755, 7, 733, 7, 722, 6, 644, 11, 1155, 3, 311, 23, 2322, 10, 1055]]
A List of 10 queuesA List of 10 queuesList<Queue<Integer>> radix = …;List<Queue<Integer>> radix = …;
3123272 73 64
10511575
0 1 2 3 4 5 6 7 8 9
Radix SortRadix Sort
Recombine in order from 0 to 9Recombine in order from 0 to 9• [3[311, 7, 722, 23, 2322, 7, 733, 6, 644, 7, 755, 11, 1155, 10, 1055]]
A List of 10 queuesA List of 10 queuesList<Queue<Integer>> radix = …;List<Queue<Integer>> radix = …;
3123272 73 64
10511575
0 1 2 3 4 5 6 7 8 9
Radix SortRadix Sort
Repeat using 10s digitRepeat using 10s digit• [[331, 1, 772, 22, 2332, 2, 773, 3, 664, 4, 775, 15, 1115, 15, 1005]5]
A List of 10 queuesA List of 10 queuesList<Queue<Integer>> radix = …;List<Queue<Integer>> radix = …;
105 115 23231 64
757372
0 1 2 3 4 5 6 7 8 9
Radix SortRadix Sort
Recombine in order from 0 to 9Recombine in order from 0 to 9• [1[1005, 15, 1115, 5, 331, 21, 2332, 2, 664, 4, 772, 2, 773, 3, 775]5]
A List of 10 queuesA List of 10 queuesList<Queue<Integer>> radix = …;List<Queue<Integer>> radix = …;
0 1 2 3 4 5 6 7 8 9
105 115 23231 64
757372
Radix SortRadix Sort
Repeat using 100s digitRepeat using 100s digit• [[1105, 05, 1115, 31, 15, 31, 2232, 64, 72, 73, 75]32, 64, 72, 73, 75]
A List of 10 queuesA List of 10 queuesList<Queue<Integer>> radix = …;List<Queue<Integer>> radix = …;
7573726431
115105 232
0 1 2 3 4 5 6 7 8 9
Radix SortRadix Sort
Recombine in order from 0 to 9Recombine in order from 0 to 9• [31, 64, 72, 73, 75, 105, 115, 232][31, 64, 72, 73, 75, 105, 115, 232]
A List of 10 queuesA List of 10 queuesList<Queue<Integer>> radix = …;List<Queue<Integer>> radix = …;
7573726431
115105 232
0 1 2 3 4 5 6 7 8 9
Radix SortRadix Sort
Quit when no number has the digit you’re Quit when no number has the digit you’re looking forlooking for• find maximum & go until it has been passedfind maximum & go until it has been passedfor (r = 1; max <= r; r *= 10) {for (r = 1; max <= r; r *= 10) {
splitList(arr, radix, r);splitList(arr, radix, r); // arr // arr radix radixrecombine(arr, radix);recombine(arr, radix); // radix // radix arr arr
}}
Complexity of Radix SortComplexity of Radix Sort
Number of operations:Number of operations:• split queue: linear in list lengthsplit queue: linear in list length• recombine queues: linear in list lengthrecombine queues: linear in list length• how many times do we split/recombine?how many times do we split/recombine?
» doesn’t depend on length of the doesn’t depend on length of the listlist
Linear in length of listLinear in length of list• looks better than quadratic or N looks better than quadratic or N loglog N N
» but not necessarily better….but not necessarily better….
Problems for Radix SortProblems for Radix Sort
Depends on “length” of biggest itemDepends on “length” of biggest item• 6-digit numbers take twice as long to sort as 3-6-digit numbers take twice as long to sort as 3-
digit numbersdigit numbers• 80-character strings take ten times as long to 80-character strings take ten times as long to
sort as 8-character stringssort as 8-character strings» and need 94 or more queuesand need 94 or more queues
Doesn’t work on numbers with decimalsDoesn’t work on numbers with decimals» unless write them into long Strings and then read unless write them into long Strings and then read
them back again afterwardsthem back again afterwards
Priority Queue OperationsPriority Queue Operations
Priority queues must have these operations:Priority queues must have these operations:• add (add (pushpush, , insertinsert)) .add(item).add(item)• remove highest priority item (remove highest priority item (poppop)) .remove().remove()
Typically have more operations….Typically have more operations….• check if emptycheck if empty .isEmpty().isEmpty()• look at highest priority itemlook at highest priority item .peek().peek()• get sizeget size .size().size()• make emptymake empty .clear().clear()
Priority QueuesPriority Queues
Priority queue is a queue where the “most Priority queue is a queue where the “most important” items get removed firstimportant” items get removed first• ““most important” = “highest priority”most important” = “highest priority”
Most important is usually the smallestMost important is usually the smallest• priority #1priority #1• priority #2priority #2• ……• priority # 1,000,004priority # 1,000,004
Priority QueuesPriority Queues
The The smallestsmallest element is removed element is removed
• pq.remove();pq.remove();
• pq.remove();pq.remove();
• pq.remove();pq.remove();
5 7 8 12 17 12 17 24 81
7 12 8 24 17 12 17 81
8 12 12 24 17 81 17
12 17 12 24 17 81
Priority QueuesPriority Queues
Additions made anywhere convenientAdditions made anywhere convenient
• pq.add(75); pq.add(75);
New items will move up as required!New items will move up as required!• pq.add(10)pq.add(10)
12 17 12 24 17 81
12 17 12 24 17 81 75
10 12 12 17 17 81 75 24
Priority QueuesPriority Queues
Each element in the queue has a Each element in the queue has a prioritypriority• in our examples that’s in our examples that’s all all they havethey have• in some app.s may have other informationin some app.s may have other information
Ties can be broken in any manner at all (!)Ties can be broken in any manner at all (!)• may be FIFO, but not necessarilymay be FIFO, but not necessarily
CanCan think of it as a sorted list think of it as a sorted list• but that’s not actually what we’re going to do!but that’s not actually what we’re going to do!
Real World Priority QueuesReal World Priority Queues
Printer queuePrinter queue Time-sharing job queueTime-sharing job queue Airport baggage check with separate Airport baggage check with separate
“Executive Class” check-in“Executive Class” check-in
PQ ApplicationsPQ Applications
Asynchronous event simulationAsynchronous event simulation Sort a listSort a list
Asynchronous EventsAsynchronous Events
Simulation of timed eventsSimulation of timed events Event recordEvent record
• event type + time it occursevent type + time it occurs Keep events in a PQKeep events in a PQ
• get next event from the PQ (minimum element)get next event from the PQ (minimum element)• update the clock with the timeupdate the clock with the time• generate any new events & insert into the PQgenerate any new events & insert into the PQ
Bank SimulationBank Simulation
Customers arrive at randomCustomers arrive at random• served in the order they arriveserved in the order they arrive
» (they queue!)(they queue!)
Multiple tellers availableMultiple tellers available• serve customers as they become availableserve customers as they become available• some customers take more time than otherssome customers take more time than others
Simulate with randomnessSimulate with randomness• random arrival times, random service timesrandom arrival times, random service times
Bank Simulation EventsBank Simulation Events
Customers arrive at randomCustomers arrive at random• serve in order of arrivalserve in order of arrival• event arrival: record time of arrivalevent arrival: record time of arrival
Customers served by first available tellerCustomers served by first available teller• service takes random amount of timeservice takes random amount of time• event departure: record time service will endevent departure: record time service will end
Handle events in the order they happenHandle events in the order they happen• priority queue sorted on time of eventpriority queue sorted on time of event
Bank Simulation ParametersBank Simulation Parameters
New arrival every 1 to 10 minutesNew arrival every 1 to 10 minutes• for an hourfor an hour• can schedule these at start of simulationcan schedule these at start of simulation
Each service takes 1 to 10 minutesEach service takes 1 to 10 minutes• can’t schedule until can’t schedule until afterafter they go to the teller! they go to the teller!• events need to be added events need to be added duringduring the simulation the simulation
Calculate average & maximum wait timeCalculate average & maximum wait time
Arrival ScheduleArrival Schedule
eventPQ
arrival(4), arrival(7), arrival(8), arrival(17), …
lineUpQ
time
0
numTellersFree
2
customers
0
totalWaitingTime
0
maxWaitingTime
0
arrival(4), arrival(7), arrival(8), arrival(17), …
First Event: Arrival @ time = 4First Event: Arrival @ time = 4
eventPQ
arrival(7), arrival(8), arrival(17), arrival(18), …
lineUpQ
time
4
numTellersFree
2
customers
0
totalWaitingTime
0
maxWaitingTime
0
arrival(7), arrival(8), departure(10), arrival(17), …
1
1
arrival(7), arrival(8), departure(10), arrival(17), …
Next Event: Arrival @ 7Next Event: Arrival @ 7
eventPQ
arrival(8), departure(10), arrival(17), arrival(18), …
lineUpQ
time
7
numTellersFree
1
customers
1
totalWaitingTime
0
maxWaitingTime
0
arrival(8), departure(10), departure(11), …
0
2
arrival(8), departure(10), departure(11), …
Next Event: Arrival @ 8Next Event: Arrival @ 8
eventPQ
departure(10), departure(11), arrival(17), …
lineUpQ
time
8
numTellersFree
0
customers
2
totalWaitingTime
0
maxWaitingTime
0
arrival(8)
departure(10), departure(11), arrival(17), …
Next Event: Departure @ 10Next Event: Departure @ 10
eventPQ
departure(11), arrival(17), arrival(18), …
lineUpQ
arrival(8)
time
10
numTellersFree
0
customers
2
totalWaitingTime
0
maxWaitingTime
0
departure(11), arrival(17), departure(17), …
0
3 2 2
General Event SimulationGeneral Event Simulation
P.Q. of eventsP.Q. of events• some events added at startsome events added at start• some events added as go alongsome events added as go along• loop until P.Q. is emptyloop until P.Q. is emptyaddStartingEvents(pq);addStartingEvents(pq);while (!pq.isEmpty()) {while (!pq.isEmpty()) { Event e = pq.remove();Event e = pq.remove(); processEvent(e, pq);processEvent(e, pq);}}
Sorting with a P.Q.Sorting with a P.Q.
Add all items from array into P.Q.Add all items from array into P.Q.for (int i = 0; i < a.length; ++i) {for (int i = 0; i < a.length; ++i) { pq.add(a[i]);pq.add(a[i]);}}
Remove them in order back into the arrayRemove them in order back into the arrayfor (int i = 0; i < a.length; ++i) {for (int i = 0; i < a.length; ++i) { a[i] = pq.remove();a[i] = pq.remove();}}
That’s That’s almostalmost heap sort heap sort
QuestionsQuestions
Next time:Next time:• reviewreview