![]() |
VOOZH | about |
Dynamic Programming is an algorithmic paradigm that solves a given complex problem by breaking it into subproblems using recursion and storing the results of subproblems to avoid computing the same results again. Following are the two main properties of a problem that suggests that the given problem can be solved using Dynamic programming.
In this post, we will discuss the first property Overlapping Subproblems in detail.
Let us take the example of following a recursive program for Fibonacci Numbers, there are many subproblems that are solved again and again.
5
Time Complexity: O(2N)
Auxiliary Space: O(1)
Illustration of Recursion tree for the execution of fib(5) :
We can see that the function fib(3) is being called 2 times. If we would have stored the value of fib(3), then instead of computing it again, we could have reused the old stored value. There are following two different ways to store the values so that these values can be reused:
The memoized program for a problem is similar to the recursive version with a small modification that looks into a lookup table before computing solutions. We initialize a lookup array with all initial values as NIL. Whenever we need the solution to a subproblem, we first look into the lookup table. If the precomputed value is there then we return that value, otherwise, we calculate the value and put the result in the lookup table so that it can be reused later.
Following is the memoized version for the nth Fibonacci Number.
Fibonacci number is 5
Time Complexity: O(n). This is because the algorithm computes each Fibonacci number only once and stores the result in an array for future use. Subsequent calls to the function with the same input value of n will retrieve the stored value from the lookup table, avoiding the need to recompute it. Therefore, the time complexity is linear, and the algorithm is very efficient for large values of n.
Auxiliary Space: O(n) as lookup table has been created.
The tabulated program for a given problem builds a table in a bottom-up fashion and returns the last entry from the table. For example, for the same Fibonacci number, we first calculate fib(0) then fib(1) then fib(2) then fib(3), and so on. So literally, we are building the solutions to subproblems bottom-up.
Following is the tabulated version for the nth Fibonacci Number.
Fibonacci number is 5
Time Complexity: O(N)
Auxiliary Space: O(N)
Please note that this is not the best solution for finding nth Fibonacci Number, there exist better solutions. Please refer Nth Fibonacci Number for details.