Probabilistic Analysis and Randomized Algorithms

Algorithms · Advanced Placement

Probabilistic Analysis and Randomized Algorithms: the big picture

Some algorithms behave badly on particular inputs but well on most, and probabilistic analysis makes that precise. Average-case analysis assumes a distribution over inputs and computes the expected running time under it, which is informative only to the extent that the assumed distribution matches reality. A randomized algorithm instead makes random choices during execution, so its running time is a random variable even for a fixed input; this is strictly stronger, because no adversary choosing the input can force bad behaviour. Indicator random variables provide the standard technique: define a variable that is one when an event occurs and zero otherwise, so its expectation is the probability of the event, and linearity of expectation then converts a complicated count into a sum of probabilities without any independence assumption. The hiring problem, the birthday paradox, balls and bins, and streaks all yield to this method. Randomized quicksort chooses its pivot at random, which gives expected linearithmic time on every input. Randomly permuting an input before processing, by the Fisher–Yates shuffle, converts an average-case guarantee into a randomized one, and proving that the shuffle produces each permutation with equal probability is itself an instructive exercise.

Indicator Variables, Expectation & the Birthday Problem

Linearity of expectation is the most useful tool in this part of the subject, because it holds whether or not the events are independent. The technique is always the same: define an indicator variable that is one when some event happens, note that its expectation is just the probability of that event, and then sum. Complicated counting problems collapse to a sum of probabilities. The birthday problem, hash collisions and balls-in-bins all fall out this way, and the counterintuitive answers are what make the technique worth trusting.

{{illustration:algos-l04-f1}}

{{illustration:algos-l04-f2}}

Key relations

- Indicator variables and linearity. E[X_A] = Pr{A} ; E[∑ᵢ Xᵢ] = ∑ᵢ E[Xᵢ] ; Pr{collision} ≈ 1 − e^(−n²/2d)

Randomized Algorithms, Fisher–Yates & Expected Bounds

An average-case guarantee depends on your inputs being typical; a randomized guarantee does not. That is the key distinction. Randomized quicksort picks its pivot at random, so no adversary who knows your code can construct a quadratic input — the bad cases still exist but their probability is negligible. Randomly permuting the input first achieves the same thing, and the Fisher–Yates shuffle does it correctly in linear time. The naive shuffle that swaps each position with any random position does not produce a uniform permutation, which is a well-documented and often-repeated bug.

{{illustration:algos-l04-f3}}

{{illustration:algos-l04-f4}}

Key relations

- Randomization and the shuffle. E[T(n)] = Θ(nlog n) ∀ inputs ; Pr{σ} = 1/(n!) ∀ σ