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

Knapsack Problem Using Dynamic Problem Solving

Dynamic programming can be used to solve the 0-1 knapsack problem optimally. The problem involves selecting items to fill a knapsack without exceeding its weight capacity W, to maximize total value or profit. A table V[i,w] is constructed with rows for each item i and columns for weights w=0 to W. Table cells are filled based on whether item i weight is less than w, choosing that item's value plus the value of the optimal solution for the remaining capacity. The maximum table value is the optimal solution, and backtracking reveals the selected items.

Uploaded by

KANAK SHARMA
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
81 views

Knapsack Problem Using Dynamic Problem Solving

Dynamic programming can be used to solve the 0-1 knapsack problem optimally. The problem involves selecting items to fill a knapsack without exceeding its weight capacity W, to maximize total value or profit. A table V[i,w] is constructed with rows for each item i and columns for weights w=0 to W. Table cells are filled based on whether item i weight is less than w, choosing that item's value plus the value of the optimal solution for the remaining capacity. The maximum table value is the optimal solution, and backtracking reveals the selected items.

Uploaded by

KANAK SHARMA
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

0-1 Knapsack Problem Using Dynamic Programming

In 0-1 Knapsack, items cannot be broken which means the thief should take the item as a whole or should leave
it. This is reason behind calling it as 0-1 Knapsack.

Hence, in case of 0-1 Knapsack, the value of xi can be either 0 or 1, where other constraints remain the same.

0-1 Knapsack cannot be solved by Greedy approach. Greedy approach does not ensure an optimal solution. In
many instances, Greedy approach may give an optimal solution.

The following examples will establish our statement.

Example-1
Let us consider that the capacity of the knapsack is W = 25 and the items are as shown in the following table.

Item A B C D
Profit 24 18 18 10
Weight 24 10 10 7

Without considering the profit per unit weight (pi/wi), if we apply Greedy approach to solve this problem, first
item A will be selected as it will contribute maximum profit among all the elements. After selecting item A, no
more items will be selected. Hence, for this given set of items total profit is 24. Whereas, the optimal solution
can be achieved by selecting items, B and C, where the total profit is 18 + 18 = 36.

Example-2
Instead of selecting the items based on the overall benefit, in this example the items are selected based on ratio
pi/wi. Let us consider that the capacity of the knapsack is W = 60 and the items are as shown in the following
table.
Item A B C
Price 100 280 120
Weight 10 40 20
Ratio 10 7 6

Using the Greedy approach, first item A is selected. Then, the next item B is chosen. Hence, the total profit is
100 + 280 = 380. However, the optimal solution of this instance can be achieved by selecting items, B and C,
where the total profit is 280 + 120 = 400.

Hence, it can be concluded that Greedy approach may not give an optimal solution. To solve 0-1 Knapsack,
Dynamic Programming approach is required.

Problem Statement

A thief is robbing a store and can carry a maximal weight of W into his knapsack. There are n items and weight
of i th item is wi and the profit of selecting this item is pi. What items should the thief take?

35 [Dynamic Programing]
Point to remember:-
 In this problem we have a Knapsack that has a weight limit W.
 There are items i1, i2, ..., i n each having weight w 1, w2, … wn and some benefit (value or profit) associated
with it v1, v2, ... vn
 Our objective is to maximize the benefit such that the total weight inside the knapsack is at most W.
 Since this is a 0-1 Knapsack problem so we can either take an entire item or reject it completely. We
cannot break an item and fill the knapsack.

Assume that we have a knapsack with max weight capacity W = 5


Our objective is to fill the knapsack with items such that the benefit (value or profit) is maximum.

Following table contains the items along with their value and weight.

item (i) 1 2 3 4
value (val) 100 20 60 40
weight (wt) 3 2 4 1

Total items n = 4
Total capacity of the knapsack W = 5
Now we create a value table V[i,w] where, i denotes number of items and w denotes the weight of the items.
Rows denote the items and columns denote the weight.
As there are 4 items so, we have 5 rows from 0 to 4.
And the weight limit of the knapsack is W = 5 so, we have 6 columns from 0 to 5

We fill the first row i = 0 with 0. This means when 0 item is considered weight is 0.Then we fill the first column
w = 0 with 0. This means when weight is 0 then items considered is 0.

36 [Dynamic Programing]
Follow the rules fill the V[i,w] table.

if wt[i] > w then


V[i,w] = V[i-1,w]
else if wt[i] <= w then
V[i,w] = max( V[i-1,w], val[i] + V[i-1, w - wt[i]] )

Fill Row i = 1:-

37 [Dynamic Programing]
38 [Dynamic Programing]
39 [Dynamic Programing]
In this way we can calculate all the values of table V. After calculation, the value table V:-

V[i,w] w=0 1 2 3 4 5
i=0 0 0 0 0 0 0
1 0 0 0 100 100 100
2 0 0 20 100 100 120
3 0 0 20 100 100 120
4 0 40 40 100 140 140

So, its time find the maximum value that we earned and the items that we put inside the knapsack.
Maximum value earned
Max Value = V[n,W]= V[4,5] = 140
Items that were put inside the knapsack are found using the following rule

set i = n and w = W
while i and w > 0 do
if V[i,w] != V[i-1,w] then
mark the ith item

40 [Dynamic Programing]
set w = w - wt[i]
set i = i - 1
else
set i = i - 1
endif
endwhile

41 [Dynamic Programing]
Now i= 0, so we will stop here. So, items we are putting inside the knapsack are 4 and 1.

Time complexity:
Time complexity of 0-1 Knapsack problem is O (nW) where, n is the number of items and W is the capacity of
knapsack.

42 [Dynamic Programing]

You might also like