Sub topic 2
Why Collisions Are Unavoidable
This chapter assumes you know array indexing, modulo arithmetic, basic Python classes and tuples, and the difference between average-case and worst-case cost. You don't need any earlier chapter beyond the overview of how a hash table maps a key to an array index.
The mapping that causes the problem
A hash table turns a key into an array index in two steps. A hash function maps the key to an integer. The table then reduces that integer to a valid slot, usually with hash(key) % capacity.
A collision happens when two different keys land in the same slot.
capacity = 8
for key in (3, 11, 19):
print(key, "->", hash(key) % capacity)All three keys print slot 3, because 3, 11, and 19 differ by multiples of 8. A bad hash function is not the cause here. The cause is the modulo step.
The pigeonhole principle
If you put more than m items into m boxes, at least one box holds two items. For hash tables, the set of possible keys is far larger than the set of slots. There are infinitely many strings and only a few thousand slots in a typical table. No function from a large set into a small one can give every key its own slot.
Collisions happen at two levels:
- Full hash collision: two keys produce the same integer from the hash function. Python's
hash(-1)andhash(-2)are both-2in CPython. - Index collision: the integers differ, but they reduce to the same slot after
% capacity. This is much more common.
Collisions arrive before the table is full
The pigeonhole principle only guarantees a collision once you have more keys than slots. In practice collisions start much sooner. This is the birthday paradox: in a room of 23 people, there is a better than 50% chance that two share a birthday, even though there are 365 days.
For n keys hashed uniformly into m slots, the expected number of colliding pairs is about:
n * (n - 1) / (2 * m)The probability that at least one collision has occurred is roughly 1 - e^(-n(n-1) / (2m)). This grows with the square of n, so a table that is mostly empty still has collisions.
You can check this by simulation:
import random
def collision_rate(num_keys, num_slots, trials=2000):
hits = 0
for _ in range(trials):
seen = set()
for _ in range(num_keys):
slot = random.randrange(num_slots)
if slot in seen:
hits += 1
break
seen.add(slot)
return hits / trials
for n in (10, 20, 40, 80):
print(n, "keys in 1000 slots:", collision_rate(n, 1000))The output is random, but expect roughly 4%, 17%, 54%, and 96%. With 80 keys in 1000 slots, the table is only 8% full, and a collision is almost certain.
What this means for design
Because collisions cannot be prevented, every hash table needs a collision strategy. The two standard strategies differ in where the second key goes:
| Strategy | Where a colliding key is stored |
|---|---|
| Separate chaining | In a collection attached to the same slot |
| Open addressing | In another slot of the same array, found by probing |
The next chapters introduce the load factor, which measures how full the table is. They then cover each strategy in turn.
Exercise
A table has 1000 slots and holds 100 keys, hashed uniformly. About how many colliding pairs do you expect, and why can you not conclude the table is collision-free just because it is only 10% full?
Show answer
Expected colliding pairs are n(n-1)/(2m) = 100 * 99 / 2000, which is about 5. Collisions depend on the number of key pairs, which grows with the square of n, not on how full the table is. So even a 10% full table is expected to have several collisions.