Double hashing is a computer programming technique used in conjunction with open addressing in hash tables to resolve hash collisions, by using a secondary hash of the key as an offset when a collision occurs. Double hashing with open addressing is a classical data structure on a table .
The double hashing technique uses one hash value as an index into the table and then repeatedly steps forward an interval until the desired value is located, an empty location is reached, or the entire table has been searched; but this interval is set by a second, independent hash function. Unlike the alternative collision-resolution methods of linear probing and quadratic probing, the interval depends on the data, so that values mapping to the same location have different bucket sequences; this minimizes repeated collisions and the effects of clustering.
Given two random, uniform, and independent hash functions and , the th location in the bucket sequence for value in a hash table of buckets is: The locations can be conveniently calculated by incrementing the previous hash by , i.e.
Generally, and are selected from a set of universal hash functions; is selected to have a range of and to have a range of . Double hashing approximates a random distribution; more precisely, pair-wise independent hash functions yield a probability of that any pair of keys will follow the same bucket sequence.
The secondary hash function should have several characteristics:
The distribution characteristics of are irrelevant. It is analogous to a random-number generator.
In practice:
Let be the number of elements stored in , then 's load factor is . That is, start by randomly, uniformly and independently selecting two universal hash functions and to build a double hashing table . All elements are put in by double hashing using and . Given a key , the -st hash location is computed by:
Let have fixed load factor . Bradford and Katehakis showed the expected number of probes for an unsuccessful search in , still using these initially chosen hash functions, is regardless of the distribution of the inputs. Pair-wise independence of the hash functions suffices.
Like all other forms of open addressing, double hashing becomes linear as the hash table approaches maximum capacity. The usual heuristic is to limit the table loading to 75% of capacity. Eventually, rehashing to a larger size will be necessary, as with all other open addressing schemes.
Peter Dillinger's PhD thesis points out that double hashing produces unwanted equivalent hash functions when the hash functions are treated as a set, as in Bloom filters: If and , then and the sets of hashes are identical. This makes a collision twice as likely as the hoped-for .
There are additionally a significant number of mostly-overlapping hash sets; if and , then , and comparing additional hash values (expanding the range of ) is of no help.
Adding a third hash as a quadratic term (triple hashing) makes the overlap a lot less lightly, since equivalent classes now need to be generated by a collaboration of both and , at a cost of 50% more calculations due to the added hash function. Choices for the factor for this include and the triangular numbers . The added hash function should obey the same requirements as listed above for .
Using the triangular numbers make it easier to calculate the value by forward differencing: for the variety,
This kind of construction does not fully remove equivalent sets. If:
then
Adding a cubic term or (a tetrahedral number), does solve the problem, a technique known as enhanced double hashing. The tetrahedral number can be computed efficiently by forward differencing:
In addition to rectifying the collision problem, enhanced double hashing also removes double-hashing's numerical restrictions on 's properties, allowing a hash function similar in property to (but still independent of) to be used. (Using the numbering in § Selection, the first two requriements are removed.)