Proofs: Dynamic perfect hashing
These are the human proofs of Dynamic perfect hashing, at full length.
Dynamic perfect hashing has O(1) worst-case search, O(1) expected amortized updates and O(n) space full
This proof shows: time.amortized, space.extra.
The model is the word RAM with , so a hash value costs time. The structure starts with REHASH-ALL, and the sequence of operations is fixed before the random choices (an oblivious adversary). We use the invariants (D1) to (D5), Fact (U), Lemma 1 and the corollary of the correctness proof. The sum of line L8 is kept in a counter, set by line L23 and changed at line L7, so line L8 takes time. The constants 64 and 512 are those of this page, not of the paper of Dietzfelbinger and others; the structure of the proof (rebuild a bucket after a collision, double a full bucket, start a new phase after a fixed number of updates or when the space is too large) follows that paper.
A phase is the time from one call of REHASH-ALL to the next. In a phase, and are fixed. As in the correctness proof, an update is effective when it changes . Whether an update is effective depends only on , so the sequence of sets is fixed by the sequence of operations and does not depend on the random choices.
Claim 1 (search). DPH-SEARCH takes time in the worst case. So does each operation that is not effective.
Proof. Lines L14 and L15 compute two hash values and compare one key. Lines L1 and L10 call it once.
Claim 2 (space). In each state the structure uses words.
Proof. The first level stores entries of words each ( is not stored apart from ; the Python version keeps it as a set of size , which adds ). The second level has slots by (D4). By the corollary, .
Lemma 3 (one rebuild of a bucket). Given any history, REBUILD-BUCKET takes expected time, plus if line L16 allocates a new array.
Proof. By Lemma 1 of the correctness proof, each trial succeeds with probability more than , whatever happened before, because its draws are fresh. So the expected number of trials is less than . A trial computes hash values and tests that they are distinct. The test takes time with the table itself: write a mark in each slot, stop at a slot that is already marked, then clear the marked slots. Line L16 clears slots, or allocates slots. Line L19 takes .
Lemma 4 (a collision is unlikely). Consider an effective insert of that reaches line L5 with for . The probability that the slot is in use, given the history up to this point, is at most .
Proof. Look at the last rebuild of bucket before this insert, and let be the history up to its start. Given , the function of the phase and the sequence of effective updates are fixed. Let be at that rebuild, and let be together with the keys that the effective updates insert into bucket from then up to and including . So is fixed given , and , since was set to by the rebuild and raised by one at each of these inserts.
The rebuild draws functions from the family with slots until is one-to-one on , so the accepted is a raw draw conditioned on the event that is one-to-one on . Let be the event that bucket is not rebuilt between the rebuild and this insert. A rebuild in between could come from line L9 or from REHASH-ALL. The rebuilds of cases 3 and 4 of the correctness proof, and REHASH-ALL at lines L3 and L12, depend only on counters, which are fixed given ; REHASH-ALL at line L8 depends only on the limits of the other buckets, because changes only at a rebuild of bucket , and these limits depend on draws for the other buckets, which are independent of . So, given , is the intersection of an event that depends only on (that and that no insert into bucket before found its slot in use) and an event independent of , which cancels out of the conditional probability.
Let be the event that is one-to-one on . Then : if is one-to-one on , it is one-to-one on , and each key placed before found a free slot, since the slots in use hold other keys of . By Lemma 1 of the correctness proof applied to , . Let be the event that for some . The slot of is in use only if it holds a key of , so the event of the claim implies . By (U), . So
Lemma 5 (rebuilds without doubling). The rebuilds of line L9 in cases 2 and 3 of the correctness proof take expected amortized time for each effective update.
Proof. Case 2 (a collision with ). By Lemma 3 the rebuild costs expected time; no array is allocated, since did not change. By Lemma 4 it happens with probability at most . So its expected cost, for each insert, is .
Case 3 ( and ). The cost is expected time. Since the last rebuild of bucket , let at that rebuild, and let and be the numbers of effective inserts and deletes in bucket . Then and . If , then and . In both cases , so the cost is . Charge it to these updates. Each update is charged at most once, to the first rebuild of its bucket after it, since a rebuild resets the count.
Lemma 6 (doubling). Fix a phase, and let be the number of distinct keys that are in at some time during the phase. Then at each time of the phase, . The arrays allocated in the phase, including those of the REHASH-ALL that starts it, have at most slots in total, and the rebuilds of case 4 take expected time in total.
Proof. REHASH-ALL sets . Line L7 doubles to only when , so and the new limit . A bucket allocates an array at the start of the phase and after each doubling that passes line L8, of sizes , which grow by a factor of 4. Their sum is less than times the last one. The last ones are the current sizes, whose sum is at most by (D4); a doubling that fails line L8 allocates nothing in this phase. So the total is at most slots, and allocating them takes time. A rebuild of case 4 costs, by Lemma 3, expected time plus its allocation of slots, so these rebuilds take expected time in total.
Lemma 7 (REHASH-ALL). REHASH-ALL takes expected time, where , given any history.
Proof. By the proof of Lemma 2 of the correctness proof, a trial of lines L21 and L22 succeeds with probability more than , so the expected number of trials is less than , and each takes time. Line L23 then calls REBUILD-BUCKET for buckets. By Lemma 3, this takes expected time plus the allocation of slots.
Lemma 8 (line L8 rarely ends a phase). Let be the history up to the start of a phase, before its first-level function is drawn. Then the probability, given , that the phase ends at line L8 is less than .
Proof. Given , the set at the start, with keys, and the sequence of effective updates after it, are fixed. Let be together with the keys inserted by the next effective updates. Then . The phase reaches line L4 for at most effective updates, because the next one makes and ends the phase at line L3 or L12. So each key that is ever in a bucket during the phase is in . Let , so . By Lemma 6, at each time of the phase,
So line L8 can end the phase only if . For a raw draw of the first-level function, the identity , with the number of colliding pairs of , and (U) give , so by Markov's inequality . The function of the phase is a raw draw conditioned on the acceptance test of line L22, which passes with probability more than (Lemma 7). So the probability is less than .
Theorem (time). A sequence of operations, from the empty structure, takes expected time in total. So each operation takes expected amortized time.
Proof. Each operation does work outside the rebuilds (Claim 1, the counter of line L8). The rebuilds of cases 2 and 3 cost expected in total (Lemma 5). The rest of the cost is in the phases: each phase , with buckets, costs expected time given its history , for its REHASH-ALL (Lemma 7) and its doublings (Lemma 6). So the expected total is . There are at most phases, since each phase after the first starts during an effective update.
Let be the indicator that phase exists and ends at line L8. is fixed by , so by Lemma 8, , which gives . A phase with either ends at line L3 or L12, and then it contains effective updates of its own, or it is the last phase, and then . The phases are disjoint, so
The paper of Dietzfelbinger, Karlin, Mehlhorn, Meyer auf der Heide, Rohnert and Tarjan (SIAM Journal on Computing 23(4), 1994, pages 738 to 761) proves the same three bounds for its own version of the scheme; the constants and the accounting above are not taken from it.
Dynamic perfect hashing keeps its invariants and answers searches correctly full
This proof shows: correctness, termination.
Keys are in with prime. The structure starts with REHASH-ALL. We say that an operation is effective when it changes : an insert of a key not in , or a delete of a key in . Only effective operations reach line L2 or L11.
Invariants. After each operation:
- (D1) is the disjoint union of , and .
- (D2) has slots, maps into them and is one-to-one on , and the slots of that are not NIL hold exactly the keys of , each in slot .
- (D3) .
- (D4) .
- (D5) .
Fact (U). For each and distinct , . For this is the universality of the Carter and Wegman family. For , the reduction mod does nothing, and because and is prime, so the probability is 0.
Lemma 1 (REBUILD-BUCKET). If when REBUILD-BUCKET starts, it ends with probability 1, and afterwards (D2) holds for bucket and .
Proof. A random into slots has, by (U) and linearity of expectation, an expected number of colliding pairs in of at most . By Markov's inequality, a trial fails with probability less than , and the trials are independent, so the loop of lines L17 and L18 ends with probability 1. Line L16 leaves with slots, all NIL: a new array is NIL everywhere, and an old one has, by (D2) before the rebuild, no key outside the slots of the old keys , which line L16 clears (the new key of line L4, if any, was never stored). Line L19 then stores each key of in its own slot, since is one-to-one on , and sets .
Lemma 2 (REHASH-ALL). REHASH-ALL ends with probability 1 and establishes (D1) to (D5) for .
Proof. Let and . If , then and the sum of line L22 is . If , then , and with , , where is the number of colliding pairs, so by (U), . Since , , and by Markov's inequality a trial of lines L21 and L22 fails with probability less than . So the loop ends with probability 1. Line L21 makes (D1) hold. Line L23 sets , so Lemma 1 applies to each bucket and gives (D2) and (D3). Then , which is (D4), and gives (D5).
Lemma 3 (search). If (D1) and (D2) hold, DPH-SEARCH returns true exactly when .
Proof. Let . If , then by (D1), and by (D2), so line L15 returns true. If , then , and by (D2) each non-NIL slot of holds a key of , so and line L15 returns false.
Theorem. Each operation ends with probability 1 and keeps (D1) to (D5). So each search, and the test of lines L1 and L10, is correct.
Proof. By induction on the operations. The start is REHASH-ALL, covered by Lemma 2. Assume (D1) to (D5) before an operation. A search changes nothing. By Lemma 3, lines L1 and L10 return exactly for the operations that are not effective, which change nothing.
Effective insert of . If line L3 calls REHASH-ALL, Lemma 2 gives the invariants for . Otherwise after line L2, which keeps (D5). Line L4 adds to and nothing else, which keeps (D1), and raises and by 1 each, which keeps . Now there are four cases.
- Line L5 stores . Then , which is (D3). The slot was NIL, so by (D2) no key of has this slot, is one-to-one on , and the stored keys are exactly . (D4) is unchanged.
- but the slot was in use. Lines L6 to L8 do nothing, and line L9 rebuilds with . Lemma 1 applies.
- and . Line L9 rebuilds with , and Lemma 1 applies.
- and . Before line L4, , so now and . Line L7 doubles , so for the new limit. If line L8 calls REHASH-ALL, Lemma 2 applies to , which contains since line L4. Otherwise , which is (D4), and line L9 rebuilds with , so Lemma 1 applies.
In cases 2 to 4, the other buckets and do not change, so their invariants still hold.
Effective delete of . If line L12 calls REHASH-ALL, Lemma 2 gives the invariants for . Otherwise (D5) holds as above. By (D1) and (D2), for , and . Line L13 sets this slot to NIL and removes from , so the stored keys are again exactly , and stays one-to-one on the smaller set: (D1) and (D2) hold. goes down and and do not change, so (D3) and (D4) hold.
Corollary (the size of ). In each state, .
Proof. Let be the size of at the last REHASH-ALL. Since then, there were effective updates by (D5), each changing by 1, so . If , . Otherwise , and , which gives .