3.7.19 · HinglishAlgorithm Paradigms

Randomized algorithms — Las Vegas, Monte Carlo

2,105 words10 min readRead in English

3.7.19 · Coding › Algorithm Paradigms


Ye do classes actually hain kya?

Figure — Randomized algorithms — Las Vegas, Monte Carlo

Las Vegas algorithm ka analysis kaise karte hain — expected time nikalna

Sabse simple Las Vegas idea lo: tab tak random attempt retry karte raho jab tak succeed na ho. Maano har independent attempt probability se succeed karti hai aur kaam karta hai.

Geometric distribution kyun? Har trial ek independent coin flip hai — "success" prob se. Trials ki sankhya pehli success tak geometric hoti hai.

ki derivation scratch se. Maano .

  • Prob se: hum trial 1 par succeed karte hain, cost = trial.
  • Prob se: hum fail karte hain (cost 1 trial) aur wapas start par hain, toh extra trials expected hain.

Ye step kyun? Process memoryless hai: failure ke baad situation bilkul start jaisi hai, isliye remaining expectation phir se hai. Ye self-reference algebraically solve karne deta hai.

Expand karo:

Toh expected total work .


Monte Carlo algorithm ka analysis kaise karte hain — confidence boost karna

Maano EK run probability se galat hoti hai (aur runs independent hain). Ise baar chalao.

One-sided error (e.g. test jo "yes" kahta hai galat ho sakta hai, lekin "no" hamesha pakka hai): "yes" tabhi lo jab saare runs "yes" kahein. Chance ki saare galat "yes" hain:

Ye multiply kyun hota hai? Independent runs ⇒ joint probability product hoti hai. Har galat run ki prob hai, toh simultaneous galat runs ki prob hai, jo exponentially shrink hoti hai.

Error ko target se neeche karne ke liye:


Dono ke beech convert karna


Worked examples


Common mistakes (Steel-man + fix)


Flashcards

Las Vegas algorithm — kya random hai?
Running time (ek random variable); output hamesha correct hota hai.
Monte Carlo algorithm — kya random hai?
Answer ki correctness (prob ≤ q se galat ho sakta hai); running time bounded/fixed hota hai.
Pehli success tak expected trials jab har ek prob p se succeed kare
(geometric distribution).
Retry loop ke liye E[N] derive karo
(memorylessness).
k independent Monte Carlo runs ke baad error (one-sided, har ek ≤ q galat)
.
Error ≤ ε ke liye kitne runs k chahiye
.
Randomized QuickSort ke expected comparisons
, se.
Randomized QuickSort mein do ranks i<j ke compare hone ki probability
.
Karger's min-cut ki single-run success prob
.
Karger's min-cut one-sided ya two-sided error hai?
One-sided — ye ek real cut report karta hai, toh sahi minimum se kabhi chhota return nahi karta, sirf shayad bada.
Las Vegas → Monte Carlo kaise convert karein
Time budget ke baad cut off karo; finish na hone par guess output karo (Markov's inequality error bound karta hai).
Monte Carlo → Las Vegas kaise convert karein
Sirf tab agar answers cheaply verifiable hain: verification pass hone tak repeat karo.
Repeated Karger failure bound karne ke liye inequality
.

Recall Feynman: 12-saal ke bachche ko samjhao

Ek maze imagine karo. Las Vegas robot HAMESHA exit dhundh leta hai — lekin kabhi-kabhi woh bhatakta hai aur zyada time leta hai, kabhi-kabhi fast hota hai. Monte Carlo robot HAMESHA exactly 5 minute mein ruk jaata hai — lekin agar abhi tak exit nahi mila, toh woh bas guess karta hai darwaza kahan hai, aur galat ho sakta hai. Coins kyun use karein? Kyun ki ek bully jo tumhare robot se nafrat karta hai ek aisa maze bana sakta hai jo ek predictable robot ko har baar trap kare. Agar tumhara robot randomly mude, toh bully use trap nahi kar sakta — buri kismat rare ho jaati hai pakki nahi. Aur agar tum darr rahe ho ki Monte Carlo ne galat guess kiya, toh ise kai baar chalao aur vote karo — chance ki SAARE galat hain bahut fast shrink hota hai (aadha, aadha, aadha...).

Connections

  • Probability and Expectation — linearity of expectation, geometric distribution.
  • Markov's Inequality aur Chernoff Bounds — cut-off aur majority voting ke liye tail bounds.
  • QuickSort / Quickselect — classic Las Vegas examples.
  • Primality Testing — Miller–Rabin as Monte Carlo.
  • Min-Cut and Max-Flow — Karger's contraction algorithm.
  • Amortized vs Expected Analysis — coins par expectation, inputs par nahi.

Concept Map

defeats

splits into

splits into

always

random

bounded

maybe wrong

reduced by

types

analysed via

gives

Randomized algorithm

Adversary worst-case

Las Vegas

Monte Carlo

Correct answer

Running time varies

Fixed running time

Error prob 1-p

Repetition

One-sided or two-sided

Geometric distribution

E of N equals 1 over p