Sub topic 3
Aggregate method: totalling the cost of n appends
This chapter assumes you know Big-O notation, how fixed-size arrays are allocated, and how to read short Python programs. It also assumes you have seen the doubling dynamic array from the previous chapter.
The idea
The aggregate method is the simplest way to prove an amortised bound. You pick a sequence of n operations and compute the total cost T(n) of the whole sequence in the worst case. The amortised cost per operation is then T(n) / n.
Every operation gets the same amortised cost, even when individual operations differ a lot. The method makes no assumption about the input, so it is a worst-case guarantee over sequences, not a probabilistic claim.
Setting up the cost
Take a dynamic array that starts with capacity 1 and doubles when full. Count one unit for each element written and one unit for each element copied during a resize.
An append at size k costs 1 unless k equals the current capacity. In that case the array first copies k elements into a new array of capacity 2k, so the append costs k + 1.
Resizes happen when the size is 1, 2, 4, 8, and so on. For n appends, the resizes copy 1, 2, 4, ..., up to the largest power of 2 below n.
The proof
Let m be the number of resizes, so the largest copy is at most n. The total copy cost is a geometric series:
1 + 2 + 4 + ... + 2^m = 2^(m+1) - 1
Because 2^m < n, we get 2^(m+1) - 1 < 2n. The n writes add n more. So:
T(n) < n + 2n = 3n
The amortised cost per append is T(n) / n < 3, which is O(1).
Checking it by running code
The code below counts copies directly. The assertion matches the bound: copies stay below 2n.
class DynamicArray:
def __init__(self):
self._capacity = 1
self._size = 0
self._data = [None] * self._capacity
self.copies = 0 # element copies caused by resizing
def append(self, value):
if self._size == self._capacity:
self._resize(2 * self._capacity)
self._data[self._size] = value
self._size += 1
def _resize(self, new_capacity):
new_data = [None] * new_capacity
for i in range(self._size):
new_data[i] = self._data[i]
self.copies += 1
self._data = new_data
self._capacity = new_capacity
for n in (10, 100, 1000, 10000):
arr = DynamicArray()
for i in range(n):
arr.append(i)
assert arr.copies < 2 * n
print(n, arr.copies, arr.copies / n)The ratio of copies to n stays between 1 and 2 for every n. Adding the one write per append gives a total under 3 per append, as the proof says.
Common errors
- Reporting the worst single append. One append can cost n, but that does not make the sequence O(n^2). The expensive appends are rare, and their costs shrink geometrically as you look at earlier ones.
- Forgetting the writes. The copies alone are below 2n. The bound of 3n only appears once you add the n writes.
- Treating amortised as average over random input. The bound holds for every sequence of n appends, so no input distribution is involved.
- Applying it to mixed operations carelessly. The aggregate method gives one cost for all operations. If appends and other operations differ, you need the accounting or potential method to price them separately.
Exercise
Suppose the array grows by tripling instead of doubling. Using the aggregate method, give an upper bound on the total number of element copies over n appends.
Show answer
Resizes copy 1, 3, 9, ..., 3^m elements, where 3^m < n. The sum is (3^(m+1) - 1) / 2, which is less than 3n / 2. So copies are below 1.5n, and with the n writes the total is below 2.5n. The amortised cost is still O(1).