Sub topic 9
Load factor and resizing
This chapter assumes you have read the chapters on chaining and linear probing. You should be comfortable with hash(key) % capacity and with the idea that a bucket is a slot in an array.
Load factor
A hash table's speed depends on how crowded its array is. The load factor measures that crowding:
load factor = number of entries / number of bucketsA table with 6 entries in 8 buckets has a load factor of 0.75.
The load factor means different things in the two collision strategies:
| Strategy | What a high load does | Maximum possible load |
|---|---|---|
| Separate chaining | Chains get longer, so each lookup scans more pairs | Above 1, because a bucket can hold many pairs |
| Linear probing | Runs of filled slots grow, so probes get longer | At most 1, and performance collapses well before that |
This is why ChainedHashTable uses a max_load of 0.75, while ProbingHashTable uses 0.5. Probing degrades faster as the table fills.
In ProbingHashTable, the load check uses _used, not _live. Tombstones still lengthen probe runs, so they count as occupied slots.
Resizing
When the load factor passes its limit, the table allocates a larger array and re-inserts every entry. This step is called rehashing. You cannot copy buckets as they are. The index is hash(key) % capacity, so a new capacity gives most keys a new index.
Both example classes follow the same steps:
- Create a new array with double the capacity.
- Walk the old array.
- Recompute each key's index with the new capacity and insert it.
Why doubling keeps inserts cheap
One resize costs O(n), because every entry moves. Resizes are rare, though. Doubling means the capacity grows geometrically, so each resize happens after twice as many inserts as the one before. The total copying work across n inserts is therefore proportional to n. Averaged over all inserts, each costs O(1). This is called amortized constant time.
This script counts how many entries are moved during resizes:
def count_moves(n, capacity=8, max_load=0.75):
size = 0
moves = 0
for _ in range(n):
size += 1
if size / capacity > max_load:
moves += size
capacity *= 2
return capacity, moves
for n in (10, 100, 1000, 10000):
capacity, moves = count_moves(n)
print(n, capacity, moves, round(moves / n, 2))The last column is the average number of moves per insert. It stays below 2 however large n becomes.
If the table grew by a fixed amount instead, such as 8 buckets at a time, resizes would stay frequent. The total work would grow with the square of n.
Common errors
- Reusing old indexes. Copying buckets to the new array without recomputing
hash(key) % new_capacityleaves entries where lookups will not find them. - Checking the load before the insert. Check after the insert, as
ChainedHashTabledoes, or count the new entry in the check, asProbingHashTabledoes. Otherwise the table briefly exceeds its limit. - Never shrinking. A table that has grown and then lost most of its entries keeps its large array. Some libraries shrink at a low load factor, such as 0.25. A gap between the grow and shrink thresholds prevents repeated resizing near one boundary.
- Holding references during a resize. Iterating over a table while inserting into it can break when a resize moves everything. Many libraries raise an error for this.
Exercise
A chained table has 16 buckets and a maximum load factor of 0.75. It currently holds 12 entries. What happens on the next new-key insert, and what is the load factor afterward?
Show answer
After the insert the table holds 13 entries, and 13/16 = 0.8125, which is above 0.75. The table doubles to 32 buckets and rehashes all 13 entries, so the load factor becomes 13/32, about 0.41.