Linearity of expectation — always try this first
regardless of dependence between the . This is the most useful fact in probabilistic problem solving, because it lets you decompose a complicated random quantity into simple indicators.
Worked examples
| Question | Decomposition | Answer |
|---|---|---|
| Expected fixed points of a random permutation | , each | |
| Expected inversions | , | |
| Expected distinct values among uniform draws from | ||
| Expected length of the longest run | harder — linearity gives bounds, not the exact value | |
| Expected number of records (“new maximum so far”) | ||
| Expected cycles in a random permutation | same as records | |
| Expected edges in a random subgraph | each edge survives | |
| Expected connected components | harder — needs more than linearity |
The records result is a good one to remember: element is the maximum of the first with probability exactly , by symmetry.
Indicator variables — the technique
- Write the quantity as a sum of 0/1 indicators.
- Compute for each, using symmetry wherever possible.
- Add them up.
Step 2 is where symmetry does the work: “by symmetry, each of the positions is equally likely to be the maximum” replaces a calculation with an observation.
Contribution technique
The deterministic cousin, and just as useful. Instead of summing over configurations, sum over elements, counting how many configurations each contributes to:
| Problem | Contribution |
|---|---|
| Sum of over all subarrays | each element × the subarrays where it is the minimum (monotonic stack) |
| Sum of all pairwise tree distances | each edge × |
| Sum of over all pairs | each × the number of pairs with that gcd |
| Sum over all subsets of the subset’s XOR | each bit, independently |
| Expected value of a max |
That last identity is the tail-sum formula: for a non-negative integer random variable,
It converts “expected maximum” — which linearity cannot handle — into a sum of probabilities, which it can.
Conditional expectation
Condition on the first step (giving a recurrence for expected time), or on a hidden variable (the value of the maximum, the position of a pivot).
The law of total expectation is what turns “expected number of steps” into the backward DP of Probability DP.
Classic results
| Problem | Answer |
|---|---|
| Expected trials until success, probability | |
| Coupon collector | |
| Expected comparisons in randomized quicksort | |
| Expected depth of a random BST / treap | |
| Expected height of a random BST | |
| Random walk on from : reach before 0 | probability ; expected time |
| Expected maximum of uniform | |
| Expected minimum of uniform | |
| Expected number of local maxima in a random permutation | (interior positions have ) |
| Birthday paradox | collision after draws |
Modular expected values
Most contest problems now ask for the answer modulo . Represent every probability as and work exactly — no floating point anywhere. This removes all precision concerns and is why “output ” has become the standard phrasing.
Pitfalls
Three traps
- unless and are independent. Linearity has no such requirement; products do.
- . Ever.
- — use the tail-sum formula instead.
See also: Probability · Probability DP · Randomized Algorithms