3.7.19 · D4Algorithm Paradigms

Exercises — Randomized algorithms — Las Vegas, Monte Carlo

2,735 words12 min readBack to topic

Level 1 — Recognition

L1.1 — Classify the algorithm

Problem. For each algorithm, say whether it is Las Vegas (always correct, random time) or Monte Carlo (fixed time, maybe wrong): (a) Randomized QuickSort. (b) Miller–Rabin primality test. (c) Karger's min-cut. (d) Randomized Quickselect (returns the -th smallest).

Recall Solution

Recall the mnemonic: Las Vegas = correct always, time varies. Monte Carlo = on time, answer risky.

  • (a) Las Vegas. The output is a fully sorted array — never wrong. Only the number of comparisons is random. See QuickSort.
  • (b) Monte Carlo (one-sided). Fixed number of rounds; may wrongly call a composite "prime." See Primality Testing.
  • (c) Monte Carlo (one-sided). Fixed contraction procedure; may return a cut that is too large. See Min-Cut and Max-Flow.
  • (d) Las Vegas. Always returns the true -th smallest; only the running time is random. See Quickselect.

L1.2 — Where does the error live?

Problem. Fill in the blank. In a Las Vegas algorithm the random variable is the ==. In a Monte Carlo algorithm the random variable is the ==.

Recall Solution

Las Vegas: the random variable is the running time (correctness is guaranteed). Monte Carlo: the random variable is the correctness of the answer (time is fixed/bounded).

Running time is random ::: Las Vegas Answer is random ::: Monte Carlo


Level 2 — Application

L2.1 — Expected trials of a retry loop

Problem. A Las Vegas step retries an independent random attempt until it succeeds. Each attempt succeeds with probability and costs units of work. (a) Expected number of attempts ? (b) Expected total work?

Recall Solution

WHY geometric: each attempt is an independent coin flip with success prob , so the number of attempts until the first success is a geometric random variable, and . WHAT this means: on average you try 5 times before hitting a success.

WHY the total work is just : every attempt costs the same fixed units, and linearity of expectation (see Probability and Expectation) says — we can pull the constant cost out of the expectation, so we never need the full distribution of , only its mean.


L2.2 — Boosting a one-sided Monte Carlo test

Problem. One run of a one-sided Monte Carlo test is wrong with probability . Runs are independent, and we accept only if all runs indicate success. (a) Error after runs? (b) How many runs to push error below ?

Recall Solution

WHY multiply: independent runs ⇒ the joint "all wrong" probability is the product , which shrinks exponentially. (a) (b) Solve . Take the natural log of both sides: Here is the delicate part: since , we have (it is negative). Dividing an inequality by a negative number flips the direction of into . So (The last equality just rewrites the two negatives as the positive ratio .) Round up (you cannot do a fraction of a run): runs.


Level 3 — Analysis

L3.1 — Cutting off a Las Vegas run (LV → MC) with Markov

Problem. A Las Vegas algorithm has expected running time ms. You convert it to Monte Carlo by aborting after a budget of ms and guessing. (a) Using Markov's Inequality, bound the probability it fails to finish in time. (b) What budget guarantees failure ?

Recall Solution

WHY Markov: running time is non-negative, and we only know its mean — Markov's inequality is exactly the tool that bounds "how often a non-negative variable exceeds a threshold" using only the mean. Markov says . (a) With : So with a ms budget the converted Monte Carlo errs at most of the time. (b) We want . Solve for :


L3.2 — Karger's min-cut success probability

Problem. A single run of Karger's algorithm on an -node graph returns the true minimum cut with probability at least . For : (a) Single-run success probability? (b) Failure probability of a single run? (c) Using , how many runs guarantee failure ?

Recall Solution

. For : . (a) Single-run success . (b) Single-run failure . (c) WHY : it turns the awkward product into a clean exponential we can read off. With and : For : , so runs suffice.


Level 4 — Synthesis

L4.1 — QuickSort's harmonic sum

Problem. Randomized QuickSort's expected comparisons obey , where is the -th harmonic number. For : (a) Compute . (b) Give the bound . (c) Explain in one line why this is and not the deterministic worst case .

Recall Solution

WHY the harmonic sum appears: two ranks are compared iff one of them is the first pivot chosen among the elements between them — probability . Linearity of expectation (see Probability and Expectation) lets us add these per-pair probabilities, and the sum collapses into harmonic terms. (a) (b) So expected comparisons for . (c) Since grows logarithmically, in expectation on every input. The worst case needs a specific unlucky coin sequence, not a bad input — and the adversary cannot control your coins. Contrast with deterministic pivots in Amortized vs Expected Analysis.


L4.2 — MC → LV via verification

Problem. A Monte Carlo algorithm outputs a candidate answer in ms per run and is correct with probability . You can verify any candidate in ms. You build a Las Vegas algorithm: run, verify, repeat until verification passes. (a) Expected number of (run + verify) rounds? (b) Expected total time?

Recall Solution

WHY this becomes Las Vegas: each round we produce a candidate and check it. We stop only when the check passes, so the output is always correct — the only random thing is how many rounds we needed. That is precisely the Las Vegas retry loop with success prob . (a) (b) Each round costs ms:


Level 5 — Mastery

L5.1 — Two-sided error needs a majority vote (Chernoff)

Problem. A two-sided Monte Carlo algorithm answers a yes/no question correctly with probability per independent run (so it is wrong of the time, either direction). You run it times and take the majority answer. (a) Why can't you just take the last run's answer? (b) Using the Chernoff bound , find the smallest (odd) making the majority wrong with probability .

Recall Solution

(a) The last run is wrong of the time — a single run gives no confidence at all. Because the error is two-sided, you cannot trust any one output; you must aggregate. Majority voting lets independent correct runs outvote the wrong ones (see Chernoff Bounds). WHY the Chernoff/Hoeffding bound and NOT the one-sided trick: the trick works only when there is a certain side (e.g. Miller–Rabin's "composite" is never wrong), so a single unanimous risky answer is trustworthy. Here both answers can be wrong, so unanimity is meaningless — the right question is "how often does the majority of runs land on the wrong side?" That is a question about a sum of independent 0/1 votes deviating from its mean, and the tool built exactly for bounding such sum-deviations is the Chernoff/Hoeffding concentration inequality — see Chernoff Bounds. (b) Let . We need Take logs: , i.e. Round up to an odd integer (odd avoids ties in the vote): . Check: . ✓


L5.2 — Comparing an LV cutoff against native MC

Problem. You have a Las Vegas algorithm with ms and a native Monte Carlo algorithm that always runs in ms with error . Your deadline is a hard ms budget. (a) If you cut off the Las Vegas run at ms and guess, bound its failure by Markov. (b) Which of the two Monte Carlo options (cutoff-LV vs native-MC) has the smaller guaranteed error?

Recall Solution

(a) Markov on the non-negative time with , threshold : When the cutoff triggers we guess, so the cutoff-LV fails at most of the time. (b) Native MC has guaranteed error , which is smaller than the cutoff-LV's bound. So within the ms budget the native Monte Carlo is the safer choice — .

Cutoff-LV worst-case error ::: at most (Markov bound) Native-MC error ::: Better option under 200 ms ::: native Monte Carlo


Flashcards

for retry loop with success prob
One-sided MC error after runs
Runs to reach error (one-sided)
Markov bound on runtime
Two-sided error aggregation
majority vote (Chernoff), not last run
Karger repetitions for failure