Unit 8

Randomized Computation

Monte Carlo and Las Vegas algorithms, amplification, and Karger's random contraction method.

Goals

  • Model randomized Turing machines as deterministic machines with a random tape.
  • Distinguish Monte Carlo, Las Vegas, one-sided, two-sided, and zero error.
  • Analyze Freivalds' test, amplification, and Karger's contraction.
  • Place RP\mathrm{RP}, coRP\mathrm{coRP}, BPP\mathrm{BPP}, and ZPP\mathrm{ZPP} correctly.

1. Random bits are not nondeterministic guesses

A probabilistic Turing machine (PTM) has ordinary work tapes plus independent fair random bits. Equivalently, at each random state it chooses one of two transitions with probability 1/21/2. Fixing a random string rr makes the computation deterministic. If m(∣x∣)m(|x|) bits are used, then

Pr⁡r[M(x;r)=1]=∣{r∈{0,1}m(∣x∣):M(x;r)=1}∣2m(∣x∣).\Pr_r[M(x;r)=1] =\frac{|\{r\in\{0,1\}^{m(|x|)}:M(x;r)=1\}|}{2^{m(|x|)}}.

Nondeterminism asks whether some accepting branch exists. Randomization measures the fraction of accepting random strings. One rare accepting branch certifies NP acceptance but does not satisfy a bounded-error randomized definition.

Always specify the sample space, whether repetitions use fresh independent bits, and whether the guarantee is per input. The complexity classes below require their probability bound for every input, not merely on average over an input distribution.

2. Monte Carlo and Las Vegas

A Monte Carlo algorithm has a fixed runtime bound but may return a wrong answer with bounded probability. Error is one-sided when one kind of answer is always correct and two-sided when either kind can be wrong.

A Las Vegas algorithm never returns a wrong answer; its runtime is random, generally bounded in expectation. If a verifiable trial finds a correct witness with probability at least 1/41/4, repeating until success is always correct and takes at most four trials in expectation. Truncating after tt trials yields a Monte Carlo algorithm whose timeout probability is at most (3/4)t(3/4)^t.

“Correct in expectation” is not Las Vegas. The defining property is always correct, random time.

3. Freivalds' matrix-product test

Given n×nn\times n matrices A,B,CA,B,C over a field, Freivalds tests AB=CAB=C in O(n2)O(n^2) field operations:

  1. Choose r∈{0,1}nr\in\{0,1\}^n uniformly with independent coordinates.
  2. Compute u=Bru=Br, v=Auv=Au, and w=Crw=Cr.
  3. Accept iff v=wv=w.

If AB=CAB=C, every run accepts. If D=AB−C≠0D=AB-C\ne0, choose a nonzero row and a coordinate jj with nonzero coefficient. Condition on all coordinates except rjr_j. Of its two values, at most one makes that row's dot product zero, because the two sums differ by the nonzero coefficient DijD_{ij}. Hence

Pr⁡[Dr=0]≤12.\Pr[Dr=0]\le\frac12.

The test has one-sided false-accept error. For a tight example over F2\mathbb F_2, let

D=(1100).D=\begin{pmatrix}1&1\\0&0\end{pmatrix}.

Dr=0Dr=0 exactly for r=00r=00 and r=11r=11, probability 1/21/2. With kk fresh vectors, a false equality survives with probability at most 2−k2^{-k}.

4. Amplification

If each independent run fails with probability at most q<1q<1 and any success suffices, all kk runs fail with probability at most qkq^k. To make this at most δ\delta, take

k≥ln⁡(1/δ)ln⁡(1/q).k\ge\frac{\ln(1/\delta)}{\ln(1/q)}.

For one-sided error, combine runs with OR or AND in the direction preserving the perfect side. For BPP's two-sided error, take the majority of an odd number of independent runs. From success probability at least 2/32/3, a Chernoff bound gives error exp⁡(−Ω(k))\exp(-\Omega(k)), so k=O(log⁡(1/δ))k=O(\log(1/\delta)).

Amplification reduces but does not literally remove error. Reusing one random string is not independent repetition. Constants such as 1/21/2 and 2/32/3 are conventional because any fixed gap from 1/21/2 can be amplified.

5. Randomized complexity classes

For a polynomial-time PTM MM:

L∈RP:x∉L⇒Pr⁡[M(x)=1]=0,x∈L⇒Pr⁡[M(x)=1]≥12;L∈coRP:L‾∈RP;L∈BPP:x∈L⇒Pr⁡[M(x)=1]≥23,x∉L⇒Pr⁡[M(x)=1]≤13.\begin{aligned} L\in\mathrm{RP}:&\quad x\notin L\Rightarrow\Pr[M(x)=1]=0,\\ &\quad x\in L\Rightarrow\Pr[M(x)=1]\ge\tfrac12;\\ L\in\mathrm{coRP}:&\quad \overline L\in\mathrm{RP};\\ L\in\mathrm{BPP}:&\quad x\in L\Rightarrow\Pr[M(x)=1]\ge\tfrac23,\\ &\quad x\notin L\Rightarrow\Pr[M(x)=1]\le\tfrac13. \end{aligned}

ZPP\mathrm{ZPP} consists of zero-error expected-polynomial-time languages. Standard relationships are

P⊆RP⊆BPP,P⊆coRP⊆BPP,ZPP=RP∩coRP.\mathrm P\subseteq\mathrm{RP}\subseteq\mathrm{BPP},\qquad \mathrm P\subseteq\mathrm{coRP}\subseteq\mathrm{BPP},\qquad \mathrm{ZPP}=\mathrm{RP}\cap\mathrm{coRP}.

For the equality, alternate an RP machine and a coRP machine until the first accepts or the second rejects. Neither stopping answer can be wrong, and each round has constant stopping probability. Conversely, truncate a zero-error expected-polynomial algorithm after a suitable multiple of its expectation. Markov's inequality bounds timeout; assigning timeout oppositely gives RP and coRP machines.

Whether BPP=P\mathrm{BPP}=\mathrm P is unknown; do not present it as a theorem.

6. Karger's random contraction

Repeatedly choose a uniformly random current edge of an undirected multigraph, contract its endpoints, delete self-loops, and retain parallel edges. At two supervertices, output their separating edges.

Fix a minimum cut FF of size λ\lambda. If it has survived while ii supervertices remain, every supervertex has degree at least λ\lambda; otherwise its incident cut is smaller. Thus there are at least iλ/2i\lambda/2 edges, and only λ\lambda are dangerous. The chance of contracting FF is at most 2/i2/i, so its survival probability is at least

∏i=n3(1−2i)=∏i=n3i−2i=2n(n−1).\prod_{i=n}^{3}\left(1-\frac2i\right) =\prod_{i=n}^{3}\frac{i-2}{i} =\frac{2}{n(n-1)}.

Worked example

The telescoping product equals 2/(n(n−1))2/(n(n-1)): inverse-polynomial, not constant, success. With RR independent runs, failure is at most

(1−2n(n−1))R≤exp⁡(−2Rn(n−1)).\left(1-\frac{2}{n(n-1)}\right)^R \le\exp\left(-\frac{2R}{n(n-1)}\right).

Thus R=Θ(n2log⁡(1/δ))R=\Theta(n^2\log(1/\delta)) gives failure at most δ\delta. For n=4n=4, the fixed-cut bound is 1/61/6. A cycle C4C_4 has several minimum cuts, so finding some optimum can be likelier. Parallel edges remain because multiplicity is both cut capacity and sampling weight.

7. Compare the guarantees

  • Freivalds: bounded O(n2)O(n^2) time and one-sided error; repeated error at most 2−k2^{-k}.
  • BPP: polynomial worst-case time and two-sided error bounded away from 1/21/2 on every input.
  • Las Vegas/ZPP: no wrong answer and expected polynomial time.
  • Karger: every output is feasible, but one run is optimal only with inverse-polynomial probability.

For Karger, “failure” means suboptimal, not malformed. Feasibility and quality are separate claims.

Common mistakes

  • Treating random and nondeterministic branches identically.
  • Defining correctness only on average over inputs.
  • Claiming independence when runs reuse randomness.
  • Combining one-sided repetitions in the wrong direction.
  • Calling expected correctness “Las Vegas.”
  • Dropping parallel edges during contraction.
  • Saying amplification makes error exactly zero.

Visualizations

Guided construction

Karger contraction

Step 1 of 6
Karger contraction on a square with one diagonalA protected two-edge minimum cut at B survives contractions of the diagonal A C and then a safe edge to D. Parallel edges are retained and a self-loop is deleted.ABCDprotected cut F at B, λ=2
Fixed cut used only in analysisContracted safe edge

Also draw a PTM tree with equally weighted leaves. Label NP “at least one accepting leaf,” RP “none on no-instances and many on yes-instances,” and BPP “a strong majority agrees.”

Exercises

Exercise 1 — Freivalds when AB=CAB=C

What happens in Freivalds' algorithm when AB=CAB=C? Prove there is no false rejection in that case.

Solution

If AB=CAB=C then (AB−C)r=0(AB-C)r=0 for every rr, so the test always accepts. One-sided error is only false acceptance when AB≠CAB\neq C.

Exercise 2 — Amplification to 2−402^{-40}

An RP\mathrm{RP}-style algorithm (on yes-instances Pr⁡[accept]≥1/2\Pr[\text{accept}]\ge1/2; on no-instances no false accepts) is run kk independent times. Accept if at least one run accepts. How many repetitions reduce the false-reject probability on a yes-instance to ≤2−40\le2^{-40}?

Solution

On a yes-instance, a single run fails (rejects) with probability ≤1/2\le1/2. Over kk independent runs we accept unless every run fails, so

Pr⁡[false reject]≤(1/2)k=2−k.\Pr[\text{false reject}]\le(1/2)^k=2^{-k}.

Thus k=40k=40 suffices. On no-instances no run accepts, so amplification introduces no false accepts. (This is OR-amplification for RP\mathrm{RP}; coRP\mathrm{coRP} uses AND.)

Exercise 3 — One accepting path is not RP

Prove that a single accepting path of a PTM does not establish membership in RP\mathrm{RP}.

Solution

RP\mathrm{RP} requires that on yes-instances at least a constant fraction (e.g. 1/21/2) of random strings accept, and on no-instances none accept. A single accepting string is NP\mathrm{NP} semantics, not RP\mathrm{RP}.

Exercise 4 — ZPP=RP∩coRP\mathrm{ZPP}=\mathrm{RP}\cap\mathrm{coRP}

Explain both directions of ZPP=RP∩coRP\mathrm{ZPP}=\mathrm{RP}\cap\mathrm{coRP}.

Solution

If L∈RP∩coRPL\in\mathrm{RP}\cap\mathrm{coRP}, alternate the two one-sided machines until one decides with certainty — zero error and expected polynomial time. If L∈ZPPL\in\mathrm{ZPP}, truncate the zero-error machine and interpret timeout in opposite ways to obtain RP and coRP machines.

Exercise 5 — Karger's edge lower bound

Why does Karger's proof lower-bound the current number of edges by iλ/2i\lambda/2 when ii supernodes remain and the min-cut is λ\lambda?

Solution

Every supernode has degree ≥λ\ge\lambda (else a smaller cut). The handshaking lemma gives ∣E∣≥iλ/2|E|\ge i\lambda/2. Hence the probability of contracting a dangerous edge is ≤λ/∣E∣≤2/i\le\lambda/|E|\le2/i.

8. Detailed probability walkthroughs

Turning biased random choices into fair-bit computation

The PTM definition uses fair bits for convenience, not because algorithms may only sample powers of two. To sample uniformly from {1,…,6}\{1,\ldots,6\}, read three bits as an integer in {0,…,7}\{0,\ldots,7\}; reject 6,76,7 and retry. Every accepted value has the same probability, and acceptance occurs with probability 3/43/4, so the expected number of rounds is 4/34/3. This is a small Las Vegas sampler. A hard worst-case cutoff would introduce a failure event and make it Monte Carlo.

If an algorithm says “choose a random edge,” it means uniform among current edge copies. Sampling endpoints uniformly would be a different distribution and would invalidate Karger's calculation. Translating prose into a precise random-bit procedure is part of specifying the algorithm.

Freivalds with integers and fields

Over the integers, the same binary-vector argument works because a nonzero coefficient cannot disappear when rjr_j flips. Implementations must account for arithmetic cost and overflow. One can instead compute modulo a suitably chosen prime, but then a nonzero integer error matrix might become zero modulo that prime. If the prime is fixed, this is a new source of error; if it is selected from a suitable range, its probability must be analyzed separately. The clean theorem stated above assumes exact field operations and AB≠CAB\ne C in that field.

Freivalds verifies a claimed product; it does not compute the product. This distinction explains the speedup. Computing BrBr, then A(Br)A(Br), uses two matrix-vector products, while explicitly forming ABAB would compute all pairwise row-column products.

Exact amplification arithmetic

Suppose a Freivalds run costs 2n2+O(n)2n^2+O(n) operations and a false product passes with probability at most 1/21/2. Ten runs cost O(10n2)=O(n2)O(10n^2)=O(n^2) because ten is constant, and reduce error to 1/10241/1024. To reach error at most 1/nc1/n^c, choose

k≥clog⁡2n.k\ge c\log_2 n.

The runtime becomes O(n2log⁡n)O(n^2\log n). To reach exponentially small 2−n2^{-n} error requires nn runs and O(n3)O(n^3) elementary time, erasing the asymptotic advantage over elementary multiplication. Error targets and runtime must be analyzed together.

For BPP majority, simple multiplication of failure probabilities is wrong because majority can be wrong even when some runs are correct. Let YiY_i indicate correctness and use concentration around E[∑Yi]≥2k/3\mathbb E[\sum Y_i]\ge2k/3. A majority error means falling below k/2k/2, a linear deviation, which Chernoff bounds exponentially. Independence is again essential.

Converting expected time safely

Let a zero-error algorithm have expected runtime at most p(n)p(n). Markov gives

Pr⁡[T>2p(n)]≤12.\Pr[T>2p(n)]\le\frac12.

Stop it after 2p(n)2p(n) steps. For an RP version, accept only if the algorithm halted and answered yes; otherwise reject. No no-instance is falsely accepted, while a yes-instance is accepted with probability at least 1/21/2. For coRP, accept on timeout and preserve the opposite perfect side. Constants can then be amplified.

The known expectation bound must be uniform and computable enough to set the cutoff. Merely observing good average runtime in experiments does not prove ZPP membership.

Karger: conditioning one stage at a time

The min-cut degree argument is conditional. After arbitrary safe contractions, each current supervertex represents a nonempty original vertex set. Its current incident edges form a cut of the original graph, so its degree is at least λ\lambda. This is why the same lower bound remains valid throughout a successful prefix.

At ii vertices, survival probability for the next contraction is at least (i−2)/i(i-2)/i. Multiplying conditional probabilities is legitimate:

Pr⁡[En−1∩⋯∩E2]=∏i=n3Pr⁡[Ei−1∣Ei∩⋯∩En−1].\Pr[E_{n-1}\cap\cdots\cap E_2] =\prod_{i=n}^{3}\Pr[E_{i-1}\mid E_i\cap\cdots\cap E_{n-1}].

No claim is made that contraction events are independent. They are strongly dependent; conditioning is the correct tool. The product telescopes because numerators n−2,n−3,…,1n-2,n-3,\ldots,1 cancel almost all denominator factors n,n−1,…,3n,n-1,\ldots,3.

A small contraction trace

Take a square with one diagonal. Its minimum cut has size two at either vertex not incident to both diagonal ends. Fix such a cut and color its two edges red. If the first chosen edge is not red, contract it and keep every resulting parallel edge. Recompute the current edge multiset before the next uniform choice. If a red edge is ever selected, that fixed partition can no longer be output; if neither is selected until two supervertices remain, all red copies cross the final cut and the run has recovered it.

The algorithm need not know λ\lambda or the protected cut. They exist only in the analysis. This is a common randomized-proof pattern: fix a desirable witness, calculate the probability that random operations preserve it, then repeat.

9. Designing a randomized proof

Use the following checklist:

  1. Define the random tape or explicit random variables.
  2. State exactly which outputs count as errors.
  3. Prove the runtime bound separately from correctness.
  4. Condition on history when choices change the future sample space.
  5. Use fresh randomness before multiplying probabilities.
  6. State whether the result is worst-case time, expected time, expected quality, or high-probability correctness.
  7. For optimization, prove every output is feasible even on unsuccessful runs.

These distinctions prevent a frequent category error: an expected approximation ratio, a bounded-error decision algorithm, and a zero-error expected-time algorithm are three different guarantees even when all use coins.

Sanity checks on class definitions

An RP machine may reject a yes-instance, but repeated OR preserves the rule that no no-instance is ever accepted. A coRP machine may accept a no-instance, and repeated AND preserves its perfect yes-side. For BPP, neither raw OR nor raw AND is appropriate because it drives one error down while driving the other up; majority balances both.

The class bound is over the machine's coins for each fixed input. It gives no promise that one random seed works for every input of a given length. Fixing a globally good seed is a derandomization question and generally needs an additional argument. Likewise, pseudorandom generators cannot simply replace true coins unless their fooling property is proved for the computation being run.

When reporting a randomized algorithm, include the confidence parameter δ\delta in its runtime. “With high probability” conventionally means failure at most 1/nc1/n^c for a desired constant cc, which usually costs O(log⁡n)O(\log n) repetitions from constant error and O(n2log⁡n)O(n^2\log n) contractions from Karger's inverse-polynomial single-run success.

Summary

Randomness is not nondeterminism: probabilities, error sidedness, and amplification are required. RP, coRP, BPP, and ZPP encode different guarantees.

Guided practice

Task 1 — Monte Carlo versus Las Vegas

Distinguish Monte Carlo from Las Vegas in time, error, and an example (Freivalds versus rejection sampling).

Solution

Monte Carlo: always-bounded time, probabilistic error (Freivalds). Las Vegas: always-correct answer, random/expected time (sampling with rejection of illegal values). Do not swap the guarantees.

Task 2 — RP versus NP

Write the quantifiers/probabilities for NP\mathrm{NP} and RP\mathrm{RP} on yes- and no-instances.

Solution

NP\mathrm{NP}: ∃\exists an accepting certificate; none on no-instances. RP\mathrm{RP}: on yes-instances Pr⁡[accept]≥1/2\Pr[\text{accept}]\ge1/2; on no-instances Pr⁡[accept]=0\Pr[\text{accept}]=0. A single witness ≠\neq constant probability.

Task 3 — Randomized proof template

Outline a five-step template for proving a randomized algorithm (as taught in the unit).

Solution

(1) Define the randomness space. (2) Conditional correctness/feasibility. (3) Compute error probability. (4) Time (worst-case or expectation). (5) Amplify if needed. Omitting the probability space is a common mistake.

Source trail and authority

  • Student-summary evidence (scope only): סיכומי שיעור/סיכום ממוזג.pdf, pp. 86–94, 96, and 136–139 (the summary's randomized-computation treatment). Karger's contraction algorithm is absent from the merged summary, so its algorithm and analysis above are textbook-supported enrichment only. The student summary establishes course emphasis, not authority for constants or class equalities.
  • Textbook authority: Arora–Barak, Computational Complexity: A Modern Approach, 2007 draft, Ch. 7, §§7.1–7.4, book pp. 115–124; Papadimitriou, Computational Complexity, Ch. 11, §§11.1–11.3, pp. 241–267; Sipser, 3rd ed., §10.2, pp. 396–408.
  • Editorial status: class definitions, Freivalds' bound, amplification, and the contraction product were checked against these textbook frameworks and standard derivations. Reviewed; high confidence.