Double hashing explained

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

h1

and

h2

, the

i

th location in the bucket sequence for value

k

in a hash table of

|T|

buckets is:

h(i,k)=(h1(k)+ih2(k))\bmod|T|.

Generally,

h1

and

h2

are selected from a set of universal hash functions;

h1

is selected to have a range of

\{0,|T|-1\}

and

h2

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

h2(k)

should have several characteristics:

h1(k)

.

h2

are irrelevant. It is analogous to a random-number generator.

h2(k)

should be relatively prime to |T|.

In practice:

h2(k)

always return an odd number. This has the side effect of doubling the chance of collision due to one wasted bit.

Analysis

Let

n

be the number of elements stored in

T

, then

T

's load factor is

\alpha=n/|T|

. That is, start by randomly, uniformly and independently selecting two universal hash functions

h1

and

h2

to build a double hashing table

T

. All elements are put in

T

by double hashing using

h1

and

h2

.Given a key

k

, the

(i+1)

-st hash location is computed by:

h(i,k) = (h_1(k) + i \cdot h_2(k)) \bmod |T|.

Let

T

have fixed load factor

\alpha:1>\alpha>0

.Bradford and Katehakis[1] showed the expected number of probes for an unsuccessful search in

T

, still using these initially chosen hash functions, is

\tfrac{1}{1-\alpha}

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[2] points out that double hashing produces unwanted equivalent hash functions when the hash functions are treated as a set, as in Bloom filters: If

h2(y)=-h2(x)

and

h1(y)=h1(x)+kh2(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

h2(y)=h2(x)

and

h1(y)=h1(x)\pmh2(x)

, then

h(i,y)=h(i\pm1,x)

, and comparing additional hash values (expanding the range of

i

) is of no help.

Triple hashing

Adding a quadratic term

i2,

[3]

i(i+1)/2

(a triangular number) or even

i2h3(x)

(triple hashing)[4] to the hash function improves the hash function somewhat[3] but does not fix this problem; if:

h1(y)=h1(x)+kh2(x)+k2h3(x),

h2(y)=-h2(x)-2kh3(x),

and

h3(y)=h3(x).

then

\begin{align} h(k-i,y)&=h1(y)+(k-i)h2(y)+(k-i)2h3(y)\\ &=h1(y)+(k-i)(-h2(x)-2kh3(x))+(k-i)2h3(x)\\ &=\ldots\\ &=h1(x)+kh2(x)+k2h3(x)+(i-k)h2(x)+(i2-k2)h3(x)\\ &=h1(x)+ih2(x)+i2h3(x)\\ &=h(i,x).\\ \end{align}

Enhanced double hashing

i3

[3] or

(i3-i)/6

(a tetrahedral number),[5] does solve the problem, a technique known as enhanced double hashing. This can be computed efficiently by forward differencing:struct key; /// Opaque/// Use other data types when needed. (Must be unsigned for guaranteed wrapping.)extern unsigned int h1(struct key const *), h2(struct key const *);

/// 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)

In addition to rectifying the collision problem, enhanced double hashing also removes double-hashing's numerical restrictions on

h2(x)

's properties, allowing a hash function similar in property to (but still independent of)

h1

to be used.[5]

See also

External links

Notes and References

  1. .
  2. Adaptive Approximate State Storage . Peter C. . Dillinger . December 2010 . Northeastern University . PhD thesis . 93–112 .
  3. Less Hashing, Same Performance: Building a Better Bloom Filter . Adam . Kirsch . Michael . Mitzenmacher . Michael Mitzenmacher . Random Structures and Algorithms . 33 . 2 . 187–218 . September 2008 . 10.1002/rsa.20208 . 10.1.1.152.579 .
  4. Alternatively defined with the triangular number, as in Dillinger 2004.
  5. Bloom Filters in Probabilistic Verification . Peter C. . Dillinger . Panagiotis . Manolios . 5h International Conference on Formal Methods in Computer Aided Design (FMCAD 2004) . Austin, Texas . November 15–17, 2004 . 10.1007/978-3-540-30494-4_26 . 10.1.1.119.628 .