Randomized Algorithms: What This Course Covers
Part I of the track used randomness in a few places: randomized quicksort and quickselect, universal hashing, Rabin-Karp and Miller-Rabin. This course, the second of Part II, treats randomness as an algorithmic resource in its own right. It answers three questions. How do we prove that a randomized algorithm is fast or correct with high probability, not just on average? Which problems become simpler, faster, or possible at all with random bits? And when can the randomness be removed again?
The topic order follows the two standard graduate texts, Randomized Algorithms by Motwani and Raghavan (1995) and Probability and Computing by Mitzenmacher and Upfal (2005; second edition 2017). Every explanation, example, program and exercise here is written from scratch. Good companions for single sections are Markov Chains and Mixing Times by Levin, Peres and Wilmer for the Markov-chain lessons, The Probabilistic Method by Alon and Spencer for the local lemma and derandomization, and Concentration of Measure for the Analysis of Randomized Algorithms by Dubhashi and Panconesi for tail bounds.
A taste in one program
Three of the course's algorithms, each in a few lines: checking a matrix product without recomputing it, finding a minimum cut by contracting random edges, and balancing load with two random choices.
import random
rng = random.Random(1)
# 1. Freivalds: is A B = C? Three matrix-vector products instead of a matrix product.
n = 30
A = [[rng.randrange(10) for _ in range(n)] for _ in range(n)]
B = [[rng.randrange(10) for _ in range(n)] for _ in range(n)]
C = [[sum(A[i][k] * B[k][j] for k in range(n)) for j in range(n)] for i in range(n)]
C[17][4] += 1 # one wrong entry
mv = lambda M, v: [sum(a * b for a, b in zip(row, v)) for row in M]
caught = 0
for _ in range(20):
r = [rng.randrange(2) for _ in range(n)]
caught += mv(A, mv(B, r)) != mv(C, r)
print(f"Freivalds: wrong product caught in {caught} of 20 rounds (each round catches it w.p. 1/2)")
# 2. Karger: contract random edges of two 5-cliques joined by one edge until 2 vertices remain.
edges = [(u + s, v + s) for s in (0, 5) for u in range(5) for v in range(u + 1, 5)] + [(0, 5)]
def contract(edges):
parent = list(range(10))
def find(x):
while parent[x] != x:
x = parent[x]
return x
order, groups = edges[:], 10
rng.shuffle(order)
for u, v in order:
if groups > 2 and find(u) != find(v):
parent[find(u)] = find(v)
groups -= 1
return sum(find(u) != find(v) for u, v in edges)
results = [contract(edges) for _ in range(200)]
print(f"Karger: minimum cut 1 found in {results.count(1)} of 200 runs (guaranteed rate >= 2/(n(n-1)) = 0.022)")
# 3. Balls into bins: one random bin, or the less loaded of two?
bins = 10000
one, two = [0] * bins, [0] * bins
for _ in range(bins):
a, b = rng.randrange(bins), rng.randrange(bins)
one[a] += 1
two[a if two[a] <= two[b] else b] += 1
print(f"{bins} balls in {bins} bins: fullest bin {max(one)} with one choice, {max(two)} with two")Freivalds: wrong product caught in 11 of 20 rounds (each round catches it w.p. 1/2)
Karger: minimum cut 1 found in 91 of 200 runs (guaranteed rate >= 2/(n(n-1)) = 0.022)
10000 balls in 10000 bins: fullest bin 7 with one choice, 3 with two
What the course covers
| Section | Topics | Main results |
|---|---|---|
| Foundations | Las Vegas and Monte Carlo algorithms, amplification, expectation tools | ZPP = RP co-RP; majority votes and the median trick decay exponentially |
| Tail bounds | Markov, Chebyshev, Chernoff, Hoeffding, the union bound, the Lovász Local Lemma, Moser-Tardos | ; set balancing within ; suffices; expected resamplings |
| Graph and algebraic algorithms | Karger, Karger-Stein, Freivalds, Schwartz-Zippel, Tutte matrices | min cut w.p. per run, overall; PIT error |
| Hashing and load balancing | -wise independence, FKS perfect hashing, maximum load, two choices | space and worst-case lookups; max load vs |
| Markov chains and random walks | 2-SAT and 3-SAT walks, mixing time, coupling, conductance, MCMC | expected steps for 2-SAT; for 3-SAT; on the hypercube; Cheeger |
| Derandomization | conditional expectations, pairwise independence, small sample spaces | deterministic cuts and for MAX-3SAT |
The mini project builds a minimum-cut finder with Karger and Karger-Stein and measures their success rates over seeded trials. The Exercises collect problems from every section.
Conventions
- "With high probability" (w.h.p.) means with probability at least for some constant , usually , where is the input size. Every bound in the course says whether it holds in expectation or w.h.p., and which independence it needs.
- Pseudocode is 0-based and in the track's style (How to read pseudocode); and is the natural logarithm.
- Every random choice in a program comes from a generator with a fixed seed (
random.Random(seed)in Python,std::mt19937in C++), so the printed numbers are reproducible and match the text exactly. The C++ examples use a different generator from Python's, so their measured rates differ slightly from the Python ones; the text says so each time. Indices are drawn asrng() % n, which has a negligible bias and gives identical results on every standard library. - Costs are counted (steps, probes, multiplications, resamplings), never timed.
- Theorems that are only stated or sketched are marked as such, with a reference for the full proof.
What you should know first
The course assumes Probability, especially expectation, independence, variance, Markov's and Chebyshev's Inequalities, Moment Generating Functions and Markov Chains. From Part I it uses Indicator Random Variables, Randomized Quicksort, Universal Hashing, union-find, Miller-Rabin and the NP-completeness of 3-CNF-SAT. Later Part II courses build on this one, above all Streaming, Sketching & Sublinear Algorithms and the randomized-rounding parts of Approximation Algorithms.
Test yourself
In this course, what does "with high probability" mean?
- With probability at least
- With probability at least for a constant
- With probability 1
- In expectation
Why do the C++ examples print slightly different measured rates than the Python ones?
- C++ is less accurate
- They use a different pseudo-random generator, so the random choices differ, although both are seeded and reproducible
- Python rounds differently
- Compiler Explorer adds noise
Which of these is a Monte Carlo algorithm with one-sided error?
- Randomized quicksort
- Freivalds' matrix-product check
- FKS perfect hashing
- Randomized quickselect