Analysis of Open Addressing
How many probes does an operation on an open-addressing table cost? With chaining the answer was ; with open addressing it is , which is about the same for small but explodes as the table fills up. This lesson proves the bound under the idealized model of uniform hashing, derives the cost of successful searches, and compares both with what linear probing and double hashing really do.
Uniform hashing
The model for open addressing is a stronger version of simple uniform hashing: the probe sequence of each key is equally likely to be any of the permutations of the slots, independently for different keys. No practical scheme achieves this (double hashing has only different sequences, linear probing only ), but double hashing comes close, as the experiments below show. As before, keys are stored in slots, , and there are no deletions.
Unsuccessful search and insertion
Theorem. Under uniform hashing, the expected number of probes in an unsuccessful search is at most
Proof. Let be the number of probes. A search makes at least probes exactly when its first probes all hit occupied slots. The first probe hits an occupied slot with probability . Given that, the second probe goes to one of the other slots, uniformly, and hits one of the other keys with probability , and so on:
because when . For a random variable with values the expectation is the sum of its tail probabilities, so
The bound has a clean meaning: each probe succeeds in finding an empty slot with probability at least , so it is the mean of a geometric distribution. Inserting a new key costs the same as an unsuccessful search for it (it stops at the same empty slot), so insertion also takes at most expected probes.
Successful search
Theorem. Under uniform hashing, with every stored key equally likely to be searched for, a successful search makes at most
probes in expectation.
Proof. A search for key retraces the probes of its insertion. If was the -st key inserted, there were keys in the table then, so by the previous theorem its insertion took at most expected probes. Averaging over the keys:
using that for a decreasing function.
What the bounds mean
import math
print(f"{'alpha':>6} {'unsuccessful 1/(1-a)':>21} {'successful (1/a)ln(1/(1-a))':>28} {'chaining 1+a':>13}")
for a in [0.25, 0.5, 0.75, 0.9, 0.95, 0.99]:
print(f"{a:>6} {1 / (1 - a):>21.2f} {math.log(1 / (1 - a)) / a:>28.2f} {1 + a:>13.2f}") alpha unsuccessful 1/(1-a) successful (1/a)ln(1/(1-a)) chaining 1+a
0.25 1.33 1.15 1.25
0.5 2.00 1.39 1.50
0.75 4.00 1.85 1.75
0.9 10.00 2.56 1.90
0.95 20.00 3.15 1.95
0.99 100.00 4.65 1.99
At half full, an unsuccessful search makes at most 2 probes and a successful one about 1.39. At 90 percent the unsuccessful search needs up to 10. That is why open-addressing tables resize long before they are full: Python's dict keeps at most , and abseil's "Swiss tables" in C++ allow , but only together with a trick that checks 16 slots per probe with one SIMD instruction. Successful searches stay cheap even when the table is nearly full, because most keys were inserted while it was still emptier.
Checking the model against real probing
The program fills a table of slots with random keys using linear probing and double hashing, and measures the average probes for successful searches (all stored keys) and unsuccessful ones (2000 absent keys). For linear probing it also prints Knuth's classic approximations, for a successful and for an unsuccessful search, which account for primary clustering.
import math, random
def build(probe, m, keys):
slots = [None] * m
for k in keys:
step = 1 + k % (m - 1)
j = k % m
while slots[j] is not None:
j = (j + (1 if probe == "linear" else step)) % m
slots[j] = k
return slots
def probes(probe, slots, k):
m = len(slots)
step, j, count = 1 + k % (m - 1), k % m, 1
while slots[j] is not None and slots[j] != k:
j = (j + (1 if probe == "linear" else step)) % m
count += 1
return count
rng = random.Random(10)
m = 1009
print(f"{'alpha':>5} | {'double hashing':^15} | {'uniform bound':^15} | {'linear probing':^15} | {'Knuth, linear':^15}")
print(f"{'':>5} | {'succ':>7} {'unsucc':>7} | {'succ':>7} {'unsucc':>7} | {'succ':>7} {'unsucc':>7} | {'succ':>7} {'unsucc':>7}")
for a in [0.5, 0.75, 0.9]:
n = int(a * m)
keys = rng.sample(range(0, 10 ** 6, 2), n) # even keys are stored ...
absent = [rng.randrange(0, 10 ** 6, 2) + 1 for _ in range(2000)] # ... odd keys are absent
row = []
for probe in ["double", "linear"]:
slots = build(probe, m, keys)
row.append(sum(probes(probe, slots, k) for k in keys) / n)
row.append(sum(probes(probe, slots, k) for k in absent) / len(absent))
al = n / m
u_s, u_u = math.log(1 / (1 - al)) / al, 1 / (1 - al)
k_s, k_u = (1 + 1 / (1 - al)) / 2, (1 + 1 / (1 - al) ** 2) / 2
print(f"{a:>5} | {row[0]:>7.2f} {row[1]:>7.2f} | {u_s:>7.2f} {u_u:>7.2f} | {row[2]:>7.2f} {row[3]:>7.2f} | {k_s:>7.2f} {k_u:>7.2f}")alpha | double hashing | uniform bound | linear probing | Knuth, linear
| succ unsucc | succ unsucc | succ unsucc | succ unsucc
0.5 | 1.35 1.97 | 1.39 2.00 | 1.56 2.46 | 1.50 2.50
0.75 | 1.83 4.03 | 1.85 3.99 | 2.43 7.73 | 2.49 8.45
0.9 | 2.67 9.65 | 2.56 9.99 | 4.27 23.26 | 5.50 50.40
Double hashing tracks the uniform-hashing bounds closely. Linear probing is much worse for unsuccessful searches at high load: at it makes about 23 probes where double hashing makes fewer than 10. That is primary clustering in numbers. Knuth's formulas are limits for large tables; at with only 1009 slots, the measurement depends heavily on a few long runs and varies a lot between tables (change the seed to see it), while at lower loads it agrees well.
Why linear probing's unsuccessful search grows like $1/(1-\alpha)^2$ (intuition)
An unsuccessful search starting at a random slot walks to the end of the run containing that slot. A run of length is hit with probability proportional to , so the expected walk is proportional to , not to : long runs are both longer and more likely to be hit. Under linear probing run lengths have a heavy tail, and the computation (due to Knuth, 1963) gives the approximation. With truly random probes there are no runs, and the cost is the geometric .
Test yourself
Under uniform hashing, what is the bound on the expected probes of an unsuccessful search at ?
- 1.8
- 2
- 5
- 25
Why does a successful search cost less than an unsuccessful one at the same load factor?
- Because successful searches stop at an empty slot
- Because a search for a stored key retraces its insertion, and most keys were inserted when the table was emptier
- Because stored keys are sorted
- It does not; both cost
In the proof of the bound, which fact gives ?
- Linearity of expectation over indicators
- Markov's inequality
- The birthday paradox
- Independence of the probes
At , about how many probes does an unsuccessful search make with linear probing, by Knuth's approximation?
- about 5
- about 10
- about 50
- about 100