ArticleslgStudy

computer science

Double hashing

Double hashing is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Double hashing rather than just read about it. In short: 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.

Key takeaways

  • Double hashing belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Double hashing to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Double hashing from memory before moving on to harder problems.

Reference excerpt

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 ith location in the bucket sequence for value x in a hash table of |T| buckets is: h(i,x)=(h1(x)+i⋅h2(x))mod|T|. The locations can be conveniently calculated by incrementing the previous hash by h2(x), i.e. h(i+1,x)=(h(i,x)+h2(x))mod|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(x)

The secondary hash function h2(x) should have several characteristics:

It should never yield an index of zero. When 0 is returned, only one index is probed. All h2(x) should be relatively prime to |T|. Otherwise the number of indices probed over k items would be min⁡(k,|T|/h2(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 h1(x). The distribution characteristics of h2 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 h2(x) always return an odd number by setting the least significant bit to 1 (h2(x)=h2,orig(x)|1). This has the side effect of doubling the chance of collision due to one wasted bit.

Analysis Suppose one selects two hash functions h1 and h2 and inserts α|T| elements into a hash table with |T| slots. Suppose each insertion of a key x places the key in the first available slot from the sequence h(0,x),h(1,x),h(2,x),… defined by h(i,x)=(h1(x)+i⋅h2(x))mod|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 h1 and h2 are uniformly random, and α<0.319, then the expected time is O(1). Subsequent work by Lueker and Molodowitch proved a bound of 1/(1−α) for any α, 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−α) 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.

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 h2(y)=−h2(x) and h1(y)=h1(x)+k⋅h2(x), then h(i,y)=h(k−i,x) and the sets of hashes {h(0,x),...,h(k,x)}={h(0,y),...,h(k,y)} 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)±h2(x), then h(i,y)=h(i±1,x), and comparing additional hash values (expanding the range of i) is of no help.

Triple hashing Adding a third hash as a quadratic term (triple hashing) makes the overlap a lot less likely, since equivalent classes now need to be generated by a collaboration of both h2(x) and h3(x), at a cost of 50% more calculations due to the added hash function. Choices for the factor for this h3(x) include i2 and the triangular numbers i(i±1)/2. The added hash function should obey the same requirements as listed above for h2(x). Using the triangular numbers make it easier to calculate the value by forward differencing: for the i(i−2)/2 variety,

This kind of construction does not fully remove equivalent sets. If:

h1(y)=h1(x)+k⋅h2(x)+k2⋅h3(x), h2(y)=−h2(x)−2k⋅h3(x), and h3(y)=h3(x). then

h(k−i,y)=h1(y)+(k−i)⋅h2(y)+(k−i)2⋅h3(y)=h1(y)+(k−i)(−h2(x)−2kh3(x))+(k−i)2h3(x)=…=h1(x)+kh2(x)+k2h3(x)+(i−k)h2(x)+(i2−k2)h3(x)=h1(x)+ih2(x)+i2h3(x)=h(i,x).

Enhanced double hashing Adding a cubic term i3 or (i3−i)/6 (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 h2(x)'s properties, allowing a hash function similar in property to (but still independent of) h1 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.

Worked examples

Example 1 — a first encounter with Double hashing

Start with the simplest possible case. Write down what Double hashing claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Double hashing before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Double hashing ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Double hashing

In research
Double hashing appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Double hashing in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Double hashing is common in secondary-school and first-year university syllabi. It links to neighbouring topics Hashing, Search algorithms, so understanding it makes those chapters shorter.
In everyday life
Look for Double hashing outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Double hashing” →

How to study Double hashing in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Double hashing means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Double hashing out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Double hashing in simple terms?

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.

Why does Double hashing matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Double hashing?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Double hashing.

Tags

  • Hashing
  • Search algorithms

Keep exploring