Load factor, resizing and universal hashing
About this lesson
A hash table is only fast while its chains stay short. Watch keys stack up in the buckets of a small Python table, measure how full it is with the load factor α = n/m, and see the table double and rehash every key when it fills up, at an average cost of O(1) per insert. Then an attacker piles five keys into one bucket, and a hash function picked at random from a universal family spreads them out again. The Software lesson "Hash maps: finding things by key" shows buckets and a table growing; "Chaining vs probing: expected cost and clustering" covers what happens inside a bucket, and "Amortised analysis: the accounting and potential methods" proves the doubling bound in full.
Keys go into chains
A table of m = 4 buckets, each holding a list called a chain. put sends key k to bucket h(k) % m. For small whole numbers, Python's hash(k) is k itself. Put 5, 12, 9 and 2: 5 and 9 both pick bucket 1.
h(k) = hash(k) = k (hash(5) == 5 in Python) put 5: 5 % 4 = 1 put 12: 12 % 4 = 0 put 9: 9 % 4 = 1 put 2: 2 % 4 = 2 bucket 0: [12] bucket 1: [5, 9] bucket 2: [2] bucket 3: []
Read the lesson step by step
Keys go into chains
A table of m = 4 buckets, each holding a list called a chain. put sends key k to bucket h(k) % m. For small whole numbers, Python's hash(k) is k itself. Put 5, 12, 9 and 2: 5 and 9 both pick bucket 1.
h(k) = hash(k) = k (hash(5) == 5 in Python) put 5: 5 % 4 = 1 put 12: 12 % 4 = 0 put 9: 9 % 4 = 1 put 2: 2 % 4 = 2 bucket 0: [12] bucket 1: [5, 9] bucket 2: [2] bucket 3: []Load factor: keys per bucket
The load factor α = n/m counts keys per bucket: here 4/4 = 1. It is the average chain length. If keys spread evenly, a search walks about 1 + α entries, so a small α keeps lookups fast.
α = n / m = 4 / 4 = 1 chain lengths: 1, 2, 1, 0 average 4/4 = 1 chaining, keys spread evenly (CLRS §11.2): average search cost Θ(1 + α) α ≤ 1 → about 2 entries per search at mostFull: time to grow
Now put 7. Line 2 checks first: n == m, so every bucket already holds one key on average. One more key would push α above 1, so put calls grow before 7 goes in.
put 7: t.n == t.m → 4 == 4 → True so grow(t) runs first without it: α = 5/4 > 1, and it keeps risingDouble and rehash
grow lifts every key out into a list, doubles m to 8, then puts each key back at h(k) % 8. 12 moves to bucket 4 and 5 to bucket 5; 9 and 2 stay. Then 7 lands in bucket 7: α = 5/8.
old = [12, 5, 9, 2] (chains read in bucket order) t.m = 2 * 4 = 8 12 % 8 = 4 (was 0) 5 % 8 = 5 (was 1) 9 % 8 = 1 (was 1) 2 % 8 = 2 (was 2) put 7: 7 % 8 = 7 bucket 0: [] bucket 1: [9] bucket 2: [2] bucket 3: [] bucket 4: [12] bucket 5: [5] bucket 6: [] bucket 7: [7] α = 5/8Rare big costs average out
A grow touches all n keys, so it costs O(n). But doubling means the next grow waits until n more keys arrive. Over N puts, grows move fewer than 2N keys in total: O(1) per put on average, or amortised.
start m = 4; grow when n == m, moving n keys puts N keys moved 2N 5 4 10 9 12 18 17 28 34 33 60 66 moves = 4 + 8 + ... + m_last < 2·m_last ≤ 2N growing by +4 each time instead: Θ(N²) movesA fixed hash can be beaten
Anyone who knows h(k) = k can pick keys that leave the same remainder: 3, 11, 19, 27 and 35 all give k % 8 = 3. One chain of 5: a search walks every key, though α is only 5/8.
new table, m = 8, h(k) = k 3 % 8 = 3 11 % 8 = 3 19 % 8 = 3 27 % 8 = 3 35 % 8 = 3 bucket 3: [3, 11, 19, 27, 35] search cost: Θ(n), not Θ(1 + α)Pick the hash at random
Pick h at random when the table is made: h(k) = ((a·k + b) % 37) % m, with a prime 37 above every key and random a, b below it (a ≠ 0). This draw, a = 4, b = 9, spreads the keys.
draw: a = 4, b = 9, p = 37 3: (4·3 + 9) % 37 = 21, % 8 = 5 11: (4·11 + 9) % 37 = 16, % 8 = 0 19: (4·19 + 9) % 37 = 11, % 8 = 3 27: (4·27 + 9) % 37 = 6, % 8 = 6 35: (4·35 + 9) % 37 = 1, % 8 = 1 bucket 0: [11] bucket 1: [35] bucket 2: [] bucket 3: [19] bucket 4: [] bucket 5: [3] bucket 6: [27] bucket 7: []Universal: collisions stay rare
The family is universal: two different keys share a bucket in at most 1/m of the draws. The worst pair here collides in 136 of 1332. So a key's chain holds at most 1 + (n − 1)/m = 1.5 keys on average.
draws (a, b): 36 × 37 = 1332 any pair x ≠ y in 0..36: collide in ≤ 1332/8 = 166.5 draws worst pair, counted: 136 expected chain of a key ≤ 1 + (n − 1)/m = 1 + 4/8 = 1.5 these 5 keys, all 1332 draws: 1.41 the attacker cannot see a and b
5 quick questions
Choose one answer per question. You can change your choices before checking them.
01A chaining hash table has m = 16 buckets and holds n = 12 keys. What is its load factor?
α = n/m = 12/16 = 0.75. It is the average number of keys per bucket, so the average chain has three quarters of a key.
02The lesson's table has m = 8 after a grow and holds 12 at bucket 4. Why must grow recompute the bucket of every key?
h(12) = 12 stays the same, but the bucket is 12 % m: 0 when m = 4, 4 when m = 8. Some keys (like 9 and 2) happen to keep their bucket, but put and get must use the new m for every key, so every key is rehashed.
03Starting at m = 4 and doubling whenever n == m, how many keys do all the grows move during the first 33 puts?
Grows happen before puts 5, 9, 17 and 33, moving 4, 8, 16 and 32 keys: 60 in total, fewer than 2 × 33 = 66. That is why the average cost per put stays O(1).
04With h(k) = k and m = 8, which set of keys builds one chain of length 4?
5, 13, 21 and 29 all leave remainder 5 when divided by 8, so all four go to bucket 5. Anyone who knows a fixed hash function can pick keys like these.
05What does it mean that the family h(k) = ((a·k + b) mod p) mod m is universal?
Collisions can still happen for a given draw, but for any fixed pair of distinct keys they happen in at most a 1/m fraction of the draws. Because the attacker cannot see a and b, the expected chain length stays at most 1 + (n − 1)/m whatever keys arrive.
Glossary
- Bucket
- One slot of a hash table. With chaining, each bucket holds a list of the keys that hash to it.
- Chain
- The list of keys stored in one bucket. Searching a bucket means walking its chain.
- Load factor (α)
- n/m: the number of keys divided by the number of buckets. With chaining it is the average chain length.
- Rehash
- Recomputing every key's bucket, h(k) % m, after m changes, and moving the key there.
- Amortised cost
- The average cost per operation over a whole sequence, so that rare expensive steps are shared out over many cheap ones.
- Universal hash family
- A set of hash functions where, for any two different keys, a randomly picked function makes them collide with probability at most 1/m.
- Prime
- A whole number above 1 that only divides by 1 and itself, such as 37. The universal family needs a prime p larger than every key.
Notes and sources
- The figure uses whole-number keys, h(k) = hash(k) = k and h(k) % m, as in the division method of MIT 6.006 Lecture 4. Python's hash() of a small int is the int itself; real dicts mix the bits further and use open addressing instead of chains.
- Growing when n == m keeps α ≤ 1. Libraries pick other limits: CPython's dict grows at two thirds full, and Sedgewick and Wayne's chaining table lets lists grow longer. Any constant limit with doubling gives amortised O(1) puts.
- H(p, m) = { ((a·k + b) mod p) mod m : a in 1..p−1, b in 0..p−1 } with a prime p above every key is universal (MIT 6.006 Lecture 4; CLRS §11.3). The figure checks it for p = 37, m = 8 over every pair of keys and every draw.
- Python defends its dicts the same way for text: str and bytes hashes are salted with a random value each time Python starts, against chosen inputs that make dict insertion O(n²).
- A random hash is drawn once per table and kept while it lives. The grow in this code keeps the same h, which is fine: (a·k + b) % p is universal for every m.
- MIT 6.006 Introduction to Algorithms (Spring 2020), Lecture 4: Hashing
- Sedgewick and Wayne, Algorithms 4th ed., §3.4 Hash Tables
- Cormen, Leiserson, Rivest and Stein, Introduction to Algorithms (MIT Press), chapter 11: Hash Tables
- Python Standard Library: Hashing of numeric types
- Python Data Model: object.__hash__ (hash randomization)
Quiz Unlocks after the last step.