,

Contents · Priority queues and heaps (binary, binomial, Fibonacci, pairing)


Overview

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:

  • Insert: Add a new element with priority
  • Extract-Min/Max: Remove and return the element with highest priority
  • Decrease-Key: Reduce the priority of an existing element
  • Merge: Combine two priority queues
  • Find-Min/Max: Peek at the highest priority element

Different heap implementations offer different trade-offs between these operations, making them suitable for various algorithms and applications.


Binary Heaps

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:

  • Array-based representation: parent at index i, children at 2i+1 and 2i+2
  • Complete binary tree ensures O(log n) height
  • Cache-friendly due to contiguous memory layout

Operations:

  • Insert: O(log n) - Add at end, bubble up
  • Extract-Min: O(log n) - Remove root, move last element to root, bubble down
  • Decrease-Key: O(log n) - Update value, bubble up
  • Build-Heap: O(n) - Floyd's algorithm (bottom-up heapify)
  • Find-Min: O(1) - Root element
  • Merge: O(n) - Concatenate and rebuild

Applications:

  • Heapsort algorithm
  • Dijkstra's shortest path (basic version)
  • Priority scheduling in operating systems
  • Median maintenance with two heaps
  • Top-K problems

Advantages: Simple implementation, excellent cache locality, low constant factors, no pointer overhead.

Disadvantages: Expensive merge operation, decrease-key requires knowing element position.


Binomial Heaps

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:

  • Forest of binomial trees with distinct orders
  • Each tree satisfies min-heap property
  • At most one tree of each order (binary representation of size)
  • Roots linked in increasing order of degree

Binomial Tree Properties:

  • Bk has exactly 2k nodes
  • Height of Bk is k
  • Root has degree k with children Bk-1, Bk-2, ..., B0
  • Number of nodes at depth d is C(k,d)

Operations:

  • Insert: O(log n) amortized, O(log n) worst-case - Create B0, merge with heap
  • Extract-Min: O(log n) - Remove min root, merge children back
  • Decrease-Key: O(log n) - Update value, bubble up in tree
  • Merge: O(log n) - Binary addition of tree collections
  • Find-Min: O(log n) - Scan roots (can be O(1) with pointer)

Merge Algorithm: Similar to binary addition - combine trees of same order, carry overflow to next order.

Applications:

  • Union-find with path compression
  • Mergeable priority queues in distributed systems
  • Theoretical analysis of data structures

Advantages: Efficient merge, all operations O(log n), good theoretical properties.

Disadvantages: Complex implementation, pointer overhead, worse constant factors than binary heaps.


Fibonacci 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:

  • Collection of heap-ordered trees (forest)
  • Circular doubly-linked list of roots
  • Each node tracks: parent, child, degree, mark bit
  • Minimum pointer maintained for O(1) access
  • Lazy consolidation: trees merged only when necessary

Operations:

  • Insert: O(1) amortized - Add new tree to root list
  • Extract-Min: O(log n) amortized - Remove min, consolidate trees
  • Decrease-Key: O(1) amortized - Cut node, cascading cuts if needed
  • Merge: O(1) - Concatenate root lists
  • Find-Min: O(1) - Return min pointer
  • Delete: O(log n) amortized - Decrease-key to -∞, extract-min

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:

  • Φ = t + 2m (t = trees, m = marked nodes)
  • Amortized cost = actual cost + ΔΦ
  • Proves O(1) for insert, decrease-key, merge

Applications:

  • Dijkstra's algorithm: O(E + V log V) with Fibonacci heaps
  • Prim's MST algorithm: O(E + V log V)
  • Network optimization problems
  • Theoretical complexity analysis baseline

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

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:

  • Heap-ordered multiway tree (each node can have many children)
  • Children stored in linked list
  • No structural constraints beyond heap property
  • Extremely simple: just nodes with value, first child, next sibling pointers

Operations:

  • Insert: O(1) - Create single-node tree, merge with root
  • Extract-Min: O(log n) amortized - Remove root, pair-merge children
  • Decrease-Key: O(log n) amortized (conjectured O(1)) - Cut subtree, merge with root
  • Merge: O(1) - Make larger root child of smaller root
  • Find-Min: O(1) - Return root

Two-Pass Pairing: After extract-min, merge children in two passes:

  1. Left-to-right pass: Pair adjacent children: merge(c1,c2), merge(c3,c4), ...
  2. Right-to-left pass: Merge results from right to left

Alternative strategies: multi-pass, front-to-back, but two-pass is most common.

Theoretical Analysis:

  • Decrease-key conjectured O(1) amortized, proven O(log log n)
  • All operations match or approach Fibonacci heap bounds
  • Simpler analysis than Fibonacci heaps

Applications:

  • Graph algorithms (Dijkstra, Prim) - often fastest in practice
  • Discrete event simulation
  • Network routing protocols
  • Practical priority queue implementations

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.


Performance Comparison

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:

  • Binary Heap: Default choice for most applications. Simple, fast, cache-friendly. Use when decrease-key is rare or not needed.
  • Binomial Heap: When you need efficient merging and can tolerate complexity. Good for mergeable priority queues.
  • Fibonacci Heap: Theoretical analysis and when decrease-key dominates (dense graphs). Rarely fastest in practice.
  • Pairing Heap: Best practical alternative to binary heaps when decrease-key is frequent. Simpler than Fibonacci, often faster.

Real-World Performance Factors:

  • Cache effects: Binary heaps win due to array layout
  • Constant factors: Pointer-based heaps have overhead
  • Operation mix: Fibonacci/pairing excel with many decrease-keys
  • Implementation quality: Well-tuned binary heap often beats complex heaps

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.


Exercises

  1. Binary Heap Implementation: Implement a binary min-heap with insert, extract-min, and heapify. Test with random data and verify heap property.
  2. Heap Sort: Use your binary heap to implement heapsort. Compare performance with quicksort and mergesort on various input sizes.
  3. K-Way Merge: Merge k sorted arrays using a min-heap. Analyze time complexity as function of k and total elements n.
  4. Median Maintenance: Maintain running median of stream using two heaps (max-heap for lower half, min-heap for upper half).
  5. Dijkstra Comparison: Implement Dijkstra's algorithm with binary heap and pairing heap. Compare performance on graphs of varying density.
  6. Binomial Tree Visualization: Draw binomial trees B₀ through B₄. Verify the recursive structure and node count properties.
  7. Fibonacci Heap Analysis: Trace the potential function through a sequence of operations. Verify amortized O(1) for decrease-key.
  8. Pairing Strategy Comparison: Implement two-pass pairing and front-to-back pairing. Compare performance on extract-min heavy workloads.
  9. Priority Queue Applications: Implement event-driven simulation using priority queue. Model customers in queue with arrival/service times.
  10. Decrease-Key Benchmark: Create workload with 50% decrease-key operations. Compare binary, pairing, and Fibonacci heaps.