Main

Lecture 5: Markov Chains, Random Walks & MC Sampling

Memoryless random motion settling into a unique stationary distribution, made into a sampler (MCMC, Metropolis–Hastings); mixing via coupling, stopping times and expanders; sampling becomes counting.

Last updated 27 June 2026

Lecture 5 — Markov Chains & Random Walks → Monte Carlo Sampling

Course 2-INF-135/15 Pravdepodobnostné algoritmy, LS 2025/26. Source slides: 05_MC.pdf (46 pages). Štátnicové syllabus topics covered here: Markov chains, transition matrix and stationary distribution; connection to eigenvalues; mixing (variation distance), strong uniform stopping times; sampling via random walks (random graphs with prescribed degrees, random knapsack solutions); reversible chains and the Metropolis–Hastings algorithm (how to build a chain with a given stationary distribution).


The one-paragraph map of the whole lecture

A Markov chain is random motion with no memory: where you go next depends only on where you are now, not on how you got there. The deep fact is that a “nice” chain forgets its starting point — run it long enough and the probability of being in state ii converges to a single number πi\pi_i that does not depend on where you began. That number is the stationary distribution.

This turns into a powerful algorithmic machine. Suppose you want to sample an object from some weird, exponentially large space SS according to a prescribed distribution σ\sigma (a uniform independent set, a random knapsack solution, a random shuffle of a deck). You cannot list SS. But if you can design a chain that walks around SS and whose stationary distribution is exactly σ\sigma, then you just run the walk and read off where you land. Two questions then dominate the entire lecture:

  1. How do I BUILD a chain with a prescribed stationary distribution σ\sigma? → the idea of reversibility / detailed balance, culminating in the Metropolis–Hastings algorithm.
  2. How LONG must I run it?mixing time, measured in variation distance, and bounded three ways: coupling, strong uniform stopping times, and eigenvalues (expanders mix fast).

The closing payoff: sampling lets you count (estimate the size of huge sets — FPRAS, #DNF), and as a bonus, expander walks recycle random bits to amplify BPP using O(k)O(k) bits instead of O(kn)O(kn).


1. What a Markov chain is

A stochastic process is just a family of random variables indexed by time, X={XttT}X = \{X_t \mid t \in T\}, where XtX_t is “the state at time tt.” We assume discrete time (t=0,1,2,t = 0, 1, 2, \dots) and a discrete state space {0,1,2,}\{0, 1, 2, \dots\} (finite or countable).

The process is a Markov chain if it has the memorylessness (Markov) property:

Pr[Xt=atXt1=at1,Xt2=at2,,X0=a0]  =  Pr[Xt=atXt1=at1].\Pr[X_t = a_t \mid X_{t-1}=a_{t-1}, X_{t-2}=a_{t-2}, \dots, X_0=a_0] \;=\; \Pr[X_t = a_t \mid X_{t-1}=a_{t-1}].

Intuition. The present screens off the past from the future. The only thing the chain “knows” is its current state; the entire history before that is irrelevant to what happens next. Equivalently, for times u<t<vu < t < v, the past (XuX_u) and the future (XvX_v) are conditionally independent given the present (XtX_t): Pr[Xu=i,Xv=kXt=j]=Pr[Xu=iXt=j]Pr[Xv=kXt=j].\Pr[X_u=i, X_v=k \mid X_t=j] = \Pr[X_u=i\mid X_t=j]\cdot\Pr[X_v=k\mid X_t=j].

The transition matrix

All the dynamics live in a single matrix. Let

pi,j=Pr[Xt=jXt1=i]p_{i,j} = \Pr[X_t = j \mid X_{t-1}=i]

be the probability of stepping from ii to jj in one move (assumed time-homogeneous — the same at every tt). Collect them into the transition matrix P=(pi,j)P = (p_{i,j}). Each row sums to 1 (from ii you must go somewhere):

j0pi,j=1for every i.\sum_{j \ge 0} p_{i,j} = 1 \quad \text{for every } i.

A matrix with non-negative entries and rows summing to 1 is called stochastic — and every such matrix is the transition matrix of some chain.

How distributions evolve — it’s just matrix multiplication

Let pi(t)=Pr[system is in state i at time t]p_i(t) = \Pr[\text{system is in state } i \text{ at time } t] and collect these into a row vector pˉ(t)=(p0(t),p1(t),)\bar p(t) = (p_0(t), p_1(t), \dots), the distribution over states at time tt. To be in state ii at time tt, you were in some state jj at time t1t-1 and stepped jij \to i:

pi(t)=j0pj(t1)pj,i  pˉ(t)=pˉ(t1)P  p_i(t) = \sum_{j\ge 0} p_j(t-1)\, p_{j,i} \qquad\Longleftrightarrow\qquad \boxed{\;\bar p(t) = \bar p(t-1)\, P\;}

Iterating, the mm-step transition matrix is just the matrix power P(m)=PmP^{(m)} = P^m, and

pˉ(t+m)=pˉ(t)Pm.\bar p(t+m) = \bar p(t)\, P^m.

Punchline. A Markov chain is a stochastic matrix. Running the chain = repeatedly multiplying a distribution vector by PP. Everything that follows — convergence, stationary distributions, mixing rates — is secretly a statement about the powers PmP^m and the eigenvalues of PP. Hold that thought; it returns in §11 (expanders).


2. First examples — random walks that solve problems (2SAT, 3SAT)

Before the theory, two examples show why we care: a random walk on assignments solves satisfiability, and the chain analysis gives the running time.

2SAT — the symmetric walk, O(n2)O(n^2)

Algorithm (Papadimitriou). Start from any truth assignment α0\alpha_0. While the formula is unsatisfied, pick an unsatisfied clause and flip a uniformly random one of its two literals. Repeat.

Why does this make progress? Fix a satisfying assignment α\alpha^* (assume one exists). An unsatisfied 2-clause has both its literals wrong under the current assignment, but α\alpha^* satisfies the clause, so α\alpha^* disagrees with us on at least one of those two variables. Flipping a random one of the two therefore moves us closer to α\alpha^* with probability 12\ge \tfrac12.

Track jj = the number of variables on which we currently agree with α\alpha^* (Hamming distance is njn - j). This is (a lower bound on) a chain on {0,1,,n}\{0,1,\dots,n\}:

Here h_j = $ expected number of steps to reach $\alpha^* starting from agreement jj. Solving the recurrence (it telescopes to hj=hj+1+(2j+1)h_j = h_{j+1} + (2j+1)) gives

h0=n2\boxed{\,h_0 = n^2\,}

so the symmetric walk finds a satisfying assignment in expected O(n2)O(n^2) steps. (This is the classic gambler’s-ruin / drift argument: a fair walk on a line of length nn takes Θ(n2)\Theta(n^2) time to cross it.)

3SAT — the biased walk, and why restarts win

For 3SAT an unsatisfied 3-clause has three literals; α\alpha^* disagrees on at least one, so a random flip moves us toward α\alpha^* with probability only 13\tfrac13 and away with probability 23\tfrac23. The recurrence becomes biased:

hj=1+23hj1+13hj+1,hj=hj+1+(2j+23).h_j = 1 + \tfrac23 h_{j-1} + \tfrac13 h_{j+1}, \qquad h_j = h_{j+1} + (2^{\,j+2}-3).

A walk biased away from the target takes exponential time: h0=O(2n)h_0 = O(2^n). Running one long walk is hopeless.

The fix (Schöning). Don’t run one long walk — run many short walks from fresh random starts. The key insight on the slide: “if a satisfying α\alpha exists, then there are many” assignments from which a short walk reaches it. A random start lands within a favourable distance often enough that capping each walk at O(n)O(n) steps and restarting gives

h0=O ⁣(n3/2(4/3)n),h_0 = O\!\left(n^{3/2}\,(4/3)^n\right),
dramatically better than 2n2^n. (This is the random-walk k-SAT algorithm previewed in the derandomization lecture — derandomized there by sweeping a covering code instead of guessing the start.)

Professor-pleaser. The same random-flip rule is polynomial for 2SAT and exponential for 3SAT, and the only thing that changed is the drift of the induced walk on Hamming distance: fair coin (12/12\tfrac12/\tfrac12) → O(n2)O(n^2); biased coin (13/23\tfrac13/\tfrac23) → 2n2^n. The behaviour of the algorithm is entirely the behaviour of the underlying Markov chain. This is the whole reason to study chains.


3. Classifying states — which chains “settle down”?

For a chain to forget its start and converge, it must be structurally well-behaved. The vocabulary (this is the part to be able to recite in the oral):

Among recurrent states there is a subtle but exam-relevant split. The mean return time is

hi,i=t1tri,i(t).h_{i,i} = \sum_{t \ge 1} t \cdot r^{(t)}_{i,i}.

The null-recurrent example (worth knowing — it’s a classic trap). Consider a chain on {1,2,3,}\{1,2,3,\dots\} where from ii you go up to i+1i+1 with probability ii+1\frac{i}{i+1} and home to 11 with probability 1i+1\frac{1}{i+1}. The probability of first returning to 11 at exactly time tt is

r1,1(t)=1223t1tclimb 1t1t+1t1=1t1t+1=1t(t+1).r^{(t)}_{1,1} = \underbrace{\tfrac12\cdot\tfrac23\cdots\tfrac{t-1}{t}}_{\text{climb }1\to t}\cdot \underbrace{\tfrac{1}{t+1}}_{t\to 1} = \frac1t\cdot\frac1{t+1} = \frac{1}{t(t+1)}.
Then f1,1=t1t(t+1)=1f_{1,1} = \sum_t \frac1{t(t+1)} = 1 (telescopes), so state 1 is recurrent — but
h1,1=tt1t(t+1)=t1t+1=.h_{1,1} = \sum_t t\cdot\frac{1}{t(t+1)} = \sum_t \frac{1}{t+1} = \infty.
Null recurrent. You always come home, but the average homecoming time is infinite. This cannot happen in a finite chain (next lemma).

Two equivalent tests for recurrence

A clean characterisation via the mm-step return probabilities:

i is recurrent    t=1pi,i(t)=,i is transient    t=1pi,i(t)<.i \text{ is recurrent} \iff \sum_{t=1}^{\infty} p^{(t)}_{i,i} = \infty, \qquad i \text{ is transient} \iff \sum_{t=1}^{\infty} p^{(t)}_{i,i} < \infty.
Why: if ii is transient with return probability q<1q<1, the number of returns is geometric, and tpi,i(t)=nqn=q1q<\sum_t p^{(t)}_{i,i} = \sum_n q^n = \frac{q}{1-q} < \infty. If recurrent, each return is certain so the expected number of returns — which equals this sum — is infinite.

Periodicity and ergodicity

Lemma (finite chains are well-behaved). In a finite Markov chain:

  1. there is at least one recurrent state;
  2. all recurrent states are positive recurrent (no null recurrence in finite chains).

Stationary distribution

A distribution πˉ\bar\pi is stationary if it is a fixed point of the dynamics:

  πˉ=πˉP  \boxed{\;\bar\pi = \bar\pi P\;}
i.e. if you start in πˉ\bar\pi, after one step you are still in πˉ\bar\pi (and hence forever). It is a left eigenvector of PP with eigenvalue 1.


4. The Fundamental Theorem of Markov Chains

This is the theorem of the lecture — the precise statement that “nice chains forget their start.”

Theorem (Fundamental / hlavná veta). In an irreducible, finite, aperiodic Markov chain:

  1. every state is ergodic;
  2. there exists a unique stationary distribution π\pi;
  3. fi,i=1f_{i,i} = 1 and the mean return time is hi,i=1πih_{i,i} = \dfrac{1}{\pi_i};
  4. the long-run fraction of time spent in ii converges to πi\pi_i: limtN(i,t)t=πi\displaystyle \lim_{t\to\infty}\frac{N(i,t)}{t} = \pi_i, where N(i,t)N(i,t) counts visits to ii in tt steps.

Equivalently (the “convergence” form): 3′. limtPj,it\displaystyle \lim_{t\to\infty} P^t_{j,i} exists and is independent of the starting state jj; 4′. πi=limtPj,it=1hi,i.\displaystyle \pi_i = \lim_{t\to\infty} P^t_{j,i} = \frac{1}{h_{i,i}}.

Read parts 3′–4′ slowly — they are the magic:

Intuition. Pj,itP^t_{j,i} is the probability of being at ii after tt steps given you started at jj. The theorem says this stops depending on jj. The chain erases the memory of its origin. Where you end up is governed only by the chain’s structure, summarised in π\pi. And πi=1/hi,i\pi_i = 1/h_{i,i} has a lovely reading: a state you return to quickly (small mean return time) is a state you spend a lot of time in (large stationary probability). Frequently visited ⇔ short return time.

The three hypotheses each pull their weight:


5. Computing π\pi — cuts and reversibility (the load-bearing trick)

How do you actually find π\pi? Solve πP=π\pi P = \pi with iπi=1\sum_i \pi_i = 1. Written out component-wise, the equation πi=jπjPj,i\pi_i = \sum_j \pi_j P_{j,i} is a balance / cut condition:

jπjPj,i  =  πi  =  πijPi,j.\sum_j \pi_j P_{j,i} \;=\; \pi_i \;=\; \pi_i \sum_j P_{i,j}.

The left side is flow into ii; the right side is flow out of ii. Stationarity = global balance: total probability flowing into each state equals total flowing out. Solving the full linear system is doable but painful. There is a much stronger, much easier-to-check sufficient condition.

Theorem (detailed balance ⇒ stationary). Let the chain be finite, irreducible, ergodic with matrix PP, and let π=(π1,,πn)\pi = (\pi_1,\dots,\pi_n) be non-negative with

  1. iπi=1\sum_i \pi_i = 1, and
  2.   πiPi,j=πjPj,i  \;\pi_i P_{i,j} = \pi_j P_{j,i}\; for every pair i,ji,j (the detailed balance / reversibility condition).

Then π\pi is the (unique) stationary distribution, and the chain is time-reversible.

Proof — one line. Sum detailed balance over ii:

iπiPi,j=iπjPj,i=πjiPj,i=πj,\sum_i \pi_i P_{i,j} = \sum_i \pi_j P_{j,i} = \pi_j \sum_i P_{j,i} = \pi_j,
which is exactly the stationarity equation (πP)j=πj(\pi P)_j = \pi_j. ∎

Why this is the whole game. Global balance (the true stationarity equation) couples all states together — a big linear system. Detailed balance is a local, pairwise condition: just check each edge i ⁣ ⁣ji\!\leftrightarrow\! j in isolation. It is strictly stronger (not every chain is reversible), but when it holds it is trivial to verify, and — crucially — it is the lever we use to engineer a chain with a prescribed π\pi. “Reversible” means the chain looks statistically the same run forwards or backwards in time. Remember this condition; the entire Metropolis construction in §8–§9 is just “rig the transition probabilities so detailed balance holds for the π\pi I want.”


6. Random walks on undirected graphs

The most important concrete chain. Given a connected undirected graph G=(V,E)G=(V,E), the simple random walk steps from vv to a uniformly random neighbour:

Pi,j=1deg(i)if (i,j)E, else 0.P_{i,j} = \frac{1}{\deg(i)} \quad \text{if } (i,j)\in E,\ \text{else } 0.

(Aperiodic     \iff GG is not bipartite — same parity-of-cycles fact as before.)

Theorem. The simple random walk on a connected non-bipartite graph converges to the stationary distribution

  πv=deg(v)2E  \boxed{\;\pi_v = \frac{\deg(v)}{2|E|}\;}
— proportional to degree. High-degree vertices are visited more.

Check. It sums to 1: vdeg(v)2E=2E2E=1\sum_v \frac{\deg(v)}{2|E|} = \frac{2|E|}{2|E|} = 1 (handshake lemma). And it satisfies detailed balance — the cleanest possible verification:

πuPu,v=deg(u)2E1deg(u)=12E=deg(v)2E1deg(v)=πvPv,u.\pi_u P_{u,v} = \frac{\deg(u)}{2|E|}\cdot\frac{1}{\deg(u)} = \frac{1}{2|E|} = \frac{\deg(v)}{2|E|}\cdot\frac{1}{\deg(v)} = \pi_v P_{v,u}.
Both directions of an edge carry the same flow 12E\frac{1}{2|E|}. (This is exactly why §5’s reversibility machinery is so handy.)

By the Fundamental Theorem, the mean return time to uu is

hu,u=1πu=2Edeg(u).h_{u,u} = \frac{1}{\pi_u} = \frac{2|E|}{\deg(u)}.

Hitting times and cover time — why a walk explores fast

Hitting time on an edge. If (u,v)E(u,v)\in E, then the expected time to go from vv to uu is bounded:

hv,u<2E.h_{v,u} < 2|E|.
Proof. Condition the return-to-uu time on the first step out of uu:
2Edeg(u)=hu,u=1deg(u)wN(u)(1+hw,u).\frac{2|E|}{\deg(u)} = h_{u,u} = \frac{1}{\deg(u)}\sum_{w\in N(u)}\bigl(1 + h_{w,u}\bigr).
Multiply by deg(u)\deg(u): 2E=wN(u)(1+hw,u)\,2|E| = \sum_{w\in N(u)}(1 + h_{w,u}). Every term is positive, so each single 1+hw,u<2E1 + h_{w,u} < 2|E|; in particular hv,u<2Eh_{v,u} < 2|E|. ∎

Cover time = expected time to visit all vertices, from the worst start.

Theorem. cover time<4VE.\text{cover time} < 4|V|\,|E|.

Proof idea (elegant). Fix a spanning tree. Walk the tree in a fixed closed tour (e.g. a DFS traversal, around the tree) that crosses each of its V1|V|-1 edges once in each direction — 2(V1)2(|V|-1) edge-traversals. The expected time for each traversal of an edge (vi,vi+1)(v_i, v_{i+1}) is hvi,vi+1<2Eh_{v_i, v_{i+1}} < 2|E|. Summing,

cover timeihvi,vi+1<2(V1)2E<4VE.\text{cover time} \le \sum_{i} h_{v_i,v_{i+1}} < 2(|V|-1)\cdot 2|E| < 4|V|\,|E|. \qquad\blacksquare

Why this matters algorithmically. A random walk explores a connected graph in polynomial time using essentially no memory — you only remember your current vertex. The headline application: undirected sstt connectivity (USTCON) in randomized log-space. To decide whether tt is reachable from ss, just walk from ss for O(VE)=polyO(|V||E|) = \mathrm{poly} steps; if you hit tt, accept. The walk needs only O(logn)O(\log n) bits (the current vertex + a counter). This is the basis of the result USTCONRL\textbf{USTCON} \in \mathbf{RL} — and famously was later derandomized to L\mathbf{L} by Reingold. The cover-time bound is exactly the running-time guarantee.


7. From walks to sampling — the Monte Carlo Markov Chain (MCMC) idea

Here is the conceptual pivot of the lecture.

Problem. Sample an object from a huge space SS according to a prescribed distribution σ\sigma. (SS is exponential — you cannot enumerate it.)

Solution (MCMC). Design an ergodic Markov chain such that

  • its state space is SS (the objects you want to sample), and
  • its stationary distribution is the desired σ\sigma.

Then run the chain. By the Fundamental Theorem it converges to σ\sigma from any starting state, so after enough steps the current state is an (almost) σ\sigma-sample.

To get several near-independent samples, run a long walk x0,x1,x2,x_0, x_1, x_2, \dots and take states spaced rr apart:

x0  r  xr  r  x2r  r  x3r  x_0 \;\xrightarrow{r}\; x_r \;\xrightarrow{r}\; x_{2r} \;\xrightarrow{r}\; x_{3r}\;\cdots
If rr is at least the mixing time, xr,x2r,x3r,x_r, x_{2r}, x_{3r},\dots are nearly independent σ\sigma-samples.

Two quantities control the cost, and they organize the rest of the lecture:

The design freedom and the two sub-problems. We get to invent the chain. That raises exactly the two questions from the map: (1) how do we force the stationary distribution to be the σ\sigma we want (uniform? weighted?) — §8–§9; and (2) how do we bound rr so we know when to stop — §10–§11.

We assume a symmetric neighbourhood structure {N(x)}\{N(x)\} on SS: $y \in N(x) \iff x \in N(y)$ (an undirected “move graph” on the objects), and we want N(x)N(x) small.


8. Building a chain with uniform stationary distribution

First the easy target: \sigma = $ **uniform** on $S. Naïvely walking the move-graph gives πvdeg(v)\pi_v \propto \deg(v) (§6) — not uniform, because different objects have different numbers of neighbours. The fix is to equalize the transition probabilities by padding with self-loops.

Lemma (uniform sampler). Let Ω\Omega be a finite state space with neighbourhood structure {N(x)}\{N(x)\}, N=maxxN(x)N = \max_x |N(x)|. Pick any M>NM > N and define the chain

P(x,y)={1/Mxy, yN(x),0xy, yN(x),1N(x)/Mx=y(self-loop).P(x,y) = \begin{cases} 1/M & x\ne y,\ y\in N(x),\\[2pt] 0 & x\ne y,\ y\notin N(x),\\[2pt] 1 - |N(x)|/M & x = y \quad(\text{self-loop}). \end{cases}
If this chain is irreducible and aperiodic, its stationary distribution is uniform, πx=1/Ω\pi_x = 1/|\Omega|.

Proof. Every off-diagonal transition probability is the same constant 1/M1/M, regardless of xx or yy (the self-loop soaks up the difference in degrees). So P(x,y)=P(y,x)=1/MP(x,y)=P(y,x)=1/M — the matrix is symmetric. Detailed balance with the uniform πx=πy=1/Ω\pi_x = \pi_y = 1/|\Omega| reads πxP(x,y)=πyP(y,x)\pi_x P(x,y) = \pi_y P(y,x), which holds because both π\pi’s and both PP’s are equal. By §5, uniform is stationary. ∎

The idea in one phrase. Self-loops are a thermostat for degree. Every vertex “spends” N(x)/M|N(x)|/M of its probability stepping out and parks the remaining 1N(x)/M1 - |N(x)|/M on itself, so outgoing edges all carry the same weight 1/M1/M. Equal edge weights ⇒ symmetric matrix ⇒ uniform stationary. We choose M>NM > N strictly so every state has a positive self-loop, which also kills periodicity for free.

Example — sampling a uniform Independent Set

State space \Omega = $ all independent sets of $G. Neighbourhood: $N(x) = $ independent sets differing from xx in one vertex. The chain:

Uniform-IS walk.

  1. X0X_0 \leftarrow any independent set.
  2. To compute Xi+1X_{i+1}: pick a uniform random vertex vVv \in V.
    • if vXiv \in X_i: set Xi+1Xi{v}X_{i+1} \leftarrow X_i \setminus \{v\} (remove);
    • if vXiv \notin X_i and Xi{v}X_i \cup \{v\} is independent: $X_{i+1} \leftarrow X_i \cup {v}$ (add);
    • otherwise Xi+1XiX_{i+1} \leftarrow X_i (stay — this is the self-loop).

Picking vv among all V|V| vertices, and staying when the move is illegal, is exactly the “M>NM > N with self-loops” construction (here M=VM = |V|). As long as GG has at least one edge there is always some illegal move from some state, guaranteeing a self-loop, hence aperiodicity. Stationary distribution: uniform over all independent sets.


9. Building a chain with a prescribed non-uniform π\pi — Metropolis

Now the general target: we want each object xx sampled with a prescribed probability πx\pi_x. Often π\pi is given only up to normalization:

πx=b(x)B,b(x)>0,B=xΩb(x),\pi_x = \frac{b(x)}{B}, \qquad b(x) > 0,\quad B = \sum_{x\in\Omega} b(x),
and the normalizer BB (a sum over the entire exponential space!) is unknown and hopeless to compute. The triumph of Metropolis is that we never need BB.

Lemma (Metropolis). Given {N(x)}\{N(x)\}, N=maxxN(x)N=\max_x|N(x)|, M>NM > N, and any desired πx>0\pi_x > 0, define

P(x,y)={1Mmin ⁣{1, πyπx}xy, yN(x),0xy, yN(x),1yxP(x,y)x=y.P(x,y) = \begin{cases} \dfrac1M \cdot \min\!\left\{1,\ \dfrac{\pi_y}{\pi_x}\right\} & x\ne y,\ y\in N(x),\\[8pt] 0 & x\ne y,\ y\notin N(x),\\[4pt] 1 - \displaystyle\sum_{y\ne x} P(x,y) & x = y. \end{cases}
If irreducible and aperiodic, its stationary distribution is exactly π\pi.

Proof — detailed balance again. Take xyx\ne y neighbours with (WLOG) πxπy\pi_x \le \pi_y. Then min{1,πy/πx}=1\min\{1, \pi_y/\pi_x\} = 1, so P(x,y)=1/MP(x,y) = 1/M; and $\min{1,\pi_x/\pi_y} = \pi_x/\pi_y, so $P(y,x) = \frac1M\cdot\frac{\pi_x}{\pi_y}. Check:

πxP(x,y)=πxM,πyP(y,x)=πy1Mπxπy=πxM.\pi_x P(x,y) = \frac{\pi_x}{M}, \qquad \pi_y P(y,x) = \pi_y\cdot\frac1M\cdot\frac{\pi_x}{\pi_y} = \frac{\pi_x}{M}.
Equal — detailed balance holds, so π\pi is stationary. ∎

The two beautiful features.

  1. Only ratios πy/πx\pi_y/\pi_x appear. The unknown normalizer BB cancels: πy/πx=b(y)/b(x)\pi_y/\pi_x = b(y)/b(x). We can sample from π\pi without ever knowing the total weight BB — exactly the situation in every real counting/physics application.
  2. The acceptance rule is a thermostat toward π\pi. Propose a uniform neighbour move (1/M1/M). Always accept a move to a more-probable state (πyπx\pi_y \ge \pi_x); accept a move to a less-probable state only with probability πy/πx\pi_y/\pi_x. Detailed balance is satisfied by construction, so the walk pools where π\pi is large.

Metropolis for weighted Independent Sets

We want Pr[independent set I]λI\Pr[\text{independent set } I] \propto \lambda^{|I|} for a parameter λ>0\lambda>0:

πx=λIxB,so{λ=1:uniform,λ<1:smaller sets favoured,λ>1:larger sets favoured (toward max IS).\pi_x = \frac{\lambda^{|I_x|}}{B}, \quad\text{so}\quad \begin{cases} \lambda = 1: & \text{uniform},\\ \lambda < 1: & \text{smaller sets favoured},\\ \lambda > 1: & \text{larger sets favoured (toward max IS).} \end{cases}

The ratio for adding a vertex is πy/πx=λI+1/λI=λ\pi_y/\pi_x = \lambda^{|I|+1}/\lambda^{|I|} = \lambda; for removing, 1/λ1/\lambda. So the algorithm needs only λ\lambda:

Metropolis-IS.

  1. X0X_0 \leftarrow some independent set.
  2. To compute Xi+1X_{i+1}: pick vVv \in V uniformly (prob 1/M1/M, M=VM=|V|).
    • if vXiv \in X_i: remove it with probability min{1,1/λ}\min\{1, 1/\lambda\};
    • if vXiv \notin X_i and Xi{v}X_i\cup\{v\} independent: add it with probability min{1,λ}\min\{1, \lambda\};
    • otherwise stay.

The detailed-balance check (πxP(x,y)=πyP(y,x)\pi_x P(x,y) = \pi_y P(y,x)) goes through with the λ\lambda ratios, and — the punchline on the slide — BB sme nepotrebovali :-)”: we never needed the normalizer.

Metropolis–Hastings — the general recipe (asymmetric proposals)

What if the proposal mechanism itself is not symmetric? Let QQ be any irreducible proposal chain (q_{ij} = $ probability of proposing $j from ii), and π\pi the desired distribution.

Metropolis–Hastings. From ii, propose jj according to QQ. Accept the move with probability

α=min ⁣{πjqjiπiqij, 1}\alpha = \min\!\left\{\,\frac{\pi_j\, q_{ji}}{\pi_i\, q_{ij}},\ 1\right\}
(when QQ is symmetric, qij=qjiq_{ij}=q_{ji}, this collapses to the plain Metropolis α=min{πj/πi,1}\alpha = \min\{\pi_j/\pi_i,\,1\}). Otherwise stay at ii.

The resulting transition pij=qijαp_{ij} = q_{ij}\,\alpha satisfies detailed balance with π\pi: the correction factor qji/qijq_{ji}/q_{ij} exactly cancels the asymmetry of the proposal, so πipij=πjpji\pi_i p_{ij} = \pi_j p_{ji}.

Professor-pleaser — the unifying statement of §8–§9. Metropolis–Hastings is a universal machine for manufacturing a Markov chain with any prescribed stationary distribution, using only the ratios of the target probabilities. It works by forcing detailed balance (§5): start from any convenient proposal walk, then bend the acceptance probabilities until πipij=πjpji\pi_i p_{ij} = \pi_j p_{ji} holds on every edge. Reversibility is not a curiosity — it is the design principle of all of MCMC.


10. How long? — variation distance, mixing time, and coupling

We can build the chain; now we must know when its distribution is close enough to π\pi. We need (a) a notion of “close,” and (b) tools to bound the time to get there.

Variation distance — the right notion of “close”

For two distributions D1,D2D_1, D_2 on a countable SS, the total variation distance is

D1D2:=12xSD1(x)D2(x).\|D_1 - D_2\| := \frac12 \sum_{x\in S} |D_1(x) - D_2(x)|.

Lemma (the event form). $;\displaystyle |D_1 - D_2| = \max_{A\subseteq S} |D_1(A) - D_2(A)|;$ where Di(A)=xADi(x)D_i(A) = \sum_{x\in A}D_i(x).

Intuition. Variation distance is the largest disagreement on any event: no matter what yes/no question AA you ask, the two distributions assign AA probabilities differing by at most D1D2\|D_1-D_2\|. The maximizing AA is “all xx where D1>D2D_1 > D_2.” The factor 12\tfrac12 makes the range [0,1][0,1]. This is the operational meaning: if variation distance is ε\le \varepsilon, no statistical test can tell the two apart with advantage more than ε\varepsilon.

Mixing time

Let π\pi^* be stationary, and pxtp^t_x the distribution after tt steps starting from xx. Define

Δx(t)=pxtπ,Δ(t)=maxxSΔx(t),\Delta_x(t) = \|p^t_x - \pi^*\|,\qquad \Delta(t) = \max_{x\in S}\Delta_x(t),
τx(ε)=min{t:Δx(t)ε},τ(ε)=maxxτx(ε)(mixing time).\tau_x(\varepsilon) = \min\{t : \Delta_x(t) \le \varepsilon\},\qquad \tau(\varepsilon) = \max_x \tau_x(\varepsilon) \quad(\textbf{mixing time}).
A chain is rapidly mixing if τ(ε)\tau(\varepsilon) is polylog in 1/ε1/\varepsilon (and polynomial in the input size) — fast enough to be useful as a sampler. The whole ε\varepsilon-uniform sampling goal is: run until DUε\|D - U\| \le \varepsilon.

Coupling — the workhorse for bounding mixing time

How do you prove a chain mixes fast? Coupling is the dominant technique.

Definition. A coupling of a chain MM (on SS) is a chain Zt=(Xt,Yt)Z_t = (X_t, Y_t) on S×SS \times S such that each coordinate, viewed alone, is a faithful copy of MM:

Pr[Xt+1=xZt=(x,y)]=Pr[Mt+1=xMt=x],\Pr[X_{t+1}=x'\mid Z_t=(x,y)] = \Pr[M_{t+1}=x'\mid M_t=x],
Pr[Yt+1=yZt=(x,y)]=Pr[Mt+1=yMt=y].\Pr[Y_{t+1}=y'\mid Z_t=(x,y)] = \Pr[M_{t+1}=y'\mid M_t=y].
The two copies may be correlated however we like (that’s the design freedom), as long as the marginals are correct.

Lemma (Coupling Lemma). If a coupling Zt=(Xt,Yt)Z_t=(X_t,Y_t) satisfies, for some time TT,

Pr[XTYTX0=x,Y0=y]εfor all x,y,\Pr[X_T \ne Y_T \mid X_0=x, Y_0=y] \le \varepsilon \quad\text{for all } x,y,
then the mixing time obeys   τ(ε)T.\;\tau(\varepsilon) \le T.

Why coupling works — the picture. Run two copies of the chain at once: one started at the worst state xx, the other started in the stationary distribution π\pi (so the second copy is always exactly π\pi). Couple their randomness so that once they meet, they move together forever. At any time tt, the variation distance of copy 1 from π\pi is at most the probability the two copies have not yet met. So: “design a coupling that makes the two copies collide quickly, and you have bounded the mixing time.” When the copies are close, the chain is close to stationary.


11. Card shuffling — three couplings, three mixing analyses

Shuffling a deck of nn cards is a random walk on the symmetric group SnS_n whose stationary distribution is uniform (every permutation equally likely). “How many shuffles to mix” = mixing time. Three classic shuffles, three techniques.

(a) Random transposition-to-top — coupling ⇒ coupon collector

A first attempt (move a random position to the top in both decks) does not couple well. The clever coupling:

Pick a uniform card value C{1,,n}C \in \{1,\dots,n\} (not a position). In deck XX, move the card with value CC to the top; in deck YY, also move the card with value CC to the top.

Now the key observation: once a card has been “named” CC, it sits in the same relative position in both decks forever after. So the two decks become identical as soon as every value has been chosen at least once — which is exactly the coupon collector problem. After nlnn+cnn\ln n + cn steps, the probability a specific card was never chosen is

Pr[specific card not yet on top](11n)nlnn+cne(lnn+c)=ecn,\Pr[\text{specific card not yet on top}] \le \left(1-\tfrac1n\right)^{n\ln n + cn} \le e^{-(\ln n + c)} = \frac{e^{-c}}{n},
so by a union bound over nn cards,
Pr[some card never chosen]ec.\Pr[\text{some card never chosen}] \le e^{-c}.
By the Coupling Lemma, the mixing time is nlnn+O(n)n\ln n + O(n)Θ(nlogn)\Theta(n\log n) shuffles.

(b) Top-in-at-random — strong uniform stopping time

Take the top card and insert it into a uniformly random position in the deck. A second, equally powerful technique for proving “exactly uniform now”:

Definition (strong uniform stopping time / silno uniformné pravidlo zastavenia). A (randomized) stopping rule TT such that for every KK, conditioned on T=KT=K, the permutation is exactly uniform. Stopping at TT yields a perfectly uniform sample.

The stopping rule for top-in-at-random. Track the card that was originally on the bottom. Stop the first time it gets inserted (reaches the top and goes in). Why is this strong uniform? Each card inserted below the original bottom card lands in a uniformly random position among those below it; by the time the original bottom card itself is finally inserted, the entire deck below it has been built up uniformly at random — so the full permutation is uniform, regardless of how long that took.

The time TT until the original bottom card rises and inserts is, by the same structure, a coupon-collector quantity again (the times TiTi1T_i - T_{i-1} to accumulate ii cards below match the inter-arrival times Vni+1VniV_{n-i+1}-V_{n-i} of the collector).

Lemma (stopping-time bound on variation distance). If a shuffle has a strong uniform stopping time TT, then

QkUPr[T>k].\|Q^{*k} - U\| \le \Pr[T > k].

Proof (the clean computation). For any event SSnS \subseteq S_n, split on whether the process has stopped by time kk:

Qk(S)=jkPr[XkST=j]=U(S), stoppeduniformPr[T=j]  +  Pr[XkST>k]Pr[T>k]Q^{*k}(S) = \sum_{j\le k}\underbrace{\Pr[X_k\in S \mid T=j]}_{=\,U(S),\ \text{stopped} \Rightarrow \text{uniform}}\Pr[T=j] \;+\; \Pr[X_k\in S\mid T>k]\Pr[T>k]
=U(S)(1Pr[T>k])+Pr[XkST>k]Pr[T>k]=U(S)+Pr[T>k](Pr[XkST>k]U(S))[1,1].= U(S)\bigl(1-\Pr[T>k]\bigr) + \Pr[X_k\in S\mid T>k]\Pr[T>k] = U(S) + \Pr[T>k]\underbrace{\bigl(\Pr[X_k\in S\mid T>k]-U(S)\bigr)}_{\in[-1,1]}.
Hence Qk(S)U(S)Pr[T>k]|Q^{*k}(S) - U(S)| \le \Pr[T > k] for every SS; take the max over SS. ∎

With k=nlnn+cnk = \lceil n\ln n + cn\rceil, the coupon-collector tail gives Pr[T>k]ec\Pr[T>k]\le e^{-c}, so again Θ(nlogn)\Theta(n\log n) shuffles suffice.

© Riffle shuffle — birthday paradox, 32log2n\tfrac32\log_2 n shuffles

The realistic shuffle: split the deck into two parts and interleave. The cleanest analysis runs the inverse shuffle (assign each card a random bit, pull the 0-cards to the top keeping relative order — equivalent to the forward riffle).

After kk inverse shuffles each card carries a kk-bit label (its sequence of bits). The strong uniform stopping rule: stop when all nn labels are distinct — at that moment the cards are sorted by a uniformly random ordering, hence uniform. With K=2kK = 2^k possible labels, “all nn distinct” is exactly the birthday problem:

Theorem. After kk riffle shuffles of nn cards,

RifkUPr[T>k]=1i=1n1(1i2k).\|\mathrm{Rif}^{*k} - U\| \le \Pr[T>k] = 1 - \prod_{i=1}^{n-1}\left(1 - \frac{i}{2^k}\right).

By the birthday paradox the labels become distinct once 2kn22^k \gg n^2, i.e. k2log2nk \approx 2\log_2 n; a sharper analysis gives the famous 32log2n7\tfrac32\log_2 n \approx 7 shuffles for n=52n=52 threshold.

Professor-pleaser — three shuffles, three classics. The same mixing question is answered three different ways, each reducing the analysis to an elementary probability gem: coupon collector (random-to-top, via coupling), coupon collector again (top-in-at-random, via strong uniform stopping time), and the birthday paradox (riffle). The two proof techniques — coupling and strong uniform stopping times — are the two universal hammers for upper-bounding mixing time, and both reduce “distribution is close to uniform” to “a simple combinatorial event has occurred.”


12. Eigenvalues, expanders, and rapid mixing

Recall pˉ(t)=pˉ(0)Pt\bar p(t) = \bar p(0)P^t — mixing is governed by the powers of PP, hence by its eigenvalues. This is the spectral route to bounding mixing time, and the natural home of expanders.

Spectral facts for graphs

For an undirected (multi)graph let A(G)A(G) be the adjacency matrix (symmetric, so real eigenvalues λ1λ2λn\lambda_1 \ge \lambda_2 \ge \dots \ge \lambda_n with an orthonormal eigenbasis). Key facts (state these; they recur):

The reading. λ1=d\lambda_1 = d is the uniform/stationary direction. The spectral gap dλ2d - \lambda_2 measures how fast everything else decays under repeated multiplication by P=A/dP = A/d: a big gap (λ2\lambda_2 far below dd) means all non-uniform components die quickly \Rightarrow fast mixing. The negative end (λn\lambda_n near d-d) signals near-bipartiteness \Rightarrow near-periodicity, which we fix with self-loops.

Expanders

An (n,d,c)(n,d,c)-expander is a bipartite dd-regular (multi)graph G(X,Y,E)G(X,Y,E) with X=Y=n/2|X|=|Y|=n/2 that expands every subset:

SX:Γ(S)(1+c(12Sn))S.\forall S\subseteq X:\quad |\Gamma(S)| \ge \left(1 + c\Bigl(1 - \tfrac{2|S|}{n}\Bigr)\right)|S|.
Every set has many neighbours — the graph has no bottlenecks.

Theorem (expansion ⇔ spectral gap — Cheeger-type).

  • If GG is an (n,d,c)(n,d,c)-expander, then λ2dc21024+2c2|\lambda_2| \le d - \dfrac{c^2}{1024 + 2c^2}.
  • Conversely, if λ2dε|\lambda_2| \le d - \varepsilon, then GG is an (n,d,c)(n,d,c)-expander with c2dεε2d2c \ge \dfrac{2d\varepsilon - \varepsilon^2}{d^2}.

The two notions — combinatorial expansion (no small cuts) and a spectral gap (large dλ2d-\lambda_2) — are quantitatively equivalent. This is the deep structural fact behind all of expander theory.

Random walk on an expander mixes rapidly

Take the walk P=A(G)/dP = A(G)/d. Kill periodicity by adding self-loops: Q=(I+P)/2Q = (I+P)/2, whose eigenvalues shift to λi=1+λi/d2[0,1]\lambda_i' = \frac{1 + \lambda_i/d}{2} \in [0,1]. If $\lambda_2 = d

Theorem (rapid mixing). For the lazy walk QQ on an (n,d,c)(n,d,c)-expander with λ2dε\lambda_2 \le d-\varepsilon, from any start q0q^0, the relative deviation from stationary obeys

Δ(t)n1.5(λ2)tn1.5(1ε2d)t.\Delta(t) \le n^{1.5}\,(\lambda_2')^{t} \le n^{1.5}\left(1 - \frac{\varepsilon}{2d}\right)^{t}.

Since λ2<1\lambda_2' < 1 is a constant bounded away from 1, Δ(t)\Delta(t) drops below any ε\varepsilon after only O(logn)O(\log n) steps — expanders mix in logarithmically many steps. This is what “rapidly mixing” means in its purest form.

★ The application: amplifying BPP with O(k)O(k) random bits

A gorgeous payoff, and a classic exam topic linking this lecture to the complexity lecture.

Setup. A BPP algorithm AA uses nn random bits and errs with probability 1/3\le 1/3. We want to push the error down to 1/2k1/2^k. The textbook way: run AA on kk independent random strings r1,,rkr_1,\dots,r_k and take the majority; Chernoff gives

Pr[majority of A(x,r1),,A(x,rk) wrong]2Ω(k).\Pr[\text{majority of } A(x,r_1),\dots,A(x,r_k) \text{ wrong}] \le 2^{-\Omega(k)}.
But that costs knk\cdot n random bits. Random bits are expensive. Can we get error 1/2k1/2^k using far fewer?

Yes — walk on an expander. Build an expander whose vertices are the nn-bit strings (so N=2nN = 2^n), dd-regular with λ2110\lambda_2'\le \tfrac1{10} (e.g. degree d=7d=7, a (N,7,2α)(N,7,2\alpha)-expander, Q=(I+A/7)/2Q=(I+A/7)/2). Take a single random walk X0,X1,,X7kX_0, X_1, \dots, X_{7k} on it, and use the visited vertices as the random strings:

ri=Xi,compute maj(A(x,r0),A(x,r1),,A(x,r7k)).r_i = X_i, \qquad \text{compute } \mathrm{maj}\bigl(A(x,r_0), A(x,r_1),\dots,A(x,r_{7k})\bigr).

The cost: the first vertex needs nn bits, but each step of the walk only needs O(logd)=O(1)O(\log d) = O(1) bits to choose a neighbour. Total:

n+O(k) random bits(vs. kn for independent sampling).n + O(k) \text{ random bits} \quad(\text{vs. } kn \text{ for independent sampling}).

Theorem. $\Pr[\text{majority of } A(x,r_0),\dots,A(x,r_{7k}) \text{ wrong}] \le 1/2^k.$

Why it works (the deep point). The walk’s steps are highly correlated — yet because the expander mixes rapidly, the sequence of visited vertices behaves almost like independent samples for the purpose of a majority vote. The spectral gap guarantees the walk doesn’t get “stuck” in a bad region: the fraction of steps landing on bad strings concentrates just as it would for independent draws (an expander Chernoff bound). Randomness is a costly resource, and expander walks let us recycle it — getting the error reduction of kk independent trials while paying for only O(k)O(k) extra bits. This is a cornerstone of the “hardness vs. randomness” / derandomization story.


13. From sampling to counting — Monte Carlo estimation and FPRAS

The final movement: once you can sample, you can count / estimate. This is the Monte Carlo method proper.

Warm-up: estimating π\pi (the constant)

Throw mm uniform random points into the unit square; let Zi=1Z_i = 1 if the ii-th lands inside the quarter disc (x2+y21x^2+y^2 \le 1). With W=iZiW = \sum_i Z_i, the fraction inside estimates the area π/4\pi/4:

E[W]=mπ4,estimator P=4Wm.E[W] = \frac{m\pi}{4}, \qquad \text{estimator } P = \frac{4W}{m}.
Chernoff (Pr[Xμδμ]2eμδ2/3\Pr[|X-\mu|\ge\delta\mu]\le 2e^{-\mu\delta^2/3}) on WW gives
Pr[Pπεπ]=Pr[WE[W]εE[W]]2emπε2/12.\Pr[\,|P - \pi| \ge \varepsilon\pi\,] = \Pr[\,|W - E[W]| \ge \varepsilon E[W]\,] \le 2\,e^{-m\pi\varepsilon^2/12}.
So PP is within επ\varepsilon\pi of π\pi with probability 12emπε2/12\ge 1 - 2e^{-m\pi\varepsilon^2/12}; this exceeds 1δ1-\delta once
m12ln(2/δ)πε2.m \ge \frac{12\ln(2/\delta)}{\pi\,\varepsilon^2}.

The (ε,δ)(\varepsilon,\delta)-approximation and FPRAS

Definition. XX is an (ε,δ)(\varepsilon,\delta)-approximation of a value VV if

Pr[XVεV]1δ.\Pr\bigl[\,|X - V| \le \varepsilon V\,\bigr] \ge 1 - \delta.
(Relative error ε\le \varepsilon with confidence 1δ\ge 1-\delta.)

Theorem (sample-size for estimation). Let X1,,XmX_1,\dots,X_m be i.i.d. indicators with mean μ=E[Xi]\mu = E[X_i]. If

m3ln(2/δ)ε2μ,m \ge \frac{3\ln(2/\delta)}{\varepsilon^2 \mu},
then 1miXi\frac1m\sum_i X_i is an (ε,δ)(\varepsilon,\delta)-approximation of μ\mu.

Definition (FPRAS). A fully polynomial randomized approximation scheme for a quantity V(X)V(X) is a randomized algorithm that, given input XX and $0<\varepsilon, \delta<1$, outputs an (ε,δ)(\varepsilon,\delta)-approximation of V(X)V(X) in time polynomial in X|X|, 1/ε1/\varepsilon, and ln(1/δ)\ln(1/\delta).

The catch that drives everything. The sample size 3ln(2/δ)ε2μ\frac{3\ln(2/\delta)}{\varepsilon^2\mu} blows up when μ\mu is tiny. If the thing you’re estimating is an exponentially small fraction of your sample space, naïve uniform sampling needs exponentially many samples to ever see a success. “We need reasonable sampling” — a sample space where the target is a non-negligible fraction. The art of counting via sampling is choosing the right space. The next example shows the fix.

Case study — counting #DNF (the Karp–Luby coverage trick)

Problem #DNF. Given a DNF formula F=C1CtF = C_1 \vee \dots \vee C_t, count C(F)C(F) = the number of satisfying assignments. (Note: DNF satisfiability is trivial — one clause satisfiable; but counting solutions is #P-hard. Also FF satisfiable $\iff #{\text{sat assignments of }\neg F} < 2^n$.)

Naïve sampling fails. Draw αR{0,1}n\alpha \in_R \{0,1\}^n, count fraction satisfying FF, scale by 2n2^n. Estimator Y=(X/m)2nY = (X/m)2^n has E[Y]=C(F)E[Y] = C(F), and X/mX/m is an (ε,δ)(\varepsilon,\delta)-approximation of C(F)/2nC(F)/2^n once

m32nε2C(F)ln(2/δ).m \ge \frac{3\cdot 2^n}{\varepsilon^2\,C(F)}\ln(2/\delta).
If C(F)2n/α(n)C(F) \ge 2^n/\alpha(n) for a polynomial α\alpha, this mm is polynomial — fine. But if C(F)C(F) is exponentially small, mm is exponential. Hopeless in general.

The fix — sample from a smartly chosen space where success is dense. A clause CiC_i with (i)\ell(i) literals has exactly 2n(i)2^{n-\ell(i)} satisfying assignments; call that set SCi\mathrm{SC}_i. Form the multiset union

U={(i,a)1it, aSCi},U=iSCi=i2n(i),U = \{(i, a) \mid 1 \le i \le t,\ a \in \mathrm{SC}_i\}, \qquad |U| = \sum_i |\mathrm{SC}_i| = \sum_i 2^{n-\ell(i)},
which is easy to compute and easy to sample from (pick clause ii with probability SCi/U|\mathrm{SC}_i|/|U|, then a uniform satisfying assignment of CiC_i). The true count is the size of the set union C(F)=iSCiC(F) = |\bigcup_i \mathrm{SC}_i|. The ratio
C(F)U=iSCiiSCi  1t\frac{C(F)}{|U|} = \frac{|\bigcup_i \mathrm{SC}_i|}{\sum_i |\mathrm{SC}_i|} \ \ge\ \frac1t
because each satisfying assignment is counted at most tt times in UU (it satisfies at most tt clauses). Success is now a 1/t\ge 1/t fraction — polynomially dense!

Coverage estimator (Karp–Luby). Sample (i,a)(i,a) uniformly from UU. Count it as a “success” iff ii is the first clause that aa satisfies (i.e. aj<iSCja\notin \bigcup_{j<i}\mathrm{SC}_j) — this picks exactly one representative per satisfying assignment, so the success probability is exactly C(F)/UC(F)/|U|. Then Y=(X/m)UY = (X/m)\cdot|U| estimates C(F)C(F).

Because the success probability is 1/t\ge 1/t, the estimation theorem needs only

m=3tε2ln(2/δ)m = \left\lceil \frac{3t}{\varepsilon^2}\ln(2/\delta)\right\rceil
samples — polynomial. This yields an FPRAS for #DNF.

The lesson (professor-pleaser). You can’t estimate the size of a needle-in-a-haystack set by uniform darts. Karp–Luby’s move: don’t sample the haystack — sample a slightly larger, easy-to-handle space UU in which the target is a 1/t\ge 1/t fraction, then correct for over-counting by keeping only the “first clause” representative. Reducing a counting problem to sampling where the answer is dense is the master template of approximate counting.

FPAUS — when even sampling is only approximate

Sometimes you cannot sample exactly from the target, only almost uniformly (this is where MCMC re-enters: the chain only reaches ε\varepsilon-close to stationary).

Definition. A sampler AA on space Ω\Omega generates an ε\varepsilon-uniform sample if for all SΩS\subseteq\Omega, $;\bigl|\Pr[w\in S] - |S|/|\Omega|\bigr| \le \varepsilon;$ (i.e. variation distance from uniform ε\le \varepsilon). A FPAUS (fully polynomial almost-uniform sampler) produces one in time polynomial in X|X| and ln(1/ε)\ln(1/\varepsilon).

The grand connection. FPAUS \Rightarrow FPRAS for many self-reducible problems (counting matchings, colourings, knapsack solutions, …): almost-uniform sampling can be bootstrapped into approximate counting. And the FPAUS itself is built by MCMC — design a rapidly mixing chain whose stationary distribution is (almost) uniform on Ω\Omega (§7–§9), run it for the mixing time (§10–§12), read off the state. This closes the entire loop of the lecture: build the chain (reversibility/Metropolis) → bound the mixing time (coupling/eigenvalues) → sample → count.


The spine — one line per movement

Markov chain = memoryless random motion = a stochastic matrix PP. A nice (irreducible, finite, aperiodic) chain forgets its start and converges to a unique stationary π\pi (Fundamental Theorem; πi=1/hi,i\pi_i = 1/h_{i,i}). Stationarity is easiest to verify and to engineer via detailed balance / reversibility $\pi_iP_{ij} = \pi_jP_{ji}$. That gives a sampling machine (MCMC): design a chain whose stationary distribution is the target σ\sigma (uniform via self-loops; arbitrary via Metropolis–Hastings, using only probability ratios so the unknown normalizer cancels), run it past its mixing time (variation distance ε\le\varepsilon), read off a sample. Mixing is bounded by coupling, strong uniform stopping times (card shuffles: coupon collector ×2, birthday paradox), or the spectral gap (expanders mix in O(logn)O(\log n) — and expander walks recycle random bits to amplify BPP with O(k)O(k) bits). Finally, sampling becomes counting: (ε,δ)(\varepsilon,\delta)-approximation → FPRAS, with #DNF (Karp–Luby coverage) as the model example, and FPAUS the bridge from almost-uniform sampling back to approximate counting.

Theme The one idea
Markov property present screens off past from future; chain = matrix PP
Fundamental Theorem nice chain forgets its start → unique π\pi; πi=1/hi,i\pi_i = 1/h_{i,i}
Reversibility local pairwise balance πiPij=πjPji\pi_iP_{ij}=\pi_jP_{ji} ⇒ stationary; the design lever
Random walk on graph $\pi_v = \deg(v)/2
MCMC invent a chain whose π\pi = desired σ\sigma; sample by running it
Metropolis–Hastings any prescribed π\pi from ratios only — normalizer cancels
Mixing / variation distance $|D_1-D_2| = \max_A
Coupling run two copies, bound time-to-meet ⇒ bound mixing
Strong uniform stopping exactly uniform once a combinatorial event fires
Card shuffles coupon collector (×2), birthday paradox ⇒ Θ(nlogn)\Theta(n\log n), 32log2n\tfrac32\log_2 n
Expanders expansion ⇔ spectral gap ⇒ O(logn)O(\log n) mixing; BPP error in O(k)O(k) bits
FPRAS / #DNF sample where the target is dense (Karp–Luby), then scale

Connections / threads to drill

markov-chainsrandom-walksstationary-distributionreversibilityMetropolis-Hastingsmixing-timecouplingcard-shufflingexpandersMonte-CarloFPRASDNF-counting

← Back to Randomized Algorithms