Sub topic 20
Open Addressing: Linear, Quadratic, and Double Hashing
This chapter assumes you know how a hash table maps a key to a slot with h(k), and what a collision is. It also assumes modular arithmetic. Chaining, the other collision strategy, is covered in the previous chapter.
The idea
In open addressing, every key is stored directly in the table array. There are no linked lists. If the home slot is taken, you look at another slot, then another, until you find an empty one. The table therefore has at most m keys, so the load factor α = n/m never exceeds 1.
The hash function takes a second argument, the probe number i:
h(k, i) for i = 0, 1, ..., m-1
The sequence h(k,0), h(k,1), ... is the probe sequence of k. A good probe sequence is a permutation of 0..m-1, so every slot is eventually tried. Insert, search, and delete all follow the same sequence.
The three probe sequences
Let h1(k) = k mod m.
| Scheme | Probe formula | Main weakness |
|---|---|---|
| Linear probing | `(h1(k) + i) mod m` | Primary clustering |
| Quadratic probing | `(h1(k) + c1·i + c2·i²) mod m` | Secondary clustering, may miss slots |
| Double hashing | `(h1(k) + i·h2(k)) mod m` | Needs a well-chosen `h2` |
Linear probing
Linear probing steps one slot at a time. It is simple and cache friendly. Its weakness is primary clustering: a run of filled slots grows whenever any key hashes into it or next to it. Long runs make later probes longer, which makes the runs longer still.
Quadratic probing
Quadratic probing jumps by growing amounts, so keys that hash near each other do not pile into one run. Two problems remain:
- Keys with the same
h1follow the same sequence. This is secondary clustering. - The sequence may not visit every slot. With
h(k,i) = (h1 + i²) mod 7, the offsetsi²mod 7 are only0, 1, 4, 2, so just 4 of the 7 slots are ever tried.
A common safe rule is a prime m with load factor below 1/2. Then an empty slot is always found. Another rule is m a power of two with c1 = c2 = 1/2, which visits every slot.
Double hashing
Double hashing uses a second hash h2(k) as the step size. Different keys get different steps, so both clustering effects mostly disappear. The step must be coprime to m, or the sequence cycles through only part of the table. Two standard choices:
mprime andh2(k) = 1 + (k mod (m-2)), which gives steps from 1 tom-2.ma power of two andh2(k)always odd.
h2(k) must never be 0, because the sequence would then never move.
Implementation
This code implements all three schemes and supports deletion with a tombstone, which is explained below.
EMPTY = None
DELETED = object() # tombstone marker
def probe(key, i, m, kind):
h1 = key % m
if kind == "linear":
return (h1 + i) % m
if kind == "quadratic":
return (h1 + i * i) % m
h2 = 1 + key % (m - 2)
return (h1 + i * h2) % m
def insert(table, key, kind):
m = len(table)
for i in range(m):
s = probe(key, i, m, kind)
if table[s] is EMPTY or table[s] is DELETED:
table[s] = key
return s
raise OverflowError("no free slot found")
def search(table, key, kind):
m = len(table)
for i in range(m):
s = probe(key, i, m, kind)
if table[s] is EMPTY:
return -1 # a never-used slot ends the search
if table[s] == key:
return s # DELETED never equals a key
return -1
def delete(table, key, kind):
s = search(table, key, kind)
if s != -1:
table[s] = DELETED
return s
for kind in ("linear", "quadratic", "double"):
t = [EMPTY] * 7
for k in [10, 17, 24, 3, 8]:
insert(t, k, kind)
print(kind, t)
t = [EMPTY] * 7
for k in [10, 17, 24, 3, 8]:
insert(t, k, "linear")
delete(t, 17, "linear")
print(search(t, 24, "linear")) # still found at slot 5Worked example
Insert 10, 17, 24, 3, 8 into a table of size 7. The home slots are 10→3, 17→3, 24→3, 3→3, 8→1. Four keys collide at slot 3.
| Scheme | Slots 0 to 6 | Notes |
|---|---|---|
| Linear | `-, 8, -, 10, 17, 24, 3` | 17 goes to 4, 24 to 5, 3 to 6 |
| Quadratic (`i²`) | `24, 8, -, 10, 17, 3, -` | 17 to 4. 24 tries 3, 4, then `3+4=7→0`. 3 tries 3, 4, 0, then `3+9=12→5` |
| Double (`h2 = 1 + k mod 5`) | `3, 24, -, 10, -, 8, 17` | 17: step 3, slot 6. 24: step 5, slot 1. 3: step 4, slot 0. 8: slot 1 is taken, step 4, slot 5 |
Probes per insert:
- Linear: 1, 2, 3, 4, 1, total 11.
- Quadratic: 1, 2, 3, 4, 1, also total 11.
The linear table has one run of four filled slots, from 3 to 6. Double hashing scatters the keys.
Deletion and tombstones
You cannot simply empty a deleted slot. Suppose you delete 17 from the linear table above and set slot 4 to empty. A search for 24 starts at slot 3, then hits the empty slot 4 and wrongly reports "not found". The fix is a tombstone:
- Search treats a tombstone as occupied and keeps probing.
- Insert may reuse a tombstone.
Many tombstones lengthen searches even when n is small. The usual remedy is to rehash the whole table periodically.
Expected cost
Under the uniform hashing assumption, the expected number of probes is at most 1/(1-α) for an unsuccessful search. For a successful search it is at most (1/α)·ln(1/(1-α)). For linear probing, Knuth's approximations are ½(1 + 1/(1-α)²) for an unsuccessful search and ½(1 + 1/(1-α)) for a successful one. At α = 0.9, uniform hashing needs about 10 probes for a miss, and linear probing needs about 50. The next chapter derives these in detail.
Exercise
A table of size 11 uses double hashing with h1(k) = k mod 11 and h2(k) = 1 + (k mod 10). Keys 22, 33, 44 are inserted in that order. Where does each key land?
Show answer
Key 22: h1 = 0, slot 0 is free, so it goes to slot 0. Key 33: h1 = 0 is taken. h2 = 1 + 3 = 4, so i = 1 gives (0 + 4) mod 11 = 4, and it goes to slot 4. Key 44: h1 = 0 is taken. h2 = 1 + 4 = 5, so i = 1 gives slot 5, which is free, and it goes to slot 5.
Exercise
Why does clearing a slot to empty on deletion break open addressing, and what is the standard fix?
Show answer
Searches stop at the first empty slot. Emptying a slot that sits inside another key's probe sequence makes that key unreachable. The fix is to mark the slot with a tombstone. Search skips over it and insert may reuse it. The table is rehashed from time to time to clear accumulated tombstones.