Perfect hash function explained

In computer science, a perfect hash function for a set is a hash function that maps distinct elements in to a set of integers, with no collisions. In mathematical terms, it is an injective function.

Perfect hash functions may be used to implement a lookup table with constant worst-case access time. A perfect hash function can, as any hash function, be used to implement hash tables, with the advantage that no collision resolution has to be implemented. In addition, if the keys are not in the data and if it is known that queried keys will be valid, then the keys do not need to be stored in the lookup table, saving space.

Disadvantages of perfect hash functions are that needs to be known for the construction of the perfect hash function. Non-dynamic perfect hash functions need to be re-constructed if changes. For frequently changing dynamic perfect hash functions may be used at the cost of additional space. The space requirement to store the perfect hash function is in where is the number of keys in the structure.

The important performance parameters for perfect hash functions are the evaluation time, which should be constant, the construction time, and the representation size.

Application

A perfect hash function with values in a limited range can be used for efficient lookup operations, by placing keys from (or other associated values) 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. With perfect hashing, the associated data can be read or written with a single access to the table.

Performance of perfect hash functions

The important performance parameters for perfect hashing are the representation size, the evaluation time, the construction time, and additionally the range requirement

m
n
(average number of buckets per key in the hash table). The evaluation time can be as fast as, which is optimal. The construction time needs to be at least, because each element in needs to be considered, and contains elements. This lower bound can be achieved in practice.

The lower bound for the representation size depends on and . Let and a perfect hash function. A good approximation for the lower bound is

loge-\varepsilonlog

1+\varepsilon
\varepsilon
Bits per element. For minimal perfect hashing,, the lower bound is bits per element.

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 (larger than the size of the universe from which is drawn), and a parameter, and maps each element of to the index

g(x)=(kx\bmodp)\bmodn.

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 .

A more recent method for constructing a perfect hash function is described by as "hash, displace, and compress". Here a first-level hash function is also used to map elements onto a range of integers. An element is stored in the Bucket .

Then, in descending order of size, each bucket's elements are hashed by a hash function of a sequence of independent fully random hash functions, starting with . If the hash function does not produce any collisions for the bucket, and the resulting values are not yet occupied by other elements from other buckets, the function is chosen for that bucket. If not, the next hash function in the sequence is tested.

To evaluate the perfect hash function one only has to save the mapping σ of the bucket index onto the correct hash function in the sequence, resulting in .

Finally, to reduce the representation size, the (are compressed into a form that still allows the evaluation in .

This approach needs linear time in for construction, and constant evaluation time. The representation size is in, and depends on the achieved range. For example, with achieved a representation size between 3.03 bits/key and 1.40 bits/key for their given example set of 10 million entries, with lower values needing a higher computation time. The space lower bound in this scenario is 0.88 bits/key.

Pseudocode

algorithm hash, displace, and compress is (1) Split S into buckets (2) Sort buckets Bi in falling order according to size |Bi| (3) Initialize array T[0...m-1] with 0's (4) for all i∈[r], in the order from (2), do (5) for l←1,2,... (6) repeat forming Ki← (6) until |Ki|=|Bi| and Ki∩=∅ (7) let σ(i):= the successful l (8) for all j∈Ki let T[j]:=1 (9) Transform (σi)0≤i into compressed form, retaining access.

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 timerequires at least a number of bits that is proportional to the size of .[1]

For minimal perfect hash functions the information theoretic space lower bound is

log2e ≈ 1.44

bits/key.

U\supseteqS

whose size tends towards infinity, the space lower bounds is

log2e-\varepsilonlog

1+\varepsilon
\varepsilon
bits/key, minus bits overall.

Extensions

Memory address identity

A trivial but pervasive example of perfect hashing is implicit in the (virtual) memory address space of a computer. Since each byte of virtual memory is a distinct, unique, directly addressable storage location, the value of the starting address where any object is stored in memory can be considered a de facto perfect hash of that object into the entire memory address range.[2]

Dynamic perfect hashing

See main article: article and 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,[3] 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 (injectivity) 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

log2e1.44

bits/key.[4] Assuming that

S

is a set of size

n

containing integers in the range

[1,2o(n)]

, it is known how to efficiently construct an explicit minimal perfect hash function from

S

to

\{1,2,\ldots,n\}

that uses space

nlog2e+o(n)

bits and that supports constant evaluation time. In practice, there are minimal perfect hashing schemes that use roughly 1.56 bits/key if given enough time.[5]

k-perfect hashing

A hash function is -perfect if at most elements from are mapped onto the same value in the range. The "hash, displace, and compress" algorithm can be used to construct -perfect hash functions by allowing up to collisions. The changes necessary to accomplish this are minimal, and are underlined in the adapted pseudocode below: (4) for all i∈[r], in the order from (2), do (5) for l←1,2,... (6) repeat forming Ki← (6) until |Ki|=|Bi| and Ki∩=∅ (7) let σ(i):= the successful l (8) for all j∈Ki set T[j]←T[j]+1

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 (ordinary) perfect hash function to store a lookup table of the positions of each key. This solution uses

O(nlogn)

bits, which is optimal in the setting where the comparison function for the keys may be arbitrary.[6] However, if the keys are integers drawn from a universe

\{1,2,\ldots,U\}

, then it is possible to construct an order-preserving hash function using only

O(nlogloglogU)

bits of space.[7] Moreover, this bound is known to be optimal.

Related constructions

While well-dimensioned hash tables have amortized average O(1) time (amortized average constant time) for lookups, insertions, and deletion, most hash table algorithms suffer from possible worst-case times that take much longer.A worst-case O(1) time (constant time even in the worst case) would be better for many applications (including network router and memory caches).[8]

Few hash table algorithms support worst-case O(1) lookup time (constant lookup time even in the worst case). The few that do include: perfect hashing; dynamic perfect hashing; cuckoo hashing; hopscotch hashing; and extendible hashing.[8]

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 (unlike perfect hashing which maps each key to a single location) 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.[9]

Further reading

External links

C Minimal Perfect Hashing Library, open source implementations for many (minimal) perfect hashes (works for big sets)

open source monotone minimal perfect hashing in Java

perfect hashing methods in C#

minimal perfect hash function in header-only C++

Notes and References

  1. .
  2. Book: Springer Science+Business Media. Advances in Databases and Information Systems. 254. Witold Litwin. Tadeusz Morzy. Gottfried Vossen. 19 August 1998. 9783540649243.
  3. .
  4. .
  5. .
  6. .
  7. .
  8. Timothy A. Davis."Chapter 5 Hashing"

    subsection "Hash Tables with Worst-Case O(1) Access"

  9. .