DP where the value is a probability or an expectation. The mechanics are ordinary DP; the difficulty is setting up the recurrence correctly.

Linearity of expectation — the most useful tool in the subject

regardless of dependence. This lets you decompose a complicated random quantity into indicator variables and sum their probabilities:

Many “expected value” problems that look like they need a DP over configurations collapse to a sum of per-element probabilities. Always try this first.

Example. Expected number of fixed points in a random permutation: each element is fixed with probability , so the answer is — no DP at all.

Forward vs backward DP

Forward (probability). dp[s] = probability of reaching state . Push probability outward:

dp[start] = 1.0;
for (state s in topological order)
    for (auto [t, p] : transitions(s)) dp[t] += dp[s] * p;

Backward (expectation). dp[s] = expected value from onward. Pull from successors:

// process states in reverse topological order
for (state s in reverse order)
    for (auto [t, p, c] : transitions(s)) dp[s] += p * (c + dp[t]);

Expectation DPs are almost always backward, with the terminal states as base cases. Getting the direction wrong is the most common structural error.

Self-loops — the algebra step

When a state can transition to itself, the recurrence is implicit:

Concretely, for (a step that with probability leaves you where you were):

Always isolate algebraically rather than iterating — iteration converges slowly and accumulates error.

Cyclic dependencies — Gaussian elimination

If states depend on each other in a cycle that cannot be ordered, you have a linear system:

Solve with Gaussian elimination in . This is how random-walk-on-a-graph problems are solved when the walk can revisit states. For or so it is entirely practical.

Special structure often helps: on a line or a tree, the system is tridiagonal or hierarchical and solvable in .

Classic results

ProblemAnswer
Expected rolls to see all faces (coupon collector)
Expected trials until success, probability
Expected number of comparisons in quicksort
Expected length of the longest run of heads in flips
Expected number of inversions in a random permutation
Expected number of cycles in a random permutation
Random walk on a line, absorbing at 0 and , starting at reaches with probability ; expected time
Birthday paradoxcollision after draws

Numerical care

  • Prefer long double for chained multiplications of probabilities.
  • Work in log space when probabilities get very small (products become sums).
  • Modular probabilities. Many problems ask for the answer mod : represent as and use modular inverses throughout. This is exact and avoids all precision questions — check the statement, because it is now the more common convention.
  • Iterating to convergence (e.g. 100 000 rounds of a value iteration) is a legitimate technique for problems with a self-loop structure and a specified precision, but the algebraic solution is better when available.

Recognising the shape

  • “Expected number of X” → linearity of expectation, sum of indicators.
  • “Expected time until Y” → backward DP, watch for self-loops.
  • “Probability that Z happens” → forward DP over states.
  • “Both players play optimally and there is randomness” → game DP with expectation at chance nodes.

See also: Probability · Expected Value Techniques · Gaussian Elimination