Sub topic 13
Choosing between chaining and open addressing
This chapter assumes you have read the earlier chapters. You should know how separate chaining and linear probing work, what a tombstone is, and how load factor triggers a resize. Here you compare the two strategies and pick one for a given job.
Cost at different load factors
Load factor α is the number of stored keys divided by the number of slots. For chaining, α can pass 1.0 because buckets just grow longer. For open addressing, α cannot exceed 1.0, and performance gets bad well before that.
The standard estimates for the average number of slots examined, assuming a good hash function, are:
- Chaining, successful lookup: 1 + α/2
- Chaining, unsuccessful lookup: α (plus the bucket access itself)
- Linear probing, successful lookup: 0.5 × (1 + 1/(1 − α))
- Linear probing, unsuccessful lookup: 0.5 × (1 + 1/(1 − α)²)
def expected_probes(alpha):
return {
"chain_hit": 1 + alpha / 2,
"chain_miss": alpha,
"linear_hit": 0.5 * (1 + 1 / (1 - alpha)),
"linear_miss": 0.5 * (1 + 1 / (1 - alpha) ** 2),
}
for a in (0.5, 0.75, 0.9):
print(a, expected_probes(a))| Load factor | Chaining hit | Chaining miss | Linear probing hit | Linear probing miss |
|---|---|---|---|---|
| 0.5 | 1.25 | 0.5 | 1.5 | 2.5 |
| 0.75 | 1.375 | 0.75 | 2.5 | 8.5 |
| 0.9 | 1.45 | 0.9 | 5.5 | 50.5 |
Unsuccessful lookups in linear probing degrade fastest. Primary clustering makes them walk to the end of a long run of occupied slots. This is why open-addressing tables usually resize at a load factor of about 0.5 to 0.75, while chaining tables often tolerate 1.0 or more.
Memory and cache behavior
Chaining stores a pointer, and often a node object, for every entry. In Python, each (key, value) tuple is a separate heap object. Open addressing keeps entries in one flat array, with no per-entry overhead beyond the empty slots you must keep for the lower load factor.
Cache behavior favors open addressing. Linear probing reads adjacent slots, which usually sit in the same cache line. Walking a linked chain follows pointers scattered across memory, and each hop can be a cache miss. In compiled languages this often outweighs the probe counts in the table above.
Chaining has the opposite advantage with large entries. If each slot must hold a big record, a table kept at α = 0.5 wastes half of that space. Chaining only allocates what it stores.
Operational differences
- Deletion: chaining removes a node and is done. Open addressing needs tombstones, and they raise lookup cost until the next rehash.
- Resizing: both rehash every key. Chaining can defer a resize without breaking, since it only gets slower. Open addressing must resize before the table fills.
- Hash quality: open addressing is more sensitive to weak hash functions, because clusters form. Chaining degrades more gracefully.
- Worst case: with chaining, a bucket can be replaced by a balanced tree to bound the worst case. Java's
HashMapdoes this. Open addressing has no equivalent fallback.
Decision guide
Common mistakes
- Comparing the two at the same load factor. Chaining at α = 1.0 and linear probing at α = 1.0 are not comparable, because the second has no free slots left.
- Forgetting that tombstones count toward fullness. A table with few live keys but many tombstones still probes slowly.
- Choosing open addressing and then storing large values inline, which wastes memory.
Exercise
A table holds 750 keys in 1000 slots. Using the formulas above, roughly how many slots does linear probing examine on an unsuccessful lookup, and how does that compare to chaining? Which strategy would you choose if lookups for missing keys are frequent and memory is not tight?
Show answer
Here α = 0.75. Linear probing examines about 0.5 × (1 + 1/(0.25)²) = 8.5 slots. Chaining examines about 0.75 entries. With frequent misses and no memory pressure, you could use either of these: chaining at this load, or open addressing resized to a lower load factor such as 0.5, which brings the miss cost down to 2.5 probes. Chaining is the simpler choice if you do not want to tune the resize threshold.