The Pigeonhole Principle
If more than pigeons fly into holes, some hole gets at least two pigeons.
It sounds trivial, yet the pigeonhole principle proves facts that are hard to see otherwise: two people in London have exactly the same number of hairs, every lossless compressor makes some files bigger, and hash collisions are unavoidable. It is the contrapositive of the injective mapping rule: if , no total function is injective.
The generalized principle
If objects are placed in boxes, some box holds at least objects. (If every box held at most , the total would be less than .)
| Situation | Conclusion |
|---|---|
| 367 people | two share a birthday |
| 10 million people, at most 1 million hairs each | at least 10 share a hair count |
| any 6 integers | two have the same remainder mod 5, so their difference is divisible by 5 |
| 5 points in a square | two are within distance (split into four squares) |
Compression cannot always shrink
Theorem. No lossless compression algorithm shrinks every file.
Proof. There are files of exactly bits, but only files shorter than bits. A lossless compressor is injective (it must be reversible), so it cannot map all files of length into the shorter ones.
Subsets with equal sums
Claim. Among any 10 distinct numbers between 1 and 100, there are two disjoint non-empty subsets with the same sum.
Proof. There are non-empty subsets, and each sum lies between 1 and . More subsets than possible sums, so two different subsets have equal sums. Remove their common elements from both: the sums stay equal and the sets become disjoint (neither becomes empty, since and the sums are equal).
The proof is non-constructive, but a computer can find such subsets quickly:
import random
from itertools import combinations
rng = random.Random(2026)
nums = sorted(rng.sample(range(1, 101), 10))
print("numbers:", nums)
seen = {}
for k in range(1, 11):
for S in combinations(nums, k):
s = sum(S)
if s in seen:
T = seen[s]
A, B = sorted(set(S) - set(T)), sorted(set(T) - set(S))
print("equal sums:", A, "and", B, "->", sum(A), "=", sum(B))
break
seen[s] = S
else:
continue
breaknumbers: [14, 16, 29, 41, 65, 66, 72, 77, 80, 83]
equal sums: [14, 66] and [80] -> 80 = 80
Hash collisions
A hash function maps a huge set of keys into buckets. As soon as you insert keys, two must collide. The pigeonhole principle says collisions are certain; the birthday bound says they show up much earlier, after about random keys.
A sequence result
Erdős–Szekeres. Every sequence of distinct numbers has an increasing or a decreasing subsequence of length .
Proof idea. Label each element with : the longest increasing and longest decreasing subsequences starting there. Two different elements always get different labels (if the earlier one is smaller, its is bigger; if larger, its is bigger). If all , there are only labels for elements: pigeonhole contradiction.
import random
def longest_runs(seq):
n = len(seq)
inc, dec = [1] * n, [1] * n
for i in range(n - 1, -1, -1):
for j in range(i + 1, n):
if seq[j] > seq[i]:
inc[i] = max(inc[i], 1 + inc[j])
else:
dec[i] = max(dec[i], 1 + dec[j])
return max(inc), max(dec)
rng = random.Random(1)
for trial in range(5):
seq = rng.sample(range(100), 10) # n = 3: 3^2 + 1 = 10 elements
a, d = longest_runs(seq)
print(seq, "longest inc:", a, "dec:", d, " some run >= 4:", max(a, d) >= 4)[17, 72, 97, 8, 32, 15, 63, 57, 60, 83] longest inc: 5 dec: 3 some run >= 4: True
[48, 26, 12, 62, 3, 49, 55, 77, 97, 98] longest inc: 6 dec: 4 some run >= 4: True
[0, 89, 57, 34, 92, 29, 75, 13, 40, 3] longest inc: 3 dec: 6 some run >= 4: True
[2, 3, 83, 69, 1, 48, 87, 27, 54, 92] longest inc: 5 dec: 4 some run >= 4: True
[3, 67, 28, 97, 56, 63, 70, 29, 44, 86] longest inc: 6 dec: 3 some run >= 4: True
In software
Pigeonhole arguments explain why hash tables need collision handling, why checksums with bits miss some errors, why a -bit counter must wrap around, and why lossless compressors only win on data with patterns. Cycle detection in iterated functions (Floyd's tortoise and hare, used in Pollard's rho factoring and in pseudo-random generator analysis) rests on the fact that a function on a finite set must eventually repeat a value.
Exercises
Exercise 1: Socks
A drawer has 10 black, 8 blue and 6 grey socks. How many must you take (in the dark) to be sure of a matching pair? Of a black pair?
Answer
4 for any pair; 16 for a black pair.
Solution
3 colours are 3 holes: 4 socks force two of the same colour. For black: in the worst case you take all 14 non-black socks first, then 2 black ones: .
Exercise 2: Same number of friends
Prove that in any group of people, two have the same number of friends in the group (friendship is mutual).
Answer
Friend counts lie in , but 0 and cannot both occur, leaving values for people.
Solution
Someone with friends is friends with everybody, so nobody has 0 friends. Hence the counts take at most different values, and by pigeonhole two people share a count. In graph terms: every simple graph with at least 2 vertices has two vertices of equal degree.
Exercise 3: Remainders (code)
Show that among any 7 integers, two have a difference divisible by 6. Test on random sets.
Answer
There are only 6 remainders mod 6 for 7 integers.
Solution
Two numbers with the same remainder differ by a multiple of 6.
import random
rng = random.Random(5)
ok = all(any((a - b) % 6 == 0 for i, a in enumerate(xs) for b in xs[i + 1:])
for xs in (rng.sample(range(-1000, 1000), 7) for _ in range(1000)))
print(ok)True
Test yourself
How many people guarantee that at least 3 share a birth month?
- 24
- 25
- 36
- 13
Why can't a lossless compressor shrink every 1000-bit file?
- Compression is slow
- There are fewer shorter files than 1000-bit files, and the compressor must be injective
- Because of entropy only for text
- It can
A hash table has 1000 buckets. After how many inserted keys is a collision guaranteed?
- 2
- 32
- 1000
- 1001