Chapter 6 Β· Design & Analysis of Algorithms
Randomized
Algorithms
Let the algorithm flip coins. On a worst-case world, internal randomization can defeat adversarial inputs and unlock solutions that deterministic algorithms cannot match efficiently.
π² Monte Carlo β fast, small error
β
Las Vegas β always correct, fast in expectation
β Freivalds O(nΒ²)
π© Nuts & Bolts O(n log n) expected
π²
01 The Randomization Idea
"When one thinks about random processes in the context of computation, it is usually in one of two distinct ways. One view is to consider the world as behaving randomly β this is average-case analysis. A second view is to consider algorithms that behave randomly: the world provides the same worst-case input as always, but we allow the algorithm to make random decisions as it processes the input."
Monte Carlo Algorithms
Fast, small error probability
Always run in polynomial time. Return the correct answer with high probability. The error probability can be reduced arbitrarily by repetition.
Example: Freivalds' algorithm
Las Vegas Algorithms
Always correct, fast in expectation
Always return the correct answer. Running time is a random variable that is small in expectation (over the algorithm's own coin flips).
Example: Nuts & Bolts, Randomized QuickSort
Why randomize?
Efficient deterministic algorithms that always give the correct answer are a
special case of efficient randomized algorithms that only need to give the correct answer with high probability (Monte Carlo). They are also a special case of Las Vegas algorithms. Even in a worst-case world, an algorithm that performs its own "internal" randomization may be able to offset certain worst-case phenomena.
02 Freivalds' Verification of Matrix Multiplication
Given three nΓn matrices A, B, and C, decide whether AB = C. The naΓ―ve approach computes AB from scratch (same cost as matrix multiplication, O(nΒ³) or O(nΒ²Β·βΈ) via Strassen). Freivalds' idea achieves O(nΒ²) with only a 1/2 chance of error.
Algorithm β O(nΒ²) per round, error β€ 1/2
Freivalds(A, B, C)
Choose Ξ± β {0,1}βΏ uniformly at random
Ξ² = A(BΞ±)
Ξ³ = CΞ±
if Ξ² = Ξ³: return "AB = C"
else: return "AB β C"
Correctness
If AB = C, then Ξ² = A(BΞ±) = (AB)Ξ± = CΞ± = Ξ³ for any Ξ±. The algorithm always returns the correct answer in this case.
The interesting case is when AB β C:
Key Theorem
Error probability β€ 1/2
If AB β C, then Pr[A(BΞ±) = CΞ±] β€ 1/2 over a uniformly random Ξ± β {0,1}βΏ.
Proof β the flipping argument
Suppose AB β C but for some Ξ±, (ABβC)Ξ± = 0. Since ABβC β 0, it has some non-zero entry d at row i, column j.
Consider Ξ±' obtained by flipping the j-th bit of Ξ±. Then the i-th coordinate of (ABβC)Ξ±' differs from that of (ABβC)Ξ± by exactly d β 0. So (ABβC)Ξ±' β 0 β A(BΞ±') β CΞ±'.
This gives a bijection: for each "bad" Ξ± (where algorithm errs), there is a distinct "good" Ξ±' (where it doesn't). So at least half the bit vectors are "good" β error probability β€ 1/2. β
Reducing Error by Repetition
Run Freivalds k times with independent random vectors Ξ±β,β¦,Ξ±β. Return "AB = C" only if all k rounds agree. The probability of error is at most (1/2)α΅:
\[\Pr[\text{error after } k \text{ rounds}] \le \left(\frac{1}{2}\right)^k\]
Choose random Ξ±, compute Ξ²=A(BΞ±) and Ξ³=CΞ±, compare. If equal β "AB=C". Click to run.
Rounds run: 0 | Errors detected: 0 | Current error prob: β
Error probability upper bound
Dramatic speed-up
Strassen computes AB in O(nΒ²Β·βΈβ°β·). Freivalds
verifies AB=C in O(nΒ²) per round with error β€ 1/2. For k=30 rounds, error β€ 10β»βΉ and total time is 30Β·O(nΒ²) β still vastly faster than recomputing AB for large n.
03 Nuts and Bolts
We are given n bolts and n nuts of different sizes with a one-to-one correspondence. We cannot compare two bolts directly or two nuts directly β only a bolt against a nut. Find all matching pairs.
Constraint
Deterministic algorithms achieving O(n log n) worst-case exist but are "highly technical and quite involved." The randomized Las Vegas approach gives O(n log n)
expected time with a simple, elegant algorithm based on the divide-and-conquer paradigm.
The Algorithm β Randomized Pivot
Las Vegas Algorithm β O(n log n) expected
NutsAndBolts(bolts, nuts)
if |bolts| β€ 1: return
b β random bolt from bolts
m β nut matching b
nuts_small, nuts_large β partition(nuts, m)
bolts_small, bolts_large β partition(bolts, b)
NutsAndBolts(bolts_small, nuts_small)
NutsAndBolts(bolts_large, nuts_large)
Why nuts first, then bolts?
We pick a random
bolt b, find its matching nut m, then use m to partition all nuts (smaller/larger than m), and then use b to partition the remaining bolts. This two-pass partition is necessary because we can't compare bolts to bolts or nuts to nuts.
8 bolts and 8 nuts to match. Press Next Step to run one level of the randomized algorithm.
Comparisons: 0
Matches found: 0 / 8
Bolts (B)
Nuts (N)
Expected Time Analysis
Let T(n) = expected comparisons. Let T(n,j) = expected comparisons when the chosen bolt has rank j (is the j-th smallest). Two passes of partition: 2nβ1 comparisons.
\[T(n,j) = 2n-1 + T(j-1) + T(n-j)\]
\[T(n) = \frac{1}{n}\sum_{j=1}^{n} T(n,j) = 2n-1 + \frac{2}{n}\sum_{j=0}^{n-1}T(j)\]
The analysis proceeds by multiplying both sides by n, writing the same for nβ1, and subtracting:
Key recurrence after subtraction
nT(n) β (nβ1)T(nβ1) = 4nβ3 + 2T(nβ1)
Rearranged
T(n)/(n+1) = T(nβ1)/n + (4nβ3)/(n(n+1)) < T(nβ1)/n + 4/n
Telescoping
T(n)/(n+1) < 4/n + 4/(nβ1) + β¦ + 4/1 = 4Β·H(n) < 4(1 + ln n)
Conclusion
T(n) = O(n log n)
\[T(n) = O(n \log n)\]
Chapter Summary
| Algorithm | Type | Time | Guarantee | Key Trick |
| Freivalds |
Monte Carlo |
O(nΒ²) per round |
Error β€ (1/2)α΅ after k rounds |
Bit-flip bijection |
| Nuts & Bolts |
Las Vegas |
O(n log n) expected |
Always correct |
Random pivot + two-pass partition |
The fundamental trade-off
Monte Carlo trades correctness probability for speed. Las Vegas trades deterministic running time for expected running time. Both are strictly more powerful than deterministic algorithms in many settings β the randomness offsets adversarial worst-case inputs that would break any deterministic strategy.
Connection to sorting
Nuts & Bolts is essentially Randomized QuickSort with the twist that you can't directly compare two elements of the same type. The same analysis gives T(n) = O(n log n) expected β the harmonic series argument is identical to QuickSort's analysis.