# Randomized Algorithms (Spring 2010)/Balls and bins

Today's materiels are the common senses for randomized algorithms.

## Probability Basics

In probability theory class we have learned the basic concepts of events and random variables. The following are some examples:

 event ${\displaystyle {\mathcal {E}}}$: random variable ${\displaystyle X}$: ${\displaystyle {\mathcal {E}}_{1}}$: 75 out of 100 coin flips are HEADs ${\displaystyle X_{1}}$ is the number of HEADs in the 100 coin flips ${\displaystyle {\mathcal {E}}_{2}}$: the result of rolling a 6-sided die is an even number (2, 4, 6) ${\displaystyle X_{2}}$ is the result of rolling a die ${\displaystyle {\mathcal {E}}_{3}}$: tomorrow is rainy ${\displaystyle X_{3}=1}$ if tomorrow is rainy and ${\displaystyle X_{3}=0}$ if otherwise

Events and random variables can be related:

• An event can be defined from random variables ${\displaystyle X,Y}$ by a predicate ${\displaystyle P(X,Y)}$. (${\displaystyle {\mathcal {E}}}$: X>Y)
• A boolean random variable ${\displaystyle X}$ can be defined from an event ${\displaystyle {\mathcal {E}}}$ as follows:
{\displaystyle {\begin{aligned}X={\begin{cases}1&{\mathcal {E}}{\text{ occurs}}\\0&{\text{ otherwise}}\end{cases}}\end{aligned}}}

We say that ${\displaystyle X}$ indicates the event ${\displaystyle {\mathcal {E}}}$. We will later see that this indicator trick is very useful for probabilistic analysis.

The formal definitions of events and random variables are related to the foundation of probability theory, which is not the topic of this class. Here we only give an informal description (you may ignore them if you feel confused):

• A probability space is a set ${\displaystyle \Omega }$ of elementary events, also called samples. (6 sides of a die)
• An event is a subset of ${\displaystyle \Omega }$, i.e. a set of elementary events. (${\displaystyle {\mathcal {E}}_{2}}$: side 2, 4, 6 of the die)
• A random variable is not really a variable, but a function! The function maps events to values. (${\displaystyle X_{2}}$ maps the first side of the die to 1, the second side to 2, the third side to 3, ...)

The axiom foundation of probability theory is laid by Kolmogorov, one of the greatest mathematician of the 20th century, who advanced various very different fields of mathematics.

### Independent events

 Definition (Independent events) Two events ${\displaystyle {\mathcal {E}}_{1}}$ and ${\displaystyle {\mathcal {E}}_{2}}$ are independent if and only if {\displaystyle {\begin{aligned}\Pr \left[{\mathcal {E}}_{1}\wedge {\mathcal {E}}_{2}\right]&=\Pr[{\mathcal {E}}_{1}]\cdot \Pr[{\mathcal {E}}_{2}].\end{aligned}}}

This definition can be generalized to any number of events:

 Definition (Independent events) Events ${\displaystyle {\mathcal {E}}_{1},{\mathcal {E}}_{2},\ldots ,{\mathcal {E}}_{n}}$ are mutually independent if and only if, for any subset ${\displaystyle I\subseteq \{1,2,\ldots ,n\}}$, {\displaystyle {\begin{aligned}\Pr \left[\bigwedge _{i\in I}{\mathcal {E}}_{i}\right]&=\prod _{i\in I}\Pr[{\mathcal {E}}_{i}].\end{aligned}}}

The independence can also be defined for variables:

 Definition (Independent variables) Two random variables ${\displaystyle X}$ and ${\displaystyle Y}$ are independent if and only if ${\displaystyle \Pr[(X=x)\wedge (Y=y)]=\Pr[X=x]\cdot \Pr[Y=y]}$ for all values ${\displaystyle x}$ and ${\displaystyle y}$. Random variables ${\displaystyle X_{1},X_{2},\ldots ,X_{n}}$ are mutually independent if and only if, for any subset ${\displaystyle I\subseteq \{1,2,\ldots ,n\}}$ and any values ${\displaystyle x_{i}}$, where ${\displaystyle i\in I}$, {\displaystyle {\begin{aligned}\Pr \left[\bigwedge _{i\in I}(X_{i}=x_{i})\right]&=\prod _{i\in I}\Pr[X_{i}=x_{i}].\end{aligned}}}

Note that in probability theory, the "mutual independence" is not equivalent with "pair-wise independence", which we will learn in the future.

### The union bound

We are familiar with the principle of inclusion-exclusion for finite sets.

 Principle of Inclusion-Exclusion Let ${\displaystyle S_{1},S_{2},\ldots ,S_{n}}$ be ${\displaystyle n}$ finite sets. Then {\displaystyle {\begin{aligned}\left|\bigcup _{1\leq i\leq n}S_{i}\right|&=\sum _{i=1}^{n}|S_{i}|-\sum _{i

The principle can be generalized to probability events.

 Principle of Inclusion-Exclusion Let ${\displaystyle {\mathcal {E}}_{1},{\mathcal {E}}_{2},\ldots ,{\mathcal {E}}_{n}}$ be ${\displaystyle n}$ events. Then {\displaystyle {\begin{aligned}\Pr \left[\bigvee _{1\leq i\leq n}{\mathcal {E}}_{i}\right]&=\sum _{i=1}^{n}\Pr[{\mathcal {E}}_{i}]-\sum _{i

The principle is implied by the axiomatic formalization of probability. The following inequality is implied (nontrivially) by the principle of inclusion-exclusion:

 Theorem (the union bound) Let ${\displaystyle {\mathcal {E}}_{1},{\mathcal {E}}_{2},\ldots ,{\mathcal {E}}_{n}}$ be ${\displaystyle n}$ events. Then {\displaystyle {\begin{aligned}\Pr \left[\bigvee _{1\leq i\leq n}{\mathcal {E}}_{i}\right]&\leq \sum _{i=1}^{n}\Pr[{\mathcal {E}}_{i}].\end{aligned}}}

The name of this inequality is Boole's inequality. It is usually referred by its nickname "the union bound". The bound holds for arbitrary events, even if they are dependent. Due to this generality, the union bound is extremely useful for analyzing the union of events.

### Linearity of expectation

Let ${\displaystyle X}$ be a discrete random variable. The expectation of ${\displaystyle X}$ is defined as follows.

 Definition (Expectation) The expectation of a discrete random variable ${\displaystyle X}$, denoted by ${\displaystyle \mathbf {E} [X]}$, is given by {\displaystyle {\begin{aligned}\mathbf {E} [X]&=\sum _{x}x\Pr[X=x],\end{aligned}}} where the summation is over all values ${\displaystyle x}$ in the range of ${\displaystyle X}$.

Perhaps the most useful property of expectation is its linearity.

 Theorem (Linearity of Expectations) For any discrete random variables ${\displaystyle X_{1},X_{2},\ldots ,X_{n}}$, and any real constants ${\displaystyle a_{1},a_{2},\ldots ,a_{n}}$, {\displaystyle {\begin{aligned}\mathbf {E} \left[\sum _{i=1}^{n}a_{i}X_{i}\right]&=\sum _{i=1}^{n}a_{i}\cdot \mathbf {E} [X_{i}].\end{aligned}}}
Proof.
 By the definition of the expectations, it is easy to verify that (try to prove by yourself): for any discrete random variables ${\displaystyle X}$ and ${\displaystyle Y}$, and any real constant ${\displaystyle c}$, ${\displaystyle \mathbf {E} [X+Y]=\mathbf {E} [X]+\mathbf {E} [Y]}$; ${\displaystyle \mathbf {E} [cX]=c\mathbf {E} [X]}$. The theorem follows by induction.
${\displaystyle \square }$

 Example Supposed that we have a biased coin that the probability of HEADs is ${\displaystyle p}$. Flipping the coin for n times, what is the expectation of number of HEADs? It looks straightforward that it must be np, but how can we prove it? Surely we can apply the definition of expectation to compute the expectation with brute force. A more convenient way is by the linearity of expectations: Let ${\displaystyle X_{i}}$ indicate whether the ${\displaystyle i}$-th flip is HEADs. Then ${\displaystyle \mathbf {E} [X_{i}]=1\cdot p+0\cdot (1-p)=p}$, and the total number of HEADs after n flips is ${\displaystyle X=\sum _{i=1}^{n}X_{i}}$. Applying the linearity of expectation, the expected number of HEADs is: ${\displaystyle \mathbf {E} [X]=\mathbf {E} \left[\sum _{i=1}^{n}X_{i}\right]=\sum _{i=1}^{n}\mathbf {E} [X_{i}]=np}$.

The real power of the linearity of expectations is that it does not require the random variables to be independent, thus can be applied to any set of random variables. For example:

${\displaystyle \mathbf {E} \left[\alpha X+\beta X^{2}+\gamma X^{3}\right]=\alpha \cdot \mathbf {E} [X]+\beta \cdot \mathbf {E} \left[X^{2}\right]+\gamma \cdot \mathbf {E} \left[X^{3}\right].}$

However, do not exaggerate this power!

• For an arbitrary function ${\displaystyle f}$ (not necessarily linear), the equation ${\displaystyle \mathbf {E} [f(X)]=f(\mathbf {E} [X])}$ does not hold generally.
• For variances, the equation ${\displaystyle var(X+Y)=var(X)+var(Y)}$ does not hold without further assumption of the independence of ${\displaystyle X}$ and ${\displaystyle Y}$.

### Law of total probability(expectation)

In probability theory, the word "condition" is a verb. "Conditioning on the event ..." means that it is assumed that the event occurs.

 Definition (conditional probability) The conditional probability that event ${\displaystyle {\mathcal {E}}_{1}}$ occurs given that event ${\displaystyle {\mathcal {E}}_{2}}$ occurs is ${\displaystyle \Pr[{\mathcal {E}}_{1}\mid {\mathcal {E}}_{2}]={\frac {\Pr[{\mathcal {E}}_{1}\wedge {\mathcal {E}}_{2}]}{\Pr[{\mathcal {E}}_{2}]}}.}$

The conditional probability is well-defined only if ${\displaystyle \Pr[{\mathcal {E}}_{2}]\neq 0}$.

For independent events ${\displaystyle {\mathcal {E}}_{1}}$ and ${\displaystyle {\mathcal {E}}_{2}}$, it holds that

${\displaystyle \Pr[{\mathcal {E}}_{1}\mid {\mathcal {E}}_{2}]={\frac {\Pr[{\mathcal {E}}_{1}\wedge {\mathcal {E}}_{2}]}{\Pr[{\mathcal {E}}_{2}]}}={\frac {\Pr[{\mathcal {E}}_{1}]\cdot \Pr[{\mathcal {E}}_{2}]}{\Pr[{\mathcal {E}}_{2}]}}=\Pr[{\mathcal {E}}_{1}].}$

It supports our intuition that for two independent events, whether one of them occurs will not affect the chance of the other.

The following fact is known as the law of total probability. It computes the probability by averaging over all possible cases.

 Theorem (law of total probability) Let ${\displaystyle {\mathcal {E}}_{1},{\mathcal {E}}_{2},\ldots ,{\mathcal {E}}_{n}}$ be mutually disjoint events, and ${\displaystyle \bigvee _{i=1}^{n}{\mathcal {E}}_{i}=\Omega }$ is the sample space. Then for any event ${\displaystyle {\mathcal {E}}}$, ${\displaystyle \Pr[{\mathcal {E}}]=\sum _{i=1}^{n}\Pr[{\mathcal {E}}\mid {\mathcal {E}}_{i}]\cdot \Pr[{\mathcal {E}}_{i}].}$
Proof.
 Since ${\displaystyle {\mathcal {E}}_{1},{\mathcal {E}}_{2},\ldots ,{\mathcal {E}}_{n}}$ are mutually disjoint and ${\displaystyle \bigvee _{i=1}^{n}{\mathcal {E}}_{i}=\Omega }$, events ${\displaystyle {\mathcal {E}}\wedge {\mathcal {E}}_{1},{\mathcal {E}}\wedge {\mathcal {E}}_{2},\ldots ,{\mathcal {E}}\wedge {\mathcal {E}}_{n}}$ are also mutually disjoint, and ${\displaystyle {\mathcal {E}}=\bigvee _{i=1}^{n}\left({\mathcal {E}}\wedge {\mathcal {E}}_{i}\right)}$. Then ${\displaystyle \Pr[{\mathcal {E}}]=\sum _{i=1}^{n}\Pr[{\mathcal {E}}\wedge {\mathcal {E}}_{i}],}$ which according to the definition of conditional probability, is ${\displaystyle \sum _{i=1}^{n}\Pr[{\mathcal {E}}\mid {\mathcal {E}}_{i}]\cdot \Pr[{\mathcal {E}}_{i}]}$.
${\displaystyle \square }$

Conditional expectation can be accordingly defined:

 Definition (conditional expectation) For random variables ${\displaystyle X}$ and ${\displaystyle Y}$, ${\displaystyle \mathbf {E} [X\mid Y=y]=\sum _{x}x\Pr[X=x\mid Y=y],}$ where the summation is taken over the range of ${\displaystyle X}$.

There is also a law of total expectation.

 Theorem (law of total expectation) Let ${\displaystyle X}$ and ${\displaystyle Y}$ be two random variables. Then ${\displaystyle \mathbf {E} [X]=\sum _{y}\mathbf {E} [X\mid Y=y]\cdot \Pr[Y=y].}$

The law of total probability (expectation) provides us a standard tool for breaking a probability (or an expectation) into sub-cases. Sometimes, it helps the analysis.

### Distributions of coin flips

We introduce several important distributions induced by independent coin flips (independent probabilistic experiments), including: Bernoulli trial, geometric distribution, binomial distribution.

Bernoulli trial (Bernoulli distribution)
Bernoulli trial describes the probability distribution of a single (biased) coin flip. Suppose that we flip a (biased) coin where the probability of HEADS is ${\displaystyle p}$. Let ${\displaystyle X}$ be the 0-1 random variable which indicates whether the result is HEADS. We say that ${\displaystyle X}$ follows the Bernoulli distribution with parameter ${\displaystyle p}$. Formally,
{\displaystyle {\begin{aligned}X&={\begin{cases}1&{\text{with probability }}p\\0&{\text{with probability }}1-p\end{cases}}\end{aligned}}}.
Geometric distribution
Suppose we flip the same coin repeatedly until HEADS appears, where each coin flip is independent and follows the Bernoulli distribution with parameter ${\displaystyle p}$. Let ${\displaystyle X}$ be the random variable denoting the total number of coin flips. Then ${\displaystyle X}$ has the geometric distribution with parameter ${\displaystyle p}$. Formally, ${\displaystyle \Pr[X=k]=(1-p)^{k-1}p}$.
For geometric ${\displaystyle X}$, ${\displaystyle \mathbf {E} [X]={\frac {1}{p}}}$. This can be verified by directly computing ${\displaystyle \mathbf {E} [X]}$ by the definition of expectations. There is also a smarter way of computing ${\displaystyle \mathbf {E} [X]}$, by using indicators and the linearity of expectations. For ${\displaystyle k=0,1,2,\ldots }$, let ${\displaystyle Y_{k}}$ be the 0-1 random variable such that ${\displaystyle Y_{k}=1}$ if and only if none of the first ${\displaystyle k}$ coin flipings are HEADS, thus ${\displaystyle \mathbf {E} [Y_{k}]=\Pr[Y_{k}=1]=(1-p)^{k}}$. A key observation is that ${\displaystyle X=\sum _{k=0}^{\infty }Y_{k}}$. Thus, due to the linearity of expectations,
{\displaystyle {\begin{aligned}\mathbf {E} [X]=\mathbf {E} \left[\sum _{k=0}^{\infty }Y_{k}\right]=\sum _{k=0}^{\infty }\mathbf {E} [Y_{k}]=\sum _{k=0}^{\infty }(1-p)^{k}={\frac {1}{1-(1-p)}}={\frac {1}{p}}.\end{aligned}}}
Binomial distribution
Suppose we flip the same (biased) coin for ${\displaystyle n}$ times, where each coin flip is independent and follows the Bernoulli distribution with parameter ${\displaystyle p}$. Let ${\displaystyle X}$ be the number of HEADS. Then ${\displaystyle X}$ has the binomial distribution with parameters ${\displaystyle n}$ and ${\displaystyle p}$. Formally, ${\displaystyle \Pr[X=k]={n \choose k}p^{k}(1-p)^{n-k}}$.
A binomial random variable ${\displaystyle X}$ with parameters ${\displaystyle n}$ and ${\displaystyle p}$ is usually denoted by ${\displaystyle B(n,p)}$.
As we saw above, by applying the linearity of expectations, it is easy to show that ${\displaystyle \mathbf {E} [X]=np}$ for an ${\displaystyle X=B(n,p)}$.

## Balls-into-bins model

Imagine that ${\displaystyle m}$ balls are thrown into ${\displaystyle n}$ bins, in such a way that each ball is thrown into a bin which is uniformly and independently chosen from all ${\displaystyle n}$ bins.

There are several information we would like to know about this random process. For example:

• the probability that there is no bin with more than one balls (the birthday problem)
• the number of balls thrown to make sure there is no empty bins (coupon collector problem)
• the distribution of the loads of bins (occupancy problem)

Why is this model useful anyway? It is useful because it is not just about throwing balls into bins, but a uniformly random function mapping ${\displaystyle [m]}$ to ${\displaystyle [n]}$. So we are actually looking at the behaviors of random mappings.

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 }$.

### Coupon collector's problem

Suppose that a chocolate company releases ${\displaystyle n}$ different types of coupons. Each box of chocolates contains one coupon with a uniformly random type. Once you have collected all ${\displaystyle n}$ types of coupons, you will get a prize. So how many boxes of chocolates you are expected to buy to win the prize?

The coupon collector problem can be described in the balls-into-bins model as follows. We keep throwing balls one-by-one into ${\displaystyle n}$ bins (coupons), such that each ball is thrown into a bin uniformly and independently at random. Each ball corresponds to a box of chocolate, and each bin corresponds to a type of coupon. Thus, the number of boxes bought to collect ${\displaystyle n}$ coupons is just the number of balls thrown until none of the ${\displaystyle n}$ bins is empty.

 Theorem Let ${\displaystyle X}$ be the number of balls thrown uniformly and independently to ${\displaystyle n}$ bins until no bin is empty. Then ${\displaystyle \mathbf {E} [X]=nH(n)}$, where ${\displaystyle H(n)}$ is the ${\displaystyle n}$th harmonic number.
Proof.
 Let ${\displaystyle X_{i}}$ be the number of balls thrown while there are exactly ${\displaystyle i-1}$ nonempty bins, then clearly ${\displaystyle X=\sum _{i=1}^{n}X_{i}}$. When there are exactly ${\displaystyle i-1}$ nonempty bins, throwing a ball, the probability that the number of nonempty bins increases (i.e. the ball is thrown to an empty bin) is ${\displaystyle p_{i}=1-{\frac {i-1}{n}}.}$ ${\displaystyle X_{i}}$ is the number of balls thrown to make the number of nonempty bins increases from ${\displaystyle i-1}$ to ${\displaystyle i}$, i.e. the number of balls thrown until a ball is thrown to a current empty bin. Thus, ${\displaystyle X_{i}}$ follows the geometric distribution, such that ${\displaystyle \Pr[X_{i}=k]=(1-p_{i})^{k-1}p_{i}}$ For a geometric random variable, ${\displaystyle \mathbf {E} [X_{i}]={\frac {1}{p_{i}}}={\frac {n}{n-i+1}}}$. Applying the linearity of expectations, {\displaystyle {\begin{aligned}\mathbf {E} [X]&=\mathbf {E} \left[\sum _{i=1}^{n}X_{i}\right]\\&=\sum _{i=1}^{n}\mathbf {E} \left[X_{i}\right]\\&=\sum _{i=1}^{n}{\frac {n}{n-i+1}}\\&=n\sum _{i=1}^{n}{\frac {1}{i}}\\&=nH(n),\end{aligned}}} where ${\displaystyle H(n)}$ is the ${\displaystyle n}$th Harmonic number, and ${\displaystyle H(n)=\ln n+O(1)}$. Thus, for the coupon collectors problem, the expected number of coupons required to obtain all ${\displaystyle n}$ types of coupons is ${\displaystyle n\ln n+O(n)}$.
${\displaystyle \square }$

Only knowing the expectation is not good enough. We would like to know how fast the probability decrease as a random variable deviates from its mean value.

 Theorem Let ${\displaystyle X}$ be the number of balls thrown uniformly and independently to ${\displaystyle n}$ bins until no bin is empty. Then ${\displaystyle \Pr[X\geq n\ln n+cn] for any ${\displaystyle c>0}$.
Proof.
 For any particular bin ${\displaystyle i}$, the probability that bin ${\displaystyle i}$ is empty after throwing ${\displaystyle n\ln n+cn}$ balls is ${\displaystyle \left(1-{\frac {1}{n}}\right)^{n\ln n+cn} By the union bound, the probability that there exists an empty bin after throwing ${\displaystyle n\ln n+cn}$ balls is ${\displaystyle \Pr[X\geq n\ln n+cn]
${\displaystyle \square }$

### Occupancy problem

Now we ask about the loads of bins. Assuming that ${\displaystyle m}$ balls are uniformly and independently assigned to ${\displaystyle n}$ bins, for ${\displaystyle 1\leq i\leq n}$, let ${\displaystyle X_{i}}$ be the load of the ${\displaystyle i}$th bin, i.e. the number of balls in the ${\displaystyle i}$th bin.

An easy analysis shows that for every bin ${\displaystyle i}$, the expected load ${\displaystyle \mathbf {E} [X_{i}]}$ is equal to the average load ${\displaystyle m/n}$.

Because there are totally ${\displaystyle m}$ balls, it is always true that ${\displaystyle \sum _{i=1}^{n}X_{i}=m}$.

Therefore, due to the linearity of expectations,

{\displaystyle {\begin{aligned}\sum _{i=1}^{n}\mathbf {E} [X_{i}]&=\mathbf {E} \left[\sum _{i=1}^{n}X_{i}\right]=\mathbf {E} \left[m\right]=m.\end{aligned}}}

Because for each ball, the bin to which the ball is assigned is uniformly and independently chosen, the distributions of the loads of bins are identical. Thus ${\displaystyle \mathbf {E} [X_{i}]}$ is the same for each ${\displaystyle i}$. Combining with the above equation, it holds that for every ${\displaystyle 1\leq i\leq m}$, ${\displaystyle \mathbf {E} [X_{i}]={\frac {m}{n}}}$. So the average is indeed the average!

Next we analyze the distribution of the maximum load. We show that when ${\displaystyle m=n}$, i.e. ${\displaystyle n}$ balls are uniformly and independently thrown into ${\displaystyle n}$ bins, the maximum load is ${\displaystyle O\left({\frac {\log n}{\log \log n}}\right)}$ with high probability.

 Theorem Suppose that ${\displaystyle n}$ balls are thrown independently and uniformly at random into ${\displaystyle n}$ bins. For ${\displaystyle 1\leq i\leq n}$, let ${\displaystyle X_{i}}$ be the random variable denoting the number of balls in the ${\displaystyle i}$th bin. Then ${\displaystyle \Pr \left[\max _{1\leq i\leq n}X_{i}\geq {\frac {3\ln n}{\ln \ln n}}\right]<{\frac {1}{n}}.}$
Proof.
 Let ${\displaystyle M}$ be an integer. Take bin 1. For any particular ${\displaystyle M}$ balls, these ${\displaystyle M}$ balls are all thrown to bin 1 with probability ${\displaystyle (1/n)^{M}}$, and there are totally ${\displaystyle {n \choose M}}$ distinct sets of ${\displaystyle M}$ balls. Therefore, applying the union bound, {\displaystyle {\begin{aligned}\Pr \left[X_{1}\geq M\right]&\leq {n \choose M}\left({\frac {1}{n}}\right)^{M}\\&={\frac {n!}{M!(n-M)!n^{M}}}\\&={\frac {1}{M!}}\cdot {\frac {n(n-1)(n-2)\cdots (n-M+1)}{n^{M}}}\\&={\frac {1}{M!}}\cdot \prod _{i=0}^{M-1}\left(1-{\frac {i}{n}}\right)\\&\leq {\frac {1}{M!}}.\end{aligned}}} According to Stirling's approximation, ${\displaystyle M!\approx {\sqrt {2\pi M}}\left({\frac {M}{e}}\right)^{M}}$, thus ${\displaystyle {\frac {1}{M!}}\leq \left({\frac {e}{M}}\right)^{M}.}$ Figure 1 Due to the symmetry. All ${\displaystyle X_{i}}$ have the same distribution. Apply the union bound again, {\displaystyle {\begin{aligned}\Pr \left[\max _{1\leq i\leq n}X_{i}\geq M\right]&=\Pr \left[(X_{1}\geq M)\vee (X_{2}\geq M)\vee \cdots \vee (X_{n}\geq M)\right]\\&\leq n\Pr[X_{1}\geq M]\\&\leq n\left({\frac {e}{M}}\right)^{M}.\end{aligned}}} When ${\displaystyle M=3\ln n/\ln \ln n}$, {\displaystyle {\begin{aligned}\left({\frac {e}{M}}\right)^{M}&=\left({\frac {e\ln \ln n}{3\ln n}}\right)^{3\ln n/\ln \ln n}\\&<\left({\frac {\ln \ln n}{\ln n}}\right)^{3\ln n/\ln \ln n}\\&=e^{3(\ln \ln \ln n-\ln \ln n)\ln n/\ln \ln n}\\&\leq e^{-3\ln n+3(\ln \ln n)(\ln n)/\ln \ln n}\\&\leq e^{-2\ln n}\\&={\frac {1}{n^{2}}}.\end{aligned}}} Therefore, {\displaystyle {\begin{aligned}\Pr \left[\max _{1\leq i\leq n}X_{i}\geq {\frac {3\ln n}{\ln \ln n}}\right]&\leq n\left({\frac {e}{M}}\right)^{M}&<{\frac {1}{n}}.\end{aligned}}}
${\displaystyle \square }$

When ${\displaystyle m>n}$, Figure 1 illustrates the results of several random experiments, which show that the distribution of the loads of bins becomes more even as the number of balls grows larger than the number of bins.

Formally, it can be proved that for ${\displaystyle m=\Omega (n\log n)}$, with high probability, the maximum load is within ${\displaystyle O\left({\frac {m}{n}}\right)}$, which is asymptotically equal to the average load.

## Applications of balls-into-bins

### Birthday attack

Suppose that ${\displaystyle h:U\rightarrow [n]}$ is a cryptographic hash function that maps a large domain of items to a small range, i.e. we have that ${\displaystyle |U|\gg n}$. As a cryptographic hash function, ${\displaystyle h}$ is deterministic and efficiently computable. The security of the crypto systems built from the hash function are usually based on the following properties of function:

• One-way-ness: Given a hash value ${\displaystyle i\in [n]}$, it is hard to generate an ${\displaystyle x\in U}$ that ${\displaystyle h(x)=i}$.
• Weak collision resistance: Given an item ${\displaystyle x\in U}$, it is hard to find an ${\displaystyle x'\in U}$ that ${\displaystyle x\neq x'}$ and ${\displaystyle h(x)=h(x')}$. Such ${\displaystyle (x,x')}$ is called a collision pair.
• Strong collision resistance: It is hard to generate a collision pair.

It can be seen that the third property is the strongest one. Some digital signature schemes are based on this assumption.

The "birthday attack" is inspired by the birthday paradox. Uniformly and independently pick a number of random items from ${\displaystyle U}$, with some fair probability, there is a collision pair among these randomly chosen items.

Suppose that we choose ${\displaystyle m}$ items from ${\displaystyle U}$. We assume that ${\displaystyle m=O({\sqrt {n}})}$. Since ${\displaystyle |U|\gg n}$, the probability that we pick the same item twice is ${\displaystyle o(1)}$ (this can be seen as a birthday problem with very small number of students). The following arguments are conditioned on that all chosen items are different.

Therefore, we can view the hash values ${\displaystyle [n]}$ as ${\displaystyle n}$ bins, and hashing a random ${\displaystyle x}$ to ${\displaystyle h(x)}$ as throwing a ball into a bin. Conditioning on that all chosen items are different, a collision occurs if and only if a bin has more than one balls.

For each ${\displaystyle i\in [n]}$, let ${\displaystyle p_{i}={\frac {|h^{-1}(i)|}{|U|}}}$ be the fraction of the domain mapped to hash value ${\displaystyle i}$. Obviously ${\displaystyle \sum _{i\in [n]}p_{i}=1}$.

If all ${\displaystyle p_{i}}$ are equal that ${\displaystyle p_{i}={\frac {1}{n}}}$, i.e. the hash function maps the items evenly over its range, then each ball is thrown to a uniform bin. It is exactly as the birthday problem. Due to our analysis of the birthday problem, for some ${\displaystyle m=O({\sqrt {n}})}$, there is a collision pair with probability ${\displaystyle 999/1000}$.

This argument is conditioned on that all chosen items are different. As we saw the probability that we choose the same item twice is only ${\displaystyle o(1)}$, by union bound the probability that this occurs or no two balls are thrown to the same bins is ${\displaystyle 1/1000+o(1)}$. So the probability that a collision pair is found by the birthday attack is ${\displaystyle 999/1000-o(1)}$.

What if ${\displaystyle h}$ does not distribute items evenly over the range, i.e. ${\displaystyle p_{i}}$'s are different. We can partition each ${\displaystyle p_{i}}$ into as many as possible "virtual bins" of probability mass ${\displaystyle 1/2n}$, and sum the remainders to a single "garbage bin". It is easy to see that there are at most ${\displaystyle 2n}$ virtual bins and the probability mass of the garbage bin is no more than 1/2. Then the problem is turned to throwing each of the ${\displaystyle m}$ balls first to the garbage bin with probability at most 1/2, and if survived, to a uniformly random virtual bin.

### Stable marriage

Suppose that there are ${\displaystyle n}$ men and ${\displaystyle n}$ women. Every man has a preference list of women, which can be represented as a permutation of ${\displaystyle [n]}$. Similarly, every women has a preference list of men, which is also a permutation of ${\displaystyle [n]}$. A marriage is a 1-1 correspondence between men and women. The stable marriage problem or stable matching problem (SMP) is to find a marriage which is stable in the following sense:

There is no such a man and a woman who are not married to each other but prefer each other to their current partners.

The famous proposal algorithm (求婚算法) solves this problem by finding a stable marriage. The algorithm is described as follows:

Each round (called a proposal)
• An unmarried man proposes to the most desirable woman according to his preference list who has not already rejected him.
• Upon receiving his proposal, the woman accepts the proposal if:
1. she's not married; or
2. her current partner is less desirable than the proposing man according to her preference list. (Her current partner then becomes available again.)

The algorithm terminates when the last available woman receives a proposal. The algorithm returns a marriage, because it is easy to see that:

once a woman is proposed to, she gets married and stays as married (and will only switch to more desirable men.)

It can be seen that this algorithm always finds a stable marriage:

If to the contrary, there is a man ${\displaystyle A}$ and a woman ${\displaystyle b}$ prefer each other than their current partners ${\displaystyle a}$ (${\displaystyle A}$'s wife) and ${\displaystyle B}$ (${\displaystyle b}$'s husband), then ${\displaystyle A}$ must have proposed to ${\displaystyle b}$ before he proposed to ${\displaystyle a}$, by which time ${\displaystyle b}$ must either be available or be with a worse man (because her current partner ${\displaystyle B}$ is worse than ${\displaystyle A}$), which means ${\displaystyle b}$ must have accepted ${\displaystyle A}$'s proposal.

Our interest is the average-case performance of this algorithm, which is measured by the expected number of proposals, assuming that each man/woman has a uniformly random permutation as his/her preference list.

Apply the principle of deferred decisions, each man can be seen as that at each time, sampling a uniformly random woman from the ones who have not already rejected him, and proposing to her. This can only be more efficient than sampling a uniformly and independently random woman to propose. All ${\displaystyle n}$ men are proposing to uniformly and independently random woman, thus it can be seen as proposals (regardless which men they are from) are sent to women uniformly and independently at random. The algorithm ends when all ${\displaystyle n}$ women have received a proposal. Due to our analysis of the coupon collector problem, the expected number of proposals is ${\displaystyle O(n\ln n)}$.

### Chain hashing and load balancing

Consider the following two scenarios:

• A hash table which resolves collisions by chaining, such that all keys mapped to the same slots are arranged by a linked list pointed from the slot.
• Distributed a number of jobs evenly among a set of machines by a random hash function.

With UHA, both scenarios can be viewed as a balls-into-bins game, and the loads of bins give the lengths of chains or the loads of machines. The maximum load of bins is critical, because it gives the worst-case searching time for the hash table and the finishing time for the machines.

By our assignment of the occupancy problem, when ${\displaystyle m=\Theta (n)}$, the maximum load is ${\displaystyle O(\ln n/\ln \ln n)}$ with high probability.