TExcelTAD

Analysis of Open Addressing

How many probes does an operation on an open-addressing table cost? With chaining the answer was 1+α1 + \alpha; with open addressing it is 1/(1−α)1/(1 - \alpha), which is about the same for small α\alpha 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 m!m! permutations of the slots, independently for different keys. No practical scheme achieves this (double hashing has only m2m^2 different sequences, linear probing only mm), but double hashing comes close, as the experiments below show. As before, nn keys are stored in mm slots, α=n/m<1\alpha = n/m < 1, 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

11−α\frac{1}{1 - \alpha}

Proof. Let XX be the number of probes. A search makes at least ii probes exactly when its first i−1i - 1 probes all hit occupied slots. The first probe hits an occupied slot with probability n/mn/m. Given that, the second probe goes to one of the other m−1m - 1 slots, uniformly, and hits one of the n−1n - 1 other keys with probability (n−1)/(m−1)(n-1)/(m-1), and so on:

Pr⁡[X≥i]=nm⋅n−1m−1⋯n−i+2m−i+2≤(nm)i−1=αi−1\Pr[X \ge i] = \frac{n}{m} \cdot \frac{n-1}{m-1} \cdots \frac{n-i+2}{m-i+2} \le \left(\frac{n}{m}\right)^{i-1} = \alpha^{i-1}

because (n−j)/(m−j)≤n/m(n - j)/(m - j) \le n/m when n<mn < m. For a random variable with values 1,2,…1, 2, \ldots the expectation is the sum of its tail probabilities, so

E[X]=∑i=1∞Pr⁡[X≥i]≤∑i=1∞αi−1=11−αE[X] = \sum_{i=1}^{\infty} \Pr[X \ge i] \le \sum_{i=1}^{\infty} \alpha^{i-1} = \frac{1}{1 - \alpha}

The bound has a clean meaning: each probe succeeds in finding an empty slot with probability at least 1−α1 - \alpha, 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 1/(1−α)1/(1 - \alpha) expected probes.

Successful search

Theorem. Under uniform hashing, with every stored key equally likely to be searched for, a successful search makes at most

1αln⁡11−α\frac{1}{\alpha} \ln \frac{1}{1 - \alpha}

probes in expectation.

Proof. A search for key kk retraces the probes of its insertion. If kk was the (i+1)(i+1)-st key inserted, there were ii keys in the table then, so by the previous theorem its insertion took at most 1/(1−i/m)=m/(m−i)1/(1 - i/m) = m/(m - i) expected probes. Averaging over the nn keys:

1n∑i=0n−1mm−i=1α∑j=m−n+1m1jsubstitute j=m−i≤1α∫m−nmdxx=1αln⁡mm−n=1αln⁡11−α\begin{aligned} \frac{1}{n} \sum_{i=0}^{n-1} \frac{m}{m - i} &= \frac{1}{\alpha} \sum_{j=m-n+1}^{m} \frac{1}{j} && \text{substitute } j = m - i \\ &\le \frac{1}{\alpha} \int_{m-n}^{m} \frac{dx}{x} = \frac{1}{\alpha} \ln \frac{m}{m - n} = \frac{1}{\alpha} \ln \frac{1}{1 - \alpha} \end{aligned}

using that 1/j≤∫j−1jdx/x1/j \le \int_{j-1}^{j} dx/x for a decreasing function.

What the bounds mean

python · try it yourself
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 α\alpha at most 2/32/3, and abseil's "Swiss tables" in C++ allow 7/87/8, 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 m=1009m = 1009 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, 12(1+11−α)\frac{1}{2}\bigl(1 + \frac{1}{1 - \alpha}\bigr) for a successful and 12(1+1(1−α)2)\frac{1}{2}\bigl(1 + \frac{1}{(1 - \alpha)^2}\bigr) for an unsuccessful search, which account for primary clustering.

python · try it yourself
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 α=0.9\alpha = 0.9 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 α=0.9\alpha = 0.9 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 ℓ\ell is hit with probability proportional to ℓ\ell, so the expected walk is proportional to E[ℓ2]/E[ℓ]E[\ell^2]/E[\ell], not to E[ℓ]E[\ell]: 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 12(1+1/(1−α)2)\frac{1}{2}(1 + 1/(1-\alpha)^2) approximation. With truly random probes there are no runs, and the cost is the geometric 1/(1−α)1/(1 - \alpha).

Test yourself

Under uniform hashing, what is the bound on the expected probes of an unsuccessful search at α=0.8\alpha = 0.8?

  1. 1.8
  2. 2
  3. 5
  4. 25

Why does a successful search cost less than an unsuccessful one at the same load factor?

  1. Because successful searches stop at an empty slot
  2. Because a search for a stored key retraces its insertion, and most keys were inserted when the table was emptier
  3. Because stored keys are sorted
  4. It does not; both cost 1/(1−α)1/(1 - \alpha)

In the proof of the 1/(1−α)1/(1 - \alpha) bound, which fact gives E[X]=∑i≥1Pr⁡[X≥i]E[X] = \sum_{i \ge 1} \Pr[X \ge i]?

  1. Linearity of expectation over indicators [X≥i][X \ge i]
  2. Markov's inequality
  3. The birthday paradox
  4. Independence of the probes

At α=0.9\alpha = 0.9, about how many probes does an unsuccessful search make with linear probing, by Knuth's approximation?

  1. about 5
  2. about 10
  3. about 50
  4. about 100