It is an algorithmic paradigm that solves a given complex problem by breaking it into subproblems and storing the results of subproblems to avoid computing the same results again.
Utilizes the fact that the optimal solution to the overall problem depends on the optimal solutions to its subproblems.
Example: Fibonacci numbers - We can calculate nth Fibonacci by below eqution
fib(n) = fib(n-1) + fib(n-2) for n > 1
Here to solve the overall problem we broke it down to smaller subproblems.
Subproblems are smaller version of the original problem.
Any problem has overlappping subproblems if finding its solution involves solving the same subproblem multiple times.
Like Divide and Conquer, Dynamic Programming combines solutions to sub-problems.
Dynamic Programming is mainly used when solutions of same subproblems are needed again and again.
In dynamic programming, computed solutions to subproblems are stored in a table so that these don’t have to be recomputed.
Dynamic Programming is not useful when there are no common (overlapping) subproblems because there is no point storing the solutions if they are not needed again.
Binary Search is broken down into subproblems but it doesn’t have common subproblems, so no sense to store the solutions.
Here we can see overlapping subproblems as fib(2) is evaluated twice and fib(1) evaluated thrice.