What is dynamic programming?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Dynamic programming (DP) is an algorithmic technique employed to solve complex problems by breaking them down into simpler subproblems. It is particularly useful in optimization problems where decisions need to be made to achieve an optimal solution. By leveraging the principle of overlapping subproblems and optimal substructure, dynamic programming avoids redundant calculations and improves computational efficiency.
Key Concepts of Dynamic Programming
Overlapping Subproblems
Dynamic programming is suitable for problems with overlapping subproblems, meaning the problem can be broken down into subproblems which are reused several times. This is a distinguishing feature from divide-and-conquer algorithms that solve independent subproblems.
Optimal Substructure
A problem is said to have an optimal substructure if an optimal solution can be constructed efficiently from optimal solutions of its subproblems. This property ensures that solving each subproblem individually and combining their solutions leads to an optimal overall solution.
Memoization and Tabulation
There are two main approaches to implementing dynamic programming:
- Memoization: This is a top-down approach. Recursive calls compute values for each subproblem and store them in a memory table (often a dictionary or an array) to avoid redundant calculations.
- Tabulation: This is a bottom-up approach. It uses an iterative process to fill up a table where each entry corresponds to a subproblem, solving all subproblems in a systematic manner without recursion.
Examples of Dynamic Programming
Fibonacci Sequence
One of the simplest examples of dynamic programming is the computation of Fibonacci numbers, where each number is the sum of the two preceding ones.
Recursive (inefficient):
Dynamic Programming (Efficient):
0/1 Knapsack Problem
This optimization problem involves selecting a set of items, each with a weight and value, to maximize the total value without exceeding the weight limit. It can be solved optimally using dynamic programming.
Dynamic Programming Solution:
Table Summarizing Key Points
| Feature | Description |
| Overlapping Subproblems | Re-use of identical subproblem solutions |
| Optimal Substructure | Construct global solutions from optimal subproblem solutions |
| Memoization | Top-down approach using recursion and caching of results |
| Tabulation | Bottom-up approach building solutions iteratively |
| Applications | Fibonacci sequence, 0/1 Knapsack, Shortest Path, Longest Common Subsequence, etc. |
Additional Topics
Complexity Analysis
Dynamic programming can significantly alter the time complexity of solving a problem. While a naive recursive approach may have exponential time complexity, dynamic programming often reduces this to polynomial time due to its reuse of solutions to subproblems.
Limitations
While powerful, dynamic programming can have limitations:
- Space Complexity: For problems requiring very large tables, space complexity can become prohibitive.
- Problem Design: Not all problems exhibit overlapping subproblems or optimal substructure, limiting their applicability to dynamic programming solutions.
Advanced Techniques
Advanced techniques, such as the use of bitmasks, state compression, and dividing structures, can extend dynamic programming's applicability even further into more complex problem domains.
In conclusion, dynamic programming is a robust approach for tackling problems with overlapping subproblems and optimal substructure. It provides a framework for transforming naive exponential solutions into efficient polynomial-time solutions by systematically solving smaller subproblems and building up to the desired result.

