Open addressing: linear, quadratic probing, double hashing
Open addressing stores every key in the bucket array itself and probes for the next free slot. Compare linear, quadratic and double-hash probing.
Before you start: Hash tables with separate chaining.
The idea
Your usual parking spot 3 is taken. You drive on and park in the next free spot, 5.
Open addressing stores keys the same way. Every key sits in the slot array itself, and a taken slot sends the key on to another slot.
Intuition
3 and 11 both hash to slot 3 in a table of 8. 3 came first, so 11 moves on to slot 4.
The list of slots a key tries is its probe sequence. Insert and search walk the same sequence, so search finds 11 where insert put it.
Complexity
A miss costs about 2.5 probes at load 0.5 but 50 at load 0.9. Keep α = n ÷ m low and each operation is O(1) on average.
With linear probing a miss costs about ½(1 + 1/(1 − α)²) probes. A nearly full table makes long probe runs, and a full one is O(n). So tables resize before α passes about 0.5 to 0.7.
Pitfalls
Linear probing builds clusters: slots 2 to 7 are one solid run. Any key that hashes into the run walks to its end at slot 8.
A long run catches more new keys, so it grows even faster. This is primary clustering. Quadratic probing and double hashing jump out of runs.
Variations
Quadratic probing tries h + 1, h + 4, h + 9, and so on. Here 19 skips from slot 4 straight to slot 7.
The growing jumps break up primary clusters. Keys with the same home slot still share one sequence, and the sequence may miss free slots unless m and the load factor are chosen with care.
Double hashing steps by a second hash, h2(k) = 1 + k mod 6. 10 and 17 share slot 3 but hop by 5 and 6.
Each key gets its own step size, so keys with the same home slot spread out. A prime m makes every step size visit all slots.
Code
Pseudocode
insert(key)
s ← h(key) mod m; j ← 0; free ← none
while slot[s] is not empty
if slot[s] = key: return
if slot[s] is a tombstone and free = none: free ← s
j ← j + 1; s ← probe(key, j)
slot[free if set, else s] ← key
search(key)
s ← h(key) mod m; j ← 0
while slot[s] is not empty
if slot[s] = key: return s
j ← j + 1; s ← probe(key, j)
return not found
remove(key)
s ← search(key)
if found: slot[s] ← tombstone
probe(key, j)
linear: (h(key) + j) mod m
quadratic: (h(key) + j²) mod m
double: (h(key) + j · h2(key)) mod m
Python
EMPTY, TOMB = None, "<tomb>"
def probe(key, j, m):
return (key + j) % m
def insert(slots, key):
m = len(slots)
s, j, free = key % m, 0, None
while slots[s] is not EMPTY:
if slots[s] == key:
return
if slots[s] is TOMB and free is None:
free = s
j += 1
s = probe(key, j, m)
slots[s if free is None else free] = key
def search(slots, key):
m = len(slots)
s, j = key % m, 0
while slots[s] is not EMPTY:
if slots[s] == key:
return s
j += 1
s = probe(key, j, m)
return -1
def remove(slots, key):
s = search(slots, key)
if s != -1:
slots[s] = TOMB
JavaScript
const EMPTY = null, TOMB = "<tomb>";
function probe(key, j, m) {
return (key + j) % m;
}
function insert(slots, key) {
const m = slots.length;
let s = key % m, j = 0, free = -1;
while (slots[s] !== EMPTY) {
if (slots[s] === key) return;
if (slots[s] === TOMB && free === -1) free = s;
j++;
s = probe(key, j, m);
}
slots[free === -1 ? s : free] = key;
}
function search(slots, key) {
const m = slots.length;
let s = key % m, j = 0;
while (slots[s] !== EMPTY) {
if (slots[s] === key) return s;
j++;
s = probe(key, j, m);
}
return -1;
}
function remove(slots, key) {
const s = search(slots, key);
if (s !== -1) slots[s] = TOMB;
}
Cost at a glance
| Access | n/a |
|---|---|
| Search | O(1) / O(n) |
| Insert | O(1) / O(n) |
| Delete | O(1) / O(n) |
| Space | O(n) |
Practice problems (7)
Follow the probes Warm-up
A table has 8 slots, uses key mod 8 and linear probing. Insert 3, 11, 5, 19 in this order and answer each question before the step plays.
Slots under linear probing Warm-up
The code inserts four keys into 10 slots with linear probing. What does it print?
Clustering Standard
Each table has 10 slots, holds 5 keys and uses linear probing. A new key hashes to a random slot. In which table does its insert take the most probes on average?
Predict a double-hashing slot Standard
A table of 7 slots uses double hashing: h(k) = k mod 7 and step h2(k) = 1 + k mod 6. The code inserts 3, 10 and 17. What does it print?
Fill a table with linear probing Standard
A table has size slots and starts empty. Insert the distinct keys in keys in order with linear probing: a key starts at slot key mod size, and while that slot is taken it moves to the next slot, wrapping from the last slot to slot 0.
Return the table as an array of length size, with −1 for every empty slot.
Implement insert with tombstones Standard
Run commands on a table of size slots with linear probing and hash key mod size, without a built-in map or set. Every command returns one number:
insert K: if K is in the table, return its slot. Otherwise put K in the first tombstone on its probe sequence, else in the empty slot that ends the sequence. Return that slot.delete K: if K is in the table, replace it with a tombstone and return its slot. Otherwise return −1.find K: return the slot of K, or −1. Probes walk past tombstones and stop at an empty slot.
Return the results of all commands in order.
Implement quadratic probing with resize Challenge
A table starts with size empty slots, where size is a power of two. Insert the distinct keys of keys in order.
- Probe j of key K tries slot (K + j(j+1)/2) mod m for j = 0, 1, 2, …, where m is the current size. The first empty slot takes K.
- Before each insert, if the table would then hold more than m ÷ 2 keys, double m. Re-insert every stored key into the new empty table, in the order the keys were first inserted.
Return the final slot of each key, in the order of keys.
Related topics
Next on the path: Load factor and resizing