Main

Lecture 1: Introduction

Motivating examples (fingerprinting, Karger min-cut, QuickSort, Freivalds) where a few coin flips replace heavy deterministic work at a tiny, controllable error.

Last updated 27 June 2026

Lecture 1 — Broader Introduction (Širší úvod)

Course 2-INF-135/15 Pravdepodobnostné algoritmy, LS 2025/26. Source slides: slidy_01_sirsi_uvod.pdf (26 pages).

Course outline

The whole course is structured into five blocks:

  1. Broader introduction — motivational examples (this lecture).
  2. Basic models and complexity classes — what a randomized algorithm is formally, and the classes RP, coRP, BPP, ZPP, PP.
  3. Methods — the recurring tricks (probability amplification, the probabilistic method, fingerprinting, random walks, …).
  4. More about complexity classes — relationships between them.
  5. Derandomization — removing the randomness while keeping the speed.

This first lecture is just a gallery. Each example is a small story whose punchline is the same: a few random coin flips can replace a lot of deterministic work, at the price of a tiny, controllable probability of error. The recurring questions are always:


1. Database equivalence — fingerprinting with primes

The problem

Two computers, RI and RII, each hold an nn-bit string:

We want to test whether X=YX = Y while sending as few bits across the network as possible. Read the bit strings as integers:

x=bin(X),y=bin(Y),0x,y<2n.x = \mathrm{bin}(X), \qquad y = \mathrm{bin}(Y), \qquad 0 \le x, y < 2^n.

The trivial deterministic solution is to ship all nn bits of one string to the other side. We want to do dramatically better.

The protocol

RI picks a random prime pp from the primes up to n2n^2:

pRPrimes(n2).p \in_R \mathrm{Primes}(n^2).

Then:

  1. RI computes the fingerprint s=xmodps = x \bmod p and sends the pair (p,s)(p, s) to RII.
  2. RII computes r=ymodpr = y \bmod p and answers “equal” iff s=rs = r.

Both pp and ss are numbers below n2n^2, so each needs about log(n2)=2logn\log(n^2) = 2\log n bits.

Communication cost: 4logn\approx 4 \log n bits, versus nn bits for the deterministic protocol. An exponential saving.

Correctness

So the one-sided error appears only on unequal inputs, and only for the “unlucky” primes that happen to divide the difference.

Probability of error

Suppose XYX \ne Y, so xy0x - y \ne 0. The fingerprint fails exactly for primes pp dividing xyx-y:

perr=#{pPrimes(n2):p(xy)}Primes(n2).p_{\text{err}} = \frac{\#\{ p \in \mathrm{Primes}(n^2) : p \mid (x - y)\}}{|\mathrm{Primes}(n^2)|}.

How many bad primes are there? Write the prime factorization

xy=pi1j1pi2j2pikjk,jt>0.|x - y| = p_{i_1}^{j_1} p_{i_2}^{j_2} \cdots p_{i_k}^{j_k}, \qquad j_t > 0.

Each prime is 2\ge 2, and xy<2n|x - y| < 2^n, so the number of distinct prime factors satisfies

2kxy<2n    kn1.2^k \le |x-y| < 2^n \;\Longrightarrow\; k \le n - 1.

So at most n1n-1 primes can divide xyx - y.

How many primes are there to choose from? By the prime-counting estimate

Primes(m)mlnm,here m=n2,|\mathrm{Primes}(m)| \sim \frac{m}{\ln m}, \qquad \text{here } m = n^2,

so Primes(n2)n2lnn2=n22lnn|\mathrm{Primes}(n^2)| \sim \dfrac{n^2}{\ln n^2} = \dfrac{n^2}{2 \ln n}.

Putting it together:

perrn1n2/(2lnn)<2lnnn.p_{\text{err}} \le \frac{n-1}{\,n^2/(2\ln n)\,} < \frac{2 \ln n}{n}.

Punchline. With only O(logn)O(\log n) communicated bits we get error O ⁣(lnnn)O\!\left(\dfrac{\ln n}{n}\right), which already vanishes as nn grows — and could be driven down further by repetition.

Why primes? The bound kn1k \le n-1 uses only that distinct primes multiply up fast. Choosing the modulus from a pool (n2/lnn\sim n^2/\ln n primes) much larger than the number of bad ones (n1\le n-1) is exactly what makes a random pick almost surely good. This trick — replacing an object by its fingerprint modulo a random prime — comes back again and again.


2. Two-way probabilistic finite automaton (2PFA) for anbna^n b^n

Naming. 2PFA = two-way probabilistic finite automaton. The “2” means the read head can move both directions, so the machine can re-scan the input as many times as it wants. That two-way ability is exactly what makes this algorithm possible — and it returns as the punchline at the end.

What we are up against

L={anbnnN}L = \{ a^n b^n \mid n \in \mathbb{N}\} is the classic non-regular language. A deterministic finite automaton has fixed, finite memory, but comparing two arbitrary counts nn and mm needs unbounded memory — so no DFA can do it.

Our model adds two small powers:

With these we can recognize LL, but only with bounded error. Constants kk and LL control how small that error is.

The one idea: compare 2n2^{-n} and 2m2^{-m} instead of nn and mm

We cannot store nn or mm, but we can turn a count into a probability:

Sweep over the block of aa’s, flipping a coin above each one. The event “all nn coins came up heads” has probability exactly 2n2^{-n}.

Do the same over the bb’s and you get an event of probability 2m2^{-m}. Now the comparison is easy in spirit:

The machine never learns nn or mm — it only feels which of the two events fires more often.

The algorithm

Input w{a,b}w \in \{a,b\}^*; constants k,Lk, L control the error.

  1. Cheap deterministic sieve. Check that ww has the shape anbma^n b^m (all aa’s, then all bb’s) and that nm(modk)n \equiv m \pmod{k}. Both need only finite memory (counting mod kk is kk states). If either fails, reject.

  2. The coin-flip race. Repeatedly sweep the input, flipping a coin over each symbol. Per sweep:

    • aa-success = every one of the nn aa’s flipped 1 (and the bb-block came out mixed, both 0 and 1) — probability 2n\approx 2^{-n};
    • bb-success = every one of the mm bb’s flipped 1 (and the aa-block came out mixed) — probability 2m\approx 2^{-m}.

    (The “other block is mixed” clause only makes aa-success and bb-success mutually exclusive — at most one per sweep — so the race below is well defined. It barely changes the probabilities.)

  3. The decision. Watch the sequence of successes. Reject if LL aa-successes occur before any bb-success, or LL bb-successes occur before any aa-success. Otherwise accept.

In words: if one side runs away with the race, the counts are unequal → reject; if the race stays balanced, the counts are equal → accept.

Why step 1 (the mod-kk check) is not optional

It does two jobs:

  1. It instantly rejects wrong-shape strings and everything with n≢m(modk)n \not\equiv m \pmod k — all of which are genuinely not in LL.
  2. The crucial one: if a string survives step 1 but still has nmn \ne m, then nmn - m is a nonzero multiple of kk, so
nmk.|n - m| \ge k.

That guarantees a minimum gap of kk between the counts — exactly what makes the two rare events differ by a usable factor. Without it, nn and mm could differ by just 11, so 2n2^{-n} and 2m2^{-m} would differ by only a factor of 22 — too weak to detect reliably.

Correctness — the two cases

Case n=mn = m (string is in LL → we want to accept). aa-success and bb-success are equally likely, so each success in the race is aa-type or bb-type with probability 12\tfrac12, independently. We wrongly reject only if the first LL successes are all the same type:

preject=2either side(12)L=21L.p_{\text{reject}} = \underbrace{2}_{\text{either side}} \cdot \left(\tfrac{1}{2}\right)^{L} = 2^{\,1-L}.

With L=3L = 3: preject=0.25p_{\text{reject}} = 0.25, i.e. we correctly accept with probability 0.750.75.

Case n>mn > m (string not in LL → we want to reject). Because step 1 forces nm(modk)n \equiv m \pmod k, if nmn \ne m then n=m+ikm+kn = m + ik \ge m + k. An aa-success now needs kk extra heads compared to a bb-success, so it is at least 2k2^k times rarer:

Pr[a-success]Pr[a- or b-success]12k+1.\frac{\Pr[a\text{-success}]}{\Pr[a\text{- or }b\text{-success}]} \le \frac{1}{2^k + 1}.

So bb-successes dominate the race, and we correctly reject with probability

preject(112k+1)L.p_{\text{reject}} \ge \left(1 - \frac{1}{2^k+1}\right)^{L}.

With L=3, k=2L = 3,\ k = 2: (45)3=0.512\left(\tfrac{4}{5}\right)^3 = 0.512.

input should correct with prob.
anbna^n b^n accept 0.75\ge 0.75
wrong counts reject 0.512\ge 0.512

Both are bounded away from 12\tfrac12, so this is genuine bounded error. The gap is modest; amplify it the usual way — run many independent copies and take a majority vote (and tune k,Lk, L).

Subtle tension (good oral-exam point). Raising LL shrinks the n=mn=m error 21L2^{1-L} — but it also shrinks the reject probability (112k+1)L\left(1 - \frac{1}{2^k+1}\right)^{L} in the nmn \ne m case (a base below 11 raised to a higher power). So you cannot just crank LL; you must raise kk alongside it (a bigger guaranteed gap ⇒ a bigger per-success bias) to keep both errors small.

The catch: correct but exponentially slow — the deep point

An aa-success has probability 2n\approx 2^{-n} per sweep, so you expect to wait about 2n2^{\,n} sweeps just to see one. Hence the machine recognizes {anbn}\{a^n b^n\} with bounded error but in expected exponential time.

This is not a flaw of this particular construction — it is unavoidable (Dwork–Stockmeyer, building on Freivalds):

Any bounded-error 2PFA recognizing a non-regular language must run in expected exponential time.

Punchline. A two-way finite automaton plus a coin is just barely powerful enough to escape regularity — and it pays for that power with exponential time. Randomness buys a new capability here, and the price tag is explicit.


3. Randomized min-cut — Karger’s contraction algorithm

The problem

Best deterministic algorithms run in O(n3)O(n^3) (or O(VElog(V2/E))O(|V|\,|E| \log(|V|^2/|E|))). The randomized algorithm is strikingly simple.

The algorithm: random edge contraction

1.  label(v) ← v              for every vertex
2.  while more than 2 vertices remain:
        pick e = (x, y) ∈_R E uniformly at random
        contract e: merge x and y into a single vertex z   (G ← contract(G, e))
        label(z) ← label(x) ∪ label(y)    (keep parallel edges, drop self-loops)
3.  now G has exactly two vertices u, v:
        return (label(u), label(v))

Each contraction merges the two endpoints of a random edge into one super-vertex, keeping multi-edges (they represent “how strongly connected” the groups are) but deleting loops. When only two super-vertices remain, their labels are the two sides of the cut. Complexity O(n2)O(n^2) per run.

Analysis

Fix a particular minimum cut CminC_{\min} of size kk (assume for simplicity it is unique). The algorithm returns CminC_{\min} iff it never contracts one of its kk edges.

Key fact — minimum degree. Every vertex has degree k\ge k (otherwise the single-vertex cut around it would be smaller than kk). Hence

E=12vdeg(v)nk2.|E| = \frac{1}{2}\sum_v \deg(v) \ge \frac{nk}{2}.

Let EiE_i be the event “CminC_{\min} has survived the first ii contractions”, and let G/FiG/F_i be the graph after contracting the edges FiF_i of the first ii steps. The same degree argument on G/FiG/F_i (which has nin - i super-vertices) gives

E(G/Fi)(ni)k2.|E(G/F_i)| \ge \frac{(n-i)\,k}{2}.

Step 1 survives: we must avoid the kk cut-edges out of nk/2\ge nk/2 edges,

Pr[E1]=EkE=1kE12n.\Pr[E_1] = \frac{|E| - k}{|E|} = 1 - \frac{k}{|E|} \ge 1 - \frac{2}{n}.

Step ii survives, given the cut survived so far:

Pr ⁣[Ei  |  1ji1Ej]=E(G/Fi1)kE(G/Fi1)12ni+1.\Pr\!\left[E_i \;\middle|\; \bigcap_{1 \le j \le i-1} E_j\right] = \frac{|E(G/F_{i-1})| - k}{|E(G/F_{i-1})|} \ge 1 - \frac{2}{\,n - i + 1\,}.

The cut survives all n2n-2 contractions (telescoping product):

Pr ⁣[jEj]1jn2(12nj+1)=1jn2nj1nj+1==2n(n1)>2n2.\Pr\!\left[\bigcap_j E_j\right] \ge \prod_{1 \le j \le n-2}\left(1 - \frac{2}{n-j+1}\right) = \prod_{1 \le j \le n-2} \frac{n - j - 1}{n - j + 1} = \cdots = \frac{2}{n(n-1)} > \frac{2}{n^2}.

One run finds a fixed minimum cut with probability >2/n2> 2/n^2. That looks tiny, but it is polynomially small, so a polynomial number of independent runs makes failure exponentially small.

Amplification by repetition. Failure of one run is 12/n2\le 1 - 2/n^2, so after tt independent runs (keeping the best cut found),

Pr[all fail](12n2)te2t/n2.\Pr[\text{all fail}] \le \left(1 - \frac{2}{n^2}\right)^{t} \le e^{-2t/n^2}.

Taking t=n22lognt = \tfrac{n^2}{2}\log n gives error elogn=1n=O(1/n)\le e^{-\log n} = \dfrac{1}{n} = O(1/n).

Punchline. A laughably simple “keep merging random edges” routine has a guaranteed good success probability per run, and repetition turns that into high confidence. (Later improvements — Karger–Stein — cut the time dramatically by recognizing that early contractions are safe and only the late ones are risky.)


4. Randomized QuickSort (RQS)

The algorithm

RQS(A):
  if A = {b}: return b
  else:
    pick pivot b ∈_R A          (uniformly random)
    S< = { a ∈ A : a < b }
    S> = { a ∈ A : a > b }
    return ( RQS(S<), b, RQS(S>) )

Assume the elements are distinct. The cost = number of comparisons, which depends entirely on the random pivot choices:

The point: any split that is “not too extreme” already gives O(nlogn)O(n\log n). With a random pivot, balanced-enough splits are the typical case. Let’s prove the expected cost is O(nlogn)O(n \log n).

Analysis via indicator variables

Let s1<s2<<sns_1 < s_2 < \cdots < s_n be the sorted output. For a particular computation CC and a pair i<ji < j, define the indicator

Xij(C)={1if si and sj are compared during C,0otherwise.X_{ij}(C) = \begin{cases} 1 & \text{if } s_i \text{ and } s_j \text{ are compared during } C, \\ 0 & \text{otherwise.}\end{cases}

Total comparisons: T(C)=1in1j>iXij(C)T(C) = \displaystyle\sum_{1 \le i \le n-1}\sum_{j > i} X_{ij}(C). By linearity of expectation,

E[T]=i<jE[Xij]=i<jpij,pij:=Pr[si,sj are compared].E[T] = \sum_{i < j} E[X_{ij}] = \sum_{i<j} p_{ij}, \qquad p_{ij} := \Pr[s_i, s_j \text{ are compared}].

(The crucial move: E[Xij]=1pij+0(1pij)=pijE[X_{ij}] = 1 \cdot p_{ij} + 0 \cdot (1 - p_{ij}) = p_{ij}. Linearity lets us add up these probabilities even though the XijX_{ij} are highly dependent.)

Computing pijp_{ij} — the key combinatorial insight

Consider the set {si,si+1,,sj}\{s_i, s_{i+1}, \dots, s_j\} of ji+1j - i + 1 consecutive elements.

sis_i and sjs_j are compared if and only if one of them is the first pivot chosen from this whole set.

Why: if some middle element rr with si<r<sjs_i < r < s_j is picked first, it separates sis_i and sjs_j into different sub-arrays, and they never meet again. They are compared only if sis_i or sjs_j itself is the first pivot among the ji+1j-i+1 candidates. Since the first pivot in that set is uniform over all ji+1j-i+1 of them, exactly 22 of those choices (sis_i or sjs_j) cause a comparison:

pij=2ji+1.p_{ij} = \frac{2}{\,j - i + 1\,}.

Summing up

E[T]=i=1n1j>i2ji+1=i=1n1  2kni+12k    2nHn2nlnn.E[T] = \sum_{i=1}^{n-1} \sum_{j > i} \frac{2}{j - i + 1} = \sum_{i=1}^{n-1}\;\sum_{2 \le k \le n-i+1} \frac{2}{k} \;\le\; 2 n H_n \approx 2 n \ln n.

(Here k=ji+1k = j - i + 1 ranges over 2,3,2, 3, \dots, and Hn=k=1n1/klnnH_n = \sum_{k=1}^n 1/k \approx \ln n is the harmonic number.)

Punchline. Expected 2nlnn\approx 2n\ln n comparisons. The art is: (1) charge cost to pairs, (2) use linearity of expectation to ignore dependencies, (3) reduce each pair’s probability to a clean “who is picked first” question. This indicator-plus-linearity pattern is one of the most reused tools in the whole course.


5. Freivalds’ test: is AB=CAB = C?

The problem

Given three n×nn \times n matrices A,B,CA, B, C, decide whether AB=CAB = C. Recomputing ABAB costs O(n2.37)O(n^{2.37\dots}) (or naively O(n3)O(n^3)). Freivalds (1977) verifies a claimed product in only O(n2)O(n^2).

The algorithm

  1. Pick a random vector xR{0,1}nx \in_R \{0,1\}^n.
  2. Check whether A(Bx)=CxA(Bx) = Cx.

Why O(n2)O(n^2): never form the matrix product. Compute BxBx first (a matrix–vector product, O(n2)O(n^2)), then A(Bx)A(Bx) (O(n2)O(n^2)), and CxCx (O(n2)O(n^2)). Three cheap matrix–vector multiplies.

Correctness

Error probability (the {0,1}\{0,1\} version)

Let D=ABC0D = AB - C \ne 0, so some entry Dij0D_{ij} \ne 0. Look at coordinate ii of y=Dxy = Dx:

yi=kDikxk=Dijxj+kjDikxk.y_i = \sum_{k} D_{ik}\, x_k = D_{ij}\, x_j + \sum_{k \ne j} D_{ik}\, x_k.

Principle of deferred decisions: fix all coordinates xkx_k (kjk \ne j) first. Then yi=0y_i = 0 forces exactly one value of xjx_j:

xj=kjDikxkDij.x_j = -\frac{\sum_{k \ne j} D_{ik}\, x_k}{D_{ij}}.

There is at most one such value, and xjx_j is a fair coin over {0,1}\{0,1\}, so it equals that value with probability 1/2\le 1/2:

Pr[error]Pr[yi=0]12.\Pr[\text{error}] \le \Pr[y_i = 0] \le \frac{1}{2}.

Repeating with independent random xx drives the error to 2t2^{-t}.

Real-valued version (Vandermonde / polynomial view)

Instead of a 0/10/1 vector, pick a random real rRRr \in_R \mathbb{R} and set x=(1,r,r2,,rn1)Tx = (1, r, r^2, \dots, r^{n-1})^T. Then coordinate ii becomes a polynomial in rr:

yi=pi(r)=k=0n1Dikrk,degpin1.y_i = p_i(r) = \sum_{k=0}^{n-1} D_{ik}\, r^k, \qquad \deg p_i \le n-1.

If D0D \ne 0, some pip_i is a nonzero polynomial of degree n1\le n-1, hence has at most n1n-1 roots. So

Pr[error]Pr[r is a root of pi]=n1R,\Pr[\text{error}] \le \Pr[r \text{ is a root of } p_i] = \frac{n-1}{|R|},

where we draw rr from a finite set RRR \subseteq \mathbb{R}. This is a baby case of the Schwartz–Zippel lemma.

Punchline. Verifying is cheaper than computing. A single random vector “probes” the matrix difference, and a nonzero difference is almost surely exposed. This is the prototypical fingerprinting of a linear-algebra identity.


6. “Derandomizing” AB=CAB = C for integer matrices

Can we remove the randomness entirely? For integer-coefficient matrices, yes — by choosing one cleverly large evaluation point instead of a random one.

Cauchy’s root bound (1829)

Theorem (Cauchy). Let P(x)=akxk++a1x+a0P(x) = a_k x^k + \dots + a_1 x + a_0 be a real polynomial. If xx is a root of PP, then

x<1+Aak,A=max0ikai.|x| < 1 + \frac{A}{|a_k|}, \qquad A = \max_{0 \le i \le k} |a_i|.

In words: all roots of a polynomial live inside a disk whose radius is controlled by its coefficients. So if we evaluate at a point bigger than this bound, we are guaranteed not to be sitting on a root — unless the polynomial is identically zero.

The derandomized check

The row polynomials pi(r)=kDikrkp_i(r) = \sum_k D_{ik} r^k of D=ABCD = AB - C have integer coefficients we can bound. If every entry of A,B,CA, B, C is bounded by

cmax=max{aij,bij,cij},c_{\max} = \max\{|a_{ij}|, |b_{ij}|, |c_{ij}|\},

then each entry of D=ABCD = AB - C is at most ncmax2+cmaxn\,c_{\max}^2 + c_{\max} in absolute value (sum of nn products, each cmax2\le c_{\max}^2, plus one subtracted entry cmax\le c_{\max}). Plugging into Cauchy’s bound, any real root has magnitude <1+ncmax2+cmax< 1 + n c_{\max}^2 + c_{\max}. So pick

α=ncmax2+cmax+1,rα,x=(1,r,,rn1)T,\alpha = n\,c_{\max}^2 + c_{\max} + 1, \qquad r \leftarrow \alpha, \qquad x = (1, r, \dots, r^{n-1})^T,

and check ABx=?CxABx \stackrel{?}{=} Cx deterministically. Because α\alpha exceeds every possible root, the only way Dx=0Dx = 0 is D=0D = 0.

Punchline. Randomness was only used to “dodge the roots”. Once we can bound where the roots are, a single well-chosen point dodges them for free. This is the spirit of derandomization — the topic of the last block of the course.


7. Nondeterministic matrix multiplication

Now turn the verifier into a way to certify a guessed product — i.e. put matrix multiplication into a nondeterministic setting.

Over Q\mathbb{Q}

Over R\mathbb{R}

Complexity: the vectors xix_i are Vandermonde, so BxiBx_i and CxiCx_i can be batched by divide-and-conquer in O(n2log2n)O(n^2 \log^2 n), or with the FFT in O(n2logn)O(n^2 \log n).

Caveat (!!): once you compute A(Bx)A(Bx), the inner result BxBx no longer has the nice Vandermonde “good form”, so you cannot recursively exploit the structure on the outer multiply. The trick has to be applied carefully.

The bilinear form trick

Fold both sides into a single quadratic form. Since D=ABCD = AB - C:

D=0    xTDx=0 for enough x    (xTA)(Bx)=?xTCx.D = 0 \iff x^T D x = 0 \text{ for enough } x \iff (x^T A)(Bx) \stackrel{?}{=} x^T C x.

Corollary. Let DD be a real n×nn \times n matrix, let r1,,r2n1r_1, \dots, r_{2n-1} be distinct reals, and xi=(1,ri,,rin1)Tx_i = (1, r_i, \dots, r_i^{n-1})^T. Then

D=0    i:  xiTDxi=0.D = 0 \iff \forall i:\; x_i^T D x_i = 0.

Why 2n12n-1 points? The scalar xiTDxi=a,bDabria+bx_i^T D x_i = \sum_{a,b} D_{ab}\, r_i^{a+b} is a polynomial in rir_i of degree at most 2n22n - 2. If D0D \ne 0 this polynomial is nonzero, hence has at most 2n22n-2 roots — so checking 2n12n-1 distinct points is enough to be sure.

Theorem. Multiplication of real matrices can be realized on an N-RealRAM (nondeterministic real RAM) using O(n2logn)O(n^2 \log n) (resp. O(n2log2n)O(n^2 \log^2 n)) algebraic operations.

Punchline. “Guess the answer, then verify it cheaply” — the verifier from §5–6 is exactly the certificate-checker that places the problem in a nondeterministic class.


8. Nondeterministic multiplication over Z\mathbb{Z} and Zp\mathbb{Z}_p

Why move to modular arithmetic

The evaluation points blow up: rn1r^{n-1} with r2n1r \approx 2n-1 is a number (2n1)n1(2n-1)^{n-1}, i.e. about O(nlogn)O(n \log n) bits long. Arithmetic on such giant integers is expensive. Fix: work modulo a prime pp in the field Zp\mathbb{Z}_p, keeping numbers small.

Two subtleties arise:

Lemma. Let DD be an integer n×nn \times n matrix with maxdijδ\max|d_{ij}| \le \delta. Let Zp\mathbb{Z}_p be a field with p>max{δ,2n1}p > \max\{\delta,\, 2n-1\}. Let r1,,r2n1<pr_1, \dots, r_{2n-1} < p be distinct, and xi=(1,ri,,rin1)Tx_i = (1, r_i, \dots, r_i^{n-1})^T. Then

D=0  (in Z)    i:  xiTDxi0  (in Zp).D = 0 \;\text{(in } \mathbb{Z}) \iff \forall i:\; x_i^T D x_i \equiv 0 \;\text{(in } \mathbb{Z}_p).

Choosing pp larger than the entries (δ\delta) and larger than the degree (2n12n-1) prevents both spurious roots and overflow-collapse, so testing over Zp\mathbb{Z}_p faithfully decides the integer question.

The sign problem when guessing CC

When we guess CC nondeterministically we must handle signs. Split each matrix into a positive-entry part and a negative-entry part, A=A++AA = A^+ + A^-, B=B++BB = B^+ + B^-:

(A++A)(B++B)=A+B++ABC++AB++A+BC.(A^+ + A^-)(B^+ + B^-) = \underbrace{A^+B^+ + A^-B^-}_{C^+} + \underbrace{A^-B^+ + A^+B^-}_{C^-}.

This costs 4 multiplications instead of 1, but each now has consistent signs.

How to find a suitable prime pp

Complexity of the whole nondeterministic multiplication:


9. Nondeterministic primality verification (Pratt certificates)

The previous section needed to certify that a number pp is prime. Pratt’s theorem says this can always be done with a short, checkable proof.

Theorem (Pratt). Every prime has a short certificate.

The idea

If pp is prime, then Zp\mathbb{Z}_p^* is cyclic and has a generator xx of order p1p - 1. Being a generator (a primitive root) is exactly the witness of primality, and it can be checked by a few modular exponentiations.

The axiomatic proof system

Triples (p,x,a)(p, x, a) mean “so far we have verified that the order of xx is a multiple of aa”:

We prove three things:

  1. pp is prime     \iff pp is a theorem of the system.
  2. The proof of primality of pp has O(logp)O(\log p) lines.
  3. Verifying a proof on a unit-cost RAM costs O(log3ploglogp)O(\log^3 p \cdot \log\log p) operations.

(1 ⇒) If pp is prime, then pp is a theorem

Let xx be a generator of Zp\mathbb{Z}_p^* and p1=q1q2qkp - 1 = q_1 q_2 \cdots q_k the factorization into primes. Build the proof by induction:

Each R1R_1 step confirms x(p1)/qi1x^{(p-1)/q_i} \ne 1, certifying that the order of xx is divisible by qiq_i; together they force the order to be exactly p1p-1, so xx really is a generator.

(1 ⇐) If pp is a theorem, then pp is prime

By contradiction. Suppose pp is not prime but is a theorem. Then Zp\mathbb{Z}_p has no generator. The last proof line must be (p,x,p1)p(p, x, p-1) \mapsto p with xp11(modp)x^{p-1} \equiv 1 \pmod p. Since xx is not a generator, xj1(modp)x^j \equiv 1 \pmod p for some j<p1j < p-1 with j(p1)j \mid (p-1). But to reach (p,x,p1)(p,x,p-1) from (p,x,1)(p,x,1) the proof had to apply R1R_1, which requires x(p1)/qi1x^{(p-1)/q_i} \ne 1 at each step — contradicting that the true order jj is a proper divisor of p1p-1. Hence pp must be prime.

(2) The proof has at most 4logp\lceil 4 \log p \rceil lines

By induction.

2+k+1uk(4logqu4)<2+k+4logq1++logqk4k<4logp4.2 + k + \sum_{1 \le u \le k}(\lfloor 4 \log q_u\rfloor - 4) < 2 + k + 4\lfloor \log q_1 + \cdots + \log q_k\rfloor - 4k < \lfloor 4 \log p\rfloor - 4.

(using logq1++logqk=log(q1qk)=log(p1)<logp\log q_1 + \cdots + \log q_k = \log(q_1 \cdots q_k) = \log(p-1) < \log p). Adding the 55 base lines for the 2,32,3 cases:

length<4logp4+54logp.\text{length} < \lfloor 4 \log p\rfloor - 4 + 5 \le \lceil 4 \log p\rceil.

So the certificate is logarithmic in the size of pp — genuinely short.

(3) Verifying the proof is cheap

The only nontrivial operation is computing xbmodpx^b \bmod p, done deterministically by repeated squaring:

Every operation keeps numbers to O(logp)O(\log p) bits, and there are O(log2p)O(\log^2 p) multiplications/squarings. Multiplying logp\log p-bit numbers costs O(logploglogp)O(\log p \log\log p) via the Schönhage–Strassen algorithm. Total: O(log3ploglogp)O(\log^3 p \cdot \log\log p) operations.

Corollary

PrimesNPcoNP.\mathrm{Primes} \in \mathrm{NP} \cap \mathrm{coNP}.

Historical note. This NPcoNP\mathrm{NP} \cap \mathrm{coNP} result long predates the unconditional AKS algorithm (2002), which finally showed PrimesP\mathrm{Primes} \in \mathrm{P} deterministically. But the randomized tests (Miller–Rabin, Solovay–Strassen) remain the practical choice — another instance of randomness winning on efficiency.


Recurring themes to carry into the rest of the course

Theme Where it appeared
Fingerprinting (test a big identity via a small random/modular projection) §1 primes, §5–8 matrix product
One-sided error (yes-instances never lie; no-instances err with small prob.) §1, §5 — these are RP/coRP-style algorithms
Probability amplification by independent repetition §1, §3 min-cut, §5 Freivalds
Indicator variables + linearity of expectation §4 QuickSort
Polynomial / Schwartz–Zippel (“a nonzero low-degree polynomial has few roots”) §5–8
Derandomization (bound the bad set, then pick deterministically) §6 Cauchy, §7
Guess-and-verify / short certificates (nondeterminism) §7–9, Pratt

The single sentence that ties them all together:

A small amount of randomness lets us probe a large object so that any “flaw” is almost surely exposed — and when we can describe where the flaws can hide, we can often remove the randomness altogether.

fingerprintingmin-cutquicksortfreivaldsderandomizationprimality

← Back to Randomized Algorithms