TExcelTAD
Home › Discrete Mathematics › The Pigeonhole Principle

The Pigeonhole Principle

If more than nn pigeons fly into nn 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 ∣A∣>∣B∣\lvert A \rvert > \lvert B \rvert, no total function A→BA \to B is injective.

The generalized principle

If NN objects are placed in nn boxes, some box holds at least ⌈N/n⌉\lceil N / n \rceil objects. (If every box held at most ⌈N/n⌉−1<N/n\lceil N/n \rceil - 1 < N/n, the total would be less than NN.)

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 2×22 \times 2 square two are within distance 2\sqrt{2} (split into four 1×11 \times 1 squares)

Compression cannot always shrink

Theorem. No lossless compression algorithm shrinks every file.

Proof. There are 2n2^n files of exactly nn bits, but only 20+21+⋯+2n−1=2n−12^0 + 2^1 + \cdots + 2^{n-1} = 2^n - 1 files shorter than nn bits. A lossless compressor is injective (it must be reversible), so it cannot map all 2n2^n files of length nn into the 2n−12^n - 1 shorter ones. ■\blacksquare

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 210−1=10232^{10} - 1 = 1023 non-empty subsets, and each sum lies between 1 and 91+92+⋯+100=95591 + 92 + \cdots + 100 = 955. More subsets than possible sums, so two different subsets S≠TS \ne T have equal sums. Remove their common elements from both: the sums stay equal and the sets become disjoint (neither becomes empty, since S≠TS \ne T and the sums are equal). ■\blacksquare

The proof is non-constructive, but a computer can find such subsets quickly:

python · try it yourself
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
    break
numbers: [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 mm buckets. As soon as you insert m+1m + 1 keys, two must collide. The pigeonhole principle says collisions are certain; the birthday bound says they show up much earlier, after about m\sqrt{m} random keys.

A sequence result

Erdős–Szekeres. Every sequence of n2+1n^2 + 1 distinct numbers has an increasing or a decreasing subsequence of length n+1n + 1.

Proof idea. Label each element with (a,d)(a, d): the longest increasing and longest decreasing subsequences starting there. Two different elements always get different labels (if the earlier one is smaller, its aa is bigger; if larger, its dd is bigger). If all a,d≤na, d \le n, there are only n2n^2 labels for n2+1n^2 + 1 elements: pigeonhole contradiction. ■\blacksquare

python · try it yourself
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 kk bits miss some errors, why a kk-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: 14+2=1614 + 2 = 16.

Exercise 2: Same number of friends

Prove that in any group of n≥2n \ge 2 people, two have the same number of friends in the group (friendship is mutual).

Answer

Friend counts lie in 0,…,n−10, \ldots, n - 1, but 0 and n−1n - 1 cannot both occur, leaving n−1n - 1 values for nn people.

Solution

Someone with n−1n - 1 friends is friends with everybody, so nobody has 0 friends. Hence the counts take at most n−1n - 1 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.

python · try it yourself
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?

  1. 24
  2. 25
  3. 36
  4. 13

Why can't a lossless compressor shrink every 1000-bit file?

  1. Compression is slow
  2. There are fewer shorter files than 1000-bit files, and the compressor must be injective
  3. Because of entropy only for text
  4. It can

A hash table has 1000 buckets. After how many inserted keys is a collision guaranteed?

  1. 2
  2. 32
  3. 1000
  4. 1001