Download presentation
Presentation is loading. Please wait.
Published byBrook Berry Modified over 9 years ago
1
Queues & Priority Queues Radix Sort Heap Sort
2
Outline n Queues queue operationsqueue operations algorithms with queuesalgorithms with queues radix sortradix sort n Priority Queues priority queue operationspriority queue operations the “heap” data structurethe “heap” data structure heap sortheap sort
3
Queue Operations n Queues require these operations add at back (enqueue, put, insert).add(item)add at back (enqueue, put, insert).add(item) delete from front (dequeue, get).remove()delete from front (dequeue, get).remove() n Typically have some others check if empty.isEmpty()check if empty.isEmpty() inspect front element (getFront, peek).front()inspect front element (getFront, peek).front() empty it out.clear()empty it out.clear() get size.size()get size.size()
4
A Queue n Arranged left to right front at the leftfront at the left n Old items are removed or dequeued at front myQueue.remove()myQueue.remove() n New items are inserted or enqueued at back myQueue.add(99)myQueue.add(99) 17 24 81 12 5 7 17 12 8 Queue Front Back
5
A Queue n Arranged left to right front at the leftfront at the left n Old items are removed or dequeued at front myQueue.remove()myQueue.remove() n New items are inserted or enqueued at back myQueue.add(99)myQueue.add(99) 24 81 12 5 7 17 12 8 Queue Front Back
6
A Queue n Arranged left to right front at the leftfront at the left n Old items are removed or dequeued at front myQueue.remove()myQueue.remove() n New items are inserted or enqueued at back myQueue.add(99)myQueue.add(99) 24 81 12 5 7 17 12 8 99 Queue Front Back
7
Real World Queues n Any line-up bank, ticket window, amusement ridebank, ticket window, amusement ride (except – people might give up & leave)(except – people might give up & leave) n Assembly line chassis inserted at one endchassis inserted at one end completed vehicle comes off the othercompleted vehicle comes off the other
8
FILO and LILO n Stack operations all at one end n Last element in the stack 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 n Last element in a queue is last one out Last In, Last Out = LILOLast In, Last Out = LILO AKA FIFO = First In, First OutAKA FIFO = First In, First Out
9
Exercise n Draw the queues that result from the following operations n Start 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
10
Exceptional Circumstances n 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 n 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
11
Using Queues n File server n Railway sidings n Wire routing (shortest path) n Image component labeling n Radix Sort
12
Wire Routing Problem n Need to connect two locations on a device can’t go over existing componentscan’t go over existing components want shortest pathwant shortest path 000000654560 011106543110 01110543S1F0 000011143450 001111051110 001110001110 000000011110 000000000000 000011110000 000000000000
13
Wire Routing Problem n Represent device by 0-1 matrix 0 = free space0 = free space 1 = component1 = component n Need start & end points locations in matrixlocations in matrix (2, 8) and (2, 10)(2, 8) and (2, 10) 000000000000011100000110011100000100000011100000001111001110001110001110000000011110000000000000000011110000000011110000000000000000
14
Wire Routing Problem n Two Pass Method to RouteWire(matrix, start, end) // find distances from start LabelDistances(matrix, start, end) // find path from end back to start FindRouteBack(matrix, start, end)
15
Distance Pass n add start cell, labeled 2 [(2, 8)][(2, 8)] n remove start cell (=2), add its neighbours, labeled 3 [(1, 8), (3, 8), (2, 7)][(1, 8), (3, 8), (2, 7)] n remove next cell (=3), add its neighbours, labeled 4 [(3, 8), (2, 7), (0, 8), (1, 7)][(3, 8), (2, 7), (0, 8), (1, 7)]000000000000 011100000110 01110000s1e0 000011100000 001111001110 001110001110 000000011110 000000000000 000011110000 000000000000000000000000 011100000110 0111000021e0 000011100000 001111001110 001110001110 000000011110 000000000000 000011110000 000000000000 0123456789ab 0123456789a0123456789a000000000000 011100003110 0111000321e0 000011103000 001111001110 001110001110 000000011110 000000000000 000011110000 000000000000 000000004000 011100043110 0111000321e0 000011103000 001111001110 001110001110 000000011110 000000000000 000011110000 000000000000
16
Distance Pass n Repeat until you reach goal or until queue is emptyor until queue is empty n Remove (3, 10), labeled 5, label 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 000000654560 011106543110 011105432160 000011143450 001111051110 001110001110 000000011110 000000000000 000011110000 000000000000 0123456789ab 0123456789a0123456789a
17
Label Distances Code to LabelDistances(matrix, start, end) matrix[start] 2; q.add(start); while (!q.empty()) if (q.front() = end)break; LabelNeighbours(q.top(), matrix); q.remove(); if (q.empty()) throw exception(“No path found”); Doesn’t stop as early as it could
18
Path Finding n Start at goal n Pick a neighbour labeled one less than you n Repeat until reach start in this example a single pathin this example a single path 000000654560 011106543110 011105432160 000011143450 001111051110 001110001110 000000011110 000000000000 000011110000 000000000000
19
Finding Path Back to FindRouteBack(matrix, start, end) current end; while (current != start) label matrix[current]; matrix[current] 1;// now used current FindNeighbour(matrix, current, label–1); matrix[start] 1;// start used, too
20
Exercises n Show how the queue develops for the rest of 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 n To think about how could we rewrite the code above to stop as soon as the end cell gets labeled?how could we rewrite the code above to stop as soon as the end cell gets labeled?
21
Radix Sort n An array of numbers to sort [75, 73, 72, 64, 115, 31, 232, 105][75, 73, 72, 64, 115, 31, 232, 105] n A List of 10 queues List > radix = …; 0123456789
22
Radix Sort n Use 1s digit to choose queue to put them in [75, 73, 72, 64, 115, 31, 232, 105][75, 73, 72, 64, 115, 31, 232, 105] n A List of 10 queues List > radix = …; 3131 232 7273736464 105 115 75 0123456789
23
Radix Sort n Recombine in order from 0 to 9 [31, 72, 232, 73, 64, 75, 115, 105][31, 72, 232, 73, 64, 75, 115, 105] n A List of 10 queues List > radix = …; 31 232 727364 105 115 75 0123456789
24
Radix Sort n Repeat using 10s digit [31, 72, 232, 73, 64, 75, 115, 105][31, 72, 232, 73, 64, 75, 115, 105] n A List of 10 queues List > radix = …; 105115 232 31 64 75 73 72 0123456789
25
Radix Sort n Recombine in order from 0 to 9 [105, 115, 31, 232, 64, 72, 73, 75][105, 115, 31, 232, 64, 72, 73, 75] n A List of 10 queues List > radix = …; 0123456789 105115 232 31 64 75 73 72
26
Radix Sort n Repeat using 100s digit [105, 115, 31, 232, 64, 72, 73, 75][105, 115, 31, 232, 64, 72, 73, 75] n A List of 10 queues List > radix = …; 75 73 72 64 31 115 105232 0123456789
27
Radix Sort n Recombine in order from 0 to 9 [31, 64, 72, 73, 75, 105, 115, 232][31, 64, 72, 73, 75, 105, 115, 232] n A List of 10 queues List > radix = …; 75 73 72 64 31 115 105232 0123456789
28
Radix Sort n Quit when no number has the digit you’re looking for find maximum & go until it has been passedfind maximum & go until it has been passed for (r = 1; max <= r; r *= 10) { splitList(arr, radix, r);// arr radix recombine(arr, radix);// radix arr }
29
Complexity of Radix Sort n 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 list n Linear in length of list looks better than quadratic or N log Nlooks better than quadratic or N log N »but not necessarily better….
30
Problems for Radix Sort n Depends on “length” of biggest item 6-digit numbers take twice as long to sort as 3- digit numbers6-digit numbers take twice as long to sort as 3- digit numbers 80-character strings take ten times as long to sort as 8-character strings80-character strings take ten times as long to sort as 8-character strings »and need 94 or more queues n Doesn’t work on numbers with decimals »unless write them into long Strings and then read them back again afterwards
31
Priority Queue Operations n Priority queues must have these operations: add (push, insert).add(item)add (push, insert).add(item) remove highest priority item (pop).remove()remove highest priority item (pop).remove() n Typically have more operations…. check if empty.isEmpty()check if empty.isEmpty() look at highest priority item.peek()look at highest priority item.peek() get size.size()get size.size() make empty.clear()make empty.clear()
32
Priority Queues n Priority queue is a queue where the “most important” items get removed first “most important” = “highest priority”“most important” = “highest priority” n Most important is usually the smallest priority #1priority #1 priority #2priority #2 … priority # 1,000,004priority # 1,000,004
33
Priority Queues n The smallest element is removed 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
34
Priority Queues n Additions made anywhere convenient pq.add(75);pq.add(75); n 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
35
Priority Queues n Each element in the queue has a priority in our examples that’s all they havein our examples that’s all they have in some app.s may have other informationin some app.s may have other information n Ties can be broken in any manner at all (!) may be FIFO, but not necessarilymay be FIFO, but not necessarily n Can 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!
36
Real World Priority Queues n Printer queue n Time-sharing job queue n Airport baggage check with separate “Executive Class” check-in
37
PQ Applications n Asynchronous event simulation n Sort a list
38
Asynchronous Events n Simulation of timed events n Event record event type + time it occursevent type + time it occurs n Keep 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
39
Bank Simulation n Customers arrive at random served in the order they arriveserved in the order they arrive »(they queue!) n Multiple 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 n Simulate with randomness random arrival times, random service timesrandom arrival times, random service times
40
Bank Simulation Events n Customers arrive at random serve in order of arrivalserve in order of arrival event arrival: record time of arrivalevent arrival: record time of arrival n Customers 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 n Handle events in the order they happen priority queue sorted on time of eventpriority queue sorted on time of event
41
Bank Simulation Parameters n New arrival every 1 to 10 minutes for an hourfor an hour can schedule these at start of simulationcan schedule these at start of simulation n Each service takes 1 to 10 minutes can’t schedule until after they go to the teller!can’t schedule until after they go to the teller! events need to be added during the simulationevents need to be added during the simulation n Calculate average & maximum wait time
42
Arrival Schedule eventPQ arrival(4), arrival(7), arrival(8), arrival(17), … lineUpQ time 0 numTellersFree 2 customers 0 totalWaitingTime 0 maxWaitingTime 0
43
arrival(4), arrival(7), arrival(8), arrival(17), … First 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
44
Next 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
45
Next Event: Arrival @ 8 eventPQ departure(10), departure(11), arrival(17), … lineUpQ time 8 numTellersFree 0 customers 2 totalWaitingTime 0 maxWaitingTime 0 arrival(8)
46
departure(10), departure(11), arrival(17), … Next 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 322
47
General Event Simulation n P.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); while (!pq.isEmpty()) { Event e = pq.remove(); Event e = pq.remove(); processEvent(e, pq); processEvent(e, pq);}
48
Sorting with a P.Q. n Add all items from array into P.Q. for (int i = 0; i < a.length; ++i) { pq.add(a[i]); pq.add(a[i]);} n Remove them in order back into the array for (int i = 0; i < a.length; ++i) { a[i] = pq.remove(); a[i] = pq.remove();} n That’s almost heap sort
49
Questions n Next time: reviewreview
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.