Dynamic Programming
Also known as: DP
Algorithmic technique that solves complex problems by breaking them into overlapping subproblems and storing each result so it is computed only once.
Dynamic programming is a method for solving problems that can be broken into smaller subproblems whose solutions combine to solve the whole, where the same subproblems recur many times. Rather than recomputing them, a dynamic programming algorithm solves each subproblem once and stores the answer in a table. Computing the 50th Fibonacci number by naive recursion takes billions of redundant calls, because each value is recalculated over and over; storing each number as it is computed reduces the work to about 50 steps. Two properties signal that the technique applies: optimal substructure and overlapping subproblems.
The mathematician Richard Bellman developed the method at the RAND Corporation in the 1950s for multistage decision problems, and the Bellman equation that expresses its core idea later became central to reinforcement learning. Bellman later wrote that he chose the name partly to shield his mathematical research from a government official who disliked the word research. Solutions are written either top-down, as recursion with memoization, or bottom-up, filling a table from the smallest subproblems upward, which avoids deep recursion and often uses less memory.
Dynamic programming solves many practical problems efficiently. The edit distance between two strings, used in spell checking, is computed this way, as are sequence alignment in bioinformatics, shortest paths in some graph algorithms, the knapsack problem of choosing the most valuable items under a weight limit and the Viterbi algorithm used in speech recognition and error correction. Its limits are memory, since tables can grow large, and applicability: problems without overlapping subproblems gain nothing, and greedy or divide-and-conquer algorithms may be simpler. It is also a staple of technical interviews and programming competitions.
Key points
- Dynamic programming stores the solutions of overlapping subproblems so that each is computed only once.
- It applies when a problem has optimal substructure and its subproblems repeat.
- Richard Bellman developed the method in the 1950s, and his equation underpins reinforcement learning.
- Solutions are built top-down with memoized recursion or bottom-up by filling a table.
Related entries
Memoization
Optimization technique that caches a function's results for given inputs so that repeated calls with the same arguments return instantly.
Recursion
Technique in which a function solves a problem by calling itself on smaller instances of the same problem until it reaches a simple base case.
Algorithm
Finite sequence of well-defined steps for solving a problem or performing a computation, such as sorting a list or finding a route.
Big O Notation
Mathematical notation describing how an algorithm's running time or memory use grows as its input size grows, ignoring constant factors.
Reinforcement Learning
Machine learning approach in which an agent learns to choose actions by trial and error, guided by rewards and penalties from its environment.
Graph Theory
Branch of mathematics that studies networks of nodes connected by edges, used to model relationships from road maps to social networks.
Data Structure
Way of organizing and storing data in a computer so that it can be accessed and modified efficiently, such as an array, list, tree or hash table.
Referenced by
Other entries that discuss or point to Dynamic Programming.