Algorithm Search⌘K

Proofs: Analysis of linear probing

These are the human proofs of Analysis of linear probing, at full length.

The exact expected cost of a search in linear probing full

This proof shows: statement.

This proof covers parts (a), (b) and (d) of the statement. Part (d) uses part (c), which is Knuth's result (The Art of Computer Programming, Volume 3, Section 6.4) and is not proved here.

Setting. Slots are 0,…,m−10, \ldots, m-1, and all slot arithmetic is modulo mm. A hash sequence is a word a=(a1,…,an)∈{0,…,m−1}na = (a_1, \ldots, a_n) \in \{0, \ldots, m-1\}^n, where aia_i is the home of the ii-th key. Running linear probing on aa (insert key ii at the first empty slot of ai,ai+1,…a_i, a_i + 1, \ldots, for i=1,…,ni = 1, \ldots, n) gives a final table τ(a)\tau(a), which says which key is in each slot. We assume n<mn < m, so every insert finds an empty slot. The path of key ii is the list of slots from aia_i to its final slot.

Fact 1. A slot that is empty in τ(a)\tau(a) is on no path, and the home of each key is full in τ(a)\tau(a).

Proof. When key ii is inserted, each slot of its path before the last one is full, and the last one becomes full. No slot is ever emptied. So every slot of every path is full at the end. The home aia_i is the first slot of the path of key ii. □\square

Lemma 1 (rotation). For integers a≥1a \ge 1 and 0≤b<a0 \le b < a, let f(a,b)f(a, b) be the number of hash sequences of bb keys in a circle of aa slots after which a fixed slot ss is empty. Then f(a,b)=(a−b) ab−1f(a, b) = (a - b)\, a^{b-1}, and this number does not depend on ss.

Proof. Let ρ\rho add 1 to each entry of a sequence. By induction on the number of inserts, the run on ρ(a)\rho(a) puts each key one slot further than the run on aa, because each probe is shifted by one and sees a shifted table. So slot ss is empty after aa exactly when slot s+1s + 1 is empty after ρ(a)\rho(a). Since ρ\rho is a bijection of the aba^b sequences, every slot is empty after the same number NN of sequences. Count the pairs (sequence, empty slot) in two ways: each sequence leaves exactly a−ba - b empty slots, so a⋅N=ab(a−b)a \cdot N = a^b (a - b), and N=(a−b)ab−1N = (a - b) a^{b-1}. For b=0b = 0 this is 11, the empty sequence. □\square

Lemma 2 (splitting). Let 1≤k≤n1 \le k \le n and k≤m−2k \le m - 2. Let g(k)g(k) be the number of hash sequences aa of length nn such that in τ(a)\tau(a) slot 0 is empty, slots 1,…,k1, \ldots, k are full, and slot k+1k+1 is empty. Then

g(k)=(nk)f(k+1,k) f(m−k−1,n−k)=(nk)(k+1)k−1f(m−k−1,n−k).g(k) = \binom{n}{k} f(k+1, k)\, f(m-k-1, n-k) = \binom{n}{k}(k+1)^{k-1} f(m-k-1, n-k).

Proof. Let A={1,…,k}A = \{1, \ldots, k\} and B={k+2,…,m−1}B = \{k+2, \ldots, m-1\}, a set of m−k−2m - k - 2 slots. We build a bijection between the sequences counted by g(k)g(k) and the triples (S,u,v)(S, u, v), where SS is a set of kk of the nn keys, uu is a hash sequence for the keys of SS (in their order) in a circle of k+1k + 1 slots {0,…,k}\{0, \ldots, k\} that leaves slot kk empty, and vv is a hash sequence for the other n−kn - k keys in a circle of m−k−1m - k - 1 slots {0,…,m−k−2}\{0, \ldots, m-k-2\} that leaves slot m−k−2m-k-2 empty. By Lemma 1 there are (nk)f(k+1,k)f(m−k−1,n−k)\binom{n}{k} f(k+1, k) f(m-k-1, n-k) triples, and f(k+1,k)=(k+1)k−1f(k+1, k) = (k+1)^{k-1}.

From a sequence to a triple. Take aa counted by g(k)g(k). By Fact 1, no path contains slot 0 or slot k+1k+1. A path is a run of consecutive slots, so each path lies inside AA or inside BB, and so does its home. Let SS be the keys with home in AA; they fill exactly the slots of AA, so ∣S∣=k|S| = k. Let uu give each key of SS the home ai−1a_i - 1, and vv give each other key the home ai−(k+2)a_i - (k+2), which lies in {0,…,m−k−3}\{0, \ldots, m-k-3\}. We claim that the run on uu in the small circle places each key of SS in its slot in τ(a)\tau(a) minus 1, and leaves slot kk empty. By induction on the inserts: before each insert of a key of SS, the full slots of AA in the run on aa are the shifted full slots of {0,…,k−1}\{0, \ldots, k-1\} in the run on uu, and slot kk of the small circle is empty. The insert of a key i∈Si \in S in the run on aa follows a path inside AA, and the same steps, shifted by one, form the first empty slot after ai−1a_i - 1 in the small circle. Keys outside SS do not change AA. In the same way, the run on vv copies the run on aa restricted to BB, shifted by k+2k + 2, and its last slot m−k−2m - k - 2, which stands for slot 0 of the big table, stays empty, since no path of the big run reaches slot 0. So (S,u,v)(S, u, v) is a triple of the kind above.

From a triple to a sequence. Take a triple (S,u,v)(S, u, v). By Fact 1 for the small circle, no key of SS has home kk, so its home in the big table, ui+1u_i + 1, is in AA. By Fact 1 for the other circle, no key has home m−k−2m - k - 2, so its home vi+k+2v_i + k + 2 is in BB. Define aa this way. By induction on the inserts, as above, the run on aa keeps slot 0 and slot k+1k+1 empty and copies the two small runs into AA and BB: an insert of a key of SS meets only slots of AA before it stops, because the small run stops before slot kk, and an insert of another key meets only slots of BB, because its small run stops before slot m−k−2m - k - 2. At the end, the kk keys of SS fill AA, so aa is counted by g(k)g(k).

The two maps are inverse to each other, because each one only shifts the homes and selects the keys by the block of their home. □\square

Proof of (a). Fix a hash sequence aa and its table τ(a)\tau(a). A failed search that starts at slot ss probes s,s+1,…s, s+1, \ldots up to and including the first empty slot. If ss is empty, it costs 1. Assume first n≤m−2n \le m - 2, so the table has at least two empty slots. Then the full slots form maximal runs, and each run of kk slots has an empty slot just before it and a different empty slot just after it. A search that starts at the tt-th slot from the end of a run, for t=1,…,kt = 1, \ldots, k, costs t+1t + 1. So the total cost over all mm start slots is

∑s=0m−1cost(s)=m+∑k≥1Rk(a) k(k+1)2,\sum_{s=0}^{m-1} \mathrm{cost}(s) = m + \sum_{k \ge 1} R_k(a)\, \frac{k(k+1)}{2},

where Rk(a)R_k(a) is the number of runs of length kk. A run of length kk is a slot jj such that jj is empty, j+1,…,j+kj + 1, \ldots, j + k are full and j+k+1j + k + 1 is empty. By the rotation of Lemma 1, the number of sequences for which this holds at jj is the same for every jj, so it is g(k)g(k) for each jj, and ∑aRk(a)=m g(k)\sum_a R_k(a) = m\, g(k). The search key has a uniform start slot, independent of aa. Hence

Cm,n′=1mn∑a1m∑scost(s)=1+1mn∑k=1ng(k) k(k+1)2,C'_{m,n} = \frac{1}{m^n} \sum_a \frac{1}{m} \sum_{s} \mathrm{cost}(s) = 1 + \frac{1}{m^n} \sum_{k=1}^{n} g(k)\, \frac{k(k+1)}{2},

and Lemma 2 gives the formula of (a). A run has at most n≤m−2n \le m - 2 slots, so Lemma 2 applies to each kk in the sum.

If n=m−1n = m - 1, the table has exactly one empty slot, and the m−1m - 1 full slots form one run. The total cost is m+(m−1)m/2m + (m-1)m/2, and Cm,m−1′=1+(m−1)/2=(m+1)/2C'_{m,m-1} = 1 + (m-1)/2 = (m+1)/2 for every sequence. □\square

Proof of (b). Fix i∈{1,…,n}i \in \{1, \ldots, n\}. The search for key ii probes ai,ai+1,…a_i, a_i + 1, \ldots until it finds key ii. The slots before the final slot pip_i of key ii were full when key ii was inserted and hold other keys, and slot pip_i holds key ii, so the search makes exactly as many probes as the insert of key ii did. The insert of key ii is a failed search for a key with home aia_i in the table τ(a1,…,ai−1)\tau(a_1, \ldots, a_{i-1}), and it costs the same number of probes, the final empty slot counted. The value aia_i is uniform and independent of a1,…,ai−1a_1, \ldots, a_{i-1}, which are themselves independent and uniform. So the expected cost of the search for key ii is Cm,i−1′C'_{m, i-1}. Averaging over ii uniform in {1,…,n}\{1, \ldots, n\} gives (b). □\square

Proof of (d), from (c). Let m→∞m \to \infty with n/m→α<1n/m \to \alpha < 1. Fix β\beta with α<β<1\alpha < \beta < 1. For all large mm, n/m≤βn/m \le \beta. For r∈{0,1}r \in \{0, 1\} and a fixed kk, the term

tk(m)=(r+kk)∏j=0k−1n−jmt_k(m) = \binom{r+k}{k} \prod_{j=0}^{k-1} \frac{n - j}{m}

tends to (r+kk)αk\binom{r+k}{k} \alpha^k, since each factor (n−j)/m(n - j)/m tends to α\alpha. For all large mm and all kk, 0≤tk(m)≤(r+kk)βk0 \le t_k(m) \le \binom{r+k}{k} \beta^k, because each factor is at most n/m≤βn/m \le \beta (factors with j≥nj \ge n make the term 0). The bound is summable, since ∑k(r+kk)βk=(1−β)−(r+1)\sum_k \binom{r+k}{k} \beta^k = (1-\beta)^{-(r+1)} by the negative binomial series. By dominated convergence for series (Tannery's theorem),

Qr(m,n)→∑k≥0(r+kk)αk=1(1−α)r+1.Q_r(m, n) \to \sum_{k \ge 0} \binom{r+k}{k} \alpha^k = \frac{1}{(1-\alpha)^{r+1}}.

The same holds for Q0(m,n−1)Q_0(m, n-1), since (n−1)/m→α(n-1)/m \to \alpha too. With (c), Cm,n′→12(1+(1−α)−2)C'_{m,n} \to \frac{1}{2}\left(1 + (1-\alpha)^{-2}\right) and Cm,n→12(1+(1−α)−1)C_{m,n} \to \frac{1}{2}\left(1 + (1-\alpha)^{-1}\right). ■\blacksquare

Back to Analysis of linear probing