Algorithm Search⌘K

Proofs: Linear hashing

These are the human proofs of Linear hashing, at full length.

Expected and amortized cost of linear hashing full

This proof shows: time.search, time.insert, time.delete, space.

We use the names of the page and of the correctness proof: M=N2LM = N 2^L, B=M+pB = M + p, and the invariant (I1) to (I4). Time is counted on the word RAM, with O(1)O(1) for one evaluation of hh, for one comparison of keys, and for one step over a pair of a list. Appending a list to the array of buckets and removing the last one cost O(1)O(1) amortized, as in a dynamic array.

Assumption (A). Let RR be a multiple of every 2M2M that occurs, for example R=N2wR = N 2^w for a large ww. The values h(k)h(k) of all keys that occur are independent and uniform on {0,…,R−1}\{0, \ldots, R - 1\}, and the sequence of operations is fixed in advance, so it does not depend on these values. Under (A), h(k) mod Mh(k) \bmod M and h(k) mod 2Mh(k) \bmod 2M are uniform for every MM that occurs, because MM and 2M2M divide RR.

Lemma 1 (a fixed bucket). In a state with (I1), for each key kk and each bucket jj, Pr⁡[addr(k)=j]≤1/M<2/B\Pr[\mathrm{addr}(k) = j] \le 1/M < 2/B.

Proof. If p≤j<Mp \le j < M, the event is h(k) mod M=jh(k) \bmod M = j, with probability 1/M1/M. If j<pj < p or j≥Mj \ge M, the event is h(k) mod 2M=jh(k) \bmod 2M = j, with probability 1/(2M)1/(2M). Each other jj has probability 0 by Lemma 1 of the correctness proof. Finally B=M+p<2MB = M + p < 2M gives 1/M<2/B1/M < 2/B. □\square

The state at any time, that is, nn, LL, pp and the set of stored keys, depends only on the sequence of operations, because the decisions of lines L10 and D4 use only nn and BB. The hash values decide only which list holds which key. This is what lets Lemma 1 apply at each step.

Search. Let the query be kk. Condition on h(k)h(k); this fixes the bucket j=addr(k)j = \mathrm{addr}(k). Each stored key k′≠kk' \ne k is in bucket jj with probability at most 1/M1/M, independently of h(k)h(k), by (A) and Lemma 1. So the expected number of pairs with a key other than kk in the list is at most n/M<2n/B≤2αmax⁡n / M < 2n / B \le 2 \alpha_{\max} by (I4). The scan of lines L4 to L6 steps over these pairs and over at most one pair with key kk. So SEARCH takes O(1+αmax⁡)O(1 + \alpha_{\max}) expected time, whether kk is present or not.

The number of splits per insertion. Before INSERT, (I4) gives n≤αmax⁡Bn \le \alpha_{\max} B. After line L9 there are at most n+1n + 1 pairs. Let s=⌈1/αmax⁡⌉s = \lceil 1 / \alpha_{\max} \rceil. After ss splits, αmax⁡(B+s)≥αmax⁡B+1≥n+1\alpha_{\max} (B + s) \ge \alpha_{\max} B + 1 \ge n + 1, so the loop of line L10 stops after at most ss splits.

The cost of one split. A split runs when the table holds some number n′n' of pairs with n′≤αmax⁡B0+1n' \le \alpha_{\max} B_0 + 1, where B0≤BB_0 \le B is the size before the insertion and BB the current size. It scans the list pp, an unsplit bucket. By Lemma 1, its expected length is at most n′/M<2n′/B≤2αmax⁡+2/B≤2αmax⁡+2n' / M < 2 n' / B \le 2 \alpha_{\max} + 2 / B \le 2 \alpha_{\max} + 2. With the O(1)O(1) amortized cost of line L11, a split costs O(1+αmax⁡)O(1 + \alpha_{\max}) expected amortized time.

The cost of one merge. A merge runs when n<αmin⁡Bn < \alpha_{\min} B. It moves the pairs of the last list, a split bucket, whose expected length is at most n/(2M)<n/B<αmin⁡n / (2M) < n / B < \alpha_{\min} by Lemma 1. So a merge costs O(1+αmin⁡)=O(1+αmax⁡)O(1 + \alpha_{\min}) = O(1 + \alpha_{\max}) expected amortized time.

Insert and delete. Lines L7 to L9 and D1 to D3 scan one list, with the bound of the search: O(1+αmax⁡)O(1 + \alpha_{\max}) expected time. Start from NEW, with B=NB = N. Each split adds one bucket and each merge removes one, and B≥NB \ge N at all times, so over any sequence the number of merges is at most the number of splits. The number of splits is at most ss times the number of insertions. So a sequence of mm operations does at most 2sm2 s m splits and merges, with expected total cost O(sm(1+αmax⁡))O(s m (1 + \alpha_{\max})), and O(m(1+αmax⁡))O(m (1 + \alpha_{\max})) for the scans. Since s≤1+1/αmax⁡s \le 1 + 1/\alpha_{\max}, the expected amortized cost of each operation is

O((1+1/αmax⁡)(1+αmax⁡))=O(1+αmax⁡+1/αmax⁡),O\left((1 + 1/\alpha_{\max})(1 + \alpha_{\max})\right) = O(1 + \alpha_{\max} + 1/\alpha_{\max}),

which is O(1)O(1) when αmax⁡\alpha_{\max} is a positive constant.

Worst case. Without (A), all keys may share one list. Then one scan costs O(n)O(n), and one split or merge costs O(n)O(n). The loop of line L10 does at most ss splits. The loop of line D4 does at most B−NB - N merges, which move at most nn pairs in total, so it costs O(n+B)O(n + B).

Space. The structure holds BB lists and nn pairs, so it uses Θ(B+n)\Theta(B + n) words. If αmin⁡>0\alpha_{\min} > 0, then B≤max⁡(N,n/αmin⁡+1)B \le \max(N, n / \alpha_{\min} + 1) after each operation. This holds after NEW, where B=NB = N. A search and a replacement of a value change neither nn nor BB. An insertion without a split makes nn larger and keeps BB. An insertion with splits ends with a split that ran because n>αmax⁡(B−1)n > \alpha_{\max} (B - 1) for the final BB, so B<n/αmax⁡+1≤n/αmin⁡+1B < n / \alpha_{\max} + 1 \le n / \alpha_{\min} + 1. A deletion ends the loop of line D4 either with B=NB = N or with n≥αmin⁡Bn \ge \alpha_{\min} B. So the space is O(N+n/αmin⁡)O(N + n / \alpha_{\min}) words. ■\blacksquare

Linear hashing keeps its invariant and answers each operation correctly full

This proof shows: correctness, termination.

We use the names of the page: N≥1N \ge 1, αmax⁡>0\alpha_{\max} > 0, 0≤αmin⁡≤αmax⁡/20 \le \alpha_{\min} \le \alpha_{\max} / 2, the level LL, the pointer pp, the number of pairs nn, the array of BB lists, and M=N2LM = N 2^L. For a pair (L,p)(L, p) we write

addrL,p(k)={h(k) mod Mif h(k) mod M≥p,h(k) mod 2Mif h(k) mod M<p.\mathrm{addr}_{L,p}(k) = \begin{cases} h(k) \bmod M & \text{if } h(k) \bmod M \ge p, \\ h(k) \bmod 2M & \text{if } h(k) \bmod M < p. \end{cases}

This is the value of ADDRESS (lines L1 to L3). We call (L,p)(L, p) extended valid if L≥0L \ge 0, 0≤p≤M0 \le p \le M and B=M+pB = M + p. It is valid if also p<Mp < M; this is (I1).

Lemma 1 (the address is a bucket). If (L,p)(L, p) is extended valid, then 0≤addrL,p(k)<B0 \le \mathrm{addr}_{L,p}(k) < B for every key kk. Moreover addrL,p(k)=j\mathrm{addr}_{L,p}(k) = j implies h(k) mod M=j mod Mh(k) \bmod M = j \bmod M.

Proof. Let r=h(k) mod Mr = h(k) \bmod M. If r≥pr \ge p, the address is rr with p≤r<M≤Bp \le r < M \le B. If r<pr < p, the address a=h(k) mod 2Ma = h(k) \bmod 2M satisfies a mod M=ra \bmod M = r, because MM divides 2M2M; so a=ra = r or a=r+Ma = r + M, and both are less than p+M=Bp + M = B. In both cases a mod M=ra \bmod M = r. □\square

Lemma 2 (end of a round). If p=Mp = M, then (L,M)(L, M) and (L+1,0)(L + 1, 0) give the same BB and the same address for every key.

Proof. B=M+M=2M=2M+0B = M + M = 2M = 2M + 0. Under (L,M)(L, M) every r=h(k) mod Mr = h(k) \bmod M is less than p=Mp = M, so the address is h(k) mod 2Mh(k) \bmod 2M. Under (L+1,0)(L + 1, 0) the small table has size 2M2M and every value is at least p=0p = 0, so the address is again h(k) mod 2Mh(k) \bmod 2M. □\square

Lemma 3 (SPLIT). Assume (I1), (I2) and (I3) before SPLIT. Then after SPLIT, (I1), (I2) and (I3) hold, nn is the same, and BB is one larger.

Proof. Let L,p,M,B=M+pL, p, M, B = M + p be the values before. Line L11 adds the list with index B=p+MB = p + M. Consider a pair (k,v)(k, v) in the old list bucket[p]\mathit{bucket}[p]. By (I2), addrL,p(k)=p\mathrm{addr}_{L,p}(k) = p, so by Lemma 1, h(k) mod M=ph(k) \bmod M = p. Then h(k) mod 2Mh(k) \bmod 2M is pp or p+Mp + M, and line L14 puts the pair in that list, which exists after line L11. Lines L12 to L14 move only the pairs of bucket[p]\mathit{bucket}[p] and keep each of them, so (I3) holds and nn does not change.

Line L15 sets p′=p+1≤Mp' = p + 1 \le M, and (L,p′)(L, p') is extended valid with B+1=M+p′B + 1 = M + p'. We show that each pair is in the list addrL,p′(k)\mathrm{addr}_{L,p'}(k). Let r=h(k) mod Mr = h(k) \bmod M.

  • If r=pr = p, the pair came from the old bucket[p]\mathit{bucket}[p], because by (I2) and Lemma 1 every pair with r=pr = p was in the list pp (its old address was r=pr = p, since r≥pr \ge p). Now r<p′r < p', so its new address is h(k) mod 2Mh(k) \bmod 2M, which is the list of line L14.
  • If r>pr > p, then r≥p′r \ge p', and both the old and the new address are rr. The pair did not move.
  • If r<pr < p, then r<p′r < p', and both addresses are h(k) mod 2Mh(k) \bmod 2M. The pair did not move.

If p′<Mp' < M, line L16 does nothing and (L,p′)(L, p') is valid. If p′=Mp' = M, line L16 sets (L+1,0)(L + 1, 0), which is valid because 0<2M0 < 2M, and by Lemma 2 neither BB nor any address changes. So (I1) and (I2) hold. □\square

Lemma 4 (MERGE). Assume (I1), (I2), (I3) and B>NB > N before MERGE. Then after MERGE, (I1), (I2) and (I3) hold, nn is the same, and BB is one smaller.

Proof. First, line M1. If p=0p = 0, then B=M=N2L>NB = M = N 2^L > N, so L≥1L \ge 1. Line M1 sets L′=L−1L' = L - 1 and p′=N2L′=M/2p' = N 2^{L'} = M / 2. The pair (L′,p′)(L', p') is extended valid with p′=N2L′p' = N 2^{L'}, and by Lemma 2 applied to it, it has the same BB and the same addresses as (L,0)(L, 0). So after line M1 we have an extended valid state with 1≤p≤M1 \le p \le M, for its own MM, in which (I2) and (I3) hold. If p≥1p \ge 1 at the start, the same is true without a change.

Now let q=p−1q = p - 1, so 0≤q<M0 \le q < M, and the last list has index B−1=M+qB - 1 = M + q. By Lemma 1, each pair in the list M+qM + q and each pair in the list qq has h(k) mod M=qh(k) \bmod M = q. Line M2 sets the pointer to qq. Let r=h(k) mod Mr = h(k) \bmod M for a pair (k,v)(k, v).

  • If r=qr = q, the pair was in the list qq or in the list M+qM + q, since its old address was h(k) mod 2Mh(k) \bmod 2M, which is one of these two. Its new address is r=qr = q, because now r≥qr \ge q. Line M3 moves the pairs of the list M+qM + q to the list qq, so the pair is in the list qq.
  • If r>qr > q, then r≥pr \ge p (the old pointer), and both the old and the new address are rr.
  • If r<qr < q, both the old and the new address are h(k) mod 2Mh(k) \bmod 2M.

After line M3 the list M+qM + q is empty, and line M4 removes it. Now B=M+qB = M + q with 0≤q<M0 \le q < M, so (I1) holds, and (I2) holds by the three cases. Line M3 moves pairs without loss or copy, so (I3) holds and nn does not change. □\square

Theorem. NEW makes a state that satisfies (I1) to (I4). If (I1) to (I4) hold before an operation, then SEARCH, INSERT and DELETE terminate, return the value that the abstract type asks for, and leave a state that satisfies (I1) to (I4) and represents the new map.

Proof.

NEW. L=0L = 0, p=0<N=Mp = 0 < N = M, and B=NB = N, so (I1) holds. There are no pairs and n=0n = 0, so (I2), (I3) and (I4) hold.

SEARCH(k)(k). By (I2), a pair with key kk can only be in the list addr(k)\mathrm{addr}(k), which exists by Lemma 1, and by (I3) there is at most one such pair. Lines L4 to L6 scan this list, which is finite, and return its value or NIL. The state does not change.

INSERT(k,v)(k, v). If kk is present, its pair is in the list aa of line L7 by (I2), and line L8 replaces its value. The keys, nn and BB do not change, so (I1) to (I4) hold, and the map now sends kk to vv. Otherwise line L9 appends (k,v)(k, v) to the list addr(k)\mathrm{addr}(k), so (I2) holds for the new pair, (I3) holds with n+1n + 1, and (I1) is unchanged. By Lemma 3, each SPLIT of line L10 keeps (I1) to (I3) and nn, and makes BB one larger. So the loop ends after at most ⌈n/αmax⁡⌉\lceil n / \alpha_{\max} \rceil steps, which is a finite number, and at the end n≤αmax⁡Bn \le \alpha_{\max} B, which is (I4).

DELETE(k)(k). By (I2) and (I3), lines D1 and D2 find the pair of kk exactly when kk is present; if it is absent, nothing changes and the result is false. Otherwise line D3 removes the pair and sets nn to n−1n - 1; (I1) to (I3) hold, and (I4) holds because nn decreased. Each step of the loop of line D4 runs with B>NB > N, so Lemma 4 applies: (I1) to (I3) stay true and BB drops by 1. The loop ends after at most B−NB - N steps. For (I4): a step runs only when n<αmin⁡Bn < \alpha_{\min} B, with B≥2B \ge 2 because B>N≥1B > N \ge 1. Then after the step

n<αmin⁡B≤αmax⁡2B≤αmax⁡(B−1),n < \alpha_{\min} B \le \frac{\alpha_{\max}}{2} B \le \alpha_{\max} (B - 1),

where the last inequality is B≤2(B−1)B \le 2(B - 1), true for B≥2B \ge 2. So (I4) holds for the new size B−1B - 1. The result is true, and the map no longer has kk.

In each case the set of pairs is exactly the new map, by (I3) and the description of each step. ■\blacksquare

Back to Linear hashing