Skip to content

Occupancy and birthday problem

Count occupied buckets and collision risk under iid random assignment. Separate the chance of a collision from the expected number of colliding pairs.

An occupancy problem asks how objects are distributed among buckets: how many buckets are empty, how many are occupied, or how large a bucket's load becomes. The birthday problem asks for the probability that at least two objects share a bucket. For a quant developer, this gives a baseline for hash collisions in a data cache or random identifier scheme, before analysing how the system resolves those collisions.
The standard model assigns nn labelled objects independently and uniformly to mm labelled buckets. Each of the mnm^n ordered assignments is equally likely. This is a mathematical model for the assignments, not evidence that a particular hash function, market identifier feed or trading-event stream behaves that way.

Four assignments, three different answers

Consider four distinct records assigned to ten buckets under that model. A collision means that two distinct records receive the same bucket; it does not mean that the records themselves are duplicates. A hash table must retain enough information to distinguish their keys.

Let KK count occupied buckets and CC count unordered pairs of records sharing a bucket. Then:

QuestionExact answer
Is there at least one collision?P(C≥1)=0.496\mathbb{P}(C\geq1)=0.496
How many colliding pairs on average?E[C]=0.6\mathbb{E}[C]=0.6 pairs
How many occupied buckets on average?E[K]=3.439\mathbb{E}[K]=3.439 buckets
Exact enumeration of four iid uniform assignments into ten buckets: one, two, three and four occupied buckets have probabilities 0.1%, 6.3%, 43.2% and 50.4%. Collision probability is 49.6%; mean colliding pairs is 0.6 and mean occupied buckets is 3.439.
Exact enumeration of four iid uniform assignments into ten buckets: one, two, three and four occupied buckets have probabilities 0.1%, 6.3%, 43.2% and 50.4%. Collision probability is 49.6%; mean colliding pairs is 0.6 and mean occupied buckets is 3.439.
Original exhaustive calculation over all 10,000 equally likely ordered assignments. The first three bars form the collision event; the three summaries measure different quantities.

The distinction matters because a run with a collision can contain several colliding pairs. Three records in one bucket already contribute (32)=3\binom{3}{2}=3 pairs. A probability counts that run once; the pair count counts each pair.

Count the assignments with no collisions

For the first record, every bucket is available. Conditional on no earlier collision, the next records must avoid one, two and then three occupied buckets. Thus

P(no collision)=10⋅9⋅8⋅7104=0.504,P(collision)=1−0.504=0.496.\mathbb{P}(\text{no collision}) =\frac{10\cdot9\cdot8\cdot7}{10^4}=0.504, \qquad \mathbb{P}(\text{collision})=1-0.504=0.496.

More generally, for 1≤n≤m1\leq n\leq m,

P(C≥1)=1−∏r=0n−1(1−rm).\mathbb{P}(C\geq1) =1-\prod_{r=0}^{n-1}\left(1-\frac{r}{m}\right).
This complement calculation is developed in Kleinberg's birthday-problem notes, §1.1. Once n>mn>m, a collision is certain by the pigeonhole principle. The product above is stated only on its valid range.
Why “birthday”? Replace records with people and buckets with calendar dates. Under the simplified model of independent, uniform birthdays on 365 dates, 23 people give a collision probability of about 50.73%. This concerns any pair in the group, rather than everyone matching one chosen person's birthday. Actual birth dates need not be uniform, and the model excludes leap day.

Count pairs and occupied buckets with indicators

For each of the (n2)\binom{n}{2} pairs, define an indicator function that equals one when its two assignments match. Each pair matches with probability 1/m1/m. Summing their expected values gives
E[C]=(n2)m.\mathbb{E}[C]=\frac{\binom{n}{2}}{m}.
For four records and ten buckets this is 6/10=0.66/10=0.6. It is not a 60% collision probability. Linearity of expectation does not require the pair indicators to be mutually independent; indeed, if two pairs among three records match, the third pair must match too.

For occupancy, use one indicator per bucket instead. A particular bucket stays empty if all nn assignments miss it, with probability (1−1/m)n(1-1/m)^n. Therefore

E[K]=m[1−(1−1m)n].\mathbb{E}[K] =m\left[1-\left(1-\frac1m\right)^n\right].
In our example, 10(1−0.94)=3.43910(1-0.9^4)=3.439. This is a mean across repeated assignments; an individual run has an integer number of occupied buckets. The empty-bucket indicator argument appears in Oliveira's notes, “Second question”.

The square-root scale is an approximation

Writing λ=n(n−1)/(2m)\lambda=n(n-1)/(2m), the usual birthday approximation is

P(C≥1)≈1−e−λ.\mathbb{P}(C\geq1)\approx1-e^{-\lambda}.
It follows by approximating log⁡(1−r/m)\log(1-r/m) with −r/m-r/m in the no-collision product. It is useful in the sparse regime; the sum of the omitted quadratic terms has order n3/m2n^3/m^2, so small individual ratios alone do not describe the whole error. Around the birthday scale nn proportional to m\sqrt m, this error shrinks as mm grows. This explains why appreciable collision risk can arrive long before most buckets are occupied. Kleinberg, §1.1, bounds the approximation error.
For our small example, 1−e−0.6≈0.45121-e^{-0.6}\approx0.4512, against the exact 0.4960.496: about 4.48 percentage points lower. Treating CC as Poisson gives this same approximation, not an exact distribution. Here it is also a lower bound on collision probability because 1−x≤e−x1-x\leq e^{-x} bounds the no-collision product from above.

What changes when assignments are uneven?

If assignments remain independent and identically distributed, but bucket jj has probability pjp_j, then

E[C]=(n2)∑j=1mpj2,E[K]=∑j=1m[1−(1−pj)n].\mathbb{E}[C]=\binom n2\sum_{j=1}^m p_j^2, \qquad \mathbb{E}[K]=\sum_{j=1}^m\left[1-(1-p_j)^n\right].

These follow from the same indicator arguments. Unequal bucket probabilities increase the pair mean relative to uniform assignment, since ∑jpj2≥1/m\sum_jp_j^2\geq1/m. Neither mean by itself determines the probability of any collision. Dependence between assignments requires another model again: knowing the marginal bucket frequencies is insufficient.

For the engineering consequences, Hash Tables: Chaining, Open Addressing, and Universal Hashing connects collisions to resolution methods, load factor and lookup costs. The public page is a preview; the full lesson requires membership.

Practice

Three distinct records are assigned independently and uniformly to four buckets. A colleague calculates (32)/4=0.75\binom32/4=0.75 and calls it the probability of a collision. What did they calculate, and what is the exact collision probability?

Answer. They calculated the expected number of colliding pairs: 0.750.75 pairs. The no-collision probability is (4⋅3⋅2)/43=3/8(4\cdot3\cdot2)/4^3=3/8, so the exact collision probability is 5/8=62.5%5/8=62.5\%. Runs in which all three records share a bucket contribute three pairs, which is why the mean pair count exceeds the probability of any collision.

Sources

Last updated
  • occupancy
  • birthday problem
  • collisions
  • hashing
  • combinatorics
  • probability
← Back to the glossaryBack to top ↑