Sub topic 1
Three Ways to Talk About Cost: Worst Case, Average Case, Amortised
Prerequisites
This chapter assumes you know:
- Big-O notation and basic asymptotic reasoning.
- How fixed-size arrays are allocated and indexed.
- What a hash table is, at the level of "a hash function picks a bucket".
- How to read short Python programs.
Why one number is not enough
Saying an operation "costs O(1)" or "costs O(n)" hides a question: cost over what? A single call, a typical call, or a long run of calls? Three different answers exist, and they are not interchangeable.
Take appending to a dynamic array. Most appends write one element into spare room. Occasionally the array is full, so the append allocates a bigger array and copies every element. The same operation is cheap almost always and expensive rarely. Each cost model describes that pattern differently.
Worst-case cost
Worst-case cost is the most one single operation can ever cost, over all inputs and all states.
For a dynamic array, the worst single append copies all n elements, so its worst case is O(n). This bound is a hard guarantee for each call. It is also pessimistic here, because the expensive call cannot happen twice in a row.
Average-case (expected) cost
Average-case cost, also called expected cost, averages over a probability distribution. The randomness comes from the input or from the algorithm's own random choices.
A hash table lookup is the standard example. If keys hash uniformly, a chain holds about the load factor α = n/m entries on average, so a lookup costs O(1 + α) in expectation. This claim is only as good as its assumption. If an adversary picks keys that all collide, the assumption fails and the bound goes with it.
Amortised cost
Amortised cost is the worst-case total cost of any sequence of n operations, divided by n. No probability is involved and no assumption about the input is made. It holds for every sequence.
For the dynamic array, any n appends cost O(n) in total. So each append is O(1) amortised, even though one of them may cost O(n).
Comparing the three
| Model | Averages over | Needs assumptions about input? | Guarantee applies to |
|---|---|---|---|
| Worst case | Nothing, takes the maximum | No | Every single operation |
| Average case | A probability distribution | Yes | Expected value of one operation |
| Amortised | A sequence of operations | No | Total cost of any sequence |
Amortised and average-case cost both say "average", but they average over different things. Amortised analysis averages over time within one worst-case sequence. Average-case analysis averages over possible inputs or random choices. A later chapter returns to this difference for hash tables.
Seeing it in code
This program counts the cost of each append to a doubling array. The cost is 1 for the write plus 1 for each element copied.
def append_costs(n):
size, capacity = 0, 1
costs = []
for _ in range(n):
cost = 1 # write the new element
if size == capacity:
cost += size # copy every element into a bigger array
capacity *= 2
size += 1
costs.append(cost)
return costs
costs = append_costs(1000)
print("worst single append:", max(costs))
print("total cost:", sum(costs))
print("amortised per append:", sum(costs) / len(costs))The worst single append costs several hundred units, yet the amortised cost per append stays below 3. The worst case describes one call. The amortised cost describes the whole run.
Common mistakes
- Treating "amortised O(1)" as "each call is O(1)". It is a bound on the average over a sequence.
- Confusing amortised with average-case. Amortised needs no randomness or input assumption.
- Quoting a hash table's O(1) lookup without saying whether it is expected or worst case.
Exercise
A data structure performs 100 operations. 99 cost 1 unit each and one costs 100 units. State the worst-case cost of a single operation, and the amortised cost per operation for this sequence.
Show answer
The worst case for a single operation is 100 units. The total is 99 + 100 = 199 units, so the amortised cost over these 100 operations is 199 / 100 = 1.99 units, which is O(1) if the pattern holds for every sequence.
Exercise
A hash table lookup is described as "O(1) expected". Which assumption does this depend on, and how does it differ from an amortised bound?
Show answer
It depends on keys being spread roughly uniformly over the buckets, for example through a good hash function. An amortised bound needs no such assumption. It holds for every sequence of operations, while the expected bound averages over random inputs or random choices.