Sub topic 2
Why Collisions Are Unavoidable
This chapter assumes you can read simple Python, know how a hash table maps a key to a bucket index with hash(key) % capacity, and understand basic Big-O notation. It explains why two keys sharing a bucket is normal behavior and not a sign of a bad hash function.
The pigeonhole argument
A collision happens when two different keys map to the same bucket index. The first reason they cannot be avoided is the pigeonhole principle. If you place more items than there are slots, at least one slot holds two items.
A hash table with m buckets has only m possible indexes. The set of possible keys is far larger. There are more possible strings than any array could hold. So for any table, many different keys must share an index.
The argument only guarantees collisions once the table holds more than m keys. Real tables see collisions much earlier, for a second reason.
Collisions arrive early: the birthday effect
Suppose the hash function spreads keys perfectly evenly and randomly over m buckets. You might expect the first collision only when the table is nearly full. It comes much sooner.
When you insert the i-th key (counting from 0), it avoids all earlier keys with probability (m - i) / m. The probability that all n keys land in different buckets is the product of these terms. The probability of at least one collision is one minus that product.
def prob_any_collision(n, m):
p_none = 1.0
for i in range(n):
p_none *= (m - i) / m
return 1 - p_none
for n in (5, 10, 23, 40):
print(n, round(prob_any_collision(n, 365), 3))The output is:
5 0.027
10 0.117
23 0.507
40 0.891This is the birthday problem. With 365 buckets, 23 keys already give about a 50% chance of a collision, even though the table is only 6% full. A good rule of thumb is that a collision becomes likely after about 1.18 × √m keys. For 1,000 buckets, that is about 37 keys.
How many collisions to expect
Collisions are not rare events that you handle once in a while. The expected number of colliding pairs among n keys in m buckets is about n(n - 1) / (2m). Written with the ratio n / m, this grows in proportion to n times that ratio. That ratio is the load factor, which the next chapters define properly. For now, note that more keys per bucket means more collisions, and the growth is faster than linear in n.
You can also see this in how keys fill buckets. A simulation puts 1,000 keys into 1,000 buckets at random:
import random
m = 1000
n = 1000
counts = [0] * m
for _ in range(n):
counts[random.randrange(m)] += 1
empty = sum(1 for c in counts if c == 0) / m
crowded = sum(1 for c in counts if c >= 2) / m
print(f"empty: {empty:.2f}, two or more keys: {crowded:.2f}")Both fractions come out near 0.37 and 0.26, and they vary slightly per run. About a third of the buckets stay empty while a quarter hold several keys, in a table with exactly one key per bucket on average. A larger table does not remove this effect. It only changes how often it occurs.
Common mistakes
- Blaming the hash function. A better hash function spreads keys more evenly, but even a perfectly uniform one produces collisions. A bad one makes things worse than the numbers above.
- Thinking an empty-looking table is safe. The birthday effect means collisions appear at low fill levels.
- Treating collisions as errors. Every practical hash table must define what happens when two keys share a bucket. The next two chapters cover chaining and open addressing, the two standard answers.
Exercise
A hash table has 1,000 buckets and holds 100 keys that hash uniformly at random. About how many colliding pairs do you expect, using n(n - 1) / (2m)? Is the table likely to be collision-free?
Show answer
The expected number of colliding pairs is 100 × 99 / (2 × 1000) = 4.95, so about 5 pairs. The table is very unlikely to be collision-free, even though it is only 10% full. The birthday effect predicts the first collision after about 37 keys.