Proofs: FKS perfect hashing
These are the human proofs of FKS perfect hashing, at full length.
FKS perfect hashing builds in O(n) expected time and uses O(n) space full
This proof shows: time.expected, space.extra.
We use one fact about the family , with uniform in and uniform in :
(U) For each and each two distinct keys of , .
This is the universality of the Carter and Wegman family, proved in CLRS, Section 11.3 (fourth edition) and Section 11.3.3 (third edition). The model is the word RAM with , so each hash value costs .
Lemma 1 (collisions). Let have keys, let be random, and let be the number of unordered pairs with and . Then .
Proof. is the sum, over the pairs, of the indicator of the event . By (U), each indicator has expectation at most . By linearity of expectation, .
Lemma 2 (sum of squares). If the buckets of on have sizes , then .
Proof. For each integer , . Bucket contains colliding pairs, and each colliding pair lies in exactly one bucket, so . Summing over and using gives the claim.
Claim 1 (first level). One trial of lines L3 to L5 succeeds with probability more than .
Proof. If , the sum is and the trial succeeds. Let , so . By Lemmas 1 and 2, . The sum is a non-negative random variable, so by Markov's inequality .
Claim 2 (second level). For a bucket with keys and , one trial of lines L9 and L10 succeeds with probability more than .
Proof. If , there is no pair and each trial succeeds. Let , and let be the number of colliding pairs of under . The choice of is independent of the choice of , and is fixed once is fixed, so Lemma 1 applies to : . is a non-negative integer, so by Markov's inequality . The trial succeeds exactly when .
Theorem (time and space). FKS-BUILD runs in expected time. The structure has at most second-level slots and words in total, in every run.
Proof. Space. The loop of lines L2 to L5 ends only when , and . The first level stores triples and the pair . So the total is at most words.
Time of the first level. A trial costs : one hash value for each key, and lists. By Claim 1 and the geometric distribution of the number of trials, the expected number of trials is less than 2. So the first level takes expected time.
Time of the second level. Condition on the accepted , so that the sizes are fixed with . Bucket costs for line L7, and for each trial of lines L9 and L10, since a trial writes at most slots and clears the ones it wrote. By Claim 2, the expected number of trials is less than 2. Line L11 costs . So the expected cost of bucket is , and, by linearity of expectation, the second level costs in expectation, for each accepted . Taking the expectation over keeps the bound.
Adding the two levels gives expected time, which is for .
Search. FKS-SEARCH evaluates two hash functions and reads two arrays, so it takes time in every case.
FKS-SEARCH answers membership correctly, and FKS-BUILD ends with probability 1 full
This proof shows: correctness, termination.
Let be a set of distinct keys, with prime. We write for the size of bucket after the loop of lines L2 to L5.
Lemma 1 (the stored table). When FKS-BUILD returns, for each with , the array holds exactly the keys of , and each is in the slot . For each with , .
Proof. Fix . Line L7 sets , so exactly when . If , the loop of lines L9 and L10 ends only with a function that gives distinct slots to distinct keys of . Line L11 writes each to . Two keys of never write the same slot, so no write is lost, and after line L11 the slots of that are not NIL hold exactly the keys of . No later step changes .
Theorem 1 (search). After FKS-BUILD, FKS-SEARCH returns true if and false if , for each .
Proof. Let , the value of line L12.
Case . By line L4, , so and , and line L13 does not return. By Lemma 1, , so line L14 returns true.
Case . If , line L13 returns false. Else, by Lemma 1, the slot holds NIL or a key of . Neither is equal to , since . So line L14 returns false.
Theorem 2 (termination). FKS-BUILD ends with probability 1, and the expected number of trials of each loop is less than 2.
Proof. The complexity proof shows that one trial of lines L3 to L5 meets the test of line L5 with probability , and that, for each with , one trial of lines L9 and L10 meets the test of line L10 with probability . The trials of one loop use independent random choices, so the probability that the first trials all fail is , which tends to 0. So each loop ends with probability 1, and the number of its trials has a geometric distribution with mean (or ). There are loops, a finite number, so the build ends with probability 1.