5.5Priority Queue
What is a priority queue?
In an ordinary queue the order is the arrival order: first come, first out. In a priority queue every element has a priority, and each time the highest-priority element leaves, no matter when it arrived. This lesson covers what a priority queue is, which operations it offers and the ways it is built.
- How a priority queue differs from an ordinary queue, and min versus max priority queues
- In which order equal-priority elements leave, and the four basic operations
- Where priority queues are used and which structures they are built with
1Priority, not arrival order
Think of a hospital emergency room. Patients are not seen in the order they registered but by how serious their condition is. This sorting is called triage. The patient with a sprained wrist may have come first, but the heart attack patient who arrived later goes in first. The emergency waiting room is a priority queue.
An ordinary queue follows the FIFO rule. In a priority queue the order is set by the priority each element carries. Every element in the queue carries a value and a priority. To keep things simple, in this section's code the two are the same number: the element is an integer and its value is its priority.
2Small number or large number?
The direction of priority is a choice. In a min priority queue a smaller number has higher priority: the smallest value leaves first. In task lists "priority 1" means the most urgent job; that is a min priority queue. In a max priority queue a larger number has higher priority: the largest value leaves first. An auction that looks at the highest bid first is an example.
There is no structural difference between the two; only the comparison flips. All the code in this section is a min priority queue: a smaller value has higher priority.
3What about equal priorities?
When two elements carry the same priority, which one leaves first is not part of the definition. The code in this section keeps the arrival order among equals: with the same priority, the one that arrived first leaves first. A new element goes behind the elements with an equal priority.
Not every implementation keeps this rule by itself. Priority queues built on a heap do not keep equals in order. Where the order matters, every element gets an arrival number, and when priorities are equal that number is compared.
4Operations
A priority queue is an abstract data type too: it is defined by the operations it offers, not by how it is built.
| Operation | What it does | What it returns |
|---|---|---|
| insert(x) | Puts x into the queue by its priority | Nothing (in C, success: true or false) |
| removeMin() | Removes the highest-priority element | The removed element |
| peek() | Looks at the highest-priority element without removing it | The highest-priority element |
| isEmpty() | Is the queue empty? | true or false |
The enqueue and dequeue of an ordinary queue are called insert and removeMin here, because they no longer work by the ends but by priority. peek keeps its name.
5Let's trace a sequence of operations
Let's run these operations on an empty min priority queue. In the queue column the elements are listed in leaving order; the leftmost leaves first. To tell the two 4s apart, the one that came first is written 4a and the later one 4b.
| Operation | Queue (leaving order) | Returned value |
|---|---|---|
| insert(5) | 5 | |
| insert(4a) | 4a 5 | |
| insert(8) | 4a 5 8 | |
| insert(4b) | 4a 4b 5 8 | |
| removeMin() | 4b 5 8 | 4a |
| peek() | 4b 5 8 | 4b |
| insert(1) | 1 4b 5 8 | |
| removeMin() | 4b 5 8 | 1 |
1 came last but went straight to the front. 4b could not get ahead of 4a: their priorities are equal and 4a came first.
6Where is it used?
- Emergency rooms and call centres: the most urgent case is handled first.
- Operating systems: the scheduler gives the processor to the highest-priority process that is ready to run.
- Network devices: voice call packets go ahead of file download packets, so the conversation does not break up.
- Shortest paths: Dijkstra's algorithm takes the node closest to the start from a priority queue at every step. The algorithm comes later, in the graph lessons.
- Event simulation: future events wait in the queue by time; at each step the earliest event is handled.
7How is a priority queue built?
The first idea is to add elements to an unsorted array. Insertion takes O(1), but every removal has to look at the whole array to find the smallest value: O(n). This section shows two sorted implementations in detail.
With a sorted array: the array is kept sorted from large to small and the highest-priority element is at the end. Removal is O(1); insertion shifts to open a slot and takes O(n). Details in the inserting with an array and removing with an array lessons.
With a sorted linked list: the list is sorted from small to large and the highest-priority element is the node head points to. Removal is O(1); insertion walks to find the place and takes O(n). Details in the inserting with a linked list and removing with a linked list lessons.
With a heap: this is the most common way once the number of elements grows. Both insertion and removal take O(log n). The four ways are compared in the Which structure? lesson.
8What this section covers
- With an array: inserting by priority and removing the highest priority.
- With a linked list: inserting by priority and removing the highest priority.
- Comparison: unsorted array, sorted structures and heap.
9Try it
6, 3 and 9 are inserted into an empty min priority queue in that order. Then removeMin() is called twice. Which values leave?
The answer is B. A min priority queue always removes the smallest value. First 3 leaves, then 6, the smaller of the remaining 6 and 9. The arrival order does not decide.
Show the answer
The answer is B. A min priority queue always removes the smallest value. First 3 leaves, then 6, the smaller of the remaining 6 and 9. The arrival order does not decide.
In this section's code, which of two equal-priority elements leaves first?
The answer is A. A new element goes behind the elements with an equal priority. So the arrival order, FIFO, is kept among equals.
Show the answer
The answer is A. A new element goes behind the elements with an equal priority. So the arrival order, FIFO, is kept among equals.
Lessons you mark show up in the curriculum. If you are signed in, they are saved to your account too.