Sub topic 5
Potential method: a stored-energy function
This chapter assumes you know the aggregate and accounting methods from the earlier chapters, and that you can read short Python programs. The potential method proves the same bound as the accounting method. It keeps the stored credit in one function of the whole data structure instead of attaching it to individual elements.
The idea
Pick a function Φ that maps each state of the data structure to a number. Think of it as stored energy. Cheap operations raise Φ, and expensive operations spend it.
For the i-th operation, with actual cost c_i, the amortised cost is:
ĉi = ci + Φ(Di) − Φ(D{i−1})
Here D_i is the structure after operation i. Summing over n operations, the Φ terms cancel in pairs:
Σ ĉi = Σ ci + Φ(Dn) − Φ(D0)
If Φ(Dn) ≥ Φ(D0) for every possible final state, then the sum of amortised costs is an upper bound on the sum of actual costs. Bounding each ĉ_i by a constant therefore bounds the total.
Choosing Φ for a doubling array
Let the array hold size elements in capacity slots. Charge 1 per write and 1 per element copied. Use:
Φ = 2 · size − capacity
Right after a resize the array is half full, so Φ is small. As the array fills, Φ rises toward capacity. When the array is full, Φ equals size, which is exactly the number of copies the next resize needs.
Append with no resize
The actual cost is 1. Size grows by 1, so Φ rises by 2. The amortised cost is 1 + 2 = 3.
Append that triggers a resize
Before the append, size = capacity = k.
- Actual cost: 1 + k (copy k elements, write one).
- Φ before: 2k − k = k.
- Φ after: size k + 1 and capacity 2k, so 2(k + 1) − 2k = 2.
- Amortised cost: (1 + k) + (2 − k) = 3.
The k copies are paid for by the drop in Φ. Every append has amortised cost exactly 3, so n appends cost at most 3n.
Checking it in code
This script tracks Φ and asserts that every append has amortised cost 3.
def potential(size, capacity):
return 2 * size - capacity
size, capacity = 0, 1
total_actual = 0
n = 1000
for _ in range(n):
phi_before = potential(size, capacity)
actual = 1 # write the new element
if size == capacity:
actual += size # copy every element on resize
capacity *= 2
size += 1
amortised = actual + potential(size, capacity) - phi_before
assert amortised == 3
total_actual += actual
assert total_actual <= 3 * n
print(total_actual) # 2023The array starts with capacity 1 and size 0, so Φ(D_0) = −1. That is fine here because every later state has Φ ≥ −1. After any resize the array is more than half full, so 2 · size − capacity is positive.
Common errors
- Forgetting the initial state. Always compute Φ(D_0) and check that no later state falls below it.
- A Φ that does not drop on the expensive step. If Φ stays the same during a resize, the amortised cost of that append is k + 1, not a constant.
- Mixing cost units. Φ must use the same units as the actual cost. Here both count element writes and copies.
- Treating Φ as something the program stores. It exists only in the proof. The code never computes it, apart from checks like the one above.
Why use it
For a doubling array, the aggregate and accounting methods are just as easy. The potential method pays off when credit has no natural home on a single element. The shrinking rule in a later chapter is an example. There you need a Φ that is large when the array is nearly full or nearly empty. Finding such a function is the creative step.
Exercise
Using Φ = 2 · size − capacity, an array has size 8 and capacity 8. Compute the actual cost, the change in potential, and the amortised cost of the next append.
Show answer
The array is full, so the append copies 8 elements and writes 1, giving an actual cost of 9. Φ before is 2·8 − 8 = 8. After the append, size is 9 and capacity is 16, so Φ is 18 − 16 = 2. The change is 2 − 8 = −6. The amortised cost is 9 + (−6) = 3.