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: , , and the invariant (I1) to (I4). Time is counted on the word RAM, with for one evaluation of , 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 amortized, as in a dynamic array.
Assumption (A). Let be a multiple of every that occurs, for example for a large . The values of all keys that occur are independent and uniform on , and the sequence of operations is fixed in advance, so it does not depend on these values. Under (A), and are uniform for every that occurs, because and divide .
Lemma 1 (a fixed bucket). In a state with (I1), for each key and each bucket , .
Proof. If , the event is , with probability . If or , the event is , with probability . Each other has probability 0 by Lemma 1 of the correctness proof. Finally gives .
The state at any time, that is, , , and the set of stored keys, depends only on the sequence of operations, because the decisions of lines L10 and D4 use only and . 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 . Condition on ; this fixes the bucket . Each stored key is in bucket with probability at most , independently of , by (A) and Lemma 1. So the expected number of pairs with a key other than in the list is at most by (I4). The scan of lines L4 to L6 steps over these pairs and over at most one pair with key . So SEARCH takes expected time, whether is present or not.
The number of splits per insertion. Before INSERT, (I4) gives . After line L9 there are at most pairs. Let . After splits, , so the loop of line L10 stops after at most splits.
The cost of one split. A split runs when the table holds some number of pairs with , where is the size before the insertion and the current size. It scans the list , an unsplit bucket. By Lemma 1, its expected length is at most . With the amortized cost of line L11, a split costs expected amortized time.
The cost of one merge. A merge runs when . It moves the pairs of the last list, a split bucket, whose expected length is at most by Lemma 1. So a merge costs expected amortized time.
Insert and delete. Lines L7 to L9 and D1 to D3 scan one list, with the bound of the search: expected time. Start from NEW, with . Each split adds one bucket and each merge removes one, and 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 times the number of insertions. So a sequence of operations does at most splits and merges, with expected total cost , and for the scans. Since , the expected amortized cost of each operation is
which is when is a positive constant.
Worst case. Without (A), all keys may share one list. Then one scan costs , and one split or merge costs . The loop of line L10 does at most splits. The loop of line D4 does at most merges, which move at most pairs in total, so it costs .
Space. The structure holds lists and pairs, so it uses words. If , then after each operation. This holds after NEW, where . A search and a replacement of a value change neither nor . An insertion without a split makes larger and keeps . An insertion with splits ends with a split that ran because for the final , so . A deletion ends the loop of line D4 either with or with . So the space is words.
Linear hashing keeps its invariant and answers each operation correctly full
This proof shows: correctness, termination.
We use the names of the page: , , , the level , the pointer , the number of pairs , the array of lists, and . For a pair we write
This is the value of ADDRESS (lines L1 to L3). We call extended valid if , and . It is valid if also ; this is (I1).
Lemma 1 (the address is a bucket). If is extended valid, then for every key . Moreover implies .
Proof. Let . If , the address is with . If , the address satisfies , because divides ; so or , and both are less than . In both cases .
Lemma 2 (end of a round). If , then and give the same and the same address for every key.
Proof. . Under every is less than , so the address is . Under the small table has size and every value is at least , so the address is again .
Lemma 3 (SPLIT). Assume (I1), (I2) and (I3) before SPLIT. Then after SPLIT, (I1), (I2) and (I3) hold, is the same, and is one larger.
Proof. Let be the values before. Line L11 adds the list with index . Consider a pair in the old list . By (I2), , so by Lemma 1, . Then is or , and line L14 puts the pair in that list, which exists after line L11. Lines L12 to L14 move only the pairs of and keep each of them, so (I3) holds and does not change.
Line L15 sets , and is extended valid with . We show that each pair is in the list . Let .
- If , the pair came from the old , because by (I2) and Lemma 1 every pair with was in the list (its old address was , since ). Now , so its new address is , which is the list of line L14.
- If , then , and both the old and the new address are . The pair did not move.
- If , then , and both addresses are . The pair did not move.
If , line L16 does nothing and is valid. If , line L16 sets , which is valid because , and by Lemma 2 neither nor any address changes. So (I1) and (I2) hold.
Lemma 4 (MERGE). Assume (I1), (I2), (I3) and before MERGE. Then after MERGE, (I1), (I2) and (I3) hold, is the same, and is one smaller.
Proof. First, line M1. If , then , so . Line M1 sets and . The pair is extended valid with , and by Lemma 2 applied to it, it has the same and the same addresses as . So after line M1 we have an extended valid state with , for its own , in which (I2) and (I3) hold. If at the start, the same is true without a change.
Now let , so , and the last list has index . By Lemma 1, each pair in the list and each pair in the list has . Line M2 sets the pointer to . Let for a pair .
- If , the pair was in the list or in the list , since its old address was , which is one of these two. Its new address is , because now . Line M3 moves the pairs of the list to the list , so the pair is in the list .
- If , then (the old pointer), and both the old and the new address are .
- If , both the old and the new address are .
After line M3 the list is empty, and line M4 removes it. Now with , so (I1) holds, and (I2) holds by the three cases. Line M3 moves pairs without loss or copy, so (I3) holds and does not change.
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. , , and , so (I1) holds. There are no pairs and , so (I2), (I3) and (I4) hold.
SEARCH. By (I2), a pair with key can only be in the list , 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. If is present, its pair is in the list of line L7 by (I2), and line L8 replaces its value. The keys, and do not change, so (I1) to (I4) hold, and the map now sends to . Otherwise line L9 appends to the list , so (I2) holds for the new pair, (I3) holds with , and (I1) is unchanged. By Lemma 3, each SPLIT of line L10 keeps (I1) to (I3) and , and makes one larger. So the loop ends after at most steps, which is a finite number, and at the end , which is (I4).
DELETE. By (I2) and (I3), lines D1 and D2 find the pair of exactly when is present; if it is absent, nothing changes and the result is false. Otherwise line D3 removes the pair and sets to ; (I1) to (I3) hold, and (I4) holds because decreased. Each step of the loop of line D4 runs with , so Lemma 4 applies: (I1) to (I3) stay true and drops by 1. The loop ends after at most steps. For (I4): a step runs only when , with because . Then after the step
where the last inequality is , true for . So (I4) holds for the new size . The result is true, and the map no longer has .
In each case the set of pairs is exactly the new map, by (I3) and the description of each step.