Contents
Double hashing
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 T. 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 h_1 and h_2, the ith location in the bucket sequence for value k in a hash table of |T| buckets is: Generally, h_1 and h_2 are selected from a set of universal hash functions; h_1 is selected to have a range of {0,|T|-1} and h_2 to have a range of {1,|T|-1}. Double hashing approximates a random distribution; more precisely, pair-wise independent hash functions yield a probability of (n/|T|)^2 that any pair of keys will follow the same bucket sequence.
Selection of h2(k)
The secondary hash function h_2(k) should have several characteristics: In practice:
Analysis
Let n be the number of elements stored in T, then T's load factor is. That is, start by randomly, uniformly and independently selecting two universal hash functions h_1 and h_2 to build a double hashing table T. All elements are put in T by double hashing using h_1 and h_2. Given a key k, the (i+1)-st hash location is computed by: Let T have fixed load factor. Bradford and Katehakis showed the expected number of probes for an unsuccessful search in T, 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.
Variants
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 1/|T|^2. There are additionally a significant number of mostly-overlapping hash sets; if and, then , and comparing additional hash values (expanding the range of i) is of no help.
Triple hashing
Adding a quadratic term i^2, i(i+1)/2 (a triangular number) or even (triple hashing) to the hash function improves the hash function somewhat but does not fix this problem; if: then
Enhanced double hashing
Adding a cubic term i^3 or (i^3-i)/6 (a tetrahedral number), does solve the problem, a technique known as enhanced double hashing. This 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 h_2(x)'s properties, allowing a hash function similar in property to (but still independent of) h_1 to be used.
This article is derived from Wikipedia and licensed under CC BY-SA 4.0. View the original article.
Wikipedia® is a registered trademark of the
Wikimedia Foundation, Inc.
Bliptext is not
affiliated with or endorsed by Wikipedia or the
Wikimedia Foundation.