Skip to content

Dynamic Programming

Prime #
476
Origin domain
Operations Research
Also from
Computer Science & Software Engineering, Mathematics
Aliases
DP, Bellman Recursion, Optimal Substructure Recursion, Memoized Recursion
Related primes
Markov Decision Processes (MDPs), Linear Programming (LP), Branch and Bound, Decomposition, Recursion, Caching, greedy algorithm, shortest path algorithms, sequence alignment

Core Idea

Dynamic Programming tackles complex optimization tasks by decomposing them into overlapping subproblems; each subproblem's solution is stored and reused, systematically building an optimal solution from smaller building blocks.

How would you explain it like I'm…

Remember small answers

If your teacher asks you to add 2+3 lots of times, after the first time you just remember the answer is 5. You don't redo the work. Dynamic programming is the same trick for harder puzzles. Solve each little piece once, write it down, then reuse it whenever it pops up again.

Solve small pieces, save them

Dynamic programming is a way to solve hard puzzles by breaking them into smaller puzzles, solving each small one just once, and saving the answers in a table to reuse later. Without saving, you'd redo the same small puzzle over and over, which can take forever. With saving, you do each one only once and look it up after. It works when the big problem can be built from optimal little pieces and the same little pieces keep showing up. Richard Bellman invented the name in the 1950s.

Optimization via cached subproblems

Dynamic programming is an optimization technique that solves complex decision problems by decomposing them into overlapping subproblems, solving each subproblem just once, and storing the results for reuse. It works when two conditions hold: optimal substructure (the best solution to the whole is built from best solutions to its parts) and overlapping subproblems (the same parts recur in a naive recursive expansion). Caching transforms problems that would take exponential time into polynomial-time ones, either top-down with memoization or bottom-up with tabulation. Bellman formalized it in the 1950s via his principle of optimality. Examples include shortest paths, sequence alignment, and HMM decoding.

 

Dynamic programming is an optimization technique that solves complex decision problems by decomposing them into overlapping subproblems, solving each subproblem exactly once, and storing the solutions for reuse either top-down via memoization or bottom-up via tabulation. The method rests on two structural conditions: optimal substructure (the optimal solution to the whole problem can be constructed from optimal subproblem solutions) and overlapping subproblems (the same subproblems recur many times in a naive recursive tree). When both hold, DP transforms exponential-time naive recursion into polynomial-time algorithms with complexity typically O(states x transitions). Bellman formalized the technique at RAND in the 1950s under his principle of optimality: whatever the initial state and first decision, the remaining decisions must constitute an optimal policy with respect to the state resulting from the first. DP subsumes Bellman-Ford and Floyd-Warshall shortest-path algorithms, the knapsack, the Viterbi algorithm for HMM decoding, Needleman-Wunsch and Smith-Waterman sequence alignment, the CYK parser, matrix-chain multiplication, and value/policy iteration for MDPs.

Broad Use

  • Knapsack Problem: Recursively computing best solutions for subsets of items and partial capacity, storing subresults in a table.

  • Shortest Path in Graphs (Dijkstra, Bellman-Ford): Iteratively refine distances to nodes, reusing partial paths to build entire route.

  • Equipment Replacement: Deciding if/when to replace a machine over multiple periods, factoring in maintenance vs. new purchase costs.

  • Sequence Alignment in Bioinformatics: Aligning DNA or protein sequences by stepwise matching subproblems.

Clarity

Exposes a divide-and-conquer logic where subproblems overlap in structure; each solution snippet is reused rather than recalculated, facilitating polynomial-time solutions for many discrete problems.

Manages Complexity

By caching subproblem results, it transforms exponential naive recursion into a more tractable polynomial approach, bridging theoretical and real-world large-scale problem solving.

Abstract Reasoning

Reflects the principle of optimal substructure: the global optimum can be built from local optima of subproblems if structured conditions (like overlapping subproblems) hold.

Knowledge Transfer

  • AI & RL: Q-learning in discrete state–action MDPs is essentially a dynamic programming approach.

  • Budget Allocation: Breaking a multi-year budgeting problem into annual subproblems, with partial solutions reused for the next iteration.

Example

A traveling site might use dynamic programming to compute the cheapest flight plan with multiple stopovers, iteratively building from "cheapest route to city X with n stops" subproblems.

Relationships to Other Primes

One-hop neighborhood: parents above, mutual partners to the right, children below.Dynamic Programmingdecompose: OptimizationOptimizationcomposition: RecurrenceRecurrencedecompose: DecompositionDecomposition

Parents (3) — more general patterns this builds on

  • Dynamic Programming presupposes Recurrence — Dynamic programming presupposes recurrence because its decomposition into overlapping subproblems is expressed as a Bellman recurrence relation.
  • Dynamic Programming is a decomposition of Decomposition — Dynamic programming is the specific shape decomposition takes when subproblems overlap and optimal substructure lets cached solutions compose into the full answer.
  • Dynamic Programming is a decomposition of Optimization — Dynamic programming is the specific shape optimization takes when problems exhibit optimal substructure and overlapping subproblems.

Path to root: Dynamic ProgrammingOptimization

Not to Be Confused With

  • Dynamic Programming is not Linear Programming because Dynamic Programming is the optimization method that breaks a problem into overlapping subproblems solved with memoization, while Linear Programming is the method for optimizing linear objective functions subject to linear constraints. Dynamic Programming works on sequential decision problems; Linear Programming works on static constrained optimization.
  • Dynamic Programming is not Optimization because Dynamic Programming is a specific technique for solving optimization problems with optimal substructure, while Optimization is the general problem of finding best solutions. Dynamic Programming is one tool among many for optimization (others include Linear Programming, Genetic Algorithms, Gradient Descent).
  • Dynamic Programming is not Markov Decision Processes because Dynamic Programming is the algorithmic technique for solving sequential decision problems, while Markov Decision Processes is the mathematical model of sequential decision-making under uncertainty. MDPs describe the problem structure; Dynamic Programming is a solution method.