Priority queues are abstract data types that maintain a collection of elements with associated priorities, supporting efficient extraction of the minimum (or maximum) element. Heaps are concrete data structures that implement priority queues with varying performance characteristics.
Core Operations:
Different heap implementations offer different trade-offs between these operations, making them suitable for various algorithms and applications.
Binary heaps are complete binary trees stored in arrays, satisfying the heap property: each parent node has priority ≥ (max-heap) or ≤ (min-heap) its children.
Structure:
i, children at 2i+1 and 2i+2Operations:
Applications:
Advantages: Simple implementation, excellent cache locality, low constant factors, no pointer overhead.
Disadvantages: Expensive merge operation, decrease-key requires knowing element position.
Binomial heaps are collections of binomial trees that support efficient merging. A binomial tree Bk has 2k nodes and is formed by linking two Bk-1 trees.
Structure:
Binomial Tree Properties:
Operations:
Merge Algorithm: Similar to binary addition - combine trees of same order, carry overflow to next order.
Applications:
Advantages: Efficient merge, all operations O(log n), good theoretical properties.
Disadvantages: Complex implementation, pointer overhead, worse constant factors than binary heaps.
Fibonacci heaps achieve theoretically optimal amortized time bounds for priority queue operations through lazy consolidation and cascading cuts. Named for Fibonacci numbers appearing in their analysis.
Structure:
Operations:
Consolidation: After extract-min, merge trees of same degree until all degrees unique. Uses array indexed by degree for O(log n) consolidation.
Cascading Cuts: When a node loses second child, cut it and move to root list. Propagate up to maintain degree bounds. Mark bit tracks first child loss.
Potential Function Analysis:
Applications:
Advantages: Best amortized bounds, O(1) decrease-key enables optimal graph algorithms.
Disadvantages: Complex implementation, high constant factors, poor cache locality, rarely faster in practice than binary heaps.
Pairing heaps are simplified heap structures that achieve performance comparable to Fibonacci heaps in practice with much simpler implementation. They use a multiway tree structure with lazy merging.
Structure:
Operations:
Two-Pass Pairing: After extract-min, merge children in two passes:
Alternative strategies: multi-pass, front-to-back, but two-pass is most common.
Theoretical Analysis:
Applications:
Advantages: Simple implementation, good practical performance, low memory overhead, competitive with Fibonacci heaps.
Disadvantages: Weaker theoretical bounds for decrease-key, performance depends on merge strategy.
Complexity Table (Amortized):
| Operation | Binary | Binomial | Fibonacci | Pairing |
|---|---|---|---|---|
| Insert | O(log n) | O(1) | O(1) | O(1) |
| Find-Min | O(1) | O(1)* | O(1) | O(1) |
| Extract-Min | O(log n) | O(log n) | O(log n) | O(log n) |
| Decrease-Key | O(log n) | O(log n) | O(1) | O(log n)** |
| Merge | O(n) | O(log n) | O(1) | O(1) |
* O(log n) without min pointer, O(1) with
** Conjectured O(1), proven O(log log n)
Practical Guidelines:
Real-World Performance Factors:
Benchmark Results (typical): For Dijkstra on random graphs, binary heaps often fastest for sparse graphs (E ≈ V), pairing heaps competitive for dense graphs (E ≈ V²), Fibonacci heaps rarely win despite optimal complexity.