Data Structures

Priority Queue Heaps

A priority queue hands back the smallest item first, and a heap is the usual way to build one. Here is a practical guide to the four heaps you will meet most often: the comparison table first, then when each one is worth using.

At a glance

A priority queue is a collection where you always take out the item with the smallest key (or the largest). A heap is the family of data structures built for this job. The key operations are insert, find-min, extract-min, and sometimes decrease-key, which lowers the key of an item already inside.

The four heaps below make different trades. The binary heap is a plain array and wins on simplicity and speed. The binomial heap adds a fast merge. The Fibonacci heap and pairing heap make insert, merge, and decrease-key cheap by delaying work. Use the table to compare them, then open each page for an interactive visualization.

Data Structures InsertFind-minExtract-minDecrease-keyMergeBest for
Binary Heap O(log n) worst case; O(1) on average for random keysO(1)O(log n)O(log n)O(n)General use; the safe default
Binomial Heap O(log n) worst, O(1) amortizedO(log n), or O(1) with a cached pointerO(log n)O(log n)O(log n)Workloads that merge heaps often
Fibonacci Heap O(1) amortizedO(1)O(log n) amortizedO(1) amortizedO(1)Proving the best theoretical bounds for graph algorithms
Pairing Heap O(1)O(1)O(log n) amortizedBetween Ω(log log n) and O(log n) amortized (exact bound is subtle)O(1)Fast in practice with cheap insert and merge; decrease-key is fast in practice but has no O(1) proof

Each algorithm in depth

Click any card for the full article with an interactive visualization, complexity analysis, code examples, and FAQ.

Which priority queue heap should I pick?

For almost every program, use a binary heap. It stores items in one array, so it is compact and fast on real hardware.

  • Default choice: a binary heap. It is what your language library already gives you.
  • You merge whole heaps often: a binomial heap merges in O(log n). A binary heap needs O(n).
  • You call decrease-key a lot on a big graph: try a pairing heap first. It is simple and usually fast.
  • You need the best proven bound for decrease-key: the Fibonacci heap gives O(1) amortized. This matters mostly in theory and in proofs.

The Fibonacci heap is rarely the fastest in practice. Its nodes are large, it chases many pointers, and its hidden constant factors are high. Unless the graph is huge and dense, a binary heap tends to win.

Binary heap vs Fibonacci heap

The binary heap does insert, extract-min, and decrease-key in O(log n). The Fibonacci heap does insert and decrease-key in O(1) amortized, and extract-min in O(log n) amortized. On paper the Fibonacci heap is better. In real programs the binary heap often runs faster, because arrays are cache-friendly and Fibonacci nodes are heavy. The Fibonacci heap pays off only when decrease-key calls greatly outnumber extract-min calls.

Binomial heap vs Fibonacci heap

Both can merge heaps cheaply. A binomial heap keeps its trees tidy at all times. Merging mirrors adding two binary numbers and costs O(log n). A Fibonacci heap is lazy. It just joins the root lists in O(1) and cleans up later, during extract-min. The binomial heap is simpler and has worst-case bounds. The Fibonacci heap has better amortized bounds but more bookkeeping, since each node carries a mark bit and extra pointers.

What decrease-key buys Dijkstra and Prim

Dijkstra's shortest-path algorithm and Prim's spanning-tree algorithm both lower the keys of vertices many times. In the worst case there is one decrease-key per edge, and one extract-min per vertex. With a binary heap the total is O((V + E) log V). With a Fibonacci heap it drops to O(E + V log V), because each of the E decrease-key calls is now O(1) amortized. That gap only shows on graphs with many more edges than vertices. Many real programs skip decrease-key entirely. They insert a duplicate entry and ignore stale ones when popped.

Amortized vs worst case, in plain words

A worst-case bound limits every single operation. An amortized bound limits the average cost over a whole sequence of operations. Some single calls may be slow, as long as the cheap ones pay for them. A Fibonacci heap extract-min can take O(n) once, after many cheap inserts piled up trees. Averaged over the sequence, it is O(log n). If you need a hard limit on each call, for example in real-time code, amortized bounds are the wrong promise. Choose a binary or binomial heap instead.

The pairing heap and decrease-key

The pairing heap has O(1) insert and merge, and O(log n) amortized extract-min. Its decrease-key cost is the open part. It is known to be at least on the order of log log n amortized, so it is not O(1) as once hoped. The best proven upper bound is a bit under O(log n) amortized. The exact answer is still subtle. Despite this, pairing heaps are fast in many experiments, so they often beat Fibonacci heaps in practice.

What real libraries use

C++ std::priority_queue and Java PriorityQueue are both binary heaps. Python's heapq is too. They have no decrease-key and no fast merge. This is a strong hint: for most jobs, you do not need more. Pairing heaps appear in some graph and routing code where decrease-key and merge matter.

Frequently asked questions

What is the best heap for a priority queue?

For most programs, a binary heap. It is simple, uses one array, and is fast on real hardware. Pick another heap only if you need fast merging (binomial or pairing heap) or you have measured that decrease-key is your bottleneck.

Is a Fibonacci heap faster than a binary heap?

On paper, yes: it has O(1) amortized insert and decrease-key. In practice, usually no. Fibonacci heap nodes are large, it follows many pointers, and its constant factors are high. A binary heap in an array tends to win unless the graph is very large and has many decrease-key calls.

What is the difference between a binomial heap and a Fibonacci heap?

A binomial heap keeps its trees in a strict shape and merges in O(log n) worst case. A Fibonacci heap is lazy: it merges in O(1), delays cleanup until extract-min, and gets O(1) amortized decrease-key. The binomial heap is simpler. The Fibonacci heap has better amortized bounds.

What does decrease-key do and why does it matter?

Decrease-key lowers the key of an item that is already in the heap, so it moves closer to the front. Dijkstra's and Prim's algorithms use it to update distances. A cheaper decrease-key can speed up those algorithms on dense graphs.

What does amortized time mean?

It is the average cost per operation over a whole sequence, not the cost of each single call. A few calls can be slow if many others are cheap. A worst-case bound, by contrast, limits every single call.

Which heap merges fastest?

The Fibonacci heap and pairing heap merge in O(1). A binomial heap merges in O(log n). A binary heap needs O(n), since it must rebuild the array.

What is the decrease-key cost of a pairing heap?

It is not fully settled. It was once conjectured to be O(1) amortized, but that was disproved: the cost is at least on the order of log log n amortized. The best proven upper bound is somewhat below O(log n) amortized. In practice it is fast.

Which heap do C++ and Java use for priority queues?

Both use a binary heap. C++ std::priority_queue and Java PriorityQueue are array-based binary heaps. Neither offers decrease-key or a fast merge.