The Pigeonhole Principle
Put n + 1 pigeons into n holes and some hole gets at least two. It sounds too obvious to be useful, but with the right choice of "holes" it proves things like two Londoners having exactly the same number of hairs, and hash collisions being impossible to avoid.
What the symbols mean
- Objects
How many items are placed.
- Boxes
How many places they can go.
Explanation
The pigeonhole principle is simply this: if there are more things than places to put them, some place has to get doubled up. The name comes from pigeons and their nesting holes (a "pigeonhole" is also an old word for a small mail slot in a desk): more pigeons than holes, and two must share. It's good for proving something has to happen, like a hash collision, without finding it. The formula just makes that precise: (how many objects), (how many boxes), (the average per box) and (round up to a whole number). Some box holds at least the average, rounded up. Everything so far in this unit has counted things exactly; this last tool is different, because it doesn't tell you how many, it tells you something must happen.
Put 4 letters into 3 mailboxes and some mailbox gets two. Why? If every mailbox had at most one, there'd be at most 3 letters. The general version works the same way. The average box holds objects, and not every box can be below average. If every box held fewer than objects, then each would hold at most , and the boxes together would hold fewer than . So some box must hold at least .
Classic uses:
- Among 13 people, two share a birth month.
- London has about 9 million people, and nobody has more than about 1 million hairs, so at least two Londoners have exactly the same number of hairs.
- Pull socks from a drawer of black and white socks in the dark: 3 socks guarantee a matching pair.
- Any 5 points in a unit square include two within of each other: cut the square into 4 smaller squares, and two points share one.
Notice that the principle proves something exists without saying which box overflows. You never find the two Londoners; you just know they're out there. The hard part, and the creative part, is choosing the right "boxes." In the square example nobody hands you the 4 smaller squares; you have to think of cutting it up.
A common mistake. It's tempting to read the principle as "some box gets " and round down, or to think it says the objects spread out evenly. It says neither. It only promises one box with at least , rounded up, and the other boxes could be empty.
See it
Worked examples
A hash function maps 1000 strings to 256 buckets. Show that some bucket gets at least 4 strings.
- 1
The strings are the objects and the buckets are the boxes, so and .
Practice
Type your answer and check it. Solve them all to complete the lesson.
How many people do you need to guarantee that two share a birth month?
50 emails are sorted into 7 folders. At least how many land in some folder?
A drawer holds black and white socks. How many must you pull, in the dark, to be sure of a matching pair?
A hash function maps 1000 strings into 256 buckets. Some bucket must hold at least how many strings?
What's the fewest integers you must pick to be sure that two of them differ by a multiple of 7?
At a party of 30 people, some pairs shake hands. Why must two people have shaken the same number of hands?
The hardest one here. Get it, and you've got this lesson.
You pick some numbers from 1 to 100. How many must you pick to guarantee that one of them divides another?
Where it's used
It proves that no compression scheme shrinks every file and that every hash table must handle collisions.