Dynamic Programming (DP) is a programming paradigm that can systematically and efficiently explore all possible solutions to a problem. As such, it is capable of solving a wide variety of problems that often have the following characteristics:
- The problem can be broken down into overlapping subprograms - smaller versions of the original problem that are re-used multiple times.
- The problem has an optimal substructure - an optimal solution can be formed from optimal solutions to the overlapping subproblems of the original problem.
The Fibonacci sequence is a classic example used to explain DP. For those who unfamiliar with the Fibonacci sequence, it is a sequence of numbers that starts with 0, 1, and each subsequent number is obtained by adding the previous two numbers together.
If you wanted to find the nth Fibonacci number F(n), you can break it down into smaller subproblems - find F(n-1) and F(n-2) instead. Then, adding the solutions to these subproblems together gives the answer to the original question, F(n-1) + F(n-2) = F(n), which means the problem has optimal substructure, since the solution F(n) to the original problem can be formed from the solutions to the subproblems. These subproblems are also overlapping - for example, we would need F(4) to calculate the F(5) and F(6).
Dynamic programming is a powerful tool because it can break a complex problem into manageable subproblems, avoid unnecessary recalculation of overlapping subproblems, and use the results of those subproblems to solve the initial complex problem. DP not only aids us in solving complex problems, but it also greatly improves the time complexity compared to brute force solutions. For example, the brute force solution for calculating the Fibonacci Sequence has exponential time complexity, while the dynamic programming solution will have linear time complexity. Throughout this explore card, you will gain a better understanding of what makes DP so powerful.