A Priority Queue and Its Tie Rule

A priority queue selects an item by its ordering key. Its interface should specify what to expect when keys tie.

Source
Anbloq
Language
English
Created
Updated
Chapters
4
Reading time
1 min
In this article4 chapters
  1. Trace three tasks
  2. Choose item identity separately
  3. Choose a tie policy that survives reordering
  4. Clarify what happens when priority changes

Trace three tasks

If lower numbers have higher priority, tasks A at two and B at one leave B next. Adding task C at one creates a tie with B. FIFO among ties can be guaranteed by including an arrival counter. Ordered keys show how a compound key defines that rule.

Choose item identity separately

Two tasks can have the same priority without being the same task. Distinct stable IDs enable updates and cancellation where the structure supports them. See indexed priority queues for these extra operations.[1]

Choose a tie policy that survives reordering

Three tickets arrive as (3, A), (1, B) and (1, C). If lower numbers mean greater urgency and arrival order breaks ties, removal yields B, C, A. A sequence number in the comparison key encodes that policy even if ticket payloads are not directly comparable. See sorting with keys.

Key (numeric priority, insertion sequence)
A: (3, 0)
B: (1, 1)
C: (1, 2)
Removal order: B, C, A

Clarify what happens when priority changes

Changing the stored payload alone can break a heap's ordering if a key changes in place. Use an update operation that restores order, or insert a new version and skip obsolete entries when they reach the front. Test a priority change across a tie.

References

  1. Jeff Erickson, Algorithms ↩