Sub topic 6
How Load Factor Changes Lookup Cost
This chapter assumes you know how a hash table maps a key to a bucket index, what a collision is, and that you can read short Python. You should also know the difference between chaining and open addressing from the earlier chapters.
The quantity that drives cost
The load factor is size / capacity: the number of stored entries divided by the number of buckets. A table with 6 entries in 8 buckets has a load factor of 0.75. With a decent hash function, keys spread roughly evenly, so the load factor is also the average number of entries that land in one bucket.
Lookup cost depends on how many entries you must examine before you find the key or conclude it is absent. Each examined entry is called a probe. Two cases are counted separately:
- A successful lookup finds the key.
- An unsuccessful lookup ends with the key absent. This case is often more expensive, because you cannot stop early.
Chaining: cost grows linearly
In separate chaining, a bucket holds a list. An unsuccessful lookup scans the whole list in the target bucket, which has load entries on average. A successful lookup scans about half of the other entries before reaching the target, plus the target itself.
- Unsuccessful: about
loadprobes - Successful: about
1 + load / 2probes
Chaining tolerates a load factor above 1. At a load of 2, each bucket averages two entries, and lookups are still cheap. The cost grows in a straight line.
Open addressing: cost blows up near full
With linear probing, all entries live in the array itself. A collision sends the key to the next free slot. Occupied slots form runs called clusters, and a key that hashes into a cluster must walk to its end. Clusters also grow as they absorb more keys, which makes the problem worse.
- Successful: about
0.5 * (1 + 1 / (1 - load))probes - Unsuccessful: about
0.5 * (1 + 1 / (1 - load)**2)probes
The 1 - load term is the fraction of free slots. As it approaches zero, the cost grows without bound. A load factor must stay below 1 for open addressing, since the table cannot hold more entries than slots.
Computing the numbers
This script prints the textbook estimates for three load factors.
def chaining_probes(load):
unsuccessful = load
successful = 1 + load / 2
return successful, unsuccessful
def linear_probing_probes(load):
successful = 0.5 * (1 + 1 / (1 - load))
unsuccessful = 0.5 * (1 + 1 / (1 - load) ** 2)
return successful, unsuccessful
for load in (0.5, 0.75, 0.9):
cs, cu = chaining_probes(load)
ls, lu = linear_probing_probes(load)
print(f"load={load}: chaining hit={cs:.2f} miss={cu:.2f} | "
f"linear hit={ls:.2f} miss={lu:.2f}")The results, rounded:
| Load factor | Chaining, successful | Chaining, unsuccessful | Linear probing, successful | Linear probing, unsuccessful |
|---|---|---|---|---|
| 0.50 | 1.25 | 0.50 | 1.50 | 2.50 |
| 0.75 | 1.38 | 0.75 | 2.50 | 8.50 |
| 0.90 | 1.45 | 0.90 | 5.50 | 50.50 |
Going from 0.5 to 0.9 raises chaining's unsuccessful cost less than twofold. For linear probing it rises twentyfold.
Common errors
- Reading the formulas as exact. They assume a uniform hash function and, for linear probing, random-looking key placement. A poor hash function or patterned keys makes real costs worse.
- Treating the load factor as a worst-case bound. These are averages. A single lookup can still hit a long chain or a long cluster.
- Forgetting that deletions matter. In open addressing, deleted slots usually leave markers that still lengthen probe runs, so the effective load is higher than
size / capacity.
Exercise
A linear probing table has 16 slots and holds 12 entries. Using the formulas in this chapter, about how many probes does an unsuccessful lookup need, and how does that compare with chaining at the same load factor?
Show answer
The load factor is 12 / 16 = 0.75. Linear probing needs 0.5 (1 + 1 / (1 - 0.75)2) = 0.5 (1 + 16) = 8.5 probes. Chaining needs about 0.75 probes. Linear probing costs more than eleven times as much at this load.