Sub topic 5
Open addressing: probing for a free slot
This chapter assumes you know arrays and indexing, basic Python classes and tuples, Big-O notation, and modulo arithmetic. It also assumes you have seen why collisions happen and how load factor is computed.
The idea
Separate chaining keeps colliding keys in a collection attached to each slot. Open addressing takes the opposite approach. Every key lives directly in the table array, and there are no lists or nodes. When the home slot of a key is taken, the table checks other slots in a fixed order until it finds a free one.
The home slot is hash(key) % capacity. The order in which a key visits slots is its probe sequence. The first entry is the home slot, and later entries are the fallback slots.
Two consequences follow from storing everything in the array:
- The table can never hold more keys than it has slots, so the load factor cannot exceed 1.0.
- Lookup must follow the same probe sequence that insertion used.
The probe loop
Every open-addressing scheme shares one structure. Only the formula for the i-th probe changes. The code below passes that formula in as a function, so you can see the shared part on its own.
def linear(key, i, n):
return (hash(key) + i) % n
def insert(slots, key, value, probe):
n = len(slots)
for i in range(n):
idx = probe(key, i, n)
if slots[idx] is None or slots[idx][0] == key:
slots[idx] = (key, value)
return idx
raise RuntimeError("table full")
def find(slots, key, probe):
n = len(slots)
for i in range(n):
idx = probe(key, i, n)
if slots[idx] is None:
return None
if slots[idx][0] == key:
return idx
return None
slots = [None] * 7
for k in (10, 17, 24): # all three have home slot 3
print(insert(slots, k, str(k), linear))
print(find(slots, 24, linear))
print(find(slots, 31, linear))This prints 3, 4, 5, then 5, then None. The keys 10, 17, and 24 all hash to slot 3 in a table of 7. The first takes slot 3, and the others are pushed to the next free slots. The function linear is the simplest possible probe, and the next chapter covers it in detail.
This version has no deletion. Chapter "Deleting keys with tombstones" adds it, because removing a key from an open-addressing table is not as simple as setting its slot to None.
How lookup knows when to stop
A lookup has two ways to end:
- It finds a slot holding the key, so the key is present.
- It reaches an empty slot, so the key is absent.
The second rule works because of how insertion behaves. A key is always placed in the first free slot on its probe sequence. If the lookup reaches an empty slot before finding the key, the key was never inserted. If it had been, insertion would have used that empty slot or an earlier one.
A lookup can also run out of probes without meeting an empty slot, which happens only when the table is completely full. The range(n) bound in the code prevents an infinite loop in that case.
Common errors
- Searching with a different probe order than insertion. If
findandinsertdisagree about the sequence, lookups miss keys that are present. Keep the probe formula in one place. - Forgetting that the table can fill up. Unlike chaining, open addressing has a hard limit. Real implementations resize well before the table is full, usually at a load factor well below 1.0.
- Assuming a probe sequence visits every slot. Some probe formulas skip slots. With a poor choice of capacity, an insert can fail even though free slots remain. The double hashing section covers this.
Exercise
A table has capacity 7 and uses the linear probe from this chapter. Slots 3, 4, and 5 are occupied by keys that all have home slot 3. You insert the integer 31. Which slot does it end up in, and how many slots does the insert examine?
Show answer
The home slot is 31 % 7 = 3. Slots 3, 4, and 5 are occupied, so the probe continues to slot 6, which is empty. The key goes in slot 6, and the insert examines 4 slots.