0% found this document useful (0 votes)
2 views

Design & Analysis of algorithm- 4

The document discusses dynamic programming, focusing on its principles, applications, and examples such as the Fibonacci series and the 0/1 knapsack problem. It explains the top-down and bottom-up approaches, highlighting their advantages and disadvantages, as well as the importance of storing computed results to optimize performance. The document serves as a comprehensive guide for understanding and implementing dynamic programming techniques in algorithm design.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Design & Analysis of algorithm- 4

The document discusses dynamic programming, focusing on its principles, applications, and examples such as the Fibonacci series and the 0/1 knapsack problem. It explains the top-down and bottom-up approaches, highlighting their advantages and disadvantages, as well as the importance of storing computed results to optimize performance. The document serves as a comprehensive guide for understanding and implementing dynamic programming techniques in algorithm design.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 71

Parul University

Session: July-Dec. 2024

05/28/2025 Department of Computer Science Slide No. 1


Parul University

203105374: Design & Analysis of


Algorithm

Date 05/28/2025 Department of Computer Science Slide No.


Unit-4
● Dynamic programming Knapsack
● All pair shortest paths– Warshall’s and Floyd’s algorithms
● Resource allocation problem.

05/28/2025 Department of Computer Science Slide No.


Dynamic Programming
● Dynamic programming is a technique that breaks the problems into sub-problems, and
saves the result for future purposes so that we do not need to compute the result again.
● The subproblems are optimized to optimize the overall solution is known as optimal
substructure property.
● The main use of dynamic programming is to solve optimization problems. Here,
optimization problems mean that when we are trying to find out the minimum or the
maximum solution of a problem.
● The dynamic programming guarantees to find the optimal solution of a problem if the
solution exists.
● The definition of dynamic programming says that it is a technique for solving a complex
problem by first breaking into a collection of simpler sub-problems, solving each sub-
problem just once, and then storing their solutions to avoid repetitive computations.

05/28/2025 Department of Computer Science Slide No.


Example
Consider an example of the Fibonacci series. The following series is the Fibonacci series:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ,…

The numbers in the above series are not randomly calculated. Mathematically, we could
write each of the terms using the below formula:

F(n) = F(n-1) + F(n-2),

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.

05/28/2025 Department of Computer Science Slide No.


● How can we calculate F(20)?
● The F(20) term will be calculated using the nth formula of the Fibonacci series. The
below figure shows that how F(20) is calculated.

05/28/2025 Department of Computer Science Slide No.


Problem:-
● As we can observe in the above figure that F(20) is calculated as the sum of F(19) and F(18).
In the dynamic programming approach, we try to divide the problem into the similar
subproblems.
● We are following this approach in the above case where F(20) into the similar subproblems,
i.e., F(19) and F(18).
● If we recap the definition of dynamic programming that it says the similar subproblem
should not be computed more than once. Still, in the above case, the subproblem is
calculated twice.
● In the above example, F(18) is calculated two times; similarly, F(17) is also calculated twice.
However, this technique is quite useful as it solves the similar subproblems, but we need to
be cautious while storing the results because we are not particular about storing the result
that we have computed once, then it can lead to a wastage of resources.
● 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.

05/28/2025 Department of Computer Science Slide No.


Solution:-
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.

05/28/2025 Department of Computer Science Slide No.


How does the dynamic programming approach work?

● 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.

05/28/2025 Department of Computer Science Slide No.


Properties
● The dynamic programming is applicable that are having properties such as:

1. 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.
2. In the case of dynamic programming, the space complexity would be increased as
we are storing the intermediate results, but the time complexity would be
decreased.

05/28/2025 Department of Computer Science Slide No.


Approaches of dynamic programming
There are two approaches to dynamic programming:
1. Top-down approach
2. Bottom-up approach

05/28/2025 Department of Computer Science Slide No.


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
1. It is very easy to understand and implement.

2. It solves the sub-problems only when it is required.

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.

5. It occupies more memory that degrades the overall performance.

05/28/2025 Department of Computer Science Slide No.


EXAMPLE
● Let's understand dynamic programming through an example.
● 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);
● }
05/28/2025 Department of Computer Science Slide No.
Cont…
● 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:

05/28/2025 Department of Computer Science Slide No.


● static int count = 0; ● memo[n] = sum;
● int fib(int n) ● }
● {
● if(memo[n]!= NULL) ● In the above code, we have used the
● return memo[n]; memorization technique in which we
store the results in an array to reuse the
● count++; values.
● if(n<0) ● This is also known as a top-down
● error; approach in which we move from the top
and break the problem into sub-
● if(n==0) problems.
● return 0;
● if(n==1)
● return 1;
● sum = fib(n-1) + fib(n-2);

05/28/2025 Department of Computer Science Slide No.


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.
● 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.
05/28/2025 Department of Computer Science Slide No.
Cont…
● Key points
○ 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.
○ We use for loop to iterate over the sub-problems.
○ The bottom-up approach is also known as the tabulation or table filling method.

05/28/2025 Department of Computer Science Slide No.


Let's understand through an example.
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:

05/28/2025 Department of Computer Science Slide No.


● 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:

05/28/2025 Department of Computer Science Slide No.


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];
}
05/28/2025 Department of Computer Science Slide No.
DiagrammaticRepresentation.
● In the above code, base cases are 0 and 1 ● When i=3 then the values 1and 1 are
and then we have used for loop to find added shown as below:
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:

05/28/2025 Department of Computer Science Slide No.


When i=4 then the values 2 and 1 are shown as below:
added shown as below:

When i=5, then the values 3 and 2 are added


05/28/2025 Department of Computer Science Slide No.
0/1 knapsack problem
The 0/1 knapsack problem means that the items are either completely or no items
are filled in a knapsack.
For example, we have two items having weights 2kg and 3kg, respectively.
If we pick the 2kg item then we cannot pick 1kg item from the 2kg item (item is not
divisible)
we have to pick the 2kg item completely. This is a 0/1 knapsack problem in which
either we pick the item completely or we will pick that item.
The 0/1 knapsack problem is solved by the dynamic programming.

05/28/2025 Department of Computer Science Slide No.


Example of 0/1 knapsack problem.
Consider the problem having weights and
profits are:
Weights: {3, 4, 6, 5}
Profits: {2, 3, 1, 4}

The weight of the knapsack is 8 kg


The number of items is 4
The above problem can be solved by using the The above are the possible combinations. 1
following method: denotes that the item is completely picked
xi = {1, 0, 0, 1} and 0 means that no item is picked. Since
= {0, 0, 0, 1} there are 4 items so possible combinations
will be:
= {0, 1, 0, 1}
05/28/2025 Department of Computer Science Slide No.
Cont…
2^4 = 16; So. There are 16 possible combinations that can be made by using the
above problem. Once all the combinations are made, we have to select the
combination that provides the maximum profit.
Another approach to solve the problem is dynamic programming approach. In
dynamic programming approach, the complicated problem is divided into sub-
problems, then we find the solution of a sub-problem and the solution of the sub-
problem will be used to find the solution of a complex problem.

V[i,w]=maximum{V[i-1,w], V[i-1,w-w[i] + P[i]}

05/28/2025 Department of Computer Science Slide No.


How this problem can be solved by using the Dynamic
programming approach?
First,
we create a matrix shown as below: V[i,w]=maximum{V[i-1,w], V[i-1,w-w[i] + P[i]}

0 1 2 3 4 5 6 7 8

05/28/2025 Department of Computer Science Slide No.


In the above matrix, columns represent the weight, i.e., 8. The rows represent the profits and
weights of items. Here we have not taken the weight 8 directly, problem is divided into sub-
problems, i.e., 0, 1, 2, 3, 4, 5, 6, 7, 8.
The solution of the sub-problems would be saved in the cells and answer to the problem would
be stored in the final cell. First, we write the weights in the ascending order and profits
according to their weights shown as below:
wi = {3, 4, 5, 6}
pi = {2, 3, 4, 1}The first row and the first column would be 0 as there is no item for w=0

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

● 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 1 0 0 0
the knapsack is 2. We cannot fill the item
of 3kg in the knapsack of capacity 2 kg so 2 0
add 0 at M[1][2] shown as below: 3 0

4 0

05/28/2025 Department of Computer Science Slide No.


When i=1, W=3 0 1 2 3 4 5 6 7 8
w1 = 3; Since we have only one item in the set
0 0 0 0 0 0 0 0 0 0
having weight equal to 3, and weight of the
knapsack is also 3; therefore, we can fill the 1 0 0 0 2
knapsack with an item of weight equal to 3. We 2 0
put profit corresponding to the weight 3, i.e., 2
at M[1][3] shown as below: 3 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

05/28/2025 Department of Computer Science Slide No.


● When i =2, W = 2
● The weight corresponding to the value 2 is 4, i.e., w2 = 4. Since we have only one item in the
set having weight equal to 4, and the weight of the knapsack is 2. We cannot put the item of
weight 4 in a knapsack, so we add 0 at M[2][2] shown as below:
● When i =2, W = 3
● The weight corresponding to the value 2 is 4, i.e., w2 = 4. Since we have two items in the set
having weights 3 and 4, and the weight of the knapsack is 3. We can put the item of weight
3 in a knapsack, so we add 2 at M[2][3] shown as below:
● When i =2, W = 4
● The weight corresponding to the value 2 is 4, i.e., w2 = 4. Since we have two items in the set
having weights 3 and 4, and the weight of the knapsack is 4. We can put item of weight 4 in
a knapsack as the profit corresponding to weight 4 is more than the item having weight 3, so
we add 3 at M[2][4] shown as below:

05/28/2025 Department of Computer Science Slide No.


● When i = 2, W = 5
● The weight corresponding to the value 2 is 4, i.e., w2 = 4. Since we have two items in the
set having weights 3 and 4, and the weight of the knapsack is 5. We can put item of weight
4 in a knapsack and the profit corresponding to weight is 3, so we add 3 at M[2][5] shown
as below:
● When i = 2, W = 6
● The weight corresponding to the value 2 is 4, i.e., w2 = 4. Since we have two items in the
set having weights 3 and 4, and the weight of the knapsack is 6. We can put item of weight
4 in a knapsack and the profit corresponding to weight is 3, so we add 3 at M[2][6] shown
as below:
● When i = 2, W = 7
● The weight corresponding to the value 2 is 4, i.e., w2 = 4. Since we have two items in the
set having weights 3 and 4, and the weight of the knapsack is 7. We can put item of weight
4 and 3 in a knapsack and the profits corresponding to weights are 2 and 3; therefore, the
total profit is 5, so we add 5 at M[2][7] shown as below:

05/28/2025 Department of Computer Science Slide No.


● When i = 2, W = 8
● The weight corresponding to the value 2 is 4, i.e., w2 = 4. Since we have two items in
the set having weights 3 and 4, and the weight of the knapsack is 7. We can put item
of weight 4 and 3 in a knapsack and the profits corresponding to weights are 2 and
3; therefore, the total profit is 5, so we add 5 at M[2][7] shown as below:

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

05/28/2025 Department of Computer Science Slide No.


● Now the value of 'i' gets incremented, and becomes 3.
● When i = 3, W = 1
● The weight corresponding to the value 3 is 5, i.e., w3 = 5. Since we have three items in the
set having weights 3, 4, and 5, and the weight of the knapsack is 1. We cannot put neither
of the items in a knapsack, so we add 0 at M[3][1] shown as below:
● When i = 3, W = 2
● The weight corresponding to the value 3 is 5, i.e., w3 = 5. Since we have three items in the
set having weight 3, 4, and 5, and the weight of the knapsack is 1. We cannot put neither of
the items in a knapsack, so we add 0 at M[3][2] shown as below:
● When i = 3, W = 3
● The weight corresponding to the value 3 is 5, i.e., w3 = 5. Since we have three items in the
set of weight 3, 4, and 5 respectively and weight of the knapsack is 3. The item with a
weight 3 can be put in the knapsack and the profit corresponding to the item is 2, so we
add 2 at M[3][3] shown as below:

05/28/2025 Department of Computer Science Slide No.


● When i = 3, W = 4
● The weight corresponding to the value 3 is 5, i.e., w3 = 5. Since we have three items in the
set of weight 3, 4, and 5 respectively, and weight of the knapsack is 4. We can keep the
item of either weight 3 or 4; the profit (3) corresponding to the weight 4 is more than the
profit corresponding to the weight 3 so we add 3 at M[3][4] shown as below:
● When i = 3, W = 5
● The weight corresponding to the value 3 is 5, i.e., w3 = 5. Since we have three items in the
set of weight 3, 4, and 5 respectively, and weight of the knapsack is 5. We can keep the
item of either weight 3, 4 or 5; the profit (3) corresponding to the weight 4 is more than
the profits corresponding to the weight 3 and 5 so we add 3 at M[3][5] shown as below:
● When i =3, W = 6
● The weight corresponding to the value 3 is 5, i.e., w3 = 5. Since we have three items in the
set of weight 3, 4, and 5 respectively, and weight of the knapsack is 6. We can keep the
item of either weight 3, 4 or 5; the profit (3) corresponding to the weight 4 is more than
the profits corresponding to the weight 3 and 5 so we add 3 at M[3][6] shown as below:

05/28/2025 Department of Computer Science Slide No.


● When i =3, W = 7
● The weight corresponding to the value 3 is 5, i.e., w3 = 5. Since we have three items in the
set of weight 3, 4, and 5 respectively, and weight of the knapsack is 7. In this case, we can
keep both the items of weight 3 and 4, the sum of the profit would be equal to (2 + 3), i.e.,
5, so we add 5 at M[3][7] shown as below:
● When i = 3, W = 8
● The weight corresponding to the value 3 is 5, i.e., w3 = 5. Since we have three items in the
set of weight 3, 4, and 5 respectively, and the weight of the knapsack is 8. In this case, we
can keep both the items of weight 3 and 4, the sum of the profit would be equal to (2 + 3),
i.e., 5, so we add 5 at M[3][8] shown as below:

05/28/2025 Department of Computer Science Slide No.


05/28/2025 Department of Computer Science Slide No.
● Now the value of 'i' gets incremented and becomes 4.
● When i = 4, W = 1
● The weight corresponding to the value 4 is 6, i.e., w4 = 6. Since we have four items in the
set of weights 3, 4, 5, and 6 respectively, and the weight of the knapsack is 1. The weight
of all the items is more than the weight of the knapsack, so we cannot add any item in the
knapsack; Therefore, we add 0 at M[4][1] shown as below:
● When i = 4, W = 2
● The weight corresponding to the value 4 is 6, i.e., w4 = 6. Since we have four items in the
set of weights 3, 4, 5, and 6 respectively, and the weight of the knapsack is 2. The weight
of all the items is more than the weight of the knapsack, so we cannot add any item in the
knapsack; Therefore, we add 0 at M[4][2] shown as below:
● When i = 4, W = 3
● The weight corresponding to the value 4 is 6, i.e., w4 = 6. Since we have four items in the
set of weights 3, 4, 5, and 6 respectively, and the weight of the knapsack is 3. The item
with a weight 3 can be put in the knapsack and the profit corresponding to the weight 4 is
2, so we will add 2 at M[4][3] shown as below:
05/28/2025 Department of Computer Science Slide No.
● When i = 4, W = 4
● The weight corresponding to the value 4 is 6, i.e., w4 = 6. Since we have four items
in the set of weights 3, 4, 5, and 6 respectively, and the weight of the knapsack is 4.
The item with a weight 4 can be put in the knapsack and the profit corresponding to
the weight 4 is 3, so we will add 3 at M[4][4] shown as below:
● When i = 4, W = 5
● The weight corresponding to the value 4 is 6, i.e., w4 = 6. Since we have four items
in the set of weights 3, 4, 5, and 6 respectively, and the weight of the knapsack is
5. The item with a weight 4 can be put in the knapsack and the profit
corresponding to the weight 4 is 3, so we will add 3 at M[4][5] shown as below:
● When i = 4, W = 6
● The weight corresponding to the value 4 is 6, i.e., w4 = 6. Since we have four items in the
set of weights 3, 4, 5, and 6 respectively, and the weight of the knapsack is 6. In this case,
we can put the items in the knapsack either of weight 3, 4, 5 or 6 but the profit, i.e., 4
corresponding to the weight 6 is highest among all the items; therefore, we add 4 at M[4]
[6] shown as below:
05/28/2025 Department of Computer Science Slide No.
● When i = 4, W = 7
● The weight corresponding to the value 4 is 6, i.e., w4 = 6. Since we have four items
in the set of weights 3, 4, 5, and 6 respectively, and the weight of the knapsack is 7.
Here, if we add two items of weights 3 and 4 then it will produce the maximum profit,
i.e., (2 + 3) equals to 5, so we add 5 at M[4][7] shown as below:
● When i = 4, W = 8
● The weight corresponding to the value 4 is 6, i.e., w4 = 6. Since we have four items
in the set of weights 3, 4, 5, and 6 respectively, and the weight of the knapsack is 8.
Here, if we add two items of weights 3 and 4 then it will produce the maximum profit,
i.e., (2 + 3) equals to 5, so we add 5 at M[4][8] shown as below:

05/28/2025 Department of Computer Science Slide No.


Final table
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 0 0 2 3 3 3 5 5

4 0 0 0 2 3 3 4 5 5

05/28/2025 Department of Computer Science Slide No.


● As we can observe in the above table that 5 is the maximum profit among all the entries.
The pointer points to the last row and the last column having 5 value. Now we will
compare 5 value with the previous row; if the previous row, i.e., i = 3 contains the same
value 5 then the pointer will shift upwards. Since the previous row contains the value 5 so
the pointer will be shifted upwards as shown in the below table:
● Again, we will compare the value 5 from the above row, i.e., i = 2. Since the above row
contains the value 5 so the pointer will again be shifted upwards :
● Again, we will compare the value 5 from the above row, i.e., i = 1. Since the above row
does not contain the same value so we will consider the row i=1, and the weight
corresponding to the row is 4. Therefore, we have selected the weight 4 and we have
rejected the weights 5 and 6 shown below:
● x = { 1, 0, 0}
● The profit corresponding to the weight is 3. Therefore, the remaining profit is (5 - 3) equals
to 2. Now we will compare this value 2 with the row i = 2. Since the row (i = 1) contains the
value 2; therefore, the pointer shifted upwards shown below:

05/28/2025 Department of Computer Science Slide No.


● Again we compare the value 2 with a above row, i.e., i = 1. Since the row i
=0 does not contain the value 2, so row i = 1 will be selected and the
weight corresponding to the i = 1 is 3 shown below:
● X = {1, 1, 0, 0}
● The profit corresponding to the weight is 2. Therefore, the remaining profit
is 0. We compare 0 value with the above row. Since the above row
contains a 0 value but the profit corresponding to this row is 0. In this
problem, two weights are selected, i.e., 3 and 4 to maximize the profit.

05/28/2025 Department of Computer Science Slide No.


0-1 Knapsack Algorithm
Dynamic-0-1-knapsack (v, w, n, c[i, w] = vi + c[i-1, w-wi]
W) else c[i, w] = c[i-1, w]
for w = 0 to W do else
c[0, w] = 0 c[i, w] = c[i-1, w]
for i = 1 to n do
c[i, 0] = 0
for w = 1 to W do
if wi ≤ w then
if vi + c[i-1, w-wi] then

05/28/2025 Department of Computer Science Slide No.


● Problem Statement − A thief is ● The algorithm takes the following
robbing a store and can carry a inputs
maximal weight of W into his ● The maximum weight W
knapsack. There are n items and
weight of ith item is wi and the profit of ● The number of items n
selecting this item is pi. What items ● The two sequences v = <v1, v2, …,
should the thief take? vn> and w = <w1, w2, …, wn>
● Let i be the highest-numbered item in ● The set of items to take can be
an optimal solution S for W dollars. deduced from the table, starting
Then S’ = S − {i} is an optimal solution at c[n, w] and tracing backwards
for W – wi dollars and the value to the where the optimal values came from.
solution S is Vi plus the value of the ● If c[i, w] = c[i-1, w], then item i is not
sub-problem. part of the solution, and we continue
● We can express this fact in the tracing with c[i-1, w]. Otherwise, item i
following formula: define c[i, w] to be is part of the solution, and we continue
the solution for items 1,2, … , i and the tracing with c [i-1, w-W].
maximum weight w.
05/28/2025 Department of Computer Science Slide No.
Floyd Warshall Algorithm
● The Floyd Warshall Algorithm is for solving all pairs of shortest-path problems.
The problem is to find the shortest distances between every pair of vertices in a
given edge-weighted directed Graph.
● It is an algorithm for finding the shortest path between all the pairs of vertices in
a weighted graph. This algorithm follows the dynamic programming approach to
find the shortest path.
● A C-function for a N x N graph is given below. The function stores the all pair
shortest path in the matrix cost [N][N]. The cost matrix of the given graph is
available in cost Mat [N][N].

05/28/2025 Department of Computer Science Slide No.


Floyd Warshall Algorithm:
# define N 4 for(j=0; j<N; j++)
void floydwarshall() if(cost [i][j]> cost [i] [k] + cost [k][j];
{ cost [i][j]=cost [i] [k]+'cost [k] [j]:
int cost [N][N]; }
int i, j, k; //display the matrix cost [N] [N]
for(i=0; i<N; i++) }
for(j=0; j<N; j++)
cost [i][j]= cost Mat [i] [i];
for(k=0; k<N; k++)
{
for(i=0; i<N; i++)

05/28/2025 Department of Computer Science Slide No.


How algorithm Works?
● Initialize the solution matrix same as the input graph matrix as a first step.
● Then update the solution matrix by considering all vertices as an intermediate
vertex.
● The idea is to one by one pick all vertices and updates all shortest paths which
include the picked vertex as an intermediate vertex in the shortest path.
● When we pick vertex number k as an intermediate vertex, we already have
considered vertices {0, 1, 2, .. k-1} as intermediate vertices.
● For every pair (i, j) of the source and destination vertices respectively, there are
two possible cases.
○ k is not an intermediate vertex in shortest path from i to j. We keep the value of dist[i][j] as it is.

○ 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]

05/28/2025 Department of Computer Science Slide No.


Let's see at an example to illustrate how the Floyd-Warshall
algorithm works:
● Follow the steps below to find the
shortest path between all the pairs of
vertices.
● Create a matrix A0 of
dimension n*n where n is the number of
vertices. The row and the column are
indexed as i and j respectively. i and j are
the vertices of the graph.

1. Each cell A[i][j] is filled with the distance


from the ith vertex to the jth vertex. If there is
no path from ith vertex to jth vertex, the cell is
left as infinity.

05/28/2025 Department of Computer Science Slide No.


2. Fill each cell with the distance between ith Let k be the intermediate vertex in the
and jth vertex shortest path from source to destination. In
this step, k is the first vertex. A[i][j] is filled
with (A[i][k] + A[k][j]) if (A[i][j] > A[i][k] + A[k]
[j]).

That is, if the direct distance from the source


to the destination is greater than the path
through the vertex k, then the cell is filled
with A[i][k] + A[k][j].

In this step, k is vertex 1. We calculate the


Now, create a matrix A1 using matrix A0. The distance from source vertex to destination
elements in the first column and the first row vertex through this vertex k.
are left as they are. The remaining cells are
filled in the following way.

05/28/2025 Department of Computer Science Slide No.


For example: For A1[2, 4], the direct distance
from vertex 2 to 4 is 4 and the sum of the
distance from vertex 2 to 4 through vertex
(ie. from vertex 2 to 1 and from vertex 1 to 4)
is 7. Since 4 < 7, A0[2, 4] is filled with 4.

05/28/2025 Department of Computer Science Slide No.


3. Similarly, A2 is created using A1. The elements in the second column and the second row are
left as they are.

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

05/28/2025 Department of Computer Science Slide No.


4. Similarly, A3 and A4 is also created.

Calculate the distance from the source vertex to destination vertex through this vertex 3

05/28/2025 Department of Computer Science Slide No.


Calculate the distance from the source vertex to destination vertex through this
vertex 4.

5. A4 gives the shortest path between each pair of vertices.

05/28/2025 Department of Computer Science Slide No.


Floyd-Warshall Algorithm
○ n = no of vertices
○ A = matrix of dimension n*n
○ for k = 1 to n
○ for i = 1 to n
○ for j = 1 to n
○ Ak[i, j] = min (Ak-1[i, j], Ak-1[i, k] + Ak-1[k, j])
○ return A

05/28/2025 Department of Computer Science Slide No.


Example 2

05/28/2025 Department of Computer Science Slide No.


Floyd Warshall Algorithm Complexity
● Time Complexity
There are three loops. Each loop has constant complexities. So, the time
complexity of the Floyd-Warshall algorithm is O(n3).
● Space Complexity
The space complexity of the Floyd-Warshall algorithm is O(n2).

05/28/2025 Department of Computer Science Slide No.


Floyd Warshall Algorithm Applications
1. To find the shortest path is a directed graph
2. To find the transitive closure of directed graphs
3. To find the Inversion of real matrices
4. For testing whether an undirected graph is bipartite

05/28/2025 Department of Computer Science Slide No.


05/28/2025 Department of Computer Science Slide No.
05/28/2025 Department of Computer Science Slide No.
05/28/2025 Department of Computer Science Slide No.
05/28/2025 Department of Computer Science Slide No.
05/28/2025 Department of Computer Science Slide No.
05/28/2025 Department of Computer Science Slide No.
05/28/2025 Department of Computer Science Slide No.
<Session Name>: Source
https://www.geeksforgeeks.org/

05/28/2025 Department of Computer Science Slide No.


Time for a Break !

05/28/2025 Department of Computer Science Slide No.


Any Doubts/Questions

05/28/2025 Department of Computer Science Slide No.


Thank You

05/28/2025 Department of Computer Science 70


Icons To Be Used (Suggestions Only)

Hands on
Doubts/ Tools Exercise
Questions

Coding Test Your


Reference
Standards Understanding

A Welcome Contacts
Demonstration Break

05/28/2025 Department of Computer Science Faculty Name: _____________ Slide No.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy