| ISSUE 117/456 · COMPUTER SCIENCE |
~4 MIN |
FOUNDATION / CS · PRIORITY QUEUES · READING DATE 2027-01-26 · JANUARY 2027 — MOTION AND COMPUTING FOUNDATIONS
How software chooses the next urgent job
START HERE
A reminder app may need the soonest alarm, not the alarm that was added first. A priority queue is a collection that returns the item with the most important ordering value next. Its rule is about which item comes out, so ordinary arrival order need not decide the result.
|
/ WHY NOW
If a newly added alarm is due earlier, it should become the next alarm immediately. Keeping that choice efficient is useful when many future items are waiting but only the next one matters now.
|
/ THE IDEA
A common implementation uses a binary heap: a branching arrangement with at most two entries directly below each entry, filling levels from left to right. An entry above those children is their parent. For a minimum-priority queue, every parent’s value is no larger than its children’s, putting the smallest value at the top entry, called the root. The entire collection is not fully sorted; siblings can be in either order while the parent rule still holds. Adding an item places it at the next open position and moves it upwards until the parent rule is restored. Removing the minimum replaces the root with the last item, then moves that replacement downwards as needed. Each operation repairs one route through the tree rather than sorting the whole collection again.
THE FORMAL IDEA
parent priority ≤ child priority; root = minimum
| priority = the chosen ordering value; in this example a smaller due-time number means sooner. | | parent and child = directly connected entries in adjacent levels of the heap; ≤ means less than or equal. A heap is an ordering data structure here, not the memory-allocation pool also called a heap. |
|
RUN THE TINY EXAMPLE
Change one thing. Predict what happens.
Toy pending alarms: Tea at time 8, Laundry at time 3, Call at time 5. The next item is Laundry, because 3 is the smallest due time. Change only Call’s due time from 5 to 2 and update the queue correctly. Call now comes first; after it is removed, Laundry at 3 becomes next, then Tea at 8.
|
Changing an entry’s number in place without repairing the structure can break the parent rule. A correct implementation must reposition the entry or use a defined replacement procedure. The queue cannot notice a changed field by magic while continuing to trust its old arrangement.
/ SO WHAT?
Ties require a separate policy. If two alarms share a due time, the queue can use an increasing sequence number to preserve their insertion order. That makes the comparison explicit: first compare due time, then sequence number. A priority number alone does not promise fairness between equally ranked jobs.
ONE CAVEAT |
| A priority queue does not guarantee that low-priority work will eventually run if more urgent work keeps arriving. Scheduling fairness needs extra policy, and changing or deleting arbitrary entries needs more bookkeeping than simply removing the root. |
KEEP THIS
Maintaining a partial ordering is enough to find the next priority item; changing an item’s priority requires restoring that ordering.
|
NEXT: When faster water really does mean lower pressure
|