Unit 5 of 6 · about 4 hours · derive, implement
Randomized correctness, expected cost and selection
Random pivots turn a quadratic worst case into linear expected time. The guarantee only means something if you can say what is expected, over what randomness, and with what tail.
You'll be able to
- Distinguish Las Vegas from Monte Carlo algorithms and state what is random in each: running time versus answer.
- Derive linear expected cost for randomized selection with indicator variables or a good-pivot recurrence, naming the exact comparison event counted.
- Separate expected cost from high-probability guarantees, and write the seed and trial contract that makes an empirical check reproducible.
Covers
Read
In this order, only the parts listed
0 of 3 done
- Nuts and Bolts: randomized analysis methodAlgorithms lecture notes - randomized algorithms
2.2 Deterministic vs. Randomized Algorithms, 2.3 Back to Nuts and Bolts and 2.4 Finding All Matches; 2.6 Recursive Analysis (the good-pivot bound) and 2.7 Iterative Analysis (indicator variables); Exercise 5 on QuickSelect
Exercise 4, reservoir sampling, belongs to the next unit. Skip 2.8 Masochistic Analysis.
- Tail Inequalities: expected versus high-probabilityAlgorithms lecture notes - randomized algorithms
4.1 Markov's Inequality, 4.3 Chebyshev's Inequality, 4.5 Chernoff Bounds, and 4.7 to 4.8 Back to Treaps as the expected-versus-high-probability example
- Expectation, linearity, and deviation boundsMIT 6.042J - probability and randomized algorithms
Session 32: Chapter 18.4 Great Expectations and 18.5 Linearity of Expectation. Session 33: Chapter 19.1 Markov's Theorem, 19.2 Chebyshev's Theorem and 19.3 Properties of Variance.
Focus on
- Las Vegas: the answer is always right and the running time is random, as with random-pivot quickselect and rejection sampling. Monte Carlo: the running time is fixed and the answer may be wrong with bounded probability, as with Miller-Rabin. Fisher-Yates and reservoir sampling fit neither: they run in fixed time and their job is to produce a distribution.
- Linearity of expectation needs no independence, so indicator variables over pivot comparisons sum directly to the expected count.
- This unit owns the expected-cost derivation for random pivots. Deterministic median of medians lives in the previous unit and partition code lives in the partition unit.
- Expected cost is a mean; high-probability bounds tail risk through Markov, Chebyshev, or Chernoff. Treap depth is the worked example of the stronger claim.
Common mistakes · 3
- Reading expected as fast on every run. The tail exists, so report trial counts and seeds, and never promise a single-run bound from an expectation.
- Unseeded RNG inside tests. The suite passes until it does not; seed
random.Randomexplicitly and record the seed with the expected output. - Modulo bias in hand-rolled reduction: mapping two
rand7calls mod 10 favors some digits because 49 outcomes do not split evenly, so rejection keeps 1 to 40 and redraws 41 to 49.
Try it yourself
Closed-book, before checking anything
rand10 from rand7 with a random.Random(0).randint(1, 7) stub over 20,000 calls: mean rand7 calls 2.459 against theory . Then sum the pair indicators for the median of 1000 elements (): 3336.3 expected comparisons. Run random-pivot quickselect ( comparisons per partition) on shuffled range(1000) for 2000 seeded trials: the mean lands within about 50 of 3336, while single runs spread with a standard deviation near 980. Record sizes, seed, trials and theory with each run.Quiz yourself
Answer first, then mark it. Missed ones go to Review.
0 got · 0 missed
1.Is quickselect Las Vegas or Monte Carlo, and what is random about it?Answer
Las Vegas. It always returns the correct th element; only the number of comparisons varies with the random pivot choices.
2.Why does linearity of expectation apply to quickselect comparison indicators?Answer
Expectation of a sum equals the sum of expectations with no independence requirement, so dependent comparison events still sum directly.
3.What is the difference between expected treap depth and depth with high probability?Answer
Expected depth is a mean over random priorities. The high-probability bound says all but a tiny fraction of runs stay within , which needs a tail inequality rather than linearity alone.
4.What must an empirical check of a randomized claim record?Answer
Input sizes, seed, trial count, theory value, and tolerance. For example: 20000 trials at seed 0 give 2.459
rand7calls perrand10against theory 2.45.
Practice
- LeetCode 470: Implement Rand10() Using Rand7()
The examples call
rand101, 2 and 3 times. Test with a seededrand7stub and count meanrand7calls over 20,000 calls, expecting about 2.45.After submitting, read the DSA Handbook randomized-algorithms rejection section, which works the 2.45 figure from .
- Erickson lecture note 2, Exercise 5: QuickSelect analysis
Parts (a) to (d): expected-time recurrence, comparison probability, subarray probability, and linear expected time for any and .
Paper analysis, not judged code. Check part (b) against your pair-indicator sum from the reproduce step; the partition implementation belongs to the partition unit.
You're done when
LC 470 is accepted with a seeded stub test, and you can derive the 2.45 figure and the linear quickselect bound without notes.
Only when you can do this without notes.
Leads to
Notes
Resources · 2 for this unit, 3 more in the section
- MIT 6.042J - probability and randomized algorithmsMIT OpenCourseWare · support
Use the probability, expectation and concentration readings to derive sampling probabilities and distinguish expected from high-probability guarantees.
- Algorithms lecture notes - randomized algorithmsJeff Erickson · support
Use note 1 Discrete Probability, note 2 Nuts and Bolts (randomized quicksort, with QuickSelect in its exercises), note 4 Tail Inequalities and note 5 Hashing to derive expected costs, separate expected from high-probability guarantees, and analyze hashing behavior.
Also in this section
- DSA Handbook - recursion, backtracking and greedyHandbook Academy · primary
Study Part 7, Part 10 and the relevant foundations on recursion and probability. Write the state definition or greedy claim before opening code.
- AlgoMaster DSA - decomposition and greedy sliceAlgoMaster · required
Attempt N-Queens and Gas Station cold, then implement reservoir sampling and test its output distribution over fixed-seed trials. Add another judged problem only for a diagnosed weakness.
- Algorithms - recursion, backtracking and greedyJeff Erickson · required
Use the recursion, backtracking, divide-and-conquer and greedy chapters for recurrence derivations, proof templates and counterexamples.