Proofs: Double hashing
These are the human proofs of Double hashing, at full length.
Expected probes of open addressing under uniform hashing full
This proof shows: time.expected, time.worst.
Worst case. Each loop of the page runs at most times, by the proof of correctness, so each operation makes at most probes. For the lower bound, take keys with the same and the same ; for and the keys work. They have the same probe sequence, so the insert of probes slots, and the last insert and each later search for probe slots. With this is .
Model for the expected case. The uniform hashing assumption says that the probe sequence of each key is a uniformly random permutation of the slots, independent of the other keys. Assume a table with no DELETED slot that holds keys, so , and that the keys were inserted without knowledge of the random sequences.
Claim 1. A search for a key that is not in the table makes at most probes in expectation.
Proof. Let be the number of probes. The search goes on after probe only if the first probed slots are all occupied. The probe sequence of the absent key is a uniform random permutation, independent of which slots are occupied. So for ,
because for . Hence .
An insert of a new key makes the same probes as a search that fails, so it also makes at most probes in expectation.
Claim 2. With no deletions, a search for a key chosen uniformly from the keys of the table makes at most probes in expectation.
Proof. A search for a key follows the same probes as its insert. With no deletions, the key inserted as the -st, for , was inserted into a table with keys, so by Claim 1 its search makes at most probes in expectation. Average over the keys:
The inequality holds because for each , as on .
How far double hashing is from the model. Double hashing does not give a uniform random permutation: there are at most sequences, one per pair . The two claims are therefore exact only for the model. Guibas and Szemerédi (1978) and Lueker and Molodowitch (1993) proved that, with and ideal random functions, the expected number of probes of double hashing equals the uniform hashing value plus a term that tends to 0 as grows, for fixed (below about 0.319 in the first paper, and for each in the second). Their proofs are long and are not repeated here.
Double hashing keeps its probe invariant and implements a dictionary full
This proof shows: correctness, termination.
Write , and assume for each key . A slot is free when it is EMPTY or DELETED, and occupied when it holds a pair. The map of the table is the set of the pairs in occupied slots.
Lemma 1 (the probe sequence is a permutation). For each key , the values are the slots, each exactly once.
Proof. Suppose with . Then divides . Since , divides . But , a contradiction. So the values are distinct, and they are values in , so they are all the slots.
Invariant.
- (D1) If key is in slot , then none of is EMPTY.
- (D2) No key is in two occupied slots.
Lemma 2 (complete scan). Assume (D1) and (D2). Follow and stop at the first EMPTY slot, or after probes. If is in the table, the scan meets its slot. If the scan does not meet , then is not in the table.
Proof. If is in slot , then by Lemma 1, and by (D1) no slot before position is EMPTY, so the scan reaches position . The second sentence is the contrapositive of the first.
Theorem. Start from a table with all slots EMPTY, and apply any sequence of DH-INSERT, DH-SEARCH and DH-DELETE. Each call ends after at most passes of its loop. After each call (D1) and (D2) hold, is the map that the same sequence gives on an abstract dictionary (except that an insert into a table with no free slot and without reports a full table and changes nothing), and each search returns the value of its key in , or NIL.
Proof. Termination. Each loop runs with , and grows by 1 at each pass, so it stops after at most passes.
Base. In the empty table no slot is occupied, so (D1) and (D2) hold, and is empty.
Search. The loop of lines L13 to L17 is the scan of Lemma 2. Line L16 returns the value of the occupied slot with key , which is the only one by (D2). If the scan ends at EMPTY or after probes, is absent by Lemma 2 and NIL is right. No slot changes.
Delete. Lines L18 to L21 are the same scan. If they find in slot , line L21 makes DELETED, so loses exactly the pair of . No slot becomes EMPTY, so for each other key the slots before it on its path are still not EMPTY, and (D1) holds. (D2) holds since a pair was only removed. If the scan does not find , then is absent and nothing changes.
Insert. The loop of lines L2 to L10 follows and stops at the first EMPTY slot or after probes, so it is the scan of Lemma 2, with a key test on each occupied slot at line L9. If it finds , line L10 changes only the value, so (D1), (D2) and the slots are as before, and maps to .
Else is absent, by Lemma 2. During the scan, is set at lines L5 or L8 to the first free slot of the path, if the scan meets one. Let . All the slots were met before and were not free, so they are occupied, and in particular not EMPTY. If is NIL, the scan met no free slot. If it stopped at an EMPTY slot, that slot would have set ; so it made probes, all occupied, and by Lemma 1 every slot is occupied by a key other than . Then line L11 reports a full table and changes nothing, as stated.
Otherwise line L12 puts in slot . Then (D1) holds for , by the fact above. For each other key, line L12 turned a free slot into an occupied one, and no slot became EMPTY, so (D1) still holds for it. (D2) holds because was absent. gains exactly the pair .
By induction on the number of calls, the claims hold after each call.