Sub topic 12
Amortized analysis: why dynamic array appends are cheap
This chapter assumes you know Big O, worst-case analysis, and how a dynamic array (Python list, Java ArrayList, C++ vector) stores items in one block of memory.
The puzzle
A dynamic array has a fixed-size block behind it. When the block is full, an append must allocate a larger block and copy every existing element. That one append costs O(n). Yet the documentation for these structures says append is O(1). Both claims are true, and amortized analysis explains how.
Amortized analysis bounds the average cost per operation over a worst-case sequence of operations. It is not an average over random inputs, and it uses no probability. The guarantee holds for any sequence of n operations.
What happens during appends
Suppose the array doubles its capacity when full, starting at capacity 1.
Resizes occur when the size is 1, 2, 4, 8, and so on. Each one copies that many elements, so the copy costs are powers of two. They become rare as the array grows.
The aggregate method
Add up the total cost of n appends and divide by n.
- Writes: n appends write n items, so n steps.
- Copies: the resizes copy 1 + 2 + 4 + ... + 2^k elements, where 2^k < n is the last size at which a resize happened. This geometric sum equals 2^(k+1) − 1, which is less than 2n.
The total is under n + 2n = 3n steps. Dividing by n gives under 3 steps per append, so append is O(1) amortized.
Checking it in code
def count_copies(appends):
capacity, size, copies = 1, 0, 0
for _ in range(appends):
if size == capacity:
copies += size # resize: copy every existing element
capacity *= 2
size += 1
return copies
for n in (10, 1000, 100000):
print(n, count_copies(n)) # total copies stay below 2 * nThe output is:
10 15
1000 1023
100000 131071Each count is below 2n (20, 2000, 200000). The copies per append stay bounded as n grows 10,000-fold.
The accounting method
Another view charges each append a fixed price of 3 "coins". One coin pays for writing the item. The other two are saved on that item. When a resize happens, the array is full. At least half of its items were added since the last resize, and each carries two saved coins. That is enough to pay for copying every item. The balance never goes negative, so 3 per append is a valid amortized cost.
Common errors
- Confusing amortized with average case. Average case assumes a distribution of inputs. Amortized cost holds for every sequence, including adversarial ones.
- Believing every append is fast. A single append can still cost O(n). That matters for latency-sensitive code such as real-time loops, even when throughput is fine.
- Growing by a constant. If the array grows by a fixed amount, say 10 slots, the number of resizes is proportional to n and each copy is proportional to n. The total becomes O(n²). Growth must be multiplicative. The factor does not have to be 2. CPython lists grow by roughly 12.5% plus a constant, and appends are still O(1) amortized, though with a larger constant.
- Ignoring other operations. The argument covers appends only. Inserting at the front of an array is O(n) every time, with no amortized discount.
Exercise
A dynamic array grows from capacity c to c + 100 whenever it is full, instead of doubling. Is append still O(1) amortized? Why or why not?
Show answer
No. Resizes happen every 100 appends, so n appends cause about n/100 resizes. The k-th resize copies about 100k elements, so total copying is roughly 100 · (1 + 2 + ... + n/100), which is proportional to n². The amortized cost per append is O(n). Growth must scale by a constant factor greater than 1 to give O(1) amortized.