The Potential Method in Depth
Designing potential functions: binary counters, growing and shrinking tables, and more.
A good potential function is a measure of disorder: it is high exactly when an expensive operation is coming and falls when that operation happens. Finding one is an art, and the hardest analyses in data structures, splay trees, Fibonacci heaps and union–find, are triumphs of choosing . This lesson builds the skill on three problems where the obvious structure fails and a well-chosen potential proves the fix.
What you’ll learn
- State the potential-method theorem precisely, and relate potentials to the accounting method.
- Prove that a table that doubles when full and halves when a quarter full has amortized cost per operation, and show that halving at one half thrashes.
- Make a counter with increments and decrements run in amortized time with a redundant number representation.
- Analyze skew heaps with the heavy-node potential: amortized meld, although a single operation can cost .
The framework, and how to design a potential
The potential method and the accounting method are two views of one idea; Tarjan's survey (1985) calls them the physicist's and the banker's view. Credits stored on elements add up to a potential, and conversely any valid assignment of amortized costs defines a potential: let be the total of over the first operations. The physicist's view wins when the credit is a global quantity that no single element owns.
How to find a good ? Ask what makes an operation expensive, and make measure exactly that, with a coefficient large enough to pay for it:
- a dynamic array is expensive when it is full: grows as the array fills;
- a binary counter is expensive when it ends in many 1s: counts 1s;
- a move-to-front list is expensive when it disagrees with a better list: counts the disagreements (in Online Algorithms & Competitive Analysis);
- a splay tree is expensive when it is deep and unbalanced: .
Tables that grow and shrink
A dynamic table should also give memory back when elements are removed. The natural rule, halve the capacity when the table becomes half full, is a trap. Fill a table of capacity completely and append once: it doubles to and copies elements. Pop once: now it is exactly half full, so it halves and copies elements. Append again: full, so it doubles. Alternating appends and pops makes every operation cost , so operations cost .
The fix leaves room between the two thresholds: double when full, but halve only when fewer than a quarter of the slots are used. After any resize the table is about half full, and operations must pass before the next one.
A table has just doubled from capacity 64 to 128 and holds 65 elements. It halves when, after a pop, fewer than a quarter of its slots are used. How many pops in a row does it take to trigger the next halving?
Show the answer
34
Fewer than elements means at most 31, so it takes pops. In the other direction, 63 appends would fill it. Either way, about half the capacity's worth of operations separates two resizes, which is what lets each resize be paid for.
Append, no resize, before and after: cost 1, rises by 2. Append that doubles a full table of capacity : cost , and falls from to :
Append while : cost 1 and falls by 1; if the append reaches exactly half full, goes from 1 to 0. Either way:
Pop, no resize: cost 1. With before and after, falls by 2; crossing below one half moves from 0 to 1; below one half, rises by 1:
Pop that halves: the pop leaves elements, which are copied into capacity . Cost , while falls from to :
starts at 0 and is never negative, so any appends and pops from an empty table cost at most element writes. A simulation of random and adversarial sequences confirms the case analysis: appends never exceed amortized cost 3 and pops never exceed 2.
Counters that count both ways
With increments only, the number of 1 bits pays for every carry. Add decrements and the potential fails: alternating between and flips all bits each time. The problem is the representation, which has one expensive state on each side of every boundary.
The fix is redundancy. Let each digit be 0, 1 or 2, still with weight , so a number has several representations. An increment adds 1 to ; a digit that would become 3 is set to 1 and carries, because . A decrement subtracts 1 from ; a digit that would become is set to 1 and borrows, because . In both directions a long chain leaves behind a run of 1s, the safest digit, one step away from both ends.
Increment. A carry chain of length turns digits 2 into 1, lowering by . The last digit touched goes from 0 to 1 (lowering by 1), or from 1 to 2 (raising it by 1), or is a new leading 1 (no change). With actual cost :
Decrement. A borrow chain of length turns digits 0 into 1, lowering by . The last digit goes from 2 to 1 (lowering ), or from 1 to 0 (raising it by 1), or is a leading 1 that disappears (no change):
starts at 0 and is never negative, so the potential-method theorem applies:
A simulation of 300,000 random increments and decrements confirms it: no operation ever had amortized cost above 2. Redundant representations reappear in worst-case structures: skew binary numbers, with digits 0, 1, 2 and a rule that only the lowest nonzero digit may be 2, support both operations in worst-case time, and Deamortization & Global Rebuilding builds on that idea.
Skew heaps: a self-adjusting example
A skew heap (Sleator & Tarjan, 1986) is a heap-ordered binary tree with no balance information at all. Its one operation is meld: walk down the right spines of both heaps, merging them in key order, and then swap the two children of every node on that merge path. Insert is a meld with a one-node heap, and delete-min melds the root's two subtrees.
class Node:
def __init__(self, key):
self.key, self.left, self.right = key, None, None
def meld(a, b):
"""Merge two skew heaps (min-heaps) and return the new root."""
if a is None: return b
if b is None: return a
if b.key < a.key:
a, b = b, a # a holds the smaller root
a.right = meld(a.right, b) # merge along the right spine
a.left, a.right = a.right, a.left # then swap a's children
return aRecursive for clarity; its depth is the length of the merge path, which, as we will see, can be linear.
Let the merge path have light and heavy nodes, taken from the right spines of heaps of sizes and . The actual cost is , and the light ones are few:
Heavy nodes become light. A heavy node on the path keeps its old left subtree, now as its right child, while its old right subtree, only bigger after the merge, becomes its left child. So ends up light. Nodes off the path keep their subtrees and their status.
Newly heavy nodes are few. After the swaps, the merge path is the leftmost path of the new heap, which has nodes, so at most of its nodes are heavy:
So insert, delete-min and meld all take amortized time, and a simulation finds no meld whose amortized cost exceeds . The constant is not tight: Kaldewaij and Schoenmakers (1991) proved amortized comparisons per meld, and Schoenmakers (1997) showed that bound is attained.
Think first The bound is amortized. Can a single skew-heap insert really take linear time?
Yes. Start from one node and repeat: insert a new minimum, which makes the whole old heap its left child, then insert a key larger than that minimum, which swaps the old heap onto the right. Each round lengthens the right spine by one. After 4000 such inserts the heap has 4001 nodes and a right spine of 2001; inserting one more large key walks the entire spine, 2001 merge steps. The potential paid for it: the spine was made of heavy nodes.
Where potentials lead
The same craft proves the landmark bounds of data structures. Splay trees (Sleator & Tarjan, 1985) use , where is the size of 's subtree, and the access lemma gives amortized time per operation, with no balance information stored. Fibonacci heaps (Fredman & Tarjan, 1987) use the number of trees plus twice the number of marked nodes to get amortized decrease-key. Union–find with path compression and union by rank reaches amortized per operation, the inverse Ackermann function, through an elaborate potential.
Key takeaways
- The potential method: , and whenever never drops below its initial value. Credits and potentials are two views of the same bookkeeping.
- Design to measure what makes the next operation expensive. For a table that doubles when full and halves when a quarter full, or gives amortized cost at most 3; halving at one half thrashes at per operation.
- A redundant representation, digits 0, 1, 2 with the number of digits other than 1, gives increments and decrements amortized cost, where plain binary costs .
- Skew heaps meld in amortized time with the heavy-node potential, although single operations can be linear; splay trees, Fibonacci heaps and union–find rest on the same method, and dynamic optimality remains open.
Check yourself
In the interactive lesson these are scored with your confidence, and what you miss comes back for review.
A table doubles when full and halves as soon as it is half full. What is the worst-case total cost of appends and pops?
- in expectation
Show the answer
C.
Right after a doubling, one pop makes the table half full and halves it, and one append doubles it again: each operation copies about half the table. Halving at a quarter leaves a gap of operations between resizes, which restores amortized cost.
In the counter with digits 0, 1, 2 and the number of digits that are not 1, why does a long carry chain have small amortized cost?
- Because carries never happen with digits up to 2
- Because each carry turns a 2 into a 1, and each such step lowers by 1, paying for itself
- Because the representation is unique, so the counter cannot oscillate
- Because a decrement always undoes the previous increment's carries
Show the answer
B. Because each carry turns a 2 into a 1, and each such step lowers by 1, paying for itself
A chain of carries costs and lowers by at least ; symmetric borrows turn 0s into 1s. Either way the chain leaves 1s behind, which are cheap for both an increment and a decrement, so an adversary cannot make the next operation expensive again without first paying to raise .
Which statement about skew heaps is true?
- Every meld takes time in the worst case
- They store a rank or a height in every node to stay balanced
- Every sequence of operations takes time, though a single meld can take
- Their melds take amortized time
Show the answer
C. Every sequence of operations takes time, though a single meld can take
The heavy-node potential bounds each meld by amortized. A single meld can still walk a right spine of linear length, which a sequence of inserts can build; the potential stored in its heavy nodes pays for that walk.
An accounting scheme stores credit on elements and never lets the total credit go negative. How do you turn it into a potential function?
- Let be the total credit currently stored in the structure
- Let be the number of operations performed so far
- It cannot be done: some accounting arguments have no potential
- Let be the largest credit on any single element
Show the answer
A. Let be the total credit currently stored in the structure
With the stored credit, is exactly the charge of the operation, starts at 0 and never goes negative. The two methods are the banker's and the physicist's view of the same idea.
Further reading
- R. E. Tarjan, "Amortized computational complexity," SIAM Journal on Algebraic and Discrete Methods 6(2) (1985), 306–318.
- D. D. Sleator & R. E. Tarjan, "Self-adjusting heaps," SIAM Journal on Computing 15(1) (1986), 52–69.
- A. Kaldewaij & B. Schoenmakers, "The derivation of a tighter bound for top-down skew heaps," Information Processing Letters 37(5) (1991), 265–271.
- T. H. Cormen, C. E. Leiserson, R. L. Rivest & C. Stein, Introduction to Algorithms, 4th ed. (MIT Press, 2022), chapter 16.