Sub topic 10
Expected versus amortised: two different averages
This chapter assumes you know the aggregate method for a doubling array, how a chained hash table with a load factor works, and basic Python. Both "expected" and "amortised" cost are averages, but they average over different things. Mixing them up leads to wrong claims about hash tables.
What each average ranges over
An amortised bound averages over a sequence of operations. It holds for every sequence, including one chosen by an adversary. No probability is involved. If a doubling array does n appends, the total cost is at most 3n. That is a guarantee about the worst sequence.
An expected bound averages over randomness. For a hash table, the randomness is the choice of hash function. The guarantee says that for any fixed set of keys, a hash function drawn at random from a suitable family gives short chains on average. The input is fixed and the dice are the hash function.
| Row | Amortised | Expected |
|---|---|---|
| Averages over | A sequence of operations | Random choices (the hash function) |
| Assumes about input | Nothing | Nothing, if the hash function is random enough |
| Holds for | Every sequence | Most hash functions, not every one |
| Typical statement | n appends cost O(n) in total | A lookup scans O(1 + α) entries, where α is the load factor |
Both appear in one hash table insertion
Insertion into the resizing table from the previous chapter has two cost sources.
- Chain scanning. The insert walks one bucket looking for the key. With a random hash function and load factor α ≤ 0.75, the expected chain length is at most α, so the expected scan cost is O(1).
- Rehashing. When the load factor passes 0.75, every entry is moved. This cost is amortised, using the same geometric argument as for arrays. Rehashing costs O(n) every so often and O(1) per insert when spread over the sequence.
The combined claim is "O(1) amortised expected time". The word "amortised" covers the resize. The word "expected" covers the hash function. Neither alone covers both.
Seeing the difference
This program counts steps: chain entries scanned during an insert, plus entries moved during a rehash. It runs the same table with a spreading hash function and with a hash function that sends every key to bucket 0.
def total_steps(keys, hash_fn):
buckets = [[] for _ in range(8)]
size, steps = 0, 0
for k in keys:
bucket = buckets[hash_fn(k) % len(buckets)]
steps += len(bucket) # scan the chain
bucket.append(k)
size += 1
if size > 0.75 * len(buckets):
new = [[] for _ in range(2 * len(buckets))]
for old in buckets:
for x in old:
new[hash_fn(x) % len(new)].append(x)
steps += 1 # move one entry
buckets = new
return steps
for n in (500, 1000, 2000):
good = total_steps(range(n), lambda k: k)
bad = total_steps(range(n), lambda k: 0)
print(n, good / n, bad / n)The first column of ratios stays at a small constant. The second grows in proportion to n, at roughly n/2 steps per insert. The resize logic is identical in both runs, so the amortised argument for rehashing holds in both. What fails in the second run is the expected-chain-length argument, because the hash function is not spreading keys.
Why the distinction matters in practice
- Adversarial keys. If an attacker can predict your hash function, they can craft colliding keys. The expected bound is gone while the amortised resizing bound remains. This is why many language runtimes randomise their string hashing per process.
- Fixed versus random. A deterministic hash function such as the identity function on integers has no randomness. The expected bound then depends on assumptions about the keys, not on a proof.
- Reporting. Say "O(1) amortised" for a dynamic array. Say "O(1) expected" for a hash lookup. Say "O(1) amortised expected" for a resizing hash table insert.
Exercise
A chained hash table never resizes, so its load factor grows as keys are inserted. Which guarantee is lost, the amortised one or the expected one? What happens to the cost per insert?
Show answer
Neither averaging argument is lost, but the expected chain length α grows with n, so the expected scan cost per insert is O(n/B), which is O(n) for a fixed bucket count B. There is no resize cost to amortise, so the problem is that the load factor is unbounded and the O(1) expected bound no longer holds. Resizing is what keeps α bounded.