0/1 Knapsack Problem
Tuesday, January 3, 2023 8:39 PM
Dynamic Programming Approach:
• We first start by building a table consisting of rows of values and columns of
weights.
• Starting from the first row and column, we keep on filling the table till the desired
value is obtained.
• Let the table be represented as 𝑇[𝑛, 𝑤] where n is the number of items to be
included and 𝑤 is the left over space in the bag.
• For every object, there can be two possibilities:
• if the object's weight is greater than the leftover space in the bag, then
𝑇[𝑛, 𝑤] = T[𝑛 − 1, 𝑤]
• else, the object might be taken or left out.
• For every object, there can be two possibilities:Type equation here.
• if it is take, the total value of the bag becomes
𝑇[𝑛, 𝑤] = 𝑣 + 𝑇[𝑛 − 1, 𝑤 − 𝑤 ]
Where “𝒗𝒏” is value of the 𝒏th object and 𝒘𝒏 is its weight.
• If the object is not included
𝑇[𝑛, 𝑤] = 𝑇[𝑛 − 1, 𝑤]
• the value of T[n,w] is the maximum of both these cases.
• In short, the optimal substructure to compute dp[n,w] is
𝑇[𝑛 − 1, 𝑤], 𝑖𝑓 𝑤 > 𝑤
𝑇[𝑛, 𝑤] =
max{𝑇[𝑛 − 1, 𝑤], 𝑣 + 𝑇[𝑛 − 1, 𝑤 − 𝑤 ]} , 𝑜𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒
Procedure
• Step-01:
• Draw a table say ‘T’ with (n+1) number of rows and (w+1) number of columns.
Fill all the boxes of 0th row and 0th column with zeroes.
• Step-02:
• Start filling the table row wise top to bottom from left to right.
• Use the following formula-
• 𝑇 (𝑖 , 𝑗) = max{ 𝑇 ( 𝑖 − 1 , 𝑗 ) , 𝑣𝑎𝑙𝑢𝑒𝑖 + 𝑇( 𝑖 − 1 , 𝑗 – 𝑤𝑒𝑖𝑔ℎ𝑡𝑖 ) }
• Here, 𝑇(𝑖 , 𝑗) = maximum value of the selected items if we can take items 1 𝑡𝑜 𝑖 and
have weight restrictions of 𝑗.
• Step-03:
• To identify the items that must be put into the knapsack to obtain that maximum profit,
• Consider the last column of the table.
• Start scanning the entries from bottom to top.
• On encountering an entry whose value is not same as the value stored in the entry
immediately above it, mark the row label of that entry.
• After all the entries are scanned, the marked labels represent the items that must
Dynamic Programming Page 1
• After all the entries are scanned, the marked labels represent the items that must
be put into the knapsack.
• EXAMPLE:
• Find the optimal solution for the 0/1 knapsack problem making use of dynamic
programming approach. Consider n = 4, w = 5 kg, (w1, w2, w3, w4) = (2, 3, 4, 5),
(b1, b2, b3, b4) = (3, 4, 5, 6)
ITEM WEIGHT VALUE
1 2 3
2 3 4
3 4 5
4 5 6
𝑻 (𝒊 , 𝒋) = 𝐦𝐚𝐱{ 𝑻 ( 𝒊 − 𝟏 , 𝒋 ) , 𝒗𝒂𝒍𝒖𝒆𝒊 + 𝑻( 𝒊 − 𝟏 , 𝒋 – 𝒘𝒆𝒊𝒈𝒕𝒉𝒊 ) }
T 0 1 2 3 4 5
ITEM WEIG VALU
HT E 0 0 0 0 0 0 0
1 2 3 1 0
2 3 4 2 0
3 4 5 3 0
4 5 6 4 0
Dynamic Programming Page 2
T 0 1 2 3 4 5
ITEM WEIG VALU
HT E 0 0 0 0 0 0 0
1 2 3 1 0
2 3 4 2 0
3 4 5 3 0
4 5 6 4 0
ITEM WEIG VALU T 0 1 2 3 4 5
HT E 0 0 0 0 0 0 0
1 2 3 1 0
2 3 4 2 0
3 4 5
3 0
4 5 6
4 0
T 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0
2 0
3 0
4 0
Dynamic Programming Page 3
2 0
3 0
4 0
T 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0
2 0
3 0
4 0
Dynamic Programming Page 4