Sub topic 6
Why growth must be geometric
This chapter assumes you know Big-O notation, fixed-size arrays, and the doubling dynamic array from the earlier chapters. It also assumes you can read short Python programs. The question here is why the new capacity must be a multiple of the old one, and why adding a fixed number of slots fails.
The two strategies
When an array is full, a resize allocates a bigger array and copies every element across. The strategy decides how much bigger.
- Geometric growth multiplies the capacity by a constant factor g > 1. Doubling is g = 2.
- Additive growth adds a constant k slots, so the capacity goes from c to c + k.
Both strategies make the same number of writes. They differ in how often they copy, and in how much each copy costs.
Measuring the difference
The function below charges 1 unit per write and 1 unit per element copied. You pass in the growth rule as a function.
def total_cost(n, grow):
"""Total cost of n appends: 1 per write plus 1 per element copied on resize."""
size, capacity, cost = 0, 1, 0
for _ in range(n):
if size == capacity:
cost += size # copy every element into the new array
capacity = grow(capacity)
cost += 1 # write the new element
size += 1
return cost
for n in (1000, 10000, 100000):
doubling = total_cost(n, lambda c: 2 * c)
additive = total_cost(n, lambda c: c + 16)
assert doubling <= 3 * n # at most 3 per append amortised
print(n, doubling / n, additive / n)The cost per append comes out as follows.
| n | Doubling | Add 16 |
|---|---|---|
| 1,000 | 2.02 | 32.3 |
| 10,000 | 2.64 | 313.1 |
| 100,000 | 2.31 | 3125.6 |
The doubling column stays below 3. The additive column grows roughly ten times for every ten-fold increase in n.
Why the numbers behave this way
Additive growth. Resizes happen every k appends. The i-th resize copies about i·k elements. Over n appends there are about n/k resizes, so the total copying is
k · (1 + 2 + … + n/k) ≈ n² / (2k).
Dividing by n gives about n / (2k) per append. For k = 16 that is n/32, which matches the table. Any fixed k gives O(n) amortised cost per append, so a bigger constant only delays the problem. The total is Θ(n²).
Geometric growth. The capacities before each resize are c, c·g, c·g², and so on. A resize copies as many elements as the old capacity. These copy counts form a geometric series dominated by its last term, so the total is a constant multiple of n. The last resize happens at a size below n, so
total copies < n · g / (g − 1).
For g = 2 this is 2n, and the earlier chapters showed it with the aggregate method. Adding the n writes gives under 3n total, or O(1) per append. For g = 1.5 the copy bound is 3n.
The trade-off in the factor
A smaller g means fewer wasted slots but more copying. A larger g does the reverse.
- g = 2 wastes up to half the array and copies at most about 2n elements in total.
- g = 1.5 wastes up to a third and copies at most about 3n.
- g close to 1 pushes the copying cost toward infinity, since g / (g − 1) grows without bound. A factor of 1.01 is still geometric but impractical.
Real libraries pick a factor between about 1.125 and 2 depending on their memory and speed priorities.
Common errors
- Treating additive growth as "close enough" because k is large. For large n, n/(2k) still dominates.
- Forgetting that the cost of one resize is proportional to the current size, not constant.
- Choosing g ≤ 1, which never makes room. Integer rounding can cause this with small capacities, for example
int(1 * 1.5)is 1. Implementations guard against it with a minimum growth of at least one slot.
Exercise
An array grows by a factor of 1.5 each time it is full. Give an upper bound on the total number of element copies over n appends, and say what that means for the amortised cost per append.
Show answer
Each resize copies as many elements as the old capacity. These capacities form a geometric series with ratio 1.5, so their sum is less than the last capacity times 1 / (1 − 1/1.5) = 3. The last resize happens at a size below n, so total copies are under 3n. Adding n writes gives under 4n total cost, so the amortised cost per append is O(1), at most about 4 units.