Dynamic Programming

Decision Sciences
Introduced by
Richard Bellman
Year
1957
Type
Exact combinatorial optimization via recursive decomposition
Family
Mathematical programming

What does it do?

Dynamic Programming (DP) is an exact optimization technique introduced by Richard Bellman in 1957 for multi-stage decision problems.

It decomposes a problem into subproblems, solves each one, and stores the results so they are not recomputed. Its correctness rests on the Principle of Optimality: when that principle holds, the recursive decomposition yields globally optimal solutions, while overlapping subproblems mainly determine how much computation is saved.

The intuition behind the method

Imagine planning the shortest route across a city by first finding the shortest paths between every pair of nearby intersections, then combining those small solutions upward. Instead of re-exploring every possible full route from scratch, you reuse pre-computed local answers. Dynamic Programming works the same way: it caches solutions to small subproblems so that building up to the full answer requires only table lookups rather than repeated heavy computation.

History

Richard Bellman introduced the term 'dynamic programming' in 1957 while at the RAND Corporation, choosing 'programming' in the sense of planning rather than computer coding. The Principle of Optimality he stated became a foundation for control theory, operations research, and later reinforcement learning. The Viterbi algorithm and the Needleman-Wunsch sequence aligner are DP methods within this framework, and the Bellman-Ford shortest-path algorithm is the standard DP procedure for that problem.

When to use it

Choose DP when the problem is sequential or staged and the following assumptions hold: the state is sufficient to describe the future (Markov property); the objective is additive and separable over stages; the Principle of Optimality holds, so an optimal solution can be built from optimal solutions to subproblems; the state and action sets are finite or can be discretized to a manageable size; and the transition function T(s,a) and reward r(s,a) (or transition probabilities for stochastic problems) are known. Overlapping subproblems are what make memoization pay off; without them, DP offers no speed advantage over plain recursion. Do not use exact DP when state variables are numerous or continuous, because the table grows rapidly; approximate DP or reinforcement learning are the usual alternatives. Avoid it when the transition and reward functions are unknown, since the method needs both in full.

Limitations and considerations

  • Memory and time grow with the number of combinations of state variables, so the curse of dimensionality appears as soon as the state representation becomes large.
  • Requires the transition function T(s,a) and reward r(s,a) to be known in advance; it cannot be run directly on an unknown environment without an added learning or approximation step.
  • The state must be discrete or discretized; continuous high-dimensional states require approximation, which gives up exactness.
  • Choosing a state representation and recurrence that capture the problem's structure requires problem-specific analysis.

Source conditions do not replace independent scientific review or validation by an analysis engine.

Strengths

  • Gives exact globally optimal solutions for problems that satisfy the Principle of Optimality, with no approximation step in the recurrence itself.
  • Memoization evaluates each state once, so for a fixed, small action set the cost grows with the number of states and stages rather than with the number of complete paths.
  • Returns an explicit optimal policy pi*(s) for every state, not only an optimal value, so the decision rule can be read and applied directly.
  • Applies to any sequential problem that can be written as a recurrence over a finite state space, including the shortest-path, sequence alignment and decoding problems listed below.

Applications

  • Shortest-path computation in networks with the Bellman-Ford algorithm.
  • Sequence alignment in computational biology (Needleman-Wunsch, Smith-Waterman).
  • Optimal resource allocation and knapsack-type problems in operations research.
  • Decoding Hidden Markov Models for speech and handwriting recognition via the Viterbi algorithm.
  • Optimal control and multi-period portfolio decisions in economics and engineering.

How it works

How it works

1

The problem is written as a recurrence. In the finite-horizon form, V_n(s) is the best immediate reward r(s,a) plus the value of the successor state T(s,a) with n-1 stages remaining, maximized over the feasible actions A(s); the recursion starts from a specified terminal value V_0. The factor gamma in the formula is a discount factor: gamma = 1 gives the undiscounted finite-horizon form, and 0 <= gamma < 1 is the usual discounted form. This Bellman equation expresses the Principle of Optimality: an optimal decision sequence must remain optimal from every state it passes through.

Formula
V_n(s) = \max_{a \in A(s)} \left[ r(s,a) + \gamma V_{n-1}(T(s,a)) \right], \quad V_0(s) \text{ given}, \ 0 \le \gamma \le 1
2

For the infinite-horizon value V*(s), the equation is a fixed-point equation: V* appears on both sides. It is typically solved by repeated updates across the state space, as in value iteration. Each update replaces the stored value of a state with the best reward-plus-discounted-successor value, using the current estimates of neighbouring states. For a finite state space, convergence to V* is guaranteed under discounting with 0 <= gamma < 1 (or a comparable proper-termination condition). Without such a condition the iteration may not converge, so check it before relying on a stopping rule such as 'values stop changing'.

Formula
V^*(s) = \max_{a \in A(s)} \left[ r(s,a) + \gamma V^*(s') \right], \quad s' = T(s,a), \ 0 \le \gamma < 1
3

Each state's value is stored in a table indexed by that state, so a subproblem is evaluated once and reused whenever it recurs. In the table definition above, terminal states take the value 0, which assumes zero continuation value at termination; if terminal states carry a reward, use that reward in place of 0. For a fixed, small action set per state, the number of table entries and lookups grows with the number of states and stages, which is the basis of the polynomial-time claim; that claim does not hold when the state space itself grows exponentially with the number of state variables.

Formula
\text{Table}[s] = \begin{cases} 0 & \text{if } s \in S_{\text{terminal}} \\ V^*(s) & \text{otherwise} \end{cases}
4

Once the value table is complete, the optimal policy pi*(s) is recovered in one greedy pass: for each state, select the action that maximizes the immediate reward plus the stored value of the successor state. This extraction is cheap relative to solving the recurrence and yields a deterministic decision rule for every state.

Formula
\pi^*(s) = \arg\max_{a \in A(s)} \left[ r(s,a) + \gamma \, \text{Table}[T(s,a)] \right]

Common pitfalls

  • A state representation that omits information needed for future decisions yields an incorrect recurrence, and the resulting policy is suboptimal without any warning.
  • Top-down memoization can exhaust the call stack on deep recursions, while bottom-up tabulation requires a correct evaluation order for subproblems; confusing the two causes errors.
  • DP does not apply when optimal substructure fails. The longest simple path in a general graph is the standard example: running a DP recurrence there returns an answer that is not guaranteed to be optimal.
  • Accumulating floating-point rewards over many stages can introduce small rounding differences that change the selected action when two values are nearly equal.

Frequently asked questions

What is the difference between dynamic programming and divide-and-conquer?

Both split a problem into subproblems. Divide-and-conquer assumes the subproblems are distinct, so each is solved once and no results are reused. Dynamic programming targets overlapping subproblems, where the same subproblem appears on several branches, and stores results so each is computed once. If subproblems do not overlap, memoization brings no saving over plain recursion.

When should I use top-down memoization versus bottom-up tabulation?

Top-down memoization is easier to implement directly from the recurrence and computes only needed subproblems, benefiting sparse state spaces. Bottom-up tabulation avoids recursion overhead and stack limits, suits dense state spaces, and is often faster in practice. Choose top-down when the set of needed subproblems is hard to predict, bottom-up when all subproblems will be needed.

How does dynamic programming relate to reinforcement learning?

Classical DP needs a fully specified Markov Decision Process, meaning known transition probabilities and rewards. Reinforcement learning uses the same Bellman-style recursion when the model is unknown and estimates values from sampled experience; Q-learning is one example, learning action values from observed transitions. Pure policy gradient methods adjust a parameterized policy directly, whereas actor-critic methods combine policy gradients with learned value estimates that rely on Bellman-style updates.

Related methods

Sources

  1. Bellman, R. (1957). Dynamic Programming. Princeton University Press.

    ISBN: 978-0-691-07951-6