# 随机算法 (Fall 2011)/Birthday Problem

There are ${\displaystyle m}$ students in the class. Assume that for each student, his/her birthday is uniformly and independently distributed over the 365 days in a years. We wonder what the probability that no two students share a birthday.

Due to the pigeonhole principle, it is obvious that for ${\displaystyle m>365}$, there must be two students with the same birthday. Surprisingly, for any ${\displaystyle m>57}$ this event occurs with more than 99% probability. This is called the birthday paradox. Despite the name, the birthday paradox is not a real paradox.

We can model this problem as a balls-into-bins problem. ${\displaystyle m}$ different balls (students) are uniformly and independently thrown into 365 bins (days). More generally, let ${\displaystyle n}$ be the number of bins. We ask for the probability of the following event ${\displaystyle {\mathcal {E}}}$

• ${\displaystyle {\mathcal {E}}}$: there is no bin with more than one balls (i.e. no two students share birthday).

We first analyze this by counting. There are totally ${\displaystyle n^{m}}$ ways of assigning ${\displaystyle m}$ balls to ${\displaystyle n}$ bins. The number of assignments that no two balls share a bin is ${\displaystyle {n \choose m}m!}$.

Thus the probability is given by:

{\displaystyle {\begin{aligned}\Pr[{\mathcal {E}}]={\frac {{n \choose m}m!}{n^{m}}}.\end{aligned}}}

Recall that ${\displaystyle {n \choose m}={\frac {n!}{(n-m)!m!}}}$. Then

{\displaystyle {\begin{aligned}\Pr[{\mathcal {E}}]={\frac {{n \choose m}m!}{n^{m}}}={\frac {n!}{n^{m}(n-m)!}}={\frac {n}{n}}\cdot {\frac {n-1}{n}}\cdot {\frac {n-2}{n}}\cdots {\frac {n-(m-1)}{n}}=\prod _{k=1}^{m-1}\left(1-{\frac {k}{n}}\right).\end{aligned}}}

There is also a more "probabilistic" argument for the above equation. To be rigorous, we need the following theorem, which holds generally and is very useful for computing the AND of many events.

 By the definition of conditional probability, ${\displaystyle \Pr[A\mid B]={\frac {\Pr[A\wedge B]}{\Pr[B]}}}$. Thus, ${\displaystyle \Pr[A\wedge B]=\Pr[B]\cdot \Pr[A\mid B]}$. This hints us that we can compute the probability of the AND of events by conditional probabilities. Formally, we have the following theorem: Theorem: Let ${\displaystyle {\mathcal {E}}_{1},{\mathcal {E}}_{2},\ldots ,{\mathcal {E}}_{n}}$ be any ${\displaystyle n}$ events. Then {\displaystyle {\begin{aligned}\Pr \left[\bigwedge _{i=1}^{n}{\mathcal {E}}_{i}\right]&=\prod _{k=1}^{n}\Pr \left[{\mathcal {E}}_{k}\mid \bigwedge _{i Proof: It holds that ${\displaystyle \Pr[A\wedge B]=\Pr[B]\cdot \Pr[A\mid B]}$. Thus, let ${\displaystyle A={\mathcal {E}}_{n}}$ and ${\displaystyle B={\mathcal {E}}_{1}\wedge {\mathcal {E}}_{2}\wedge \cdots \wedge {\mathcal {E}}_{n-1}}$, then {\displaystyle {\begin{aligned}\Pr[{\mathcal {E}}_{1}\wedge {\mathcal {E}}_{2}\wedge \cdots \wedge {\mathcal {E}}_{n}]&=\Pr[{\mathcal {E}}_{1}\wedge {\mathcal {E}}_{2}\wedge \cdots \wedge {\mathcal {E}}_{n-1}]\cdot \Pr \left[{\mathcal {E}}_{n}\mid \bigwedge _{i Recursively applying this equation to ${\displaystyle \Pr[{\mathcal {E}}_{1}\wedge {\mathcal {E}}_{2}\wedge \cdots \wedge {\mathcal {E}}_{n-1}]}$ until there is only ${\displaystyle {\mathcal {E}}_{1}}$ left, the theorem is proved. ${\displaystyle \square }$

Now we are back to the probabilistic analysis of the birthday problem, with a general setting of ${\displaystyle m}$ students and ${\displaystyle n}$ possible birthdays (imagine that we live in a planet where a year has ${\displaystyle n}$ days).

The first student has a birthday (of course!). The probability that the second student has a different birthday is ${\displaystyle \left(1-{\frac {1}{n}}\right)}$. Given that the first two students have different birthdays, the probability that the third student has a different birthday from the first two is ${\displaystyle \left(1-{\frac {2}{n}}\right)}$. Continuing this on, assuming that the first ${\displaystyle k-1}$ students all have different birthdays, the probability that the ${\displaystyle k}$th student has a different birthday than the first ${\displaystyle k-1}$, is given by ${\displaystyle \left(1-{\frac {k-1}{n}}\right)}$. So the probability that all ${\displaystyle m}$ students have different birthdays is the product of all these conditional probabilities:

{\displaystyle {\begin{aligned}\Pr[{\mathcal {E}}]=\left(1-{\frac {1}{n}}\right)\cdot \left(1-{\frac {2}{n}}\right)\cdots \left(1-{\frac {m-1}{n}}\right)&=\prod _{k=1}^{m-1}\left(1-{\frac {k}{n}}\right),\end{aligned}}}

which is the same as what we got by the counting argument.

There are several ways of analyzing this formular. Here is a convenient one: Due to Taylor's expansion, ${\displaystyle e^{-k/n}\approx 1-k/n}$. Then

{\displaystyle {\begin{aligned}\prod _{k=1}^{m-1}\left(1-{\frac {k}{n}}\right)&\approx \prod _{k=1}^{m-1}e^{-{\frac {k}{n}}}\\&=\exp \left(-\sum _{k=1}^{m-1}{\frac {k}{n}}\right)\\&=e^{-m(m-1)/2n}\\&\approx e^{-m^{2}/2n}.\end{aligned}}}

The quality of this approximation is shown in the Figure.

Therefore, for ${\displaystyle m={\sqrt {2n\ln {\frac {1}{\epsilon }}}}}$, the probability that ${\displaystyle \Pr[{\mathcal {E}}]\approx \epsilon }$.