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 . (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 ? (b) Do better by restarting: run it for steps, and if it has not finished, restart with fresh random bits. How many steps in total suffice for failure probability at most ?
Answer
(a) steps. (b) 7 segments of steps, so steps.
Solution
(a) , so . (b) Each segment is a fresh run, independent of the earlier ones, and fails to finish within steps with probability at most by Markov. All segments fail with probability at most , and . 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 on every input. How many independent runs does Hoeffding's inequality require for a majority vote to err with probability at most ? Compute the exact error of the majority for that number of runs.
Answer
, so 111 runs (an odd number). The exact error of 111 runs is about , and in fact 79 runs already reach .
Solution
With advantage , the majority is wrong with probability at most (Hoeffding's Inequality). Solving gives .
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
counts heads in 1000 independent tosses of a coin with . Bound with Chernoff's and with , and compare with Hoeffding and the exact value.
Answer
, : and . Hoeffding gives only . The exact value is about .
Solution
, so both Chernoff forms apply (Chernoff Bounds and Their Proofs). Hoeffding ignores that the mean is small compared with .
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 by averaging independent samples. How many samples make the estimate accurate to with probability at least ?
Answer
, so 9502 samples.
Solution
Hoeffding gives . Setting this to gives , 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 .
Answer
.
Solution
A fixed bin gets at least balls with probability at most , and there are bins, so (The Union Bound at Work). With the bound is . (For it would be , still below .) 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 clauses. Using and the symmetric LLL, what is the largest that guarantees satisfiability? How does it compare with the simpler sufficient condition ?
Answer
. The simpler condition gives only , so .
Solution
We need with : gives and ; gives and . The corollary in The Lovász Local Lemma rounds up, which costs one occurrence here. The Moser-Tardos algorithm then finds a satisfying assignment with at most expected resamplings.
Exercise 7: Repeating Karger
For a graph on vertices, how many independent runs of the contraction algorithm guarantee that a fixed minimum cut is found with probability at least ?
Answer
runs.
Solution
One run succeeds with probability at least , so runs all fail with probability at most . Setting gives , so (Karger's Contraction Algorithm). Using directly instead of , already suffices.
Exercise 8: The Karger-Stein recurrence
Iterate from and compare with the bound . For vertices, about how many levels does the recursion have, and what success probability per trial does the bound give?
Answer
stays above (it behaves like for large ). The recursion has about levels, so a trial succeeds with probability at least about ; the recurrence itself gives .
Solution
The induction in Karger-Stein: Recursive Contraction proves . A sharper analysis of shows , so and for large , approached slowly.
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
has exactly two nonzero entries, both equal to 1. What is the probability that one round of Freivalds' test with misses the error, (a) if the two entries are in the same row, columns , and (b) if they are in different rows and different columns?
Answer
(a) : the error survives only if . (b) as well, for the same reason. If instead the two entries were and in the same row, the probability would be (it suffices that ).
Solution
(a) The only nonzero coordinate of is , which is 0 only if both are 0. (b) has two nonzero coordinates, and , and both must vanish. In the case, has probability , 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 matrix of linear forms in many variables is identically zero, by evaluating at a random point over . How large must be for error at most in one evaluation? What is the error for ?
Answer
The determinant has degree at most 100, so suffices. For the error is at most .
Solution
Schwartz-Zippel gives error at most with (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 for , on keys , satisfies for , yet is not 2-wise independent. Then check the 2-wise independence of with by brute force for .
Answer
implies since is invertible, so there are no collisions; but then the pair never takes values , so it is not uniform on . The family with hits every pair exactly once.
Solution
2-wise independence requires for all , including , 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).
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 ?
Answer
20 segments of steps: steps.
Solution
From any assignment, the expected time to a satisfying assignment is at most , so a segment of steps fails with probability at most (Markov), regardless of where it starts. Twenty segments fail with probability at most (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 is within total-variation distance of uniform, from any start.
Answer
steps.
Solution
The coupling that uses the same coordinate and the same bit in both copies gives (Coupling). Setting gives . The exact mixing time is about half of plus , so the true answer is smaller.
Exercise 14: MAX-SAT with short clauses
A CNF formula has clauses with one literal and 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 satisfied clauses.
Solution
A random assignment satisfies a 1-clause with probability and a 2-clause with probability ; derandomization keeps the expectation (The Method of Conditional Expectations). If the formula contains both and as unit clauses, no assignment satisfies more than one of them, which is why the guarantee for unit clauses cannot beat .
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 is run in segments of steps, restarting with fresh random bits after each segment. How many segments make the failure probability at most ?
- 2
- 7
- 50
- 100
For 1000 tosses of a coin with , why is Hoeffding's bound on much weaker than Chernoff's?
- Hoeffding needs fair coins
- Hoeffding's exponent ignores that the mean is small compared with
- Chernoff uses the exact binomial
- They give the same bound
Why is the family with not 2-wise independent, although it never produces collisions?
- It is not universal
- The pair never takes values , so it is not uniform on
- It needs to be composite
- Its values are not uniform on single keys