Sub topic 17
Average Case Compared with Amortized Cost
This chapter assumes you know best, worst, and average case analysis, and can read simple Python and sums. It separates two ideas that both produce a "typical" cost but rest on different guarantees.
Two kinds of "typical"
The average case averages over many possible inputs of the same size. It needs an assumption about how likely each input is, such as "every ordering is equally likely." If real inputs do not follow that assumption, the average can be wrong.
Amortized cost averages over a sequence of operations on one data structure. It makes no assumption about the input distribution. It is a worst-case guarantee for the whole sequence, divided by the number of operations.
| Row | Average case | Amortized cost |
|---|---|---|
| Averages over | Possible inputs | Operations in a sequence |
| Needs a probability assumption | Yes | No |
| Guarantee | Holds in expectation | Holds for every sequence |
| Typical example | Linear search with a uniform target | Appending to a dynamic array |
Worked example: a growing list
A dynamic array stores items in a fixed block. When the block is full, it allocates a block twice as large and copies every item. Most appends cost one write. An occasional append costs a copy of everything.
The code below counts the writes, including copies, for n appends.
def append_cost(n):
capacity = 1
size = 0
total = 0
worst = 0
for _ in range(n):
cost = 1
if size == capacity:
cost += size # copy existing items
capacity *= 2
size += 1
total += cost
worst = max(worst, cost)
return total, worst
for n in [8, 64, 1024]:
total, worst = append_cost(n)
print(n, total, worst, total / n)The total stays below 3n, so the average per append stays below 3. The worst single append, however, grows with n, because a resize copies about n items. The worst case of one operation is O(n). The amortized cost per operation is O(1).
The reason is that copies happen at sizes 1, 2, 4, 8, and so on. Their total is less than 2n, and that total is shared across n appends.
Why the difference matters
Suppose a program must respond within a fixed time on every single call. Amortized O(1) does not help, because one call in the sequence is still slow. A real-time system needs the worst case per operation. A batch job that runs many operations and cares about total time can rely on the amortized bound.
An average-case claim is weaker in another way. A sorting routine that is fast on average over random orderings can still be slow on one adversarial input. An amortized bound has no such gap, because it holds for the worst possible sequence.
Exercise
A dynamic array doubles its capacity when full. Appending one item to a full array of size 1000 copies 1000 items. Is the cost of appending described as O(1) average case, O(1) amortized, or both? Explain which assumption each needs.
Show answer
It is O(1) amortized, with no assumption about inputs, because the total over n appends is under 3n. Calling it "average case" would require assuming some distribution over inputs, and none is involved here. The single append that triggers a resize still costs O(n) in the worst case.