TExcelTAD

Exercises

Fourteen problems covering the whole course: Las Vegas and Monte Carlo algorithms, tail bounds, the local lemma, minimum cuts, algebraic tests, hashing, random walks, mixing and derandomization. Try each one on paper first. Every problem has a short Answer to check yourself against and a full Solution; several solutions include a program that confirms the result, which you can edit and run.

Tip: Before applying a tail bound, write down the random variable as a sum, check which independence the summands have, and decide whether you need an additive (Hoeffding) or a multiplicative (Chernoff) deviation. For a union bound, count the bad events first.

# Exercise Practises
1 Stopping a Las Vegas algorithm Markov, restarts
2 How many votes? amplification, Hoeffding
3 A rare-event tail Chernoff bounds
4 Sample size for an average Hoeffding
5 A million bins union bound, max load
6 Occurrences in 7-SAT Lovász Local Lemma
7 Repeating Karger contraction algorithm
8 The Karger-Stein recurrence recursive contraction
9 Two wrong entries Freivalds
10 How big a field? Schwartz-Zippel
11 A universal family that is not 2-wise independent hash families
12 A confident 2-SAT walk Papadimitriou's walk
13 Mixing on the 100-cube coupling
14 MAX-SAT with short clauses conditional expectations

Exercise 1: Stopping a Las Vegas algorithm

A Las Vegas algorithm has expected running time E[T]≤f(n)\mathbb{E}[T] \le f(n). (a) Using Markov's inequality once, after how many steps can you stop it to get a Monte Carlo algorithm with failure probability at most 1/1001/100? (b) Do better by restarting: run it for 2f(n)2f(n) steps, and if it has not finished, restart with fresh random bits. How many steps in total suffice for failure probability at most 1/1001/100?

Answer

(a) 100f(n)100 f(n) steps. (b) 7 segments of 2f(n)2f(n) steps, so 14f(n)14 f(n) steps.

Solution

(a) Pr⁡[T≥cf(n)]≤1/c\Pr[T \ge c f(n)] \le 1/c, so c=100c = 100. (b) Each segment is a fresh run, independent of the earlier ones, and fails to finish within 2f(n)2f(n) steps with probability at most 1/21/2 by Markov. All kk segments fail with probability at most 2−k2^{-k}, and 2−7=1/128<1/1002^{-7} = 1/128 < 1/100. Restarting turns Markov's polynomial tail into an exponential one (Las Vegas and Monte Carlo Algorithms).

Exercise 2: How many votes?

A two-sided Monte Carlo test is correct with probability 3/43/4 on every input. How many independent runs does Hoeffding's inequality require for a majority vote to err with probability at most 10−610^{-6}? Compute the exact error of the majority for that number of runs.

Answer

k≥ln⁡(106)/(2⋅(1/4)2)≈110.5k \ge \ln(10^6)/(2 \cdot (1/4)^2) \approx 110.5, so 111 runs (an odd number). The exact error of 111 runs is about 7⋅10−97 \cdot 10^{-9}, and in fact 79 runs already reach 10−610^{-6}.

Solution

With advantage γ=1/4\gamma = 1/4, the majority is wrong with probability at most e−2γ2k=e−k/8e^{-2\gamma^2 k} = e^{-k/8} (Hoeffding's Inequality). Solving e−k/8≤10−6e^{-k/8} \le 10^{-6} gives k≥8ln⁡106≈110.5k \ge 8 \ln 10^6 \approx 110.5.

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

k = 111
exact = sum(comb(k, j) * 0.75 ** j * 0.25 ** (k - j) for j in range(k // 2 + 1))
print(f"bound requires k >= {8 * log(1e6):.1f}; with k = {k}: Hoeffding {exp(-k / 8):.2e}, exact {exact:.2e}")
smallest = next(k for k in range(1, 200, 2)
                if sum(comb(k, j) * 0.75 ** j * 0.25 ** (k - j) for j in range(k // 2 + 1)) <= 1e-6)
print("smallest odd k with exact error <= 1e-6:", smallest)
bound requires k >= 110.5; with k = 111: Hoeffding 9.42e-07, exact 7.40e-09
smallest odd k with exact error <= 1e-6: 79

Exercise 3: A rare-event tail

XX counts heads in 1000 independent tosses of a coin with Pr⁡[heads]=0.1\Pr[\text{heads}] = 0.1. Bound Pr⁡[X≥150]\Pr[X \ge 150] with Chernoff's e−δ2μ/3e^{-\delta^2\mu/3} and with e−δ2μ/(2+δ)e^{-\delta^2\mu/(2+\delta)}, and compare with Hoeffding and the exact value.

Answer

μ=100\mu = 100, δ=0.5\delta = 0.5: e−25/3≈2.4⋅10−4e^{-25/3} \approx 2.4 \cdot 10^{-4} and e−10≈4.5⋅10−5e^{-10} \approx 4.5 \cdot 10^{-5}. Hoeffding gives only e−2⋅502/1000=e−5≈6.7⋅10−3e^{-2 \cdot 50^2/1000} = e^{-5} \approx 6.7 \cdot 10^{-3}. The exact value is about 4.5⋅10−74.5 \cdot 10^{-7}.

Solution

δ=150/100−1=0.5≤1\delta = 150/100 - 1 = 0.5 \le 1, so both Chernoff forms apply (Chernoff Bounds and Their Proofs). Hoeffding ignores that the mean is small compared with nn.

python · try it yourself
from math import exp, lgamma, log

n, p, k = 1000, 0.1, 150
exact = sum(exp(lgamma(n + 1) - lgamma(j + 1) - lgamma(n - j + 1) + j * log(p) + (n - j) * log(1 - p))
            for j in range(k, n + 1))
mu, d = n * p, k / (n * p) - 1
print(f"exact {exact:.2e}; Chernoff /3: {exp(-d * d * mu / 3):.2e}; Chernoff /(2+d): {exp(-d * d * mu / (2 + d)):.2e}; "
      f"Hoeffding: {exp(-2 * (k - mu) ** 2 / n):.2e}")
exact 4.49e-07; Chernoff /3: 2.40e-04; Chernoff /(2+d): 4.54e-05; Hoeffding: 6.74e-03

Exercise 4: Sample size for an average

You estimate the mean of a function with values in [0,1][0, 1] by averaging independent samples. How many samples make the estimate accurate to ±0.02\pm 0.02 with probability at least 0.9990.999?

Answer

n≥ln⁡(2/0.001)2⋅0.022≈9501.1n \ge \frac{\ln(2/0.001)}{2 \cdot 0.02^2} \approx 9501.1, so 9502 samples.

Solution

Hoeffding gives Pr⁡[∣Xˉ−μ∣≥ε]≤2e−2nε2\Pr[\lvert \bar X - \mu \rvert \ge \varepsilon] \le 2e^{-2n\varepsilon^2}. Setting this to 0.0010.001 gives n≥ln⁡(2000)/(2⋅0.0004)=7.601/0.0008≈9501.1n \ge \ln(2000)/(2 \cdot 0.0004) = 7.601/0.0008 \approx 9501.1, so 9502 samples. Note that the answer does not depend on the function or on the size of its domain.

Exercise 5: A million bins

A million balls go into a million bins uniformly at random. Use the union bound to show that the fullest bin has at most 11 balls with probability above 0.990.99.

Answer

Pr⁡[max load≥12]≤106/12!≈0.0021\Pr[\text{max load} \ge 12] \le 10^6/12! \approx 0.0021.

Solution

A fixed bin gets at least kk balls with probability at most (nk)n−k≤1/k!\binom{n}{k}n^{-k} \le 1/k!, and there are nn bins, so Pr⁡[max≥k]≤n/k!\Pr[\text{max} \ge k] \le n/k! (The Union Bound at Work). With 12!=479 001 60012! = 479\,001\,600 the bound is 0.00210.0021. (For k=11k = 11 it would be 0.0250.025, still below 0.030.03.) Simulations, like the C++ run in Balls into Bins: The Maximum Load, show maxima of 8 or 9: the union bound is loose here, but it needs no computation.

Exercise 6: Occurrences in 7-SAT

In a 7-CNF formula (exactly 7 distinct variables per clause), every variable occurs in at most TT clauses. Using d≤k(T−1)d \le k(T - 1) and the symmetric LLL, what is the largest TT that guarantees satisfiability? How does it compare with the simpler sufficient condition T≤2k/(ek)T \le 2^k/(ek)?

Answer

T=7T = 7. The simpler condition gives only T≤128/(7e)≈6.7T \le 128/(7e) \approx 6.7, so T=6T = 6.

Solution

We need e(d+1)≤27=128e(d + 1) \le 2^7 = 128 with d=7(T−1)d = 7(T - 1): T=7T = 7 gives d=42d = 42 and e⋅43≈116.9≤128e \cdot 43 \approx 116.9 \le 128; T=8T = 8 gives d=49d = 49 and e⋅50≈135.9>128e \cdot 50 \approx 135.9 > 128. The corollary in The Lovász Local Lemma rounds d+1≤kTd + 1 \le kT up, which costs one occurrence here. The Moser-Tardos algorithm then finds a satisfying assignment with at most m/dm/d expected resamplings.

Exercise 7: Repeating Karger

For a graph on n=50n = 50 vertices, how many independent runs of the contraction algorithm guarantee that a fixed minimum cut is found with probability at least 0.9990.999?

Answer

T=⌈1225ln⁡1000⌉=8463T = \lceil 1225 \ln 1000 \rceil = 8463 runs.

Solution

One run succeeds with probability at least 1/(502)=1/12251/\binom{50}{2} = 1/1225, so TT runs all fail with probability at most (1−1/1225)T≤e−T/1225(1 - 1/1225)^T \le e^{-T/1225}. Setting e−T/1225≤10−3e^{-T/1225} \le 10^{-3} gives T≥1225ln⁡1000≈8462.0002T \ge 1225 \ln 1000 \approx 8462.0002, so T=8463T = 8463 (Karger's Contraction Algorithm). Using (1−1/1225)T(1 - 1/1225)^T directly instead of e−T/1225e^{-T/1225}, T=8459T = 8459 already suffices.

Exercise 8: The Karger-Stein recurrence

Iterate pk+1=pk−pk2/4p_{k+1} = p_k - p_k^2/4 from p0=1p_0 = 1 and compare with the bound 1/(k+1)1/(k+1). For n=106n = 10^6 vertices, about how many levels does the recursion have, and what success probability per trial does the bound give?

Answer

pkp_k stays above 1/(k+1)1/(k+1) (it behaves like 4/k4/k for large kk). The recursion has about 2lg⁡106≈402\lg 10^6 \approx 40 levels, so a trial succeeds with probability at least about 1/411/41; the recurrence itself gives p40≈0.086p_{40} \approx 0.086.

Solution

The induction in Karger-Stein: Recursive Contraction proves pk≥1/(k+1)p_k \ge 1/(k+1). A sharper analysis of qk=4/pk−1q_k = 4/p_k - 1 shows qk+1=qk+1+1/qkq_{k+1} = q_k + 1 + 1/q_k, so qk=k+O(log⁡k)q_k = k + O(\log k) and pk≈4/kp_k \approx 4/k for large kk, approached slowly.

python · try it yourself
from math import log2

p = 1.0
for k in range(1, 61):
    p = p - p * p / 4
    if k in (1, 2, 5, 10, 20, 40, 60):
        print(f"k = {k:2}: p_k = {p:.4f}, 1/(k+1) = {1 / (k + 1):.4f}, 4/k = {4 / k:.4f}")
print(f"levels for n = 10^6: about 2 lg n = {2 * log2(1e6):.1f}")
k =  1: p_k = 0.7500, 1/(k+1) = 0.5000, 4/k = 4.0000
k =  2: p_k = 0.6094, 1/(k+1) = 0.3333, 4/k = 2.0000
k =  5: p_k = 0.3992, 1/(k+1) = 0.1667, 4/k = 0.8000
k = 10: p_k = 0.2585, 1/(k+1) = 0.0909, 4/k = 0.4000
k = 20: p_k = 0.1538, 1/(k+1) = 0.0476, 4/k = 0.2000
k = 40: p_k = 0.0858, 1/(k+1) = 0.0244, 4/k = 0.1000
k = 60: p_k = 0.0597, 1/(k+1) = 0.0164, 4/k = 0.0667
levels for n = 10^6: about 2 lg n = 39.9

Exercise 9: Two wrong entries

AB−CAB - C has exactly two nonzero entries, both equal to 1. What is the probability that one round of Freivalds' test with r∈{0,1}nr \in \{0, 1\}^n misses the error, (a) if the two entries are in the same row, columns j≠j′j \ne j', and (b) if they are in different rows and different columns?

Answer

(a) 1/41/4: the error survives only if rj=rj′=0r_j = r_{j'} = 0. (b) 1/41/4 as well, for the same reason. If instead the two entries were 11 and −1-1 in the same row, the probability would be 1/21/2 (it suffices that rj=rj′r_j = r_{j'}).

Solution

(a) The only nonzero coordinate of DrDr is rj+rj′r_j + r_{j'}, which is 0 only if both are 0. (b) DrDr has two nonzero coordinates, rjr_j and rj′r_{j'}, and both must vanish. In the ±1\pm 1 case, rj−rj′=0r_j - r_{j'} = 0 has probability 1/21/2, which matches the worst case of the theorem in Freivalds' Matrix-Product Verification.

Exercise 10: How big a field?

You test whether the determinant of a 100×100100 \times 100 matrix of linear forms in many variables is identically zero, by evaluating at a random point over Zp\mathbb{Z}_p. How large must pp be for error at most 10−910^{-9} in one evaluation? What is the error for p=261−1p = 2^{61} - 1?

Answer

The determinant has degree at most 100, so p≥1011p \ge 10^{11} suffices. For p=261−1p = 2^{61} - 1 the error is at most 100/p≈4.3⋅10−17100/p \approx 4.3 \cdot 10^{-17}.

Solution

Schwartz-Zippel gives error at most d/∣S∣d/\lvert S \rvert with d=100d = 100 (Polynomial Identity Testing and the Schwartz-Zippel Lemma). Each entry is linear, so each term of the determinant is a product of 100 linear forms, of degree 100.

Exercise 11: A universal family that is not 2-wise independent

Show that the family ha(x)=ax mod ph_a(x) = ax \bmod p for a∈{1,…,p−1}a \in \{1, \ldots, p-1\}, on keys x∈{1,…,p−1}x \in \{1, \ldots, p - 1\}, satisfies Pr⁡[ha(x)=ha(y)]=0\Pr[h_a(x) = h_a(y)] = 0 for x≠yx \ne y, yet is not 2-wise independent. Then check the 2-wise independence of ha,b(x)=(ax+b) mod ph_{a,b}(x) = (ax + b) \bmod p with a,b∈Zpa, b \in \mathbb{Z}_p by brute force for p=7p = 7.

Answer

ax≡ayax \equiv ay implies x≡yx \equiv y since aa is invertible, so there are no collisions; but then the pair (h(x),h(y))(h(x), h(y)) never takes values (u,u)(u, u), so it is not uniform on Zp2\mathbb{Z}_p^2. The family (ax+b) mod p(ax + b) \bmod p with a,b∈Zpa, b \in \mathbb{Z}_p hits every pair exactly once.

Solution

2-wise independence requires Pr⁡[h(x)=u,h(y)=v]=p−2\Pr[h(x) = u, h(y) = v] = p^{-2} for all u,vu, v, including u=vu = v, which has probability 0 here. Collision-freeness is "better than universal" but the joint distribution is wrong, which breaks variance calculations that need exact pairwise behaviour (Universal and k-Wise Independent Hash Families).

python · try it yourself
from collections import Counter
from itertools import combinations

p = 7
mult = Counter()
lin = Counter()
for x, y in combinations(range(1, p), 2):
    mult.update((a * x % p, a * y % p) for a in range(1, p))
lin_ok = all(len(Counter(((a * x + b) % p, (a * y + b) % p) for a in range(p) for b in range(p))) == p * p
             for x, y in combinations(range(p), 2))
print("ax mod p: pairs (u, u) ever produced:", any(u == v for u, v in mult))
print("(ax + b) mod p: every key pair hits all", p * p, "value pairs exactly once:", lin_ok)
ax mod p: pairs (u, u) ever produced: False
(ax + b) mod p: every key pair hits all 49 value pairs exactly once: True

Exercise 12: A confident 2-SAT walk

A 2-CNF formula has 100 variables. How many steps of Papadimitriou's walk make the probability of wrongly answering "unsatisfiable" at most 10−610^{-6}?

Answer

20 segments of 2n2=20 0002n^2 = 20\,000 steps: 400 000400\,000 steps.

Solution

From any assignment, the expected time to a satisfying assignment is at most n2=104n^2 = 10^4, so a segment of 2n22n^2 steps fails with probability at most 1/21/2 (Markov), regardless of where it starts. Twenty segments fail with probability at most 2−20<10−62^{-20} < 10^{-6} (Randomized 2-SAT).

Exercise 13: Mixing on the 100-cube

Use the coupling bound to give a number of steps after which the lazy random walk on {0,1}100\{0, 1\}^{100} is within total-variation distance 0.010.01 of uniform, from any start.

Answer

t=nln⁡n+nln⁡(1/ε)=100(ln⁡100+ln⁡100)≈921t = n \ln n + n \ln(1/\varepsilon) = 100(\ln 100 + \ln 100) \approx 921 steps.

Solution

The coupling that uses the same coordinate and the same bit in both copies gives d(t)≤ne−t/nd(t) \le n e^{-t/n} (Coupling). Setting 100 e−t/100=0.01100\,e^{-t/100} = 0.01 gives t=100ln⁡104≈921t = 100 \ln 10^4 \approx 921. The exact mixing time is about half of nln⁡nn \ln n plus O(n)O(n), so the true answer is smaller.

Exercise 14: MAX-SAT with short clauses

A CNF formula has m1m_1 clauses with one literal and m2m_2 clauses with two distinct literals. What does the method of conditional expectations guarantee? Run Johnson's algorithm on a small example and check the guarantee.

Answer

At least 12m1+34m2\frac12 m_1 + \frac34 m_2 satisfied clauses.

Solution

A random assignment satisfies a 1-clause with probability 12\frac12 and a 2-clause with probability 34\frac34; derandomization keeps the expectation (The Method of Conditional Expectations). If the formula contains both xx and ¬x\lnot x as unit clauses, no assignment satisfies more than one of them, which is why the guarantee for unit clauses cannot beat 12\frac12.

python · try it yourself
import random

def expected_sat(clauses, assign):
    total = 0.0
    for c in clauses:
        if any(assign.get(v) == w for v, w in c):
            total += 1
        else:
            total += 1 - 2.0 ** -sum(1 for v, _ in c if v not in assign)
    return total

rng = random.Random(190)
n = 12
clauses = [[(rng.randrange(n), rng.random() < 0.5)] for _ in range(10)]
clauses += [[(v, rng.random() < 0.5) for v in rng.sample(range(n), 2)] for _ in range(30)]
assign = {}
for v in range(n):
    options = []
    for val in (True, False):
        assign[v] = val
        options.append((expected_sat(clauses, assign), val))
    assign[v] = max(options)[1]
sat = sum(any(assign[v] == w for v, w in c) for c in clauses)
print(f"guarantee 10/2 + 30 * 3/4 = {10 / 2 + 30 * 0.75}; satisfied by the derandomized assignment: {sat}")
guarantee 10/2 + 30 * 3/4 = 27.5; satisfied by the derandomized assignment: 37

Test yourself

A Las Vegas algorithm with expected time f(n)f(n) is run in segments of 2f(n)2f(n) steps, restarting with fresh random bits after each segment. How many segments make the failure probability at most 1/1001/100?

  1. 2
  2. 7
  3. 50
  4. 100

For 1000 tosses of a coin with Pr⁡[heads]=0.1\Pr[\text{heads}] = 0.1, why is Hoeffding's bound on Pr⁡[X≥150]\Pr[X \ge 150] much weaker than Chernoff's?

  1. Hoeffding needs fair coins
  2. Hoeffding's exponent 2t2/n2t^2/n ignores that the mean μ=100\mu = 100 is small compared with nn
  3. Chernoff uses the exact binomial
  4. They give the same bound

Why is the family ha(x)=ax mod ph_a(x) = ax \bmod p with a≠0a \ne 0 not 2-wise independent, although it never produces collisions?

  1. It is not universal
  2. The pair (h(x),h(y))(h(x), h(y)) never takes values (u,u)(u, u), so it is not uniform on Zp2\mathbb{Z}_p^2
  3. It needs pp to be composite
  4. Its values are not uniform on single keys