Design & Analysis of algorithm- 4
Design & Analysis of algorithm- 4
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.
● The following are the steps that the dynamic programming follows:
● It breaks down the complex problem into simpler subproblems.
● It finds the optimal solution to these sub-problems.
● It stores the results of subproblems (memoization). The process of storing the results of
subproblems is known as memorization.
● It reuses them so that same sub-problem is calculated more than once.
● Finally, calculate the result of the complex problem.
3. It is easy to debug.
Disadvantages
4. 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.
● 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:
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[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:
0 1 2 3 4 5 6 7 8
0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0 0
1 0
2 0
3 0
4 0
05/28/2025 Department of Computer Science Slide No.
● When i=1, W=1 0 1 2 3 4 5 6 7 8
● w1 = 3; Since we have only one item in the 0 0 0 0 0 0 0 0 0 0
set having weight 3, but the capacity of the
1 0 0
knapsack is 1. We cannot fill the item of
3kg in the knapsack of capacity 1 kg so 2 0
add 0 at M[1][1] shown as below:
3 0
4 0
● When i = 1, W = 2 0 1 2 3 4 5 6 7 8
4 0
4 0
When i=1, W = 4
0 1 2 3 4 5 6 7 8
W1 = 3; Since we have only one item in the set
having weight equal to 3, and weight of the 0 0 0 0 0 0 0 0 0 0
knapsack is 4; therefore, we can fill the
1 0 0 0 2 2
knapsack with an item of weight equal to 3. We
put profit corresponding to the weight 3, i.e., 2 2 0
at M[1][4] shown as below: 3 0
4 0
05/28/2025 Department of Computer Science Slide No.
● When i=1, W = 5 1 2 3 4 5 6 7 8
0
● W1 = 3; Since we have only one item in the
set having weight equal to 3, and weight of 0 0 0 0 0 0 0 0 0 0
the knapsack is 5; therefore, we can fill the 1 0 0 0 2 2 2
knapsack with an item of weight equal to 3.
We put profit corresponding to the weight 2 0
3, i.e., 2 at M[1][5] shown as below: 3 0
4 0
● When i =1, W=6
0 1 2 3 4 5 6 7 8
● W1 = 3; Since we have only one item in the
set having weight equal to 3, and weight of 0 0 0 0 0 0 0 0 0 0
the knapsack is 6; therefore, we can fill the 1 0 0 0 2 2 2 2
knapsack with an item of weight equal to 3.
We put profit corresponding to the weight 2 0
3, i.e., 2 at M[1][6] shown as below: 3 0
4 0
05/28/2025 Department of Computer Science Slide No.
● When i=1, W = 7 0 1 2 3 4 5 6 7 8
● W1 = 3; Since we have only one item in the 0 0 0 0 0 0 0 0 0 0
set having weight equal to 3, and weight of
1 0 0 0 2 2 2 2 2
the knapsack is 7; therefore, we can fill the
knapsack with an item of weight equal to 3. 2 0
We put profit corresponding to the weight
3 0
3, i.e., 2 at M[1][7] shown as below:
4 0
● When i =1, W =8
0 1 2 3 4 5 6 7 8
● W1= 3; Since we have only one item in the
set having weight equal to 3, and weight of 0 0 0 0 0 0 0 0 0 0
the knapsack is 8; therefore, we can fill the 1 0 0 0 2 2 2 2 2 2
knapsack with an item of weight equal to 3.
We put profit corresponding to the weight 2 0
3, i.e., 2 at M[1][8] shown as below: 3 0
4 0
0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0 0
1 0 0 0 2 2 2 2 2 2
2 0 0 0 2 3 3 3 5 5
3 0
4 0
0 0 0 0 0 0 0 0 0 0
1 0 0 0 2 2 2 2 2 2
2 0 0 0 2 3 3 3 5 5
3 0 0 0 2 3 3 3 5 5
4 0 0 0 2 3 3 4 5 5
○ k is an intermediate vertex in shortest path from i to j. We update the value of dist[i][j] as dist[i]
[k] + dist[k][j] if dist[i][j] > dist[i][k] + dist[k][j]
In this step, k is the second vertex (i.e. vertex 2). The remaining steps are the same as in step 2.
Calculate the distance from the source vertex to destination vertex through this vertex 2
Calculate the distance from the source vertex to destination vertex through this vertex 3
Hands on
Doubts/ Tools Exercise
Questions
A Welcome Contacts
Demonstration Break