Take the simplest Las Vegas idea: keep retrying a random attempt until it succeeds.
Suppose each independent attempt succeeds with probability p and costs c work.
WHY a geometric distribution? Each trial is an independent coin flip — "success" with prob p.
The number of trials N until first success is geometric.
Derivation of E[N] from scratch. Let E=E[N].
With prob p: we succeed on trial 1, cost = 1 trial.
With prob 1−p: we fail (cost 1 trial) and are back to the start, so an extraE trials expected.
E=p⋅1+(1−p)(1+E)
Why this step? The process is memoryless: after a failure the situation is identical to the start,
so the remaining expectation is again E. This self-reference lets us solve algebraically.
Suppose ONE run is wrong with probability ≤q (and runs are independent). Run it k times.
One-sided error (e.g. test that says "yes" might be wrong, but "no" is always certain):
take "yes" only if allk runs say "yes". The chance all k are wrongly "yes":
Pr[still wrong]≤qk
Why this multiplies? Independent runs ⇒ joint probability is the product. Each wrong run has
prob ≤q, so k simultaneous wrong runs has prob ≤qk, which shrinks exponentially.
To force error below a target ε:
qk≤ε⟹k≥lnqlnε=ln(1/q)ln(1/ε)
The running time (a random variable); the output is always correct.
Monte Carlo algorithm — what is random?
The correctness of the answer (may be wrong with prob ≤ q); the running time is bounded/fixed.
Expected number of trials until first success when each succeeds w.p. p
1/p (geometric distribution).
Derive E[N] for a retry loop
E=p⋅1+(1−p)(1+E)⇒E=1/p (memorylessness).
Error after k independent Monte Carlo runs (one-sided, each wrong ≤ q)
≤qk.
How many runs k to get error ≤ ε
k≥ln(1/ε)/ln(1/q).
Expected comparisons of randomized QuickSort
O(nlogn), from ∑i<jj−i+12.
Probability two ranks i<j are compared in randomized QuickSort
j−i+12.
Single-run success prob of Karger's min-cut
≥1/(2n).
Is Karger's min-cut one-sided or two-sided error?
One-sided — it reports a real cut, so it never returns smaller than the true minimum, only possibly larger.
How to convert Las Vegas → Monte Carlo
Cut off after a time budget; output a guess if not finished (Markov's inequality bounds error).
How to convert Monte Carlo → Las Vegas
Only if answers are cheaply verifiable: repeat until verification passes.
Inequality used to bound repeated Karger failure
1−x≤e−x.
Recall Feynman: explain to a 12-year-old
Imagine a maze. A Las Vegas robot ALWAYS finds the exit — but sometimes it wanders and takes
longer, sometimes it's fast. A Monte Carlo robot ALWAYS stops after exactly 5 minutes — but if
it didn't reach the exit yet, it just guesses where the door is, and might be wrong. Why use coins
at all? Because a bully who hates your robot can build a maze that traps a predictable robot every
time. If your robot turns randomly, the bully can't trap it — bad luck becomes rare instead of certain.
And if you're worried Monte Carlo guessed wrong, just run it many times and vote — the chance ALL of
them are wrong shrinks super fast (halve, halve, halve...).
Dekho, randomized algorithm matlab algorithm ke andar hum coin flip (random number) use karte hain.
Iski wajah se same input par bhi algorithm har baar thoda alag chal sakta hai. Do flavour hote hain.
Las Vegas wala hamesha sahi answer deta hai, bas time random hota hai — kabhi jaldi, kabhi der,
par answer galat kabhi nahi. Monte Carlo wala fixed time mein khatam ho jata hai, par answer kabhi
galat ho sakta hai (chhoti si probability se). Yaad rakhne ka trick: Las Vegas = aLways correct,
Monte Carlo = Maybe wrong par time pe.
Randomness kyun? Kyunki agar tumhara algorithm predictable (deterministic) hai, to ek dushman aisa
worst-case input bana sakta hai jo har baar tumhe phasa de — jaise sorted array par normal QuickSort
O(n2) ho jata hai. Lekin agar tum pivot random choose karo, to dushman ko pata hi nahi ki tum
kya chunoge, isliye bura case rare ho jata hai, guaranteed nahi. Expected time nikalne ka formula
simple hai: agar har try success hone ka chance p hai, to expected tries =1/p (geometric distribution,
memoryless property se derive hota hai).
Monte Carlo mein agar ek run galat hone ka chance q hai, to k baar chala kar error qk tak gir
jata hai — yeh exponentially fast girta hai. Jaise q=1/2 par sirf 20 runs se error 10−6 se kam!
Isiliye Miller-Rabin primality test bahut reliable hai. Ek important baat: Karger ka min-cut algorithm
one-sided error wala hai — wo jo bhi cut deta hai woh ek asli cut hota hai, isliye kabhi true
minimum se chhota nahi ho sakta, sirf bada ho sakta hai. Isiliye hum bahut runs ka sabse chhota cut
rakhte hain. Dhyan rahe: one-sided error mein "certain" wala answer lo, two-sided error mein majority
vote lo, last run ka answer mat uthao.