Sub topic 12
Expected cost of lookups and inserts
This chapter assumes you know Big-O notation, the difference between average and worst-case cost, and the chaining and linear probing tables from the earlier chapters. Here you will put numbers on how fast each design is, using the load factor α (keys stored divided by slots).
The assumption behind the numbers
Every formula below assumes the hash function spreads keys uniformly and independently over the slots. Under that assumption the cost depends on α and not on the number of keys n. That is why a well-resized hash table is described as O(1) on average.
The worst case is different. If every key lands in one slot, a lookup is O(n) for both strategies. Expected cost describes typical behavior, not a guarantee.
Cost with separate chaining
With chaining, a lookup hashes the key, goes to one slot, and scans that chain. The average chain holds α entries.
- Unsuccessful lookup (key absent, also the cost of an insert that must check for duplicates): about 1 + α steps. The 1 is the slot itself, and α is the chain you scan.
- Successful lookup: about 1 + α/2 steps. On average you find the key halfway down the chain.
Cost grows linearly with α. Even at α = 1.0 you do about two steps.
Cost with open addressing
With open addressing, a lookup follows a probe sequence until it finds the key or an empty slot. A full table is a problem, so α must stay below 1. The cost rises sharply as α approaches 1.
For linear probing, Knuth's classic results give these expected probe counts:
- Unsuccessful: ½ · (1 + 1/(1 − α)²)
- Successful: ½ · (1 + 1/(1 − α))
The squared term comes from primary clustering. Runs of occupied slots grow and merge, so a new key is more likely to land in a long run.
For an idealized probe sequence with no clustering (double hashing approximates this), the counts are 1/(1 − α) for unsuccessful and (1/α) · ln(1/(1 − α)) for successful lookups.
Computing the table
import math
def chaining(a):
return 1 + a, 1 + a / 2
def linear(a):
return 0.5 * (1 + 1 / (1 - a) ** 2), 0.5 * (1 + 1 / (1 - a))
def uniform(a):
return 1 / (1 - a), math.log(1 / (1 - a)) / a
for a in (0.5, 0.75, 0.9):
for name, f in (("chaining", chaining), ("linear", linear), ("uniform", uniform)):
miss, hit = f(a)
print(f"alpha={a:<5} {name:<9} miss={miss:6.2f} hit={hit:5.2f}")The results, rounded:
| α | Chaining miss / hit | Linear probing miss / hit | Double hashing miss / hit |
|---|---|---|---|
| 0.50 | 1.50 / 1.25 | 2.50 / 1.50 | 2.00 / 1.39 |
| 0.75 | 1.75 / 1.38 | 8.50 / 2.50 | 4.00 / 1.85 |
| 0.90 | 1.90 / 1.45 | 50.50 / 5.50 | 10.00 / 2.56 |
Reading the curves
Both strategies are cheap at low load. Past α ≈ 0.7 the open-addressing curves climb steeply, while chaining stays nearly flat.
This explains the resize thresholds in practice. Chaining tables can run at α around 1.0. Open-addressing tables resize much earlier. CPython's dict, for example, grows when it is about two-thirds full.
Common errors
- Quoting O(1) without naming the conditions: it needs uniform hashing and a bounded α, which resizing provides.
- Using the successful-lookup formula to judge insert cost. An insert must first confirm the key is absent, so it follows the unsuccessful cost.
- Forgetting that tombstones count toward probe length. Many deletions can make probe sequences long even when α for live keys looks low.
Exercise
A linear probing table is two-thirds full (α = 2/3). Using Knuth's formula, how many probes does an unsuccessful lookup need on average, and how does that compare with chaining at the same load?
Show answer
Linear probing: ½ · (1 + 1/(1 − 2/3)²) = ½ · (1 + 9) = 5 probes. Chaining: 1 + 2/3 ≈ 1.67 steps. Linear probing needs about three times as many.