Sub topic 13
Priority Queues: The Abstract Interface
Prerequisites and working model
Assume you know arrays, linked lists, Big-O notation, and basic classes and functions. A priority queue is an abstract data type (ADT). An ADT describes what operations mean, but not how the data is stored.
A normal queue removes items in first-in, first-out (FIFO) order. A priority queue removes the item with the highest priority first. Depending on the definition, “highest” may mean the largest key or the smallest key.
For a min-priority queue, the smallest key is removed first. For a max-priority queue, the largest key is removed first. The priority key may be separate from the item itself.
Core operations
A priority queue commonly supports these operations:
- insert(item, priority): add an item.
- peek(): return the next item without removing it.
- extract-min(): remove and return the smallest-priority item.
- extract-max(): remove and return the largest-priority item.
- is-empty(): report whether the structure has no items.
The names extract-min and extract-max identify the ordering rule. A question that says “the lowest cost is processed first” describes a min-priority queue.
When two items have equal priority, the ADT does not always define their order. If the implementation preserves insertion order for equal priorities, it is stable. Do not assume stability unless the question states it.
One simple implementation
An unsorted array can implement a min-priority queue. Insertion appends an entry. Peeking and extraction scan the entire array.
class UnsortedMinPriorityQueue:
def __init__(self):
self.items = []
def insert(self, item, priority):
self.items.append((priority, item))
def peek(self):
if not self.items:
raise IndexError("priority queue is empty")
return min(self.items)[1]
def extract_min(self):
if not self.items:
raise IndexError("priority queue is empty")
index = min(range(len(self.items)),
key=lambda i: self.items[i][0])
priority, item = self.items.pop(index)
return item
def is_empty(self):
return len(self.items) == 0
pq = UnsortedMinPriorityQueue()
pq.insert("compile", 4)
pq.insert("test", 1)
pq.insert("deploy", 3)
print(pq.extract_min()) # test| Implementation | Insert | Peek minimum | Extract minimum |
|---|---|---|---|
| Unsorted array | O(1) | O(n) | O(n) |
| Sorted array | O(n) | O(1) | O(1) |
| Binary heap | O(log n) | O(1) | O(log n) |
A binary heap is usually the preferred general-purpose implementation because it balances insertion and extraction. Its internal structure is covered separately.
Common errors and GATE patterns
Do not confuse a priority queue with a sorted queue. The interface requires access to the next-priority item, not necessarily a fully sorted sequence.
For complexity questions, first identify which operation is repeated. Repeatedly extracting from an unsorted array costs O(n) per extraction. A heap reduces this to O(log n) per extraction.
A priority queue is useful in scheduling, shortest-path algorithms, event simulation, and selecting the next smallest or largest candidate.
Exercise
In a min-priority queue, items have priorities 7, 2, and 5. In what order are they extracted?
Show answer
They are extracted in priority order 2, 5, then 7.