Double Hashing

When a hash table stores every key directly in its array, a collision forces the table to look elsewhere, following a fixed sequence of slots called a probe sequence. Double hashing chooses that sequence with a second hash function, so keys that collide at the same slot usually scatter in different directions. This lesson shows how the probe sequence works, what the second function must satisfy, and how to remove a key without breaking later searches.

The probe sequence

Double hashing is an open-addressing scheme in which one hash function, h1h_1, chooses a key's home slot and a second, h2h_2, chooses how far each later probe jumps. For a table of mm slots, probe number ii for key kk is


h(k,i)=(h1(k)+i⋅h2(k)) mod m,i=0,1,2,…h(k, i) = \big(h_1(k) + i \cdot h_2(k)\big) \bmod m, \qquad i = 0, 1, 2, \ldots


Probe 0 is the home slot. Every later probe adds the same step h2(k)h_2(k) and wraps around the end of the array. The step must satisfy two conditions: h2(k)h_2(k) is never 0, and h2(k)h_2(k) shares no factor greater than 1 with mm. A step of 0 revisits the home slot forever; a step that shares a factor greater than 1 with mm cycles through only part of the table, so an insertion can fail while empty slots remain.


Two standard designs meet both conditions:

  • Make mm prime and use h2(k)=q−(k mod q)h_2(k) = q - (k \bmod q) for a smaller prime qq, or h2(k)=1+(k mod m′)h_2(k) = 1 + (k \bmod m') for some positive integer m′<mm' < m. Every step from 1 to m−1m - 1 is then coprime with mm.
  • Make mm a power of 2 and design h2h_2 so that it always returns an odd number.

Example: Take m=11m = 11, h1(k)=k mod 11h_1(k) = k \bmod 11 and h2(k)=7−(k mod 7)h_2(k) = 7 - (k \bmod 7). For key 58, the home slot is 58 mod 11=358 \bmod 11 = 3 and the step is 7−(58 mod 7)=7−2=57 - (58 \bmod 7) = 7 - 2 = 5. If slots 3 and 8 are already full, the next probe is (3+2⋅5) mod 11=13 mod 11=2(3 + 2 \cdot 5) \bmod 11 = 13 \bmod 11 = 2, and 58 goes into slot 2.


A row of eleven hash table slots in which key 58 probes slot 3, then slot 8, then wraps around to slot 2.



Under the idealised model of uniform hashing, a search for an absent key at load factor α=n/m\alpha = n/m takes about 1/(1−α)1/(1 - \alpha) probes. Analyses and experiments show double hashing performs close to this ideal: about 2 probes at α=0.5\alpha = 0.5 and about 10 at α=0.9\alpha = 0.9.

Removing a key

Deletion is where open addressing needs care. A search for kk stops as soon as it reaches an empty slot, because an insertion of kk would have used that slot. Simply emptying a deleted slot while leaving affected keys in place can make keys that probed past it unreachable.


The deletion method taught here replaces the removed key with a tombstone, a marker meaning that a key used to live here. Each operation treats the four slot states differently:


Slot state

Search for kk

Insertion of kk

Empty

Stop: kk is absent

Stop: place kk in the first tombstone passed, or here if none

Holds kk

Stop: found

Stop: kk is already present

Holds another key

Continue probing

Continue probing

Tombstone

Continue probing

Remember the first one, then continue


Limit each operation to mm probes, with i=0,…,m−1i = 0, \ldots, m - 1. If all mm slots have been examined without finding kk or an empty slot, a search reports absence. An insertion then reuses the first remembered tombstone, or reports a full table if none was found.



Tombstones have a cost. A search never stops at one, so for unsuccessful searches tombstones count as occupied slots: with nn live keys and dd tombstones, the effective load is (n+d)/m(n + d)/m, even though only n/mn/m of the table holds data. Insertions can reuse tombstone slots, which slows the build-up but does not stop it when deletions are frequent. When tombstones accumulate, rebuild the table by reinserting the live keys into a fresh array, which discards every tombstone. Many implementations rebuild when (n+d)/m(n + d)/m crosses the same threshold that triggers resizing.

What to remember

  • Probe ii for key kk is (h1(k)+i⋅h2(k)) mod m(h_1(k) + i \cdot h_2(k)) \bmod m.
  • h2(k)h_2(k) must be nonzero and coprime with mm so the sequence can reach every slot.
  • Keys that share a home slot usually follow different paths, which avoids clustering.
  • Remove a key by leaving a tombstone; searches continue past tombstones and insertions may reuse them.
  • Stop after at most mm probes: report absence, reuse a remembered tombstone for insertion, or report a full table.
  • Tombstones raise the effective load, so rebuild the table when they pile up.

Example: Tracing inserts and a deletion with double hashing

A hash table has 13 slots, numbered 0 to 12, and resolves collisions by double hashing with


h1(k)=k mod 13,h2(k)=1+(k mod 11)h_1(k) = k \bmod 13, \qquad h_2(k) = 1 + (k \bmod 11)


Probe ii for key kk is slot (h1(k)+i⋅h2(k)) mod 13(h_1(k) + i \cdot h_2(k)) \bmod 13. A removal leaves a tombstone. An insertion probes until it reaches an empty slot, which confirms the key is absent, and then stores the key in the first tombstone it passed, or in that empty slot if it passed none. The table starts empty.


a) Insert 27, 53 and 79 in that order. In which slot does each key end up?


For 27, the home slot is 27 mod 13=127 \bmod 13 = 1. Slot 1 is empty, so 27 goes into slot 1.


For 53, the home slot is 53 mod 13=153 \bmod 13 = 1 because 4×13=524 \times 13 = 52. Slot 1 holds 27, so compute the step: h2(53)=1+(53 mod 11)=1+9h_2(53) = 1 + (53 \bmod 11) = 1 + 9, a step of ________ slots. Probe 1 is (1+10) mod 13=11(1 + 10) \bmod 13 = 11, which is empty, so 53 goes into slot 11.


For 79, the home slot is 79 mod 13=179 \bmod 13 = 1 because 6×13=786 \times 13 = 78. The step is h2(79)=1+(79 mod 11)=1+2h_2(79) = 1 + (79 \bmod 11) = 1 + 2, which is ________ slots. Probe 1 is (1+3) mod 13=4(1 + 3) \bmod 13 = 4, which is empty, so 79 goes into slot 4.


All three keys share home slot 1, yet they end up in slots 1, 11 and 4. Linear probing would have packed them into slots 1, 2 and 3.


b) Insert 15 and then 17. Give each slot, and count how many slots the insertion of 17 examines.


For 15, the home slot is 15 mod 13=215 \bmod 13 = 2, which is empty, so 15 goes into slot 2 after one probe.


For 17, the home slot is 17 mod 13=417 \bmod 13 = 4, which holds 79. The step is h2(17)=1+(17 mod 11)=1+6h_2(17) = 1 + (17 \bmod 11) = 1 + 6, or ________ slots. Probe 1 is (4+7) mod 13=11(4 + 7) \bmod 13 = 11, which holds 53. Probe 2 is 4+2×74 + 2 \times 7, which is ________ before wrapping, and 18 mod 13=518 \bmod 13 = 5. Slot 5 is empty, so 17 goes into slot 5.


The insertion of 17 examined three slots: 4, 11 and 5. The table now holds:

  • slot 1: 27
  • slot 2: 15
  • slot 4: 79
  • slot 5: 17
  • slot 11: 53

c) Remove 53, then search for 17. Which slots does the search examine? What would go wrong if the removal had simply emptied the slot instead?


Removing 53 first searches for it: slot 1 holds 27, and probe 1 reaches slot 11, which holds 53. Slot 11 now becomes a tombstone.


The search for 17 starts at slot 4, which holds 79, so it continues. Probe 1 reaches slot 11, the tombstone; a search never stops at a tombstone, so it continues. Probe 2 reaches slot 5, which holds 17. The search examines slots 4, 11 and 5 and finds 17.


If the removal had emptied slot 11, the search would stop there, since an empty slot means an insertion of 17 would have used it. It would report 17 as absent while 17 sits in slot 5.


d) Now insert 24. Which slots does the insertion examine, and where does 24 end up?


The home slot is 24 mod 13=1124 \bmod 13 = 11, the tombstone. Note it as the first tombstone passed, then keep probing to make sure 24 is not already stored further along. The step is h2(24)=1+(24 mod 11)=1+2h_2(24) = 1 + (24 \bmod 11) = 1 + 2, or ________ slots.

  • Probe 1: (11+3) mod 13=1(11 + 3) \bmod 13 = 1, which holds 27.
  • Probe 2: (11+6) mod 13=4(11 + 6) \bmod 13 = 4, which holds 79.
  • Probe 3: 11+9=2011 + 9 = 20, and 20 mod 1320 \bmod 13 is ________, an empty slot, so the search stops and 24 is confirmed absent.

The insertion examined four slots, 11, 1, 4 and 7, and stores 24 in slot 11, the first tombstone it passed. Because slot 11 is the home slot of 24, every later search for 24 succeeds on its first probe.

In double hashing with table size mm, probe ii for key kk is slot (h1(k)+i⋅h2(k)) mod m(h_1(k) + i \cdot h_2(k)) \bmod m. If h2(k)h_2(k) returned for some key, every probe for that key would revisit its home slot. More generally, the probe sequence reaches every slot only when the greatest common divisor of h2(k)h_2(k) and mm equals . That is why a table whose size mm is a power of 2 uses a second hash function that always returns an number.

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

A cache uses double hashing and needs rebuilding, the reconstruction of its table from the key–value pairs still stored. Its current table contains tombstones, and the replacement table will have a larger capacity. No cache operations run during rebuilding.


Arrange the stages into a valid execution order.

Loading ordering steps…

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

An open-addressed hash table has 7 slots, numbered 0 to 6, and uses double hashing with h1(k)=k mod 7h_1(k) = k \bmod 7 and h2(k)=5−(k mod 5)h_2(k) = 5 - (k \bmod 5). Starting from an empty table, the keys 18 and 25 are inserted in that order. Key 18 is then removed by setting its slot back to empty, with no tombstone. What happens when the table is next searched for 25?

Choose an answer

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

Extra Practice