Simple Algorithm
Algorithms & Data Structures · Optimal · 28 min

Fibonacci & Pairing Heaps

Decrease-key in O(1) amortized, and the heaps used in practice.

Dijkstra's algorithm makes up to mm decrease-keys but only nn extract-mins, yet a binary heap charges O(log⁡n)O(\log n) for each of them. Fredman and Tarjan's Fibonacci heap (1987) cuts decrease-key to O(1)O(1) amortized by postponing all tidying until the next extract-min, which brought Dijkstra's algorithm down to O(m+nlog⁡n)O(m + n\log n). Its simpler rival, the pairing heap, is usually faster in practice, and the true cost of its decrease-key is still an open problem.

What you’ll learn

  • Describe a Fibonacci heap (root list, minimum pointer, degrees and marks) and its operations: lazy insert and meld, consolidation in extract-min, and cuts with cascading cuts in decrease-key.
  • Prove the amortized bounds with the potential Φ=t(H)+2 m(H)\Phi = t(H) + 2\,m(H): O(1)O(1) for insert, meld and decrease-key, and O(log⁡n)O(\log n) for extract-min and delete.
  • Prove that a node of degree kk has at least Fk+2≥φkF_{k+2} \ge \varphi^k descendants, so every degree is at most log⁡φn≈1.44log⁡2n\log_\varphi n \approx 1.44\log_2 n.
  • Implement a pairing heap, and state what is known about its decrease-key, from Fredman's Ω(log⁡log⁡n)\Omega(\log\log n) lower bound to the best upper bounds, and which heaps reach Fibonacci bounds more simply or in the worst case.

Lazy trees, tidied on demand

A Fibonacci heap stores its keys in a collection of heap-ordered trees. Their roots sit in a circular doubly linked root list, and a pointer marks the smallest root. Every node keeps its degree (its number of children) and a mark bit that records whether it has lost a child since it last became a child itself. The children of a node also form a doubly linked list, so any node can be unlinked in O(1)O(1) time.

OperationWhat it doesAmortized cost
insertAdd a one-node tree to the root list; update the minimum pointerO(1)O(1)
meldConcatenate the two root lists; keep the smaller minimumO(1)O(1)
find-minFollow the pointerO(1)O(1)
extract-minRemove the minimum root and move its children to the root list, then consolidate: link roots of equal degree, the larger key becoming a child of the smaller, until all degrees differO(log⁡n)O(\log n)
decrease-keyLower the key; if it now beats its parent, cut the node into the root list, then cascade: an unmarked parent gets marked, a marked parent is cut too, and the cascade moves upO(1)O(1)
deleteDecrease the key to −∞-\infty, then extract-minO(log⁡n)O(\log n)
All bounds are amortized (Fredman & Tarjan, 1987). Single operations can be slow: an extract-min right after nn insertions consolidates nn one-node trees.
text
EXTRACT-MIN(H):
    z ← H.min
    move every child of z to the root list; remove z from it
    A[0 .. D(n)] ← nil                     # A[d]: the root of degree d seen so far
    for each root w in the root list:
        x ← w
        while A[x.degree] ≠ nil:            # two roots of equal degree: link them
            y ← A[x.degree];  A[x.degree] ← nil
            if y.key < x.key: swap x and y
            make y a child of x;  y.mark ← false    # x.degree grows by 1
        A[x.degree] ← x
    rebuild the root list from A;  H.min ← its smallest root
    return z

DECREASE-KEY(H, x, k):                      # requires k ≤ x.key
    x.key ← k;  p ← x.parent
    if p ≠ nil and x.key < p.key:
        CUT(x, p)                            # x becomes an unmarked root
        while p.parent ≠ nil:                # cascading cut (roots stay unmarked)
            if not p.mark: p.mark ← true; break
            q ← p.parent;  CUT(p, q);  p ← q
    if x.key < H.min.key: H.min ← x

Pseudocode in the style of Cormen et al. (3rd ed., chapter 19). The array AA needs D(n)+1D(n) + 1 slots, where D(n)D(n) bounds every degree; the third step of this lesson shows that D(n)=O(log⁡n)D(n) = O(\log n).

The potential argument

DerivationAmortized costs
  1. c^insert=O(1)+1=O(1),c^meld=O(1)+0=O(1)\hat c_{\text{insert}} = O(1) + 1 = O(1), \qquad \hat c_{\text{meld}} = O(1) + 0 = O(1)

    Insert and meld. Insert does O(1)O(1) work and adds one tree; meld does O(1)O(1) work and changes neither tt nor mm:

  2. c^extract ≤ O(D(n)+t(H))+(D(n)+1)−t(H) = O(D(n))\hat c_{\text{extract}}\ \le\ O\big(D(n) + t(H)\big) + \big(D(n) + 1\big) - t(H)\ =\ O\big(D(n)\big)

    Extract-min. Let D(n)D(n) bound every degree. At most D(n)D(n) children join the t(H)t(H) roots, and consolidation spends O(1)O(1) per root and per link, each link removing a root, so the actual cost is O(D(n)+t(H))O(D(n) + t(H)). Afterwards the roots have distinct degrees, so at most D(n)+1D(n) + 1 trees remain, and no mark is added:

  3. ΔΦ ≤ c+2(1−(c−1))=4−c,c^decrease ≤ O(c)+4−c = O(1)\Delta\Phi\ \le\ c + 2\big(1 - (c-1)\big) = 4 - c, \qquad \hat c_{\text{decrease}}\ \le\ O(c) + 4 - c\ =\ O(1)

    Decrease-key with c≥1c \ge 1 cuts in all, the first one and c−1c - 1 cascading ones. Each cut adds a tree, each cascading cut removes a mark, and at most one node becomes marked:

  4. c^delete=O(1)+O(D(n))\hat c_{\text{delete}} = O(1) + O\big(D(n)\big)

    The factor 2 is the point of the design: a marked node holds two units, one to pay for its own cut and one for the tree it becomes. Delete is a decrease-key followed by an extract-min:

Everything now hinges on D(n)D(n). Without the marks it could be huge: a node could lose almost all of its descendants and still keep its degree. The marks guarantee that no node loses more than one child before it is cut itself, and the next step turns that into a logarithmic bound.

Check your understanding

In a Fibonacci heap with Φ=t+2m\Phi = t + 2m, a decrease-key cuts an unmarked node xx, then cuts two marked ancestors in a cascade, and finally marks the next ancestor up. By how much does Φ\Phi change?

  1. +3+3
  2. +5+5
  3. −1-1
  4. +1+1
Show the answer

D. +1+1

Three cuts make three new trees: +3+3. The two cascading cuts unmark two nodes: −2⋅2=−4-2 \cdot 2 = -4. The new mark adds +2+2. In total 3−4+2=+13 - 4 + 2 = +1, exactly the bound 4−c4 - c with c=3c = 3. The actual work was three cuts, yet the amortized cost is O(1)O(1), because the potential released by the unmarked nodes paid for the cascade.

Why the degrees stay logarithmic

DerivationSubtrees grow like Fibonacci numbers
  1. s0=1,s1=2,sk ≥ 2+∑i=2ksi−2s_0 = 1, \qquad s_1 = 2, \qquad s_k\ \ge\ 2 + \sum_{i=2}^{k} s_{i-2}

    Let sks_k be the fewest nodes that a subtree whose root has degree kk can contain. Count the root, y1y_1, and the subtrees of y2,…,yky_2, \dots, y_k, using the lemma:

  2. sk ≥ Fk+2 ≥ φk,φ=1+52≈1.618s_k\ \ge\ F_{k+2}\ \ge\ \varphi^k, \qquad \varphi = \tfrac{1+\sqrt5}{2} \approx 1.618

    The Fibonacci numbers (F0=0F_0 = 0, F1=1F_1 = 1) satisfy Fk+2=1+∑i=0kFiF_{k+2} = 1 + \sum_{i=0}^{k} F_i, the same recurrence with equality, so by induction on kk, with the golden ratio φ\varphi:

  3. D(n) ≤ ⌊log⁡φn⌋ ≈ 1.4404 log⁡2nD(n)\ \le\ \lfloor \log_\varphi n \rfloor\ \approx\ 1.4404\,\log_2 n

    A node of degree kk therefore has at least φk\varphi^k nodes in its subtree. In a heap of nn nodes:

This is where the heap gets its name. With D(n)=O(log⁡n)D(n) = O(\log n), extract-min and delete cost O(log⁡n)O(\log n) amortized, and Dijkstra's and Prim's algorithms, with nn extract-mins and up to mm decrease-keys, run in O(m+nlog⁡n)O(m + n\log n). The lesson Dijkstra's Algorithm in Depth compares this bound with every other heap.

Predict before you look

A Fibonacci heap holds n=1000n = 1000 keys. What is the largest degree that the size bound allows any node to have?

Show the answer

14

A node of degree kk needs at least Fk+2F_{k+2} nodes. Since F16=987≤1000<F17=1597F_{16} = 987 \le 1000 < F_{17} = 1597, we get k+2≤16k + 2 \le 16, so k≤14k \le 14; indeed log⁡φ1000≈14.4\log_\varphi 1000 \approx 14.4. Consolidation's degree array never needs more than 15 slots.

Think first Why does the bound need the marks at all? Imagine cuts without cascades.

Without cascades, a node could lose any number of children and keep its place. Build a root of high degree by linking, decrease keys deep inside its children until everything below them has been cut away, and delete the cut nodes: you are left with a root of degree kk over only k+1k + 1 nodes. Degrees can then approach nn, and the extract-min that removes such a root hands its kk children to consolidation. A mark records the first lost child; the second loss triggers a cut.

Pairing heaps, and the heaps people use

Fibonacci heaps are rarely the fastest choice. Each node stores a key, a degree, a mark and four pointers, every operation chases pointers through memory, and the O(1)O(1) decrease-key pays off only when decrease-keys vastly outnumber extract-mins on large inputs. Experimental studies, such as Larkin, Sen and Tarjan's (2014), found simple array-based heaps and pairing heaps faster than Fibonacci heaps on typical workloads.

python
class Node:
    __slots__ = ('key', 'kids')
    def __init__(self, key):
        self.key, self.kids = key, []        # kids: oldest first, newest last

def meld(a, b):                              # O(1): the larger root becomes a child
    if a is None: return b
    if b is None: return a
    if b.key < a.key:
        a, b = b, a
    a.kids.append(b)
    return a

def insert(root, key):                       # O(1)
    return meld(root, Node(key))

def pop(root):                               # (minimum, new root); O(log n) amortized
    kids = root.kids[::-1]                   # newest child first, as in the paper
    pairs = [meld(kids[i], kids[i + 1] if i + 1 < len(kids) else None)
             for i in range(0, len(kids), 2)]        # pass 1: pair up, left to right
    new = None
    for t in reversed(pairs):                # pass 2: meld the pairs right to left
        new = meld(t, new)
    return root.key, new

Python lists stand in for the sibling pointers, so pop reverses them to obtain the paper's newest-first order. Real implementations link siblings in both directions, which also gives decrease-key its O(1)O(1) cut.

Heapdecrease-keyextract-minNotes
Binary heap with a position mapO(log⁡n)O(\log n)O(log⁡n)O(\log n)worst case; one array, fastest in practice
Fibonacci heap (1987)O(1)O(1)O(log⁡n)O(\log n)marks and cascading cuts
Pairing heap (1986)Ω(log⁡log⁡n)\Omega(\log\log n) (Fredman, 1999) and O(22log⁡log⁡n)O(2^{2\sqrt{\log\log n}}) (Pettie, 2005)O(log⁡n)O(\log n)exact cost of decrease-key open; insert O(1)O(1) (Iacono, 2000)
Smooth and slim heapsO(log⁡log⁡n)O(\log\log n)O(log⁡n)O(\log n)optimal in Iacono and Özkan's pure heap model (Sinnamon & Tarjan, preprint 2023)
Rank-pairing heap (2009), hollow heap (2015)O(1)O(1)O(log⁡n)O(\log n)Fibonacci bounds without cascading cuts
Strict Fibonacci heap (2012)O(1)O(1) worst caseO(log⁡n)O(\log n) worst caseBrodal, Lagogiannis & Tarjan, on a pointer machine
Amortized bounds unless stated. Fredman's lower bound covers a broad class of heaps that includes pairing heaps; the pure heap model is a later formalization of such self-adjusting heaps, in which Θ(log⁡log⁡n)\Theta(\log\log n) is the price of decrease-key. The inventors of pairing heaps had conjectured O(1)O(1).

Key takeaways

  • A Fibonacci heap is a lazy forest: insert and meld only add trees, extract-min consolidates roots of equal degree, and decrease-key cuts a node out and cascades through marked ancestors.
  • With Φ=t+2m\Phi = t + 2m, insert, meld and decrease-key cost O(1)O(1) amortized, and extract-min and delete cost O(D(n))O(D(n)), where D(n)D(n) bounds the degrees.
  • Marks let a node lose at most one child before it is cut, so a node of degree kk has at least Fk+2≥φkF_{k+2} \ge \varphi^k descendants and D(n)≤log⁡φn≈1.44log⁡2nD(n) \le \log_\varphi n \approx 1.44\log_2 n: Dijkstra's and Prim's algorithms run in O(m+nlog⁡n)O(m + n\log n).
  • Pairing heaps are simpler and usually faster; their decrease-key costs between Ω(log⁡log⁡n)\Omega(\log\log n) (Fredman, 1999) and O(22log⁡log⁡n)O(2^{2\sqrt{\log\log n}}) (Pettie, 2005), exactly how much is open. Rank-pairing and hollow heaps reach Fibonacci bounds more simply, and strict Fibonacci heaps reach them in the worst case.

Check yourself

In the interactive lesson these are scored with your confidence, and what you miss comes back for review.

Question 1

What would go wrong if decrease-key cut nodes but never marked parents or cascaded?

  1. Decrease-key would no longer be correct: heap order could be violated
  2. Nothing: cascading cuts only speed up extract-min by a constant factor
  3. A node could keep a large degree while its subtree shrank to almost nothing, so the degree bound, and with it the O(log⁡n)O(\log n) extract-min, would fail
  4. Insert would cost O(log⁡n)O(\log n) amortized
Show the answer

C. A node could keep a large degree while its subtree shrank to almost nothing, so the degree bound, and with it the O(log⁡n)O(\log n) extract-min, would fail

The size lemma needs each child yiy_i to keep degree at least i−2i - 2, which holds because a node is cut as soon as it loses a second child. Without that, cuts can strip a node's children of all their descendants, leaving a degree-kk root over only k+1k + 1 nodes, and consolidation after its extract-min costs far more than O(log⁡n)O(\log n).

Question 2

Which statement about pairing heaps was established by 2025?

  1. They match Fibonacci heaps: decrease-key costs O(1)O(1) amortized
  2. In a broad class of heaps that includes them, decrease-key needs Ω(log⁡log⁡n)\Omega(\log\log n) amortized time when insert and extract-min cost O(log⁡n)O(\log n)
  3. Their decrease-key costs exactly Θ(log⁡log⁡n)\Theta(\log\log n) amortized
  4. Every operation costs O(log⁡n)O(\log n) in the worst case
Show the answer

B. In a broad class of heaps that includes them, decrease-key needs Ω(log⁡log⁡n)\Omega(\log\log n) amortized time when insert and extract-min cost O(log⁡n)O(\log n)

Fredman (1999) proved the Ω(log⁡log⁡n)\Omega(\log\log n) lower bound, refuting the original conjecture that pairing heaps match Fibonacci heaps. The best upper bound for the standard pairing heap is Pettie's O(22log⁡log⁡n)O(2^{2\sqrt{\log\log n}}) (2005), so its exact cost is still open.

Question 3

Dijkstra's algorithm runs in O(m+nlog⁡n)O(m + n\log n) with a Fibonacci heap and O((n+m)log⁡n)O((n+m)\log n) with a binary heap. Why do most real implementations still use a binary heap?

  1. Fibonacci heaps do not support decrease-key
  2. The Fibonacci bound holds only for integer weights
  3. Binary heaps are asymptotically faster on sparse graphs
  4. Fibonacci heaps have large constant factors, pointer-heavy nodes and poor cache behavior, so their asymptotic edge rarely pays off
Show the answer

D. Fibonacci heaps have large constant factors, pointer-heavy nodes and poor cache behavior, so their asymptotic edge rarely pays off

The asymptotic gain appears only when mm is much larger than nn on very large graphs, and even then a binary heap with lazy deletion, stored in one contiguous array, is often faster. Fibonacci heaps matter for theory, where O(m+nlog⁡n)O(m + n\log n) became the bound to beat.

Further reading

  • M. L. Fredman & R. E. Tarjan, "Fibonacci heaps and their uses in improved network optimization algorithms," Journal of the ACM 34(3) (1987), 596–615.
  • M. L. Fredman, R. Sedgewick, D. D. Sleator & R. E. Tarjan, "The pairing heap: a new form of self-adjusting heap," Algorithmica 1 (1986), 111–129.
  • M. L. Fredman, "On the efficiency of pairing heaps and related data structures," Journal of the ACM 46(4) (1999), 473–501.
  • S. Pettie, "Towards a final analysis of pairing heaps," Proceedings of the 46th IEEE Symposium on Foundations of Computer Science (FOCS) (2005).
  • T. H. Cormen, C. E. Leiserson, R. L. Rivest & C. Stein, Introduction to Algorithms, 3rd ed. (MIT Press, 2009), chapter 19.