Double hashing

Uses your system voice; voice and rate in settings.

Snapshot

≈ 8 min read

Names in other languages

No names in other languages are held for this entry yet.

Article bodies are English and are not machine-translated. A name in another language is not a translation of the article.

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 i th location in the bucket sequence for value x in a hash table of |T| buckets is: h(i,x)=(h_1(x) + i \cdot h_2(x))\bmod|T|. The locations can be conveniently calculated by incrementing the previous hash by h_2(x) , i.e. h(i+1,x)=(h(i,x) + h_2(x))\bmod|T|. 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 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 i th location in the bucket sequence for value x in a hash table of |T| buckets is: h(i,x)=(h_1(x) + i \cdot h_2(x))\bmod|T|. The locations can be conveniently calculated by incrementing the previous hash by h_2(x) , i.e. h(i+1,x)=(h(i,x) + h_2(x))\bmod|T|. 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 h 2 (x)

The secondary hash function h_2(x) should have several characteristics: It should never yield an index of zero. When 0 is returned, only one index is probed. All h_2(x) should be relatively prime to |T| . Otherwise the number of indices probed over k items would be \min(k,|T|/h_2(x)) , which could be as small as 2. It should cycle through the whole table. It should be very fast to compute. It should be pair-wise independent of h_1(x) . The distribution characteristics of h_2 are irrelevant. It is analogous to a random-number generator. In practice: If division hashing is used for both functions, the divisors are chosen as primes. If |T| is a power of 2, the first two requirements are usually satisfied by making h_2(x) always return an odd number by setting the least significant bit to 1 ( h_2(x) = h_{2,\text{orig}}(x) | 1 ). This has the side effect of doubling the chance of collision due to one wasted bit. h(i,x) = ( h_1(x) + i \cdot h_2(x) ) \bmod |T|. Given this setup, theoretical analyses seek to determine the time needed to perform an additional insertion (or, equivalently, the time needed to perform an unsuccessful search). Guibas and Szemerédi proved in 1978 that, if h_1 and h_2 are uniformly random, and \alpha , then the expected time is O(1) . Subsequent work by Lueker and Molodowitch proved a bound of 1/(1-\alpha) for any \alpha , and established that the behavior of the hash table can be directly coupled to that of a standard random-probing based solution. Much more recently, in 2007, Bradford and Katehakis showed that even using universal hash functions, rather than fully random ones, suffices to get a 1/(1-\alpha) bound. 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 h_2(y) = -h_2(x) and h_1(y) = h_1(x) + k\cdot h_2(x) , then h(i, y) = h(k - i, x) and the sets of hashes \left\{h(0, x), ..., h(k, x)\right\} = \left\{h(0, y), ..., h(k, y)\right\} 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 h_2(y) = h_2(x) and h_1(y) = h_1(x) \pm h_2(x) , then h(i, y) = h(i\pm 1, x) , and comparing additional hash values (expanding the range of i ) is of no help. from collections.abc import Iterator, Callable from typing import TypeVar T = TypeVar('T') hashfunc = Callable[T, int] assume h1, h2, h3 are defined and of type hashfunc MODULUS = (1 Iterator[int]: """Return k iterations of a triple hash.""" x, y, z = h1(key), h2(key), h3(key) yield x for i in range(1, k-1): x = (x + y) % MODULUS y = (y + z) % MODULUS yield x This kind of construction does not fully remove equivalent sets. If: h_1(y) = h_1(x) + k \cdot h_2(x) + k^2 \cdot h_3(x), h_2(y) = -h_2(x) - 2k \cdot h_3(x), and h_3(y) = h_3(x). then \begin{align} h(k-i, y) &= h_1(y) + (k - i) \cdot h_2(y) + (k-i)^2 \cdot h_3(y) \\ &= h_1(y) + (k - i) (-h_2(x) - 2k h_3(x)) + (k-i)^2 h_3(x) \\ &= \ldots \\ &= h_1(x) + k h_2(x) + k^2 h_3(x) + (i - k) h_2(x) + (i^2 - k^2) h_3(x) \\ &= h_1(x) + i h_2(x) + i^2 h_3(x) \\ &= h(i, x). \\ \end{align} struct key; /// Opaque /// Replace "unsigned int" with other types as needed. (Must be unsigned for guaranteed wrapping.) typedef unsigned int hashfunc(struct key const *); extern hashfunc h1, h2; /// Calculate k hash values from two underlying hash functions /// h1() and h2() using enhanced double hashing. On return, /// hashes[i] = h1(x) + i*h2(x) + (i*i*i - i)/6. /// Takes advantage of automatic wrapping (modular reduction) /// of unsigned types in C. void ext_dbl_hash(struct key const *x, unsigned int hashes[], unsigned int n) { unsigned int a = h1(x), b = h2(x), i = 0; hashes[i] = a; for (i = 1; i 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. (Using the numbering in § Selection, the first two requirements are removed.)

See also

Cuckoo hashing 2-choice hashing

References

External links

How Caching Affects Hashing by Gregory L. Heileman and Wenbin Luo 2005. Hash Table Animation klib a C library that includes double hashing functionality.

Text derived from https://en.wikipedia.org/w/index.php?oldid=1374753379 under CC BY-SA 4.0; modifications have been made.

Provenance

snapshot enwiki-001374753379 · captured 1789333740.0 · version history

Cite this page as: encyclopegia.org, “Double hashing”, snapshot enwiki-001374753379. https://encyclopegia.org/a/double-hashing

Sources cited in this revision

Extracted from the cited revision's reference blocks; absent citations are not invented.