Sub topic 5
Potential method: a stored-energy function
The potential method is the third way to prove an amortised bound, after the aggregate method and the accounting method. It does the same job as the accounting method, but it stores the savings as one number that depends only on the data structure's current state. This chapter assumes you have read the aggregate and accounting chapters, you can read short Python programs, and you know how a doubling dynamic array works.
The idea
Define a potential function Φ that maps each state D of the data structure to a non-negative number. Think of Φ as energy stored in the structure. Cheap operations add energy and expensive operations spend it.
Let ci be the actual cost of operation i. Let D{i-1} be the state before it and D_i the state after it. The amortised cost is:
amortised_i = c_i + Φ(D_i) − Φ(D_{i−1})Sum this over n operations. The potential terms telescope, which means every intermediate Φ appears once with a plus sign and once with a minus sign, so they cancel:
Σ amortised_i = Σ c_i + Φ(D_n) − Φ(D_0)If Φ(Dn) ≥ Φ(D0) for every n, then the total amortised cost is at least the total actual cost. If each amortised cost is at most some constant k, the n operations cost at most k·n in total.
The condition Φ(Dn) ≥ Φ(D0) is the whole safety requirement. The usual way to meet it is to make Φ(D_0) = 0 and Φ ≥ 0 everywhere. The dynamic array below shows that this is sufficient but not necessary.
Choosing Φ for a doubling array
You want the potential to be high just before an expensive operation and low just after it. A resize copies C elements when the array is full, so the potential must have grown to about C by then. Immediately after a resize the array is half full, so the potential should be near zero.
This potential has both properties:
Φ = 2 · size − capacity- It is 0 when the array is exactly half full.
- It equals capacity when the array is full, which is the number of copies the next resize needs.
- Each append that does not resize raises it by 2.
The initial state of the array in this series has size 0 and capacity 1, so Φ(D0) = −1. That is negative. The requirement is only Φ(Dn) ≥ Φ(D_0). After at least one append, the array is always at least half full, so Φ ≥ 0 > −1 and the requirement holds.
The two cases
Let s be the size and C the capacity before an append. Cost is 1 per write plus 1 per copied element.
| Case | Actual cost | Φ before | Φ after | ΔΦ | Amortised cost |
|---|---|---|---|---|---|
| s < C (no resize) | 1 | 2s − C | 2s + 2 − C | +2 | 1 + 2 = 3 |
| s = C (resize to 2C) | 1 + C | C | 2(C+1) − 2C = 2 | 2 − C | (1 + C) + (2 − C) = 3 |
In the resize case the +C in the actual cost cancels against the −C drop in potential. Every append has amortised cost exactly 3. So n appends cost at most 3n, which is O(1) amortised per append.
The potential rises and falls in a sawtooth pattern. It climbs by 2 on each cheap append and drops back to 2 on each append that triggers a resize.
Checking the proof by running it
The following program applies the definition literally. It measures the actual cost of each append, computes the potential before and after, and asserts that the amortised cost is 3 every time. It then checks the telescoping identity.
class DynamicArray:
def __init__(self):
self.capacity = 1
self.size = 0
def potential(self):
return 2 * self.size - self.capacity
def append(self):
"""Return the actual cost: 1 per write, 1 per element copied."""
cost = 0
if self.size == self.capacity:
cost += self.size # copy every element into the new array
self.capacity *= 2
cost += 1 # write the new element
self.size += 1
return cost
arr = DynamicArray()
phi_0 = arr.potential()
total_actual = 0
total_amortised = 0
for i in range(1, 1001):
before = arr.potential()
actual = arr.append()
amortised = actual + arr.potential() - before
assert amortised == 3, (i, amortised)
total_actual += actual
total_amortised += amortised
# Telescoping: sum(amortised) = sum(actual) + Φ(D_n) − Φ(D_0)
assert total_amortised == total_actual + arr.potential() - phi_0
assert total_actual <= total_amortised
print(total_actual, total_amortised) # 2023 3000The array doubles at sizes 1, 2, 4, …, 512, which copies 1023 elements in total. Adding 1000 writes gives an actual cost of 2023. The amortised total is 3000 and Φ at the end is 2000 − 1024 = 976. The identity checks out: 2023 + 976 − (−1) = 3000.
Relationship to the accounting method
The two methods are the same bookkeeping seen from different sides. In the accounting method you attach credit to particular elements, such as "each element in the second half pays for itself and one earlier element". In the potential method you keep one total, Φ, that is a function of the state alone. Φ is the sum of all credit currently stored.
The potential method is usually easier to apply when no single element obviously owns the credit. You only need a formula over the state, such as size and capacity, and algebra does the rest.
Another example: a binary counter
The method is not specific to arrays. Take a binary counter stored as an array of bits, and let each bit flip cost 1. Increment flips t trailing 1s to 0 and one 0 to 1. The actual cost is t + 1.
Choose Φ = the number of 1 bits in the counter. This is 0 at the start and never negative. An increment removes t ones and adds one, so ΔΦ = 1 − t. The amortised cost is (t + 1) + (1 − t) = 2. Any n increments from zero cost at most 2n flips, even though a single increment can flip every bit.
Generalising to other growth factors
Suppose the array grows by a factor g > 1 instead of 2. Ignore rounding of capacities. The potential:
Φ = (g / (g − 1)) · size − (1 / (g − 1)) · capacityhas the same two properties as before. It equals C when the array is full at capacity C, and after a resize it is g/(g−1). Redoing the case analysis gives an amortised cost of 1 + g/(g−1) for both cases.
| Growth factor g | Amortised cost per append |
|---|---|
| 1.5 | 4 |
| 2 | 3 |
| 4 | about 2.33 |
A larger factor lowers the amortised cost but leaves more unused capacity after each resize. The growth-strategy chapter treats that memory trade-off.
For additive growth, such as adding 16 slots at a time, no potential of this form can work. A resize still copies about C elements, but the array gains only 16 slots of room before the next resize. The potential would have to rise by about C/16 per append, so the amortised cost grows with n.
Finding a potential
When you meet a new structure, work through these steps:
- Write the actual cost of each operation in each case: cheap and expensive.
- Ask which state quantity is large just before an expensive operation and small just after it. For arrays it is the fill level relative to capacity. For counters it is the number of 1 bits.
- Build Φ from that quantity, with coefficients that make the expensive operation's cost cancel against the drop in Φ.
- Check that Φ(Dn) ≥ Φ(D0) for every reachable state.
- Compute the amortised cost for each case and confirm they are all bounded by a constant.
Common errors
- Skipping the Φ(Dn) ≥ Φ(D0) check. If Φ can fall below its starting value, the amortised total no longer bounds the actual total.
- Reading the amortised cost as a per-call bound. The resize append has an actual cost of C + 1. Its amortised cost of 3 is only an accounting figure. Latency-sensitive code still sees the full spike.
- Using a potential that fails in one case. If the amortised costs of the two cases differ, or one grows with n, the potential does not fit. Rework the coefficients.
- Ignoring shrink operations. The potential 2·size − capacity goes negative once deletions leave the array less than half full. Supporting removals needs a different potential and a careful shrinking rule, which is the subject of a later chapter.
Exercises
Exercise
An array has capacity 8 and size 8, and Φ = 2·size − capacity. An append triggers a resize to 16. State the actual cost, Φ before, Φ after, and the amortised cost.
Show answer
The actual cost is 8 copies + 1 write = 9. Φ before is 2·8 − 8 = 8. After the append, size is 9 and capacity is 16, so Φ is 18 − 16 = 2. The amortised cost is 9 + 2 − 8 = 3.
Exercise
The array grows by a factor of 4 instead of 2, using Φ = (4/3)·size − (1/3)·capacity. What is the amortised cost of an append that does not resize, and what is it for one that does (with capacity C and size C before the append)?
Show answer
With no resize, the actual cost is 1 and ΔΦ = 4/3, so the amortised cost is 7/3. With a resize, the actual cost is 1 + C. Φ before is (4C − C)/3 = C. Φ after is (4(C+1) − 4C)/3 = 4/3, so ΔΦ = 4/3 − C. The amortised cost is 1 + C + 4/3 − C = 7/3. Both cases give 7/3, about 2.33.
Exercise
A binary counter uses Φ = number of 1 bits. Incrementing 0111 gives 1000. What are the actual cost, ΔΦ and the amortised cost?
Show answer
The actual cost is 4 flips: three 1s become 0 and one 0 becomes 1. The potential goes from 3 to 1, so ΔΦ = −2. The amortised cost is 4 − 2 = 2.