Fibonacci & Pairing Heaps
Decrease-key in O(1) amortized, and the heaps used in practice.
Dijkstra's algorithm makes up to decrease-keys but only extract-mins, yet a binary heap charges for each of them. Fredman and Tarjan's Fibonacci heap (1987) cuts decrease-key to amortized by postponing all tidying until the next extract-min, which brought Dijkstra's algorithm down to . 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 : for insert, meld and decrease-key, and for extract-min and delete.
- Prove that a node of degree has at least descendants, so every degree is at most .
- Implement a pairing heap, and state what is known about its decrease-key, from Fredman's 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 time.
| Operation | What it does | Amortized cost |
|---|---|---|
| insert | Add a one-node tree to the root list; update the minimum pointer | |
| meld | Concatenate the two root lists; keep the smaller minimum | |
| find-min | Follow the pointer | |
| extract-min | Remove 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 differ | |
| decrease-key | Lower 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 up | |
| delete | Decrease the key to , then extract-min |
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 ← xPseudocode in the style of Cormen et al. (3rd ed., chapter 19). The array needs slots, where bounds every degree; the third step of this lesson shows that .
The potential argument
Insert and meld. Insert does work and adds one tree; meld does work and changes neither nor :
Extract-min. Let bound every degree. At most children join the roots, and consolidation spends per root and per link, each link removing a root, so the actual cost is . Afterwards the roots have distinct degrees, so at most trees remain, and no mark is added:
Decrease-key with cuts in all, the first one and cascading ones. Each cut adds a tree, each cascading cut removes a mark, and at most one node becomes marked:
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 . 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.
In a Fibonacci heap with , a decrease-key cuts an unmarked node , then cuts two marked ancestors in a cascade, and finally marks the next ancestor up. By how much does change?
Show the answer
D.
Three cuts make three new trees: . The two cascading cuts unmark two nodes: . The new mark adds . In total , exactly the bound with . The actual work was three cuts, yet the amortized cost is , because the potential released by the unmarked nodes paid for the cascade.
Why the degrees stay logarithmic
Let be the fewest nodes that a subtree whose root has degree can contain. Count the root, , and the subtrees of , using the lemma:
The Fibonacci numbers (, ) satisfy , the same recurrence with equality, so by induction on , with the golden ratio :
A node of degree therefore has at least nodes in its subtree. In a heap of nodes:
This is where the heap gets its name. With , extract-min and delete cost amortized, and Dijkstra's and Prim's algorithms, with extract-mins and up to decrease-keys, run in . The lesson Dijkstra's Algorithm in Depth compares this bound with every other heap.
A Fibonacci heap holds keys. What is the largest degree that the size bound allows any node to have?
Show the answer
14
A node of degree needs at least nodes. Since , we get , so ; indeed . 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 over only nodes. Degrees can then approach , and the extract-min that removes such a root hands its 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 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.
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, newPython 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 cut.
| Heap | decrease-key | extract-min | Notes |
|---|---|---|---|
| Binary heap with a position map | worst case; one array, fastest in practice | ||
| Fibonacci heap (1987) | marks and cascading cuts | ||
| Pairing heap (1986) | (Fredman, 1999) and (Pettie, 2005) | exact cost of decrease-key open; insert (Iacono, 2000) | |
| Smooth and slim heaps | optimal in Iacono and Özkan's pure heap model (Sinnamon & Tarjan, preprint 2023) | ||
| Rank-pairing heap (2009), hollow heap (2015) | Fibonacci bounds without cascading cuts | ||
| Strict Fibonacci heap (2012) | worst case | worst case | Brodal, Lagogiannis & Tarjan, on a pointer machine |
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 , insert, meld and decrease-key cost amortized, and extract-min and delete cost , where bounds the degrees.
- Marks let a node lose at most one child before it is cut, so a node of degree has at least descendants and : Dijkstra's and Prim's algorithms run in .
- Pairing heaps are simpler and usually faster; their decrease-key costs between (Fredman, 1999) and (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.
What would go wrong if decrease-key cut nodes but never marked parents or cascaded?
- Decrease-key would no longer be correct: heap order could be violated
- Nothing: cascading cuts only speed up extract-min by a constant factor
- A node could keep a large degree while its subtree shrank to almost nothing, so the degree bound, and with it the extract-min, would fail
- Insert would cost 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 extract-min, would fail
The size lemma needs each child to keep degree at least , 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- root over only nodes, and consolidation after its extract-min costs far more than .
Which statement about pairing heaps was established by 2025?
- They match Fibonacci heaps: decrease-key costs amortized
- In a broad class of heaps that includes them, decrease-key needs amortized time when insert and extract-min cost
- Their decrease-key costs exactly amortized
- Every operation costs in the worst case
Show the answer
B. In a broad class of heaps that includes them, decrease-key needs amortized time when insert and extract-min cost
Fredman (1999) proved the lower bound, refuting the original conjecture that pairing heaps match Fibonacci heaps. The best upper bound for the standard pairing heap is Pettie's (2005), so its exact cost is still open.
Dijkstra's algorithm runs in with a Fibonacci heap and with a binary heap. Why do most real implementations still use a binary heap?
- Fibonacci heaps do not support decrease-key
- The Fibonacci bound holds only for integer weights
- Binary heaps are asymptotically faster on sparse graphs
- 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 is much larger than 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 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.