Double Hashing and Removal
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, h1, chooses a key's home slot and a second, h2, chooses how far each later probe jumps. For a table of m slots, probe number i for key k is
Probe 0 is the home slot. Every later probe adds the same step h2(k) and wraps around the end of the array. The step must satisfy two conditions: h2(k) is never 0, and h2(k) shares no factor greater than 1 with m. A step of 0 revisits the home slot forever; a step that shares a factor greater than 1 with m cycles through only part of the table, so an insertion can fail while empty slots remain.
Two standard designs meet both conditions:
- Make m prime and use h2(k)=q−(kmodq) for a smaller prime q, or h2(k)=1+(kmodm′) for some positive integer m′<m. Every step from 1 to m−1 is then coprime with m.
- Make m a power of 2 and design h2 so that it always returns an odd number.
Example: Take m=11, h1(k)=kmod11 and h2(k)=7−(kmod7). For key 58, the home slot is 58mod11=3 and the step is 7−(58mod7)=7−2=5. If slots 3 and 8 are already full, the next probe is (3+2⋅5)mod11=13mod11=2, and 58 goes into slot 2.
Under the idealised model of uniform hashing, a search for an absent key at load factor α=n/m takes about 1/(1−α) probes. Analyses and experiments show double hashing performs close to this ideal: about 2 probes at α=0.5 and about 10 at α=0.9.
Removing a key
Deletion is where open addressing needs care. A search for k stops as soon as it reaches an empty slot, because an insertion of k 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 k | Insertion of k |
|---|---|---|
Empty | Stop: k is absent | Stop: place k in the first tombstone passed, or here if none |
Holds k | Stop: found | Stop: k is already present |
Holds another key | Continue probing | Continue probing |
Tombstone | Continue probing | Remember the first one, then continue |
Limit each operation to m probes, with i=0,…,m−1. If all m slots have been examined without finding k 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 n live keys and d tombstones, the effective load is (n+d)/m, even though only n/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 crosses the same threshold that triggers resizing.
What to remember
- Probe i for key k is (h1(k)+i⋅h2(k))modm.
- h2(k) must be nonzero and coprime with m 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 m 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
Probe i for key k is slot (h1(k)+i⋅h2(k))mod13. 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 27mod13=1. Slot 1 is empty, so 27 goes into slot 1.
For 53, the home slot is 53mod13=1 because 4×13=52. Slot 1 holds 27, so compute the step: h2(53)=1+(53mod11)=1+9, a step of ________ slots. Probe 1 is (1+10)mod13=11, which is empty, so 53 goes into slot 11.
For 79, the home slot is 79mod13=1 because 6×13=78. The step is h2(79)=1+(79mod11)=1+2, which is ________ slots. Probe 1 is (1+3)mod13=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 15mod13=2, which is empty, so 15 goes into slot 2 after one probe.
For 17, the home slot is 17mod13=4, which holds 79. The step is h2(17)=1+(17mod11)=1+6, or ________ slots. Probe 1 is (4+7)mod13=11, which holds 53. Probe 2 is 4+2×7, which is ________ before wrapping, and 18mod13=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 24mod13=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+(24mod11)=1+2, or ________ slots.
- Probe 1: (11+3)mod13=1, which holds 27.
- Probe 2: (11+6)mod13=4, which holds 79.
- Probe 3: 11+9=20, and 20mod13 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.