0-1 knapsack problem

ASMShafi 161 views 6 slides Jul 06, 2021
Slide 1
Slide 1 of 6
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6

About This Presentation

0-1 knapsack problem


Slide Content

Dynamic Programming
Dynamic Programming (DP) is an algorithmic technique for solving an optimization problem by
breaking it down into simpler subproblems and utilizing the fact that the optimal solution to the
overall problem depends upon the optimal solution to its subproblems.
Dynamic programming approach is similar to divide and conquer in breaking down the problem
into smaller and yet smaller possible sub-problems. But unlike, divide and conquer, these sub-
problems are not solved independently. Rather, results of these smaller sub-problems are
remembered and used for similar or overlapping sub-problems.
Dynamic programming is used where we have problems, which can be divided into similar sub-
problems, so that their results can be re-used.
Example
The following computer problems can be solved using dynamic programming approach −
 Fibonacci number series
 Knapsack problem
 Tower of Hanoi
 All pair shortest path by Floyd-Warshall
 Shortest path by Dijkstra
 Project scheduling

0–1 Knapsack Problem
In the 0-1 Knapsack problem, we are given a set of items, each with a weight and a value, and we
need to determine the number of each item to include in a collection so that the total weight is less
than or equal to a given limit and the total value is large as possible.
Please note that the items are invisible, we can either take an item or not (0-1) property. For
example,
Input:

value = [20, 5, 10, 40, 15, 25]
weight = [1, 2, 3, 8, 7, 4]
Maximum Weight, W = 10

Output: Knapsack value is 60

value = 20 + 40 = 60
weight = 1 + 8 = 9 < W

Problem
For the given set of items and knapsack capacity = 5 kg, find the optimal solution for the 0/1
knapsack problem making use of dynamic programming approach.
Item Weight Value
1 2 3
2 3 4
3 4 5
4 5 6

Solution-
Given-
 Knapsack capacity (w) = 5 kg
 Number of items (n) = 4
Step-01
 Draw a table say ‘T’ with (n+1) = 4 + 1 = 5 number of rows and (w+1) = 5 + 1 = 6 number
of columns.
 Fill all the boxes of 0
th
row and 0
th
column with 0.


Step-02:
Start filling the table row wise top to bottom from left to right using the formula-
T (i , j) = max { T ( i-1 , j ) , valuei + T( i-1 , j – weighti ) }
Finding T(1,1)
We have,
 i = 1
 j = 1
 (value)i = (value)1 = 3

 (weight)i = (weight)1 = 2
Substituting the values, we get-
T(1,1) = max { T(1-1 , 1) , 3 + T(1-1 , 1-2) }
T(1,1) = max { T(0,1) , 3 + T(0,-1) }
T(1,1) = T(0,1) { Ignore T(0,-1) }
T(1,1) = 0
Finding T(1,2)-
We have,
 i = 1
 j = 2
 (value)i = (value)1 = 3
 (weight)i = (weight)1 = 2
Substituting the values, we get-
T(1,2) = max { T(1-1 , 2) , 3 + T(1-1 , 2-2) }
T(1,2) = max { T(0,2) , 3 + T(0,0) }
T(1,2) = max {0 , 3+0}
T(1,2) = 3
Finding T(1,3)-
We have,
 i = 1
 j = 3
 (value)i = (value)1 = 3
 (weight)i = (weight)1 = 2
Substituting the values, we get-
T(1,3) = max { T(1-1 , 3) , 3 + T(1-1 , 3-2) }
T(1,3) = max { T(0,3) , 3 + T(0,1) }
T(1,3) = max {0 , 3+0}
T(1,3) = 3
Finding T(1,4)-
We have,
 i = 1
 j = 4
 (value)i = (value)1 = 3

 (weight)i = (weight)1 = 2
Substituting the values, we get-
T(1,4) = max { T(1-1 , 4) , 3 + T(1-1 , 4-2) }
T(1,4) = max { T(0,4) , 3 + T(0,2) }
T(1,4) = max {0 , 3+0}
T(1,4) = 3
Finding T(1,5)-
We have,
 i = 1
 j = 5
 (value)i = (value)1 = 3
 (weight)i = (weight)1 = 2
Substituting the values, we get-
T(1,5) = max { T(1-1 , 5) , 3 + T(1-1 , 5-2) }
T(1,5) = max { T(0,5) , 3 + T(0,3) }
T(1,5) = max {0 , 3+0}
T(1,5) = 3
Finding T(2,1)-
We have,
 i = 2
 j = 1
 (value)i = (value)2 = 4
 (weight)i = (weight)2 = 3
Substituting the values, we get-
T(2,1) = max { T(2-1 , 1) , 4 + T(2-1 , 1-3) }
T(2,1) = max { T(1,1) , 4 + T(1,-2) }
T(2,1) = T(1,1) { Ignore T(1,-2) }
T(2,1) = 0
Finding T(2,2)-
We have,
 i = 2
 j = 2
 (value)i = (value)2 = 4

 (weight)i = (weight)2 = 3
Substituting the values, we get-
T(2,2) = max { T(2-1 , 2) , 4 + T(2-1 , 2-3) }
T(2,2) = max { T(1,2) , 4 + T(1,-1) }
T(2,2) = T(1,2) { Ignore T(1,-1) }
T(2,2) = 3
Finding T(2,3)-
We have,
 i = 2
 j = 3
 (value)i = (value)2 = 4
 (weight)i = (weight)2 = 3
Substituting the values, we get-
T(2,3) = max { T(2-1 , 3) , 4 + T(2-1 , 3-3) }
T(2,3) = max { T(1,3) , 4 + T(1,0) }
T(2,3) = max { 3 , 4+0 }
T(2,3) = 4
Finding T(2,4)-
We have,
 i = 2
 j = 4
 (value)i = (value)2 = 4
 (weight)i = (weight)2 = 3
Substituting the values, we get-
T(2,4) = max { T(2-1 , 4) , 4 + T(2-1 , 4-3) }
T(2,4) = max { T(1,4) , 4 + T(1,1) }
T(2,4) = max { 3 , 4+0 }
T(2,4) = 4
Finding T(2,5)-
We have,
 i = 2
 j = 5
 (value)i = (value)2 = 4

 (weight)i = (weight)2 = 3
Substituting the values, we get-
T(2,5) = max { T(2-1 , 5) , 4 + T(2-1 , 5-3) }
T(2,5) = max { T(1,5) , 4 + T(1,2) }
T(2,5) = max { 3 , 4+3 }
T(2,5) = 7
Similarly, compute all the entries.
After all the entries are computed and filled in the table, we get the following table-

0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0 0 3 4 5 7
4 0 0 3 4 5 7


 The last entry represents the maximum possible value that can be put into the knapsack.
 So, maximum possible value that can be put into the knapsack = 7.
Algorithm of Knapsack Problem
KNAPSACK (n, W)
1. for w = 0, W
2. do V [0,w] ← 0
3. for i=0, n
4. do V [i, 0] ← 0
5. for w = 0, W
6. do if (wi≤ w & vi + V [i-1, w - wi]> V [i -1,W])
7. then V [i, W] ← v i + V [i - 1, w - wi]
8. else V [i, W] ← V [i - 1, w]