Sub topic 8
Quadratic probing
This chapter assumes you know linear probing, tombstones, and load factor from the earlier chapters. It also assumes you are comfortable with modulo arithmetic and basic Python.
The idea
Linear probing checks the slots right after the home slot. Occupied slots form long runs, which is primary clustering. Quadratic probing breaks up those runs by making the gap between probes grow. On probe number j (starting at 0), the table checks:
index = (start + j * j) % capacityThe offsets from the home slot are 0, 1, 4, 9, 16, and so on. A key that lands in the middle of a run jumps past it quickly instead of crawling along it.
Seeing the sequences
This script prints probe sequences for three setups.
def quadratic_probes(start, capacity, count):
return [(start + j * j) % capacity for j in range(count)]
def triangular_probes(start, capacity, count):
return [(start + j * (j + 1) // 2) % capacity for j in range(count)]
print(quadratic_probes(3, 8, 8))
print(sorted(set(quadratic_probes(3, 8, 8))))
print(triangular_probes(3, 8, 8))
print(quadratic_probes(3, 7, 7))It prints:
[3, 4, 7, 4, 3, 4, 7, 4]
[3, 4, 7]
[3, 4, 6, 1, 5, 2, 0, 7]
[3, 4, 0, 5, 5, 0, 4]With capacity 8, plain squares visit only slots 3, 4, and 7. The table could have free slots elsewhere, but this key would never reach them. The triangular offsets 0, 1, 3, 6, 10, ... visit all 8 slots.
Guaranteeing that a free slot is found
The probe sequence must be able to reach a free slot. There are two standard ways to guarantee this.
- Prime capacity with load factor at most 0.5. For a prime capacity
m, the first(m + 1) / 2probes ofstart + j*jhit distinct slots. At least half the table is free, so one of those probes must find an empty slot. The capacity-7 line above shows 4 distinct slots: 3, 4, 0, and 5. - Power-of-two capacity with triangular offsets. The sequence visits every slot, so insertion succeeds whenever any slot is free. Many production tables use this scheme.
Pick one rule and apply it everywhere, including resizing. If you double a prime capacity, the new size is no longer prime. Choose the next prime instead.
Secondary clustering
Quadratic probing removes primary clustering, but two keys with the same start slot still follow the same sequence. They collide on every probe, which is called secondary clustering. It is milder than primary clustering. Double hashing, covered next, gives each key its own step size and avoids it.
Common errors
- Probing past the safe limit. With prime capacity and plain squares, inserting into a table more than half full can loop without finding a slot, even though free slots exist. Resize at load factor 0.5.
- Mixing offset formulas. Use the same probe formula for
put,get, andremove. If lookup uses a different sequence from insertion, keys become unreachable. - Stopping at tombstones. As with linear probing, lookups continue past a tombstone and stop only at a never-used slot. Insertions may reuse the first tombstone seen, after confirming the key is absent.
- Forgetting the loop bound. Cap the number of probes at the capacity, or at
(m + 1) / 2for the prime scheme, so a full table cannot cause an endless loop.
Trade-offs
| Property | Linear probing | Quadratic probing |
|---|---|---|
| Primary clustering | Yes | No |
| Secondary clustering | Yes | Yes |
| Cache behavior | Best, probes are adjacent | Worse, probes jump |
| Load factor limit | Close to 1 with care | About 0.5 (prime scheme) |
Exercise
A table has capacity 11 and uses plain quadratic probing, start + j*j. A key's home slot is 5. List the slots checked on probes j = 0, 1, 2, 3.
Show answer
The slots are 5, 6, 9, and 3. The offsets are 0, 1, 4, and 9, and 5 + 9 = 14, which is 3 modulo 11.