Balls into Bins: The Maximum Load
Throw balls into bins, each independently and uniformly. The average load is 1, but the fullest bin has about 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 balls in bins, for every fixed and all large enough ,
In particular the maximum load is with high probability. A precise version, with the exact second-order terms and any number of balls, is due to Raab and Steger (1998); Gonnet (1981) computed the expectation, , where 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 be the load of bin . From The Union Bound at Work, , so
Take . By Stirling, , and , so
which exceeds for large . Then . With the same calculation gives probability at most .
Lower bound: the second-moment method
Now let and let count the bins with load at least , where indicates . We want .
The mean is large. , and the same Stirling calculation as above gives for large . So
The variance is small. The loads of different bins are negatively correlated: a heavy bin leaves fewer balls for bin . Formally the loads are negatively associated (Dubhashi and Ranjan, 1998), which gives for . All covariances are then at most 0, so
Conclusion. By the second-moment inequality from Markov and Chebyshev: A Short Recap,
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 ) has probability at most in the exact model (Mitzenmacher and Upfal, chapter 5).
Measured maxima
The leading term converges very slowly; the hides terms like . A much better predictor comes straight from the proof: the largest for which the expected number of bins with load at least is still at least 1, computed with the exact binomial tail. The program compares both with simulated maxima.
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 , 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.)
Heavier loading
With balls, the picture changes once is large compared with . Each load is then a binomial with mean , and Chernoff bounds show that every bin is within of the mean with high probability. For the Chernoff upper bound is matched from below, so
Raab and Steger (1998) also determine the constant: for the gap is . The gap above the average grows like for a fixed number of bins. The two-choice process, by contrast, keeps the gap at independently of (Berenbrink, Czumaj, Steger and Vöcking, 2000).
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 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.
#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 balls in bins, what is the maximum load with high probability?
In the lower-bound proof, why is for the number of bins with load at least ?
- Because is Poisson
- Because the indicators are negatively correlated, so all covariances are at most 0, and each indicator has variance at most its mean
- Because the bins are independent
- Because is large
Which tool proves the upper bound on the maximum load?
- The second-moment method
- A union bound over the bins, with
- Hoeffding's inequality
- The Lovász Local Lemma
With balls, how far above the average is the maximum load, with high probability?
- , the order of