Simple Algorithm
Algorithms & Data Structures · Optimal · 28 min

Tail Bounds for Algorithms

Markov, Chebyshev and Chernoff: from “on average” to “with high probability”.

Flip a fair coin 100 times. Markov's inequality says that 75 or more heads has probability at most 0.67, Chebyshev's says at most 0.04, and a Chernoff–Hoeffding bound says at most 3.7×10−63.7 \times 10^{-6}; the truth is 2.8×10−72.8 \times 10^{-7}. Each step uses more about the random variable (its mean, its variance, then the independence of its parts), and that is exactly what turns "fast on average" into "fast with high probability".

What you’ll learn

  • Prove Markov's and Chebyshev's inequalities, and apply Chebyshev's with only pairwise independence.
  • Derive the multiplicative Chernoff bounds with the moment-generating-function method, state Hoeffding's additive bound, and know which one is tight in which regime.
  • Combine tail bounds with the union bound to prove high-probability results: O(nlog⁡n)O(n \log n) comparisons for randomized quicksort, O(log⁡n/log⁡log⁡n)O(\log n / \log\log n) maximum load for balls into bins, and ln⁡ln⁡n/ln⁡2+O(1)\ln\ln n / \ln 2 + O(1) with two choices.
  • Use the probabilistic method (Erdős's Ramsey bound) and derandomize with conditional expectations and pairwise-independent sample spaces.

Markov and Chebyshev

An expected running time of O(nlog⁡n)O(n \log n) does not rule out a rare run that takes n2n^2 steps. A tail bound caps the probability of such runs. The weakest needs only the mean: Markov's inequality, Pr⁡[X≥a]≤E[X]/a\Pr[X \ge a] \le \E[X]/a for X≥0X \ge 0, proved in Randomized Algorithms. The next one also uses the variance.

For a sum X=∑iXiX = \sum_i X_i, the variance is ∑iVar⁡[Xi]+∑i≠jCov(Xi,Xj)\sum_i \Var[X_i] + \sum_{i \ne j} \mathrm{Cov}(X_i, X_j), and the covariances vanish as soon as the XiX_i are pairwise independent; full independence is not needed. That is why Chebyshev's inequality is the tool for algorithms whose randomness comes from pairwise-independent hash functions, which need only O(log⁡n)O(\log n) random bits.

Here is how the bounds compare on XX = the number of heads in 100 fair flips (μ=50\mu = 50, Var⁡[X]=25\Var[X] = 25):

BoundUsesPr⁡[X≥60]\Pr[X \ge 60]Pr⁡[X≥75]\Pr[X \ge 75]
Markovthe mean0.8330.667
Chebyshevthe variance0.250.04
Chernoff, multiplicative formindependence0.3910.0045
Hoeffding, e−2t2/ne^{-2t^2/n}independence and boundedness0.1353.7×10−63.7 \times 10^{-6}
Exactthe whole distribution0.02842.8×10−72.8 \times 10^{-7}
Chebyshev bounds the two-sided event ∣X−50∣≥t\abs{X - 50} \ge t. For the small deviation, it even beats the multiplicative Chernoff bound, whose strength is large deviations of sums of rare events.

Chernoff bounds

DerivationProof of the upper tail: the moment-generating-function method
  1. Pr⁡[X≥a]  =  Pr⁡[etX≥eta]  ≤  e−ta E[etX]\Pr[X \ge a] \;=\; \Pr\big[e^{tX} \ge e^{ta}\big] \;\le\; e^{-ta}\,\E\big[e^{tX}\big]

    For any t>0t > 0, the map x↦etxx \mapsto e^{tx} is increasing, so apply Markov's inequality to etXe^{tX}:

  2. E[etX]  =  ∏i(1+pi(et−1))  ≤  ∏iepi(et−1)  =  eμ(et−1)\E\big[e^{tX}\big] \;=\; \prod_i \big(1 + p_i(e^t - 1)\big) \;\le\; \prod_i e^{p_i(e^t - 1)} \;=\; e^{\mu(e^t - 1)}

    Independence turns the expectation of a product into a product of expectations; then 1+x≤ex1 + x \le e^x removes the individual pip_i:

  3. Pr⁡[X≥(1+δ)μ]  ≤  eμδ (1+δ)−(1+δ)μ  =  (eδ(1+δ)1+δ)μ\Pr[X \ge (1+\delta)\mu] \;\le\; e^{\mu\delta}\,(1+\delta)^{-(1+\delta)\mu} \;=\; \Big(\frac{e^{\delta}}{(1+\delta)^{1+\delta}}\Big)^{\mu}

    Take a=(1+δ)μa = (1+\delta)\mu and the best tt, namely t=ln⁡(1+δ)t = \ln(1+\delta):

  4. (eδ(1+δ)1+δ)μ  ≤  (e1+δ)(1+δ)μ  ≤  (e6)R  ≤  2−R\Big(\frac{e^{\delta}}{(1+\delta)^{1+\delta}}\Big)^{\mu} \;\le\; \Big(\frac{e}{1+\delta}\Big)^{(1+\delta)\mu} \;\le\; \Big(\frac{e}{6}\Big)^{R} \;\le\; 2^{-R}

    For 0<δ≤10 < \delta \le 1, calculus shows δ−(1+δ)ln⁡(1+δ)≤−δ2/3\delta - (1+\delta)\ln(1+\delta) \le -\delta^2/3, which gives e−δ2μ/3e^{-\delta^2\mu/3}. For R=(1+δ)μ≥6μR = (1+\delta)\mu \ge 6\mu, bound eδ≤e1+δe^{\delta} \le e^{1+\delta}:

The lower tail is proved the same way with t<0t < 0. The only lossy step is 1+x≤ex1 + x \le e^x, which is nearly tight when the pip_i are small: the multiplicative bound is the right tool for sums of rare events, such as balls landing in one bin. For fair coins, Hoeffding's inequality (1963) is sharper: if the XiX_i are independent and take values in [0,1][0, 1], then Pr⁡[X−μ≥t]≤e−2t2/n\Pr[X - \mu \ge t] \le e^{-2t^2/n}, the 3.7×10−63.7 \times 10^{-6} in the table. Optimizing the same method exactly for fair coins gives 2.1×10−62.1 \times 10^{-6}, within a factor of 10 of the truth.

Predict before you look

A Monte Carlo test is correct with probability 2/32/3. You run it kk times independently and take the majority answer. The Chernoff lower-tail bound gives an error of at most e−k/48e^{-k/48} (with μ=2k/3\mu = 2k/3 and δ=1/4\delta = 1/4). How large must kk be for this bound to reach 10−610^{-6}?

Show the answer

664

The majority is wrong only if at most k/2=(1−14)⋅2k3k/2 = (1 - \tfrac14) \cdot \tfrac{2k}{3} runs are correct, so the bound is e−δ2μ/2=e−k/48e^{-\delta^2\mu/2} = e^{-k/48}, and k≥48ln⁡106≈663.1k \ge 48 \ln 10^6 \approx 663.1: 664 runs. The exact binomial tail is already below 10−610^{-6} at k=193k = 193. Chernoff's constants are loose, but the shape, error falling exponentially in kk, is exactly right.

With high probability: quicksort and balls into bins

The second tool is the union bound, Pr⁡[A1∪⋯∪Ak]≤∑iPr⁡[Ai]\Pr[A_1 \cup \cdots \cup A_k] \le \sum_i \Pr[A_i], which needs no independence at all. The standard pattern: bound each of nn bad events by n−(c+1)n^{-(c+1)} with a tail bound, and conclude that none of them happens with high probability, at least 1−n−c1 - n^{-c}.

DerivationProof sketch
  1. g  ≥  log⁡4/3n  =  ln⁡nln⁡(4/3)  ≈  3.48ln⁡ng \;\ge\; \log_{4/3} n \;=\; \frac{\ln n}{\ln(4/3)} \;\approx\; 3.48 \ln n

    Fix a key xx and follow the subarrays that contain it. Call a partition step good if its pivot's rank lies in the middle half of the subarray, so that both parts have at most 3/43/4 of its size. Whatever happened before, a step is good with probability at least about 1/21/2. After gg good steps the subarray has at most (3/4)gn(3/4)^g n keys, so xx is finished once

  2. Pr⁡[G<(1−δ)μ]  ≤  e−δ2μ/2  =  n−5δ2  ≈  n−2.13\Pr[G < (1 - \delta)\mu] \;\le\; e^{-\delta^2 \mu / 2} \;=\; n^{-5\delta^2} \;\approx\; n^{-2.13}

    Among L=20ln⁡nL = 20 \ln n steps, the number GG of good ones dominates a binomial variable with μ=10ln⁡n\mu = 10 \ln n. The key xx takes part in more than LL steps only if G<3.48ln⁡n=(1−δ)μG < 3.48 \ln n = (1 - \delta)\mu with δ≈0.652\delta \approx 0.652, and the lower-tail Chernoff bound gives

  3. ∑x(steps involving x)  ≤  20 nln⁡n\sum_{x} (\text{steps involving } x) \;\le\; 20\, n \ln n

    By the union bound over the nn keys, with probability at least 1−n⋅n−2.13≥1−1/n1 - n \cdot n^{-2.13} \ge 1 - 1/n, every key takes part in at most 20ln⁡n20 \ln n partition steps. Each comparison involves one non-pivot key in one step, so the total is at most

Balls into bins. Throw nn balls into nn bins independently and uniformly, the model of hashing with chaining and a random hash function. The average load is 1, but the fullest bin is larger. A fixed bin receives at least kk balls with probability at most (nk)n−k≤(e/k)k\binom nk n^{-k} \le (e/k)^k, the union over the kk-sets of balls. With k=⌈3ln⁡n/ln⁡ln⁡n⌉k = \lceil 3 \ln n / \ln\ln n \rceil this is at most 1/n21/n^2 for every n≥16n \ge 16, so by the union bound over bins, no bin holds more than 3ln⁡n/ln⁡ln⁡n3 \ln n / \ln\ln n balls with probability at least 1−1/n1 - 1/n. The true maximum is (1+o(1))ln⁡n/ln⁡ln⁡n(1 + o(1)) \ln n / \ln\ln n with high probability (Raab & Steger, 1998, give sharp bounds).

Predict before you look

One million balls go into one million bins, each into the less loaded of two random bins. About how many balls does the fullest bin hold?

Show the answer

4

ln⁡ln⁡106/ln⁡2=ln⁡13.8/0.693≈3.8\ln\ln 10^6 / \ln 2 = \ln 13.8 / 0.693 \approx 3.8, plus a constant: in five simulated runs the maximum was exactly 4 every time, against 8 to 10 with a single random choice.

The probabilistic method and derandomization

Randomness also proves that objects exist: if a random object has a property with positive probability, some object has it. Erdős's 1947 argument for Ramsey numbers is the classic example. The union bound again does the work.

DerivationProof
  1. Pr⁡[some monochromatic Kk]  ≤  (nk) 2 1−(k2)\Pr[\text{some monochromatic } K_k] \;\le\; \binom{n}{k}\, 2^{\,1 - \binom{k}{2}}

    Color each edge red or blue independently with probability 12\tfrac12. A fixed set of kk vertices spans (k2)\binom k2 edges, all red or all blue with probability 2⋅2−(k2)2 \cdot 2^{-\binom k2}. The union bound over all (nk)\binom nk sets gives

  2. (nk) 2 1−(k2)  <  2k2/2 2 1−k(k−1)/2k!  =  2 1+k/2k!  <  1(k≥3)\binom{n}{k}\, 2^{\,1 - \binom{k}{2}} \;<\; \frac{2^{k^2/2}\, 2^{\,1 - k(k-1)/2}}{k!} \;=\; \frac{2^{\,1 + k/2}}{k!} \;<\; 1 \qquad (k \ge 3)

    If this is below 1, some coloring has no monochromatic KkK_k. For n=⌊2k/2⌋n = \lfloor 2^{k/2} \rfloor, use (nk)<nk/k!≤2k2/2/k!\binom nk < n^k / k! \le 2^{k^2/2} / k!:

For k=10k = 10 and n=32n = 32, a random coloring has on average only about 3.7×10−63.7 \times 10^{-6} monochromatic copies of K10K_{10}, so almost every coloring of K32K_{32} avoids them. Yet no one knows how to write down such colorings efficiently for large kk: explicit constructions of Ramsey graphs remain far weaker than random ones.

Derandomization by conditional expectations. A random cut, each vertex on a random side, cuts each edge with probability 12\tfrac12, so some cut has at least m/2m/2 edges. To find one deterministically, place the vertices one at a time on the side that cuts more edges to the vertices already placed. The conditional expectation of the final cut, given the choices so far, is the average over the two options for the next vertex, so the better option never lowers it: it starts at m/2m/2 and ends as the actual cut.

python
def derandomized_cut(n, edges):
    """Method of conditional expectations for MAX-CUT: always cuts at least m/2 edges."""
    adj = [[] for _ in range(n)]
    for u, v in edges:
        adj[u].append(v); adj[v].append(u)
    side = [None] * n
    for v in range(n):                         # E[cut | sides fixed so far] never decreases
        to0 = sum(1 for w in adj[v] if side[w] == 0)
        to1 = sum(1 for w in adj[v] if side[w] == 1)
        side[v] = 1 if to0 >= to1 else 0       # join the side opposite most placed neighbors
    return sum(1 for u, v in edges if side[u] != side[v]), side

print(derandomized_cut(3, [(0, 1), (1, 2), (0, 2)]))   # (2, [1, 0, 1]): 2 of 3 triangle edges

Checked on 400 random graphs: always at least m/2m/2, never above the true maximum. The same method gives Johnson's (1974) algorithm satisfying 7/87/8 of the clauses of a 3-CNF formula whose clauses have three distinct variables, and Håstad (2001) proved that beating 7/8+ε7/8 + \varepsilon is NP-hard, so this simple derandomized coin flip is optimal unless P = NP.

Small sample spaces. The cut analysis needs only that the two endpoints of each edge get independent sides: pairwise independence. Label the vertices 1,…,n1, \dots, n, pick a random aa of ⌈log⁡2(n+1)⌉\lceil \log_2(n+1) \rceil bits, and put vertex vv on side parity(a∧v)\text{parity}(a \wedge v). Any two distinct labels then get independent uniform sides, and the sample space has fewer than 2n2n points, few enough to try them all: a deterministic algorithm that again guarantees m/2m/2.

Key takeaways

  • Markov uses only the mean; Chebyshev adds the variance, which needs only pairwise independence; Chernoff and Hoeffding use full independence to get exponentially small tails.
  • The Chernoff method: apply Markov to etXe^{tX}, factor the expectation by independence, optimize tt. The multiplicative bounds e−δ2μ/3e^{-\delta^2\mu/3} and e−δ2μ/2e^{-\delta^2\mu/2} suit sums of rare events; Hoeffding's e−2t2/ne^{-2t^2/n} suits bounded variables such as fair coins.
  • Tail bound per element plus a union bound over elements gives "with high probability": randomized quicksort makes O(nlog⁡n)O(n \log n) comparisons with probability 1−1/n1 - 1/n, and nn balls in nn bins leave no bin above 3ln⁡n/ln⁡ln⁡n3 \ln n / \ln\ln n, while two choices cut the maximum to ln⁡ln⁡n/ln⁡2+O(1)\ln\ln n / \ln 2 + O(1).
  • The probabilistic method proves existence (R(k,k)>2k/2R(k, k) > 2^{k/2}), and conditional expectations or small pairwise-independent sample spaces turn random guarantees, such as a cut with m/2m/2 edges, into deterministic algorithms.

Check yourself

In the interactive lesson these are scored with your confidence, and what you miss comes back for review.

Question 1

An analysis needs Var⁡[∑iXi]=∑iVar⁡[Xi]\Var\big[\sum_i X_i\big] = \sum_i \Var[X_i] to apply Chebyshev's inequality. Which assumption is enough?

  1. The XiX_i are mutually independent; nothing weaker works
  2. The XiX_i are pairwise independent
  3. The XiX_i are identically distributed
  4. The XiX_i are nonnegative
Show the answer

B. The XiX_i are pairwise independent

The variance of a sum adds the covariances of all pairs, and pairwise independence makes each of them 0. This is why pairwise-independent hash functions, which cost few random bits, suffice for Chebyshev-based analyses, while Chernoff bounds need more independence.

Question 2

XX counts heads in 100 fair coin flips. Which bound on Pr⁡[X≥60]\Pr[X \ge 60] is the smallest?

  1. Markov: 50/60≈0.8350/60 \approx 0.83
  2. Chebyshev: 25/102=0.2525/10^2 = 0.25
  3. Hoeffding: e−2⋅102/100=e−2≈0.135e^{-2 \cdot 10^2/100} = e^{-2} \approx 0.135
  4. Multiplicative Chernoff with δ=0.2\delta = 0.2: about 0.390.39
Show the answer

C. Hoeffding: e−2⋅102/100=e−2≈0.135e^{-2 \cdot 10^2/100} = e^{-2} \approx 0.135

Hoeffding's bound uses that each flip lies in [0,1][0, 1] and is sharp for fair coins. The multiplicative Chernoff bound loses in the step 1+x≤ex1 + x \le e^x, which is poor for p=1/2p = 1/2, and at this small deviation it is even weaker than Chebyshev. The exact value is about 0.028.

Question 3

Each of nn bins receives more than kk balls with probability at most 1/n21/n^2, but the bins' loads are dependent. What can you conclude about the event "some bin receives more than kk balls"?

  1. Nothing, because the events are not independent
  2. Its probability is at most 1/n1/n
  3. Its probability is at most 1/n21/n^2
  4. Its probability is exactly 1−(1−1/n2)n1 - (1 - 1/n^2)^n
Show the answer

B. Its probability is at most 1/n1/n

The union bound needs no independence: Pr⁡[⋃iAi]≤∑iPr⁡[Ai]≤n⋅1/n2=1/n\Pr[\bigcup_i A_i] \le \sum_i \Pr[A_i] \le n \cdot 1/n^2 = 1/n. This is how per-element tail bounds become "with high probability" statements about all elements at once.

Question 4

nn balls are placed one by one, each into the less loaded of two bins chosen uniformly at random. What is the maximum load, with high probability?

  1. Θ(ln⁡n/ln⁡ln⁡n)\Theta(\ln n / \ln\ln n), as with one choice
  2. ln⁡ln⁡n/ln⁡2+O(1)\ln\ln n / \ln 2 + O(1)
  3. At most 2
  4. About half the one-choice maximum
Show the answer

B. ln⁡ln⁡n/ln⁡2+O(1)\ln\ln n / \ln 2 + O(1)

This is Azar, Broder, Karlin and Upfal's theorem. Roughly, the number of bins with load at least ii shrinks doubly exponentially in ii once it is small, because a ball reaches load i+1i + 1 only if both its bins already have load ii. That squaring is what turns ln⁡n/ln⁡ln⁡n\ln n / \ln\ln n into ln⁡ln⁡n\ln\ln n.

Further reading

  • H. Chernoff, "A measure of asymptotic efficiency for tests of a hypothesis based on the sum of observations," Annals of Mathematical Statistics 23(4) (1952), 493–507.
  • W. Hoeffding, "Probability inequalities for sums of bounded random variables," Journal of the American Statistical Association 58(301) (1963), 13–30.
  • Y. Azar, A. Z. Broder, A. R. Karlin & E. Upfal, "Balanced allocations," SIAM Journal on Computing 29(1) (1999), 180–200.
  • P. Erdős, "Some remarks on the theory of graphs," Bulletin of the American Mathematical Society 53 (1947), 292–294.
  • M. Mitzenmacher & E. Upfal, Probability and Computing, 2nd ed. (Cambridge University Press, 2017), chapters 3–6.