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 // random bit vector Ξ² = A(BΞ±) // two matrix-vector products: O(nΒ²) Ξ³ = 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\]
βœ” Freivalds' Simulator Monte Carlo
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 // random pivot! m ← nut matching b // partition nuts around m: O(n) nuts_small, nuts_large ← partition(nuts, m) bolts_small, bolts_large ← partition(bolts, b) // partition bolts around b: O(n) // Total comparisons this step: 2nβˆ’1 NutsAndBolts(bolts_small, nuts_small) // recurse left NutsAndBolts(bolts_large, nuts_large) // recurse right
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.
πŸ”© Nuts & Bolts Simulator Las Vegas
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

AlgorithmTypeTimeGuaranteeKey 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.