A priority queue maintains a collection of elements, each with a priority. The defining operations: insert an element, and extract the highest-priority element.
The most common implementation is the heap. Heaps power Dijkstra's algorithm, event-driven simulation, scheduling, and many ranking systems.
Standard operations:
Optional:
The standard implementation. Complete binary tree with the heap property:
Stored in an array. Parent at i, children at 2i+1 and 2i+2.
No pointers. Cache-friendly. Classic.
Default priority queue. Simple, fast, cache-friendly.
Java's PriorityQueue, Python's heapq, C++'s std::priority_queue are binary heaps.
Like binary heap but each node has d children.
Tradeoff:
For Dijkstra, d = 4 or 8 is sometimes optimal.
Collection of binomial trees. Supports merge in O(log n).
Useful when frequent merging is needed.
Theoretical workhorse. Supports decrease-key in amortized O(1).
Fibonacci heaps make Dijkstra theoretically O(m + n log n) instead of O((m + n) log n).
In practice: high constant factors. Binary heaps usually win for typical inputs.
Simpler than Fibonacci; competitive in practice.
Often the fastest in practice for graph algorithms.
Maps elements to their position in the heap. Enables decrease-key.
Used in Dijkstra implementations.
Fixed-size; eviction on overflow.
Used for top-K queries.
Allows incorrect results; faster bounds. Niche use.
Find shortest paths from source. Priority queue holds frontier.
With binary heap: O((m + n) log n). With Fibonacci heap: O(m + n log n).
Similar structure to Dijkstra.
Sort by inserting all elements then extracting all.
O(n log n) worst case. In-place. Not stable.
Less common than introsort or quicksort in practice.
Events have scheduled times. Priority queue extracts next event.
Discrete-event simulation, networking simulators, game engines.
Tasks have priorities. Scheduler extracts highest priority.
OS process schedulers, thread pools, job queues.
Find K largest/smallest. Use heap of size K.
For K=1: just track max. For K small: heap is efficient.
For very large K, sort might be better.
Two heaps: max-heap for lower half, min-heap for upper half. Median is at the boundary.
Like Dijkstra but with heuristic. Priority = cost so far + heuristic.
Build optimal prefix code by repeatedly merging two least-frequent nodes.
Min-heap and max-heap have identical operations on negated priorities.
Some libraries support both; some only one.
Equal-priority elements: which extracts first?
Not stable by default. Add insertion-order tiebreaker if needed.
If priorities change, you need decrease-key. Not all heaps support efficiently.
Concurrent heaps are tricky. Lock-free implementations exist but complex.
Subtle bug; results look almost right.
Equal-priority elements come out in surprising order.
If priority depends on mutable state, heap invariant breaks.
If you're extracting all elements, sort + iterate may be simpler.
Standard library implementations are well-tested. Use them.
For small heaps, sorted array is faster (cache effects).
For binary heap with N elements:
For N = 1M: ~20 comparisons per operation. Fast.
Modern CPUs: ~50 nanoseconds per operation.
For high-performance:
For correctness:
Most are binary heaps. For specialized needs (decrease-key, merge), check available libraries or implement.
If you need: