Dynamic Programming: How Can We Calculate F (20) ?
Dynamic Programming: How Can We Calculate F (20) ?
The numbers in the above series are not randomly calculated. Mathematically, we
could write each of the terms using the below formula:
With the base values F(0) = 0, and F(1) = 1. To calculate the other numbers, we follow
the above relationship. For example, F(2) is the sum f(0) and f(1), which is equal to 1.
In the above example, if we calculate the F(18) in the right subtree, then it leads to
the tremendous usage of resources and decreases the overall performance.
The solution to the above problem is to save the computed results in an array. First,
we calculate F(16) and F(17) and save their values in an array. The F(18) is calculated
by summing the values of F(17) and F(16), which are already saved in an array. The
computed value of F(18) is saved in an array. The value of F(19) is calculated using
the sum of F(18), and F(17), and their values are already saved in an array. The
computed value of F(19) is stored in an array. The value of F(20) can be calculated by
adding the values of F(19) and F(18), and the values of both F(19) and F(18) are
stored in an array. The final computed value of F(20) is stored in an array.
The above five steps are the basic steps for dynamic programming. The dynamic
programming is applicable that are having properties such as:
Those problems that are having overlapping subproblems and optimal substructures.
Here, optimal substructure means that the solution of optimization problems can be
obtained by simply combining the optimal solution of all the subproblems.
o Top-down approach
o Bottom-up approach
Top-down approach
The top-down approach follows the memorization technique, while bottom-up
approach follows the tabulation method. Here memorization is equal to the sum of
recursion and caching. Recursion means calling the function itself, while caching
means storing the intermediate results.
Advantages
Disadvantages
It uses the recursion technique that occupies more memory in the call stack.
Sometimes when the recursion is too deep, the stack overflow condition will occur.
It occupies more memory that degrades the overall performance.
int fib(int n)
{
if(n<0)
error;
if(n==0)
return 0;
if(n==1)
return 1;
sum = fib(n-1) + fib(n-2);
}
In the above code, we have used the recursive approach to find out the
Fibonacci series. When the value of 'n' increases, the function calls will also
increase, and computations will also increase. In this case, the time complexity
increases exponentially, and it becomes 2n.
One solution to this problem is to use the dynamic programming approach.
Rather than generating the recursive tree again and again, we can reuse the
previously calculated value. If we use the dynamic programming approach,
then the time complexity would be O(n).
When we apply the dynamic programming approach in the implementation of
the Fibonacci series, then the code would look like:
static int count = 0;
int fib(int n)
{
if(memo[n] != NULL)
return memo[n];
count++;
if(n<0)
error;
if(n==0)
return 0;
if(n==1)
return 1;
sum = fib(n-1) + fib(n-2);
memo[n] = sum; }
In the above code, we have used the memorization technique in which we store the
results in an array to reuse the values. This is also known as a top-down approach in
which we move from the top and break the problem into sub-problems.
Bottom-Up approach
The bottom-up approach is also one of the techniques which can be used to
implement the dynamic programming. It uses the tabulation technique to implement
the dynamic programming approach. It solves the same kind of problems but it
removes the recursion. If we remove the recursion, there is no stack overflow issue
and no overhead of the recursive functions. In this tabulation technique, we solve the
problems and store the results in a matrix.
o Top-Down
o Bottom-Up
The bottom-up is the approach used to avoid the recursion, thus saving the memory
space. The bottom-up is an algorithm that starts from the beginning, whereas the
recursive algorithm starts from the end and works backward. In the bottom-up
approach, we start from the base case to find the answer for the end. As we know,
the base cases in the Fibonacci series are 0 and 1. Since the bottom approach starts
from the base cases, so we will start from 0 and 1.
Key points
o We solve all the smaller sub-problems that will be needed to solve the larger
sub-problems then move to the larger problems using smaller sub-problems.
o We use for loop to iterate over the sub-problems.
o The bottom-up approach is also known as the tabulation or table filling
method.
Suppose we have an array that has 0 and 1 values at a[0] and a[1] positions,
respectively shown as below:
Since the bottom-up approach starts from the lower values, so the values at a[0] and a[1] are
added to find the value of a[2] shown as below:
The value of a[3] will be calculated by adding a[1] and a[2], and it becomes 2 shown as
below:
The value of a[4] will be calculated by adding a[2] and a[3], and it becomes 3 shown as
below:
The value of a[5] will be calculated by adding the values of a[4] and a[3], and it becomes 5
shown as below:
The code for implementing the Fibonacci series using the bottom-up approach is given
below:
int fib(int n)
{
int A[];
A[0] = 0, A[1] = 1;
for( i=2; i<=n; i++)
{
A[i] = A[i-1] + A[i-2]
}
return A[n];
}
In the above code, base cases are 0 and 1 and then we have used for loop to find
other values of Fibonacci series.
Initially, the first two values, i.e., 0 and 1 can be represented as:
When i=2 then the values 0 and 1 are added shown as below:
When i=3 then the values 1and 1 are added shown as below:
When i=4 then the values 2 and 1 are added shown as below:
When i=5, then the values 3 and 2 are added shown as below:
in the above case, we are starting from the bottom and reaching to the top.
1.It deals (involves) three steps at each level of 1.It involves the sequence of four steps:
recursion:
Divide the problem into a number of subproblems. o Characterize the structure of optimal
Conquer the subproblems by solving them solutions.
recursively. o Recursively defines the values of
Combine the solution to the subproblems into the
optimal solutions.
solution for original subproblems.
o Compute the value of optimal solutions
in a Bottom-up minimum.
o Construct an Optimal Solution from
computed information.
2. It is Recursive. 2. It is non Recursive.
3. It does more work on subproblems and hence 3. It solves subproblems only once and then
has more time consumption. stores in the table.
5. In this subproblems are independent of each 5. In this subproblems are interdependent.
other.
6. For example: Merge Sort & Binary Search etc. 6. For example: Matrix Multiplication.