A perfect hash function with values in a limited range can be used for efficient lookup operations, by placing keys from in a lookup table indexed by the output of the function. One can then test whether a key is present in, or look up a value associated with that key, by looking for it at its cell of the table. Each such lookup takes constant time in the worst case.
Construction
A perfect hash function for a specific set that can be evaluated in constant time, and with values in a small range, can be found by a randomized algorithm in a number of operations that is proportional to the size of S. The original construction of uses a two-level scheme to map a set of elements to a range of indices, and then map each index to a range of hash values. The first level of their construction chooses a large prime , and a parameter, and maps each element of to the index If is chosen randomly, this step is likely to have collisions, but the number of elements that are simultaneously mapped to the same index is likely to be small. The second level of their construction assigns disjoint ranges of integers to each index. It uses a second set of linear modular functions, one for each index, to map each member of into the range associated with. As show, there exists a choice of the parameter such that the sum of the lengths of the ranges for the different values of is. Additionally, for each value of, there exists a linear modular function that maps the corresponding subset of into the range associated with that value. Both, and the second-level functions for each value of, can be found in polynomial time by choosing values randomly until finding one that works. The hash function itself requires storage space to store,, and all of the second-level linear modular functions. Computing the hash value of a given key may be performed in constant time by computing, looking up the second-level function associated with, and applying this function to. A modified version of this two-level scheme with a larger number of values at the top level can be used to construct a perfect hash function that maps into a smaller range of length.
Space lower bounds
The use of words of information to store the function of is near-optimal: any perfect hash function that can be calculated in constant time requires at least a number of bits that is proportional to the size of.
Extensions
Dynamic perfect hashing
Using a perfect hash function is best in situations where there is a frequently queried large set,, which is seldom updated. This is because any modification of the set may cause the hash function to no longer be perfect for the modified set. Solutions which update the hash function any time the set is modified are known as dynamic perfect hashing, but these methods are relatively complicated to implement.
Minimal perfect hash function
A minimal perfect hash function is a perfect hash function that maps keys to consecutive integers – usually the numbers from to or from to. A more formal way of expressing this is: Let and be elements of some finite set. Then is a minimal perfect hash function if and only if implies and there exists an integer such that the range of is. It has been proven that a general purpose minimal perfect hash scheme requires at least 1.44 bits/key. The best currently known minimal perfect hashing schemes can be represented using less than 1.56 bits/key if given enough time.
Order preservation
A minimal perfect hash function is order preserving if keys are given in some order and for any keys and, implies. In this case, the function value is just the position of each key in the sorted ordering of all of the keys. A simple implementation of order-preserving minimal perfect hash functions with constant access time is to use an perfect hash function or cuckoo hashing to store a lookup table of the positions of each key. If the keys to be hashed are themselves stored in a sorted array, it is possible to store a small number of additional bits per key in a data structure that can be used to compute hash values quickly. Order-preserving minimal perfect hash functions require necessarily bits to be represented.
Related constructions
A simple alternative to perfect hashing, which also allows dynamic updates, is cuckoo hashing. This scheme maps keys to two or more locations within a range but does so in such a way that the keys can be assigned one-to-one to locations to which they have been mapped. Lookups with this scheme are slower, because multiple locations must be checked, but nevertheless take constant worst-case time.