Priority Queue — concept (heap-based implementation covered later)
WHAT is a Priority Queue?
WHY the word "abstract"? Because we only promise a contract ("you always get the highest-priority item next"). How we keep things ordered — array, linked list, heap, BST — is a separate choice with different speeds.
WHY do we need it? (The 80/20 core)
Everyday algorithmic uses (the 20% that gives 80% of value):
- Dijkstra's shortest path — always expand the closest unvisited node.
- Prim's MST — always add the cheapest connecting edge.
- Huffman coding — repeatedly merge the two least-frequent symbols.
- A* search, event simulation, task schedulers, top-K problems.
HOW does it behave? (Contract, not code)
Imagine a min-priority queue (smaller number = more urgent).
| Operation | Meaning | What you get |
|---|---|---|
insert(A, 5) |
add A, priority 5 | — |
insert(B, 2) |
add B, priority 2 | — |
insert(C, 8) |
add C, priority 8 | — |
peek() |
look at top | B (priority 2) |
extract-min() |
remove top | B |
extract-min() |
remove next | A (priority 5) |
Notice C was inserted last but leaves after A and B, because priority — not arrival order — rules.

Naive implementations — DERIVE the costs from scratch
Before heaps, let's reason out the simple ways and why they're slow. This shows why heaps are worth learning later.
Option 1: Unsorted array/list
- insert: just append at the end. WHY fast? No ordering to maintain → .
- extract-min: you must scan all elements to find the smallest → .
Option 2: Sorted array/list (kept sorted by priority)
- insert: find the correct slot and shift elements → .
- extract-min: the smallest is already at one end → .
Derivation of the "why balance matters": Suppose you do inserts and extracts.
- Unsorted: .
- Sorted: .
- Heap: .
For : vs — about 50,000× faster. That's the payoff.
Worked Examples
Common Mistakes (Steel-man them)
Active Recall
Recall Q: What single property must a priority queue always guarantee?
That peek/extract always returns the element with the highest priority (min or max), regardless of insertion order.
Recall Q: Why is a heap preferred over sorted/unsorted lists?
Naive lists make one operation but the other , giving overall. A heap makes both insert and extract → total.
Recall Q: How do you get max-behaviour from a min-PQ?
Insert negated priorities (); negate again on extraction.
Recall Feynman: explain to a 12-year-old
Imagine a magic ticket line. Everyone holds a number that says how important they are. No matter when you joined the line, the machine always calls the most important person next. When someone with a scarier number walks in, they don't shove to the front by hand — the machine quietly figures out they should go next. That machine is a priority queue.
Connections
- Heap — the standard implementation (covered next).
- Binary Tree — a heap is a complete binary tree in disguise.
- Queue (FIFO) — contrast: order by arrival, not importance.
- Stack (LIFO) — the other basic ordering discipline.
- Dijkstra's Algorithm — biggest real-world consumer of a PQ.
- Huffman Coding — repeatedly extract two minimums.
- Abstract Data Type — PQ is an ADT, heap is a data structure.
What is a priority queue (ADT)?
Core operations of a PQ?
Does a PQ follow FIFO order?
extract-min cost in an UNSORTED list?
insert cost in a SORTED list?
Insert & extract cost in a binary heap?
Total cost of n inserts + n extracts with a heap vs naive list?
How to simulate a max-PQ using a min-PQ?
How to make ties break in FIFO order?
Why is a PQ called an ADT?
Two flagship algorithms using a PQ?
Concept Map
Hinglish (regional understanding)
Intuition Hinglish mein samjho
Socho ek hospital ka emergency room. Log kisi bhi order mein aayen, doctor sabse pehle usko dekhega jiska case sabse serious hai — heart attack wala finger-cut wale se pehle. Yahi Priority Queue ka core idea hai: har element ke saath ek priority number hota hai, aur jab bhi tum item nikaalte ho, sabse important (min ya max priority) wala nikalta hai — arrival order se koi matlab nahi. Isiliye ye normal FIFO queue se alag hai, jahan sirf "pehle aaya, pehle gaya" chalta hai.
PQ ek ADT hai, matlab wo sirf behaviour (contract) define karta hai — "top hamesha highest priority hoga" — banane ka tareeka alag choice hai. Simple tareeke se banao to do options: unsorted list (insert fast O(1), lekin extract-min slow O(n) kyunki poora scan karna padta hai) ya sorted list (extract fast O(1), lekin insert slow O(n) kyunki shift karna padta hai). Dono mein ek kaam sasta, doosra mehnga — total O(n²).
Yahi problem heap solve karta hai (jo baad mein aayega): insert aur extract dono O(log n), to total O(n log n). Bade n ke liye ye zameen-aasman ka farak hai — n = 10 lakh pe hazaaron guna tez. Isiliye Dijkstra, Prim, Huffman jaise algorithms PQ pe chalte hain.
Do handy tricks yaad rakho: max-PQ chahiye lekin sirf min-PQ hai? Priority ko negative (-p) daalo, nikaalte waqt wapas negate kar do. Aur agar equal priority pe FIFP fairness chahiye, to key ko (priority, sequence-number) bana do — tab purana insert tie jeet jaayega. Bas ye samajh lo: Priority In, Priority Out — queue jo favouritism karti hai!