In this chapter we shall learn about below topics:
3.1 What is dynamic programming
3.2 Top down / Memonization approach
3.3 C++ program to find Fibonacci series using Memonization technique
3.4 Bottom up approach / Tabular method
3.5 C++ program to find Fibonacci series using Tabular technique.
3.1 What is dynamic programming
In the previous chapter, we studied about recursion and saw recursion tree as below:
From the above, the time complexity will be 2^n and it you observe carefully we are repeating the calculation for the values that are already been calculated.
We are calculating the values for “fib(2)” “fib(1)” “fib(0)” for more than one time.
So is there a way to calculate them for only once?
Yes, there is a way. We store the result for already calculated value in an array.
Next time when we try to calculate the value for already calculated value, we check in our array if the value is present or not. If present, then we take from the array and use it. Else we calculate the value and store it in the array for further use.
As we are storing the result for already calculated value, for it ca be used in further in our problem is called as dynamic programming.
There are 2 approaches of dong dynamic programming.
1. Top down approach / Memonization
2. Bottom up approach / Tabular method.
We shall discuss both of the approach below:
3.2 Top down approach / Memonization
Let us understand this approach by using the same Fibonacci number as an example:
In this approach we take an array to store the values that are previously been calculated.
Step 1: Take an array and initialize with -1. As we are calculating for fib( 5 ), we take 5 element array.
First we calculate for “fib ( 5 )”. The value of fib ( 5 ) is -1, we calculate further, hence make a recursive call to “fib ( 4 )”
Check the 4thindex of the array, it is -1, make a recursive call for “ fib ( 3 )”
As “fib ( 3 )” is also -1, make a recursive call again to “fib ( 2 )”.
As “fib ( 2 )” is also -1 call for “fib ( 1 )”.
As the index is “1” we return 1 and update the array with 1 for index 1.
Again “fib ( 2 )” will call “fib ( 0 )”. As “0” will return “0” update the array.
Now we can calculate the value for fib ( 2 ) = fib ( 1 ) + fib ( 0 ) = 1 + 0 = 1, update it the array.
Now we have to make 2ndrecursive call to “fib ( 3 )”.
i.e “fib ( 2)”. But as we have already know the value of fib ( 2 ) form the array, we use that value to calculate fib ( 3 ).
Now we need to make 2ndrecursive call to “fib ( 4 )”.
The 2nd recursive call to “fib ( 4)” is “fib ( 2 )”. We know the value of fib ( 2), we can calculate the value for “ fib ( 4)” and update the array.
Now make 2ndrecursive call for fib ( 5 ). The 2ndrecursive call for “fib ( 5)” is “fib ( 3)”. As we already know the value for fib ( 3), use it and get the final result.
Hence as you can see, by using Memonization approach, we have reduced the time complexity from 2 ^n to O ( n) by using dynamic programming;
And, here we have solved the problem from top to bottom to get the result. This is called as top down approach. We have stored intermediate result in an array. This is called as Memonization technique.
3.3 C++ program to find Fibonacci series using Memonization technique.
#include<iostream>
using namespace std;
int array_memo [6];
int fib(int n)
{
if (array_memo[n] == -1)
{
if (n <= 1)
array_memo[n] = n;
else
array_memo[n] = fib(n - 1) + fib(n - 2);
}
return array_memo[n];
}
int main ()
{
int n = 5;
//initialize array to -1
for(int i = 0; i < 6 ; i++)
array_memo [i] = -1;
cout << "Fibonacci number is " << fib(n)<<endl;
return 0;
}
Output
Fibonacci number is 5
3.4 Bottom up approach / Tabular method.
Now we shall learn about bottom up or tabular method.
Tabular method can be achieved by iterative method instead of recursive method. Below is the function that calculate Fibonacci in iterative method.
int fib ( n)
{
int arr[5]
arr[0] = 0
arr[1] = 1
if (n <= 1)
return n
for(i = 2; i <= n ; i++)
{
arr[i] = arr[i-1] + arr [i -2]
}
return arr[n]
}
In the above program, we have to generate an array, and we shall start filling the array from lower index to upper index. As first 2 index are prefilled we shall start with
Pass 3:
arr [0] = 0
arr[1] = 1
arr [2] = arr [0] + arr [1]
= 1 + 0
arr[2] = 1
array:
0, 1, 1, 0, 0, 0
Pass 4:
arr [3] = arr [2] + arr [1]
= 1 + 1
arr[2] = 2
array:
0, 1, 1, 2, 0, 0
Pass 5:
arr [4] = arr [3] + arr [2]
= 2 + 1
arr[4] = 3
array:
0, 1, 1, 2, 3, 0
Pass 5:
arr [5] = arr [4] + arr [3]
= 2 + 3
arr[4] = 5
array:
0, 1, 1, 2, 3, 5
Here if you observe carefully, we are filling from lower index to higher index. Hence it is bottom up approach using tabular method.
3.5 C++ program to find Fibonacci series using Tabular technique.
#include<iostream>
using namespace std;
int fib(int n)
{
int fib_arr[n+1];
fib_arr[0] = 0;
fib_arr[1] = 1;
for (int i = 2; i <= n; i++)
fib_arr[i] = fib_arr[i-1] + fib_arr[i-2];
return fib_arr[n];
}
int main ()
{
int n = 5;
cout<<"Fibonacci number is = "<<fib(n)<<endl;
return 0;
}
Output:
Fibonacci number is = 5