TExcelTAD
Home › Randomized Algorithms & Probabilistic Analysis › Balls into Bins: The Maximum Load

Balls into Bins: The Maximum Load

Throw nn balls into nn bins, each independently and uniformly. The average load is 1, but the fullest bin has about ln⁡n/ln⁡ln⁡n\ln n / \ln \ln n balls, and the statement holds with high probability in both directions. This number is the longest chain of a hash table at load factor 1, the busiest server under random assignment, and the benchmark that the power of two choices beats. Balls into Bins, Coupons and Streaks introduced the model; this lesson proves the matching upper and lower bounds and looks at heavier loading.

The theorem

Theorem. With nn balls in nn bins, for every fixed ε>0\varepsilon > 0 and all large enough nn,

Pr⁡[(1−ε)ln⁡nln⁡ln⁡n≤max load≤(1+ε)ln⁡nln⁡ln⁡n]≥1−o(1).\Pr\left[(1 - \varepsilon)\frac{\ln n}{\ln \ln n} \le \text{max load} \le (1 + \varepsilon)\frac{\ln n}{\ln\ln n}\right] \ge 1 - o(1).

In particular the maximum load is Θ(ln⁡n/ln⁡ln⁡n)\Theta(\ln n / \ln\ln n) with high probability. A precise version, with the exact second-order terms and any number mm of balls, is due to Raab and Steger (1998); Gonnet (1981) computed the expectation, Γ−1(n)−32+o(1)\Gamma^{-1}(n) - \frac32 + o(1), where Γ−1\Gamma^{-1} is the inverse of the gamma function. The two directions use different tools: a union bound for the upper bound and the second-moment method for the lower bound.

Upper bound: union bound over bins

Let LiL_i be the load of bin ii. From The Union Bound at Work, Pr⁡[Li≥k]≤(nk)n−k≤1k!\Pr[L_i \ge k] \le \binom{n}{k} n^{-k} \le \frac{1}{k!}, so

Pr⁡[max load≥k]≤nk!.\Pr[\text{max load} \ge k] \le \frac{n}{k!} .

Take k=(1+ε)ln⁡nln⁡ln⁡nk = (1 + \varepsilon)\frac{\ln n}{\ln\ln n}. By Stirling, ln⁡k!=kln⁡k−O(k)\ln k! = k \ln k - O(k), and ln⁡k=ln⁡ln⁡n−ln⁡ln⁡ln⁡n+O(1)\ln k = \ln\ln n - \ln\ln\ln n + O(1), so

ln⁡k!=(1+ε)ln⁡nln⁡ln⁡n(ln⁡ln⁡n−ln⁡ln⁡ln⁡n−O(1))=(1+ε)ln⁡n (1−o(1)),\ln k! = (1 + \varepsilon)\frac{\ln n}{\ln\ln n}\bigl(\ln\ln n - \ln\ln\ln n - O(1)\bigr) = (1 + \varepsilon)\ln n\,(1 - o(1)),

which exceeds (1+ε/2)ln⁡n(1 + \varepsilon/2)\ln n for large nn. Then nk!≤n−ε/2→0\frac{n}{k!} \le n^{-\varepsilon/2} \to 0. With k=3ln⁡nln⁡ln⁡nk = \frac{3 \ln n}{\ln\ln n} the same calculation gives probability at most 1n\frac1n.

Lower bound: the second-moment method

Now let k=(1−ε)ln⁡nln⁡ln⁡nk = (1 - \varepsilon)\frac{\ln n}{\ln\ln n} and let X=∑iIiX = \sum_i I_i count the bins with load at least kk, where IiI_i indicates Li≥kL_i \ge k. We want Pr⁡[X=0]→0\Pr[X = 0] \to 0.

The mean is large. Pr⁡[Li≥k]≥Pr⁡[Li=k]=(nk)n−k(1−1n)n−k≥1e k!(1−O(k2/n))\Pr[L_i \ge k] \ge \Pr[L_i = k] = \binom{n}{k} n^{-k}(1 - \frac1n)^{n-k} \ge \frac{1}{e\,k!}\bigl(1 - O(k^2/n)\bigr), and the same Stirling calculation as above gives ln⁡k!≤(1−ε/2)ln⁡n\ln k! \le (1 - \varepsilon/2)\ln n for large nn. So

E[X]≥ne k!(1−o(1))≥nε/2e(1−o(1))→∞.\mathbb{E}[X] \ge \frac{n}{e\,k!}(1 - o(1)) \ge \frac{n^{\varepsilon/2}}{e}(1 - o(1)) \to \infty .

The variance is small. The loads of different bins are negatively correlated: a heavy bin ii leaves fewer balls for bin jj. Formally the loads are negatively associated (Dubhashi and Ranjan, 1998), which gives Pr⁡[Ii=1,Ij=1]≤Pr⁡[Ii=1]Pr⁡[Ij=1]\Pr[I_i = 1, I_j = 1] \le \Pr[I_i = 1]\Pr[I_j = 1] for i≠ji \ne j. All covariances are then at most 0, so

Var⁡[X]≤∑iVar⁡[Ii]≤E[X].\operatorname{Var}[X] \le \sum_i \operatorname{Var}[I_i] \le \mathbb{E}[X] .

Conclusion. By the second-moment inequality from Markov and Chebyshev: A Short Recap,

Pr⁡[X=0]≤Var⁡[X]E[X]2≤1E[X]→0.□\Pr[X = 0] \le \frac{\operatorname{Var}[X]}{\mathbb{E}[X]^2} \le \frac{1}{\mathbb{E}[X]} \to 0. \qquad \square

A common alternative replaces negative association by the Poisson approximation: the loads behave like independent Poisson(1) variables, and any event that is unlikely for the Poisson model (say with probability pp) has probability at most en pe\sqrt{n}\,p in the exact model (Mitzenmacher and Upfal, chapter 5).

Measured maxima

The leading term ln⁡n/ln⁡ln⁡n\ln n / \ln\ln n converges very slowly; the o(1)o(1) hides terms like ln⁡ln⁡ln⁡n/ln⁡ln⁡n\ln\ln\ln n / \ln\ln n. A much better predictor comes straight from the proof: the largest kk for which the expected number of bins with load at least kk is still at least 1, computed with the exact binomial tail. The program compares both with simulated maxima.

python · try it yourself
import random
from math import comb, log

def predicted(n):
    # largest k with n * Pr[Bin(n, 1/n) >= k] >= 1
    tail, k = 1.0, 0
    while n * tail >= 1:
        tail -= comb(n, k) * (1 / n) ** k * (1 - 1 / n) ** (n - k)
        k += 1
    return k - 1

rng = random.Random(110)
print("      n   mean max load   observed range   predictor   ln n / ln ln n")
for n, trials in [(100, 200), (1000, 60), (10000, 10), (100000, 2)]:
    maxima = []
    for _ in range(trials):
        load = [0] * n
        for _ in range(n):
            load[rng.randrange(n)] += 1
        maxima.append(max(load))
    print(f"{n:7}   {sum(maxima) / trials:13.2f}   {min(maxima):6} .. {max(maxima):<5}   {predicted(n):9}   {log(n) / log(log(n)):14.2f}")
      n   mean max load   observed range   predictor   ln n / ln ln n
    100            4.19        3 .. 8               4             3.02
   1000            5.52        4 .. 7               5             3.57
  10000            6.70        6 .. 8               6             4.15
 100000            7.50        7 .. 8               8             4.71

The maximum is concentrated: its spread does not grow with nn, and its mean stays within about one of the predictor. The leading term is off by a factor of about 1.5 at these sizes, which is why the growth rate, not the formula, is the practical answer.

The lab throws the balls one per frame. With 60 bins and 60 balls, the one-choice side (left) ends with a fullest bin of 4 in run 1; runs 2 to 5 give 4, 3, 6 and 3. (The right side, with two choices per ball, is the next lesson.)

One random bin per ball60 balls, 18 bins emptymax 460 binsLess loaded of two random bins60 balls, 13 bins emptymax 260 bins

Heavier loading

With mm balls, the picture changes once mm is large compared with nln⁡nn \ln n. Each load is then a binomial with mean μ=m/n≥ln⁡n\mu = m/n \ge \ln n, and Chernoff bounds show that every bin is within O(μln⁡n)O(\sqrt{\mu \ln n}) of the mean with high probability. For m≥nln⁡nm \ge n \ln n the Chernoff upper bound is matched from below, so

max load=mn+Θ(mln⁡nn)with high probability.\text{max load} = \frac{m}{n} + \Theta\left(\sqrt{\frac{m \ln n}{n}}\right) \quad \text{with high probability}.

Raab and Steger (1998) also determine the constant: for nln⁡n≪m≤n polylog(n)n \ln n \ll m \le n \,\mathrm{polylog}(n) the gap is (1+o(1))2(m/n)ln⁡n(1 + o(1))\sqrt{2(m/n)\ln n}. The gap above the average grows like m\sqrt{m} for a fixed number of bins. The two-choice process, by contrast, keeps the gap at ln⁡ln⁡nln⁡2+O(1)\frac{\ln\ln n}{\ln 2} + O(1) independently of mm (Berenbrink, Czumaj, Steger and Vöcking, 2000).

python · try it yourself
import random
from math import log, sqrt

rng = random.Random(111)
n = 200
print("    m      m/n   max load   gap = max - m/n   sqrt((m/n) ln n)")
for m in [200, 2000, 20000, 100000]:
    load = [0] * n
    for _ in range(m):
        load[rng.randrange(n)] += 1
    avg = m / n
    print(f"{m:6}   {avg:6.0f}   {max(load):8}   {max(load) - avg:15.1f}   {sqrt(avg * log(n)):16.1f}")
    m      m/n   max load   gap = max - m/n   sqrt((m/n) ln n)
   200        1          5               4.0                2.3
  2000       10         18               8.0                7.3
 20000      100        131              31.0               23.0
100000      500        563              63.0               51.5

In C++

The one-choice experiment for larger nn in C++ (fixed-seed std::mt19937, bin index rng() % n). The numbers come from a different random stream than Python's, but the concentration is the same.

cpp · try it yourself
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <random>
#include <vector>

int main() {
    std::mt19937 rng(110);
    std::printf("        n   max loads of 5 runs   ln n / ln ln n\n");
    for (int n : {1000, 10000, 100000, 1000000}) {
        std::printf("%9d  ", n);
        for (int run = 0; run < 5; ++run) {
            std::vector<int> load(n, 0);
            for (int b = 0; b < n; ++b) ++load[rng() % n];
            std::printf(" %3d", *std::max_element(load.begin(), load.end()));
        }
        std::printf("   %14.2f\n", std::log(n) / std::log(std::log(n)));
    }
}
        n   max loads of 5 runs   ln n / ln ln n
     1000     6   6   5   5   5             3.57
    10000     6   7   7   6   6             4.15
   100000     7   9   8   8   8             4.71
  1000000     8   9   8   9   8             5.26

Test yourself

With nn balls in nn bins, what is the maximum load with high probability?

  1. Θ(1)\Theta(1)
  2. Θ(ln⁡n/ln⁡ln⁡n)\Theta(\ln n / \ln\ln n)
  3. Θ(ln⁡n)\Theta(\ln n)
  4. Θ(n)\Theta(\sqrt n)

In the lower-bound proof, why is Var⁡[X]≤E[X]\operatorname{Var}[X] \le \mathbb{E}[X] for the number XX of bins with load at least kk?

  1. Because XX is Poisson
  2. Because the indicators are negatively correlated, so all covariances are at most 0, and each indicator has variance at most its mean
  3. Because the bins are independent
  4. Because kk is large

Which tool proves the upper bound on the maximum load?

  1. The second-moment method
  2. A union bound over the bins, with Pr⁡[Li≥k]≤1/k!\Pr[L_i \ge k] \le 1/k!
  3. Hoeffding's inequality
  4. The Lovász Local Lemma

With m=100nln⁡nm = 100n \ln n balls, how far above the average m/nm/n is the maximum load, with high probability?

  1. Θ(1)\Theta(1)
  2. Θ(ln⁡n)\Theta(\ln n), the order of (m/n)ln⁡n\sqrt{(m/n) \ln n}
  3. Θ(m/n)\Theta(m/n)
  4. Θ(n)\Theta(n)