Sub topic 3
Load factor: how full is the table?
This chapter assumes you know arrays and indexing, basic Python classes, Big-O notation, and modulo arithmetic. It also assumes you have read the previous chapter on why collisions are unavoidable.
Definition
The load factor of a hash table measures how full it is. It is usually written as α (alpha):
α = n / m
Here n is the number of stored keys and m is the number of slots (the capacity). A table with 6 keys and 8 slots has α = 0.75.
def load_factor(n_keys, capacity):
return n_keys / capacity
for n in (4, 8, 12):
print(n, load_factor(n, 8))This prints 4 0.5, 8 1.0, and 12 1.5.
What the number tells you
The load factor is the average number of keys per slot. With a good hash function, keys spread evenly, so α predicts how likely a new key is to land on an occupied slot. As α grows, collisions become more common and each operation does more work.
The two collision strategies treat α differently:
- Separate chaining: α can be above 1.0. Each slot holds a list, so 12 keys in 8 slots just means lists of about 1.5 entries. The average chain length is α, so a lookup costs about 1 + α steps.
- Open addressing: every key lives in the array itself, so α can never exceed 1.0. Cost also rises much faster as α approaches 1.0, because probe sequences get long.
How cost grows with α
For linear probing with evenly spread keys, the textbook estimate of probes for an unsuccessful lookup is ½(1 + 1/(1 − α)²). The next chapters cover the derivation. For now, look at how the numbers behave:
| Load factor α | Chaining (about 1 + α) | Linear probing (unsuccessful) |
|---|---|---|
| 0.50 | 1.5 | 2.5 |
| 0.75 | 1.75 | 8.5 |
| 0.90 | 1.9 | 50.5 |
Chaining degrades gently. Linear probing is fine at 0.5 and poor at 0.9. This is why open-addressing tables usually keep α below roughly 0.5 to 0.75.
Using a maximum load factor
Real tables set a maximum load factor (also called the threshold). When an insert would push α past it, the table grows. The ChainedHashMap from the plan uses a threshold of 1.0:
self._size += 1
if self._size / len(self._buckets) > 1.0:
self._resize(len(self._buckets) * 2)Doubling the capacity halves α, which restores short chains. The resize step itself is covered in a later chapter. The point here is that the load factor is the trigger.
Common errors
- Dividing by the wrong count. Use the number of live keys for n. In open addressing, deleted slots (tombstones, covered later) still lengthen probe chains, so some implementations count them too.
- Assuming α above 1.0 is a bug. It is normal for chaining. It is impossible for open addressing.
- Setting the threshold too high for probing. A limit of 0.95 with linear probing makes lookups of missing keys scan long runs of occupied slots.
- Never resizing. With a fixed capacity, α grows without bound as keys are added, and chaining slowly turns into a linear scan of lists.
Exercise
A linear-probing table has 16 slots and holds 12 keys. What is its load factor, and can you insert 5 more keys without resizing if the maximum load factor is 0.75?
Show answer
α = 12 / 16 = 0.75, which is already at the maximum. Inserting even one more key gives 13 / 16 = 0.8125, above 0.75, so the table must resize first. You cannot insert 5 more keys at this capacity under that threshold.