Sub topic 9
Double hashing
This chapter assumes you know linear probing, primary clustering, and modulo arithmetic from the earlier chapters. Double hashing is an open-addressing strategy that gives each key its own step size, so keys that collide at the start do not follow the same path afterward.
The problem it solves
Linear probing always steps by 1. If two keys land in the same home slot, they follow the same sequence of slots, and long runs of occupied slots (primary clusters) form. Quadratic probing breaks up those runs, but any two keys with the same home slot still share one probe sequence. This is called secondary clustering.
Double hashing removes that shared path. It uses two hash functions:
h1(key)gives the home slot.h2(key)gives the step size.
The slot tried on attempt j is:
(h1(key) + j * h2(key)) % capacityTwo keys with the same h1 usually have different h2 values, so their sequences split after the first slot.
A worked example
The function below builds the first few probe slots for an integer key. It uses key // capacity as the input to the second hash, so h2 differs for keys that share a home slot.
def probe_sequence(key, capacity, count):
# capacity should be prime so every slot can be reached; key is an int
h1 = key % capacity
h2 = 1 + (key // capacity) % (capacity - 1)
return [(h1 + j * h2) % capacity for j in range(count)]
print(probe_sequence(10, 7, 4))
print(probe_sequence(17, 7, 4))Output:
[3, 5, 0, 2]
[3, 6, 2, 5]Keys 10 and 17 both start at slot 3. Key 10 has h2 = 2 and key 17 has h2 = 3, so their second probes are slots 5 and 6.
Rules for choosing h2
Three rules keep the probe sequence valid.
h2must never be 0. A step of 0 probes the same slot forever. The1 +in the code above prevents this.h2must be coprime with the capacity. If they share a factor, the sequence visits only some of the slots. With capacity 8 andh2 = 4, the probes visit just two slots, so an insert can fail while the table still has free space.- The easiest way to guarantee rule 2 is a prime capacity. Every
h2in1..capacity-1is then coprime with it, and the sequence can reach every slot. A power-of-two capacity also works ifh2is always odd.
Comparing the probing schemes
| Scheme | Step on attempt j | Primary clustering | Secondary clustering | Cache behavior |
|---|---|---|---|---|
| Linear | `j` | Yes | Yes | Best |
| Quadratic | `j²` | No | Yes | Good |
| Double hashing | `j * h2(key)` | No | No | Worst |
Double hashing comes closest to the ideal of every key having an independent random probe sequence. The cost is memory locality. Jumping by h2 slots touches distant cache lines, while linear probing reads neighboring slots that are already in cache. In practice linear probing is often faster at moderate load factors, even though it makes more probes.
What stays the same
Everything else about open addressing carries over unchanged:
- Lookup follows the probe sequence until it finds the key or an empty slot.
- Deletion still needs tombstones. Marking a slot empty would cut the probe chain of other keys that passed through it.
- Resizing still triggers on load factor. Double hashing tolerates higher load than linear probing, but performance still degrades sharply as the table approaches full. Keep it below about 0.7 to 0.8. When you resize, choose a new prime capacity and reinsert every key, because
h1andh2both depend on the capacity.
Exercise
Using probe_sequence with capacity 7, what are the first four probe slots for key 24, and why is the capacity chosen as 7?
Show answer
h1 = 24 % 7 = 3. h2 = 1 + (24 // 7) % 6 = 1 + 3 = 4. The slots are 3, (3+4)%7 = 0, (3+8)%7 = 4, and (3+12)%7 = 1, so the result is [3, 0, 4, 1]. The capacity 7 is prime, so every step size from 1 to 6 is coprime with it and the sequence can reach all seven slots.