Sub topic 10
Why Doubling Gives Amortized O(1) Inserts
This chapter assumes you know Big-O notation, can read simple Python, and have seen a hash table that resizes when its load factor passes a threshold. Here you will see why an occasional O(n) resize does not make inserts slow on average.
The apparent problem
A single put is usually cheap. It hashes the key, finds a bucket, and appends. But when the load factor passes the threshold, _resize rehashes every entry into a new array. That one insert costs O(n).
If any insert can cost O(n), a loop of n inserts looks like it could cost O(n²). It does not, because resizes become rarer as the table grows.
Amortized cost
Amortized cost is the total cost of a sequence of operations divided by the number of operations. It is an average over a worst-case sequence, not a probabilistic average. No randomness or lucky input is involved.
Counting the moves
Take a starting capacity of 8 and a threshold of 0.75. A resize happens when the size passes 6, then 12, then 24, and so on. The capacity doubles each time, so the size that triggers the next resize also doubles.
Each resize moves every entry once. The moves form a geometric series:
- Resize at size 7 moves 7 entries.
- Resize at size 13 moves 13 entries.
- Resize at size 25 moves 25 entries.
Each trigger size is roughly twice the previous one. The sum of a series that doubles is less than twice its last term. The last resize happens at a size no larger than n, so the total moves are below about 2n. Spread over n inserts, that is a constant number of moves per insert.
def total_rehash_moves(n, start_capacity=8, max_load=0.75):
capacity, size, moves = start_capacity, 0, 0
for _ in range(n):
size += 1
if size / capacity > max_load:
moves += size
capacity *= 2
return moves
for n in (1_000, 10_000, 100_000):
print(n, total_rehash_moves(n) / n)The ratio of moves to inserts stays near a constant as n grows by factors of ten. With these settings it settles at roughly 1.3 to 2, not at a value that grows with n. A constant ratio is the amortized O(1) claim.
The accounting view
You can also reason with credits. Charge each insert a fixed amount, for example 3 units. One unit pays for the insert itself. The other two are saved. After a resize, the table has room for about as many new entries as it already holds. By the time the next resize arrives, those new entries have saved enough credit to pay for moving all entries, old and new. The fixed charge covers every resize, so each insert is O(1) amortized.
Why the growth must be multiplicative
The argument depends on multiplying the capacity. Compare two policies:
| Policy | Resizes for n inserts | Total moves | Amortized per insert |
|---|---|---|---|
| Double the capacity | about log₂ n | O(n) | O(1) |
| Add a fixed 100 slots | about n / 100 | O(n²) | O(n) |
Adding a constant makes resizes stay frequent. Each one still moves a table of growing size, so the total is quadratic. Any constant growth factor above 1, such as 1.5, keeps the geometric series and gives O(1). A smaller factor means more resizes but less wasted memory.
Gotchas
- The bound needs the resize threshold to scale with capacity. Resizing at a fixed size, such as 1,000 entries, breaks it.
- Rehashing must be linear. If your hash function or bucket scan is slow, the constant grows, but the shape stays the same.
- Amortization assumes the table only grows. Mixed inserts and deletes need a shrinking policy that avoids repeated grow and shrink cycles.
Exercise
A table starts at capacity 8 and, when it resizes, adds 8 slots instead of doubling. Why is insertion no longer amortized O(1)?
Show answer
Resizes occur every 8 or so inserts, regardless of table size. Each resize moves all current entries, so the k-th resize costs about 8k. Summing over n/8 resizes gives O(n²) total moves, or O(n) per insert on average. Growing by a constant factor, such as doubling, is what makes the total linear.