1
Problem Statement
You have prepared a list of n objects for which you are interested to
buy, The items are numbered as i1, i2, . . ., in and Capacity of bag is W.
Each item i has value vi, and weigh wi.We want to select a set of items
among i1, i2, . . .,in which do not exceed capacity W of the bag. Total
value of selected items must be maximum
0-1 Knapsack Problem
Dynamic Programming – Bottom Up Approach
2
Step 1: Characterize the Structure of the problem
Original Problem:
V[n, W] = contains maximum value of the items selected from
{1,2,…,n} that can fit into the bag with capacity W
storage.
Generalized Problem:
V[i, w] = maximum value of items selected from {1, 2,. . ., i}, that
can fit into a bag with capacity w, where 1 ≤ i ≤ n, 1 ≤ w ≤
W
0-1 Knapsack Problem
Dynamic Programming – Bottom Up Approach
3
Step 1: Characterize the Structure of the problem
To compute V[i, w], we have only two choices for i
1. Do not Select Item i
Items left = {1,2,. . . , i - 1} and storage limit = w
Hence, Max. value selected from {1,2, …,i} = V[i-1,w]
2. Select Item i (possible wi ≤ w)
In this way, we gain value vi but use capacity wi
Items left = {1,2,. . . , i-1} and storage limit = w - wi
Max. value, from items {1,2, …,i-1} = V[i-1,w – wi]
Total value = vi + V[i-1,w – wi] - if we select item i
Finally, the solution will be optimal if we take the maximum of
V[i-1,w] and vi + V[i-1,w – wi]
0-1 Knapsack Problem
Dynamic Programming – Bottom Up Approach
4
Step 2: A recursive solution
Base Case:
V[0, w] = 0 for 0 ≤ w ≤ W, no items are available
V[0, w] = -∞ for w < 0, invalid
V[i, 0] = 0 for 0 ≤ i ≤ n, no capacity available
Recursion:
V[i, w] = max ( V[i-1, w], vi + V[i-1, w - wi] )
1 ≤ i ≤ n , 0 ≤ w ≤ W
0-1 Knapsack Problem
Dynamic Programming – Bottom Up Approach
5
Step 3: Computing the Optimal Value
V[1, 1] = V[1, 2] = V[1, 3] = V[1, 4] = 0
V[i, w] = max ( V[i-1, w], vi + V[i-1, w - wi] )
V[1, 5] = max ( V[0, 5], v1 + V[0, 5 – w1] ) = max ( 0 , 10 + V[0, 5 - 5] )
= max ( 0 , 10 + 0 ) = 10 Keep [1, 5] = 1
V[1, 6] = max ( V[0, 6], v1 + V[0, 6 – w1] ) = max ( 0 , 10 + V[0, 6 - 5] )
= max ( 0 , 10 + 0 ) = 10 Keep [1, 6] = 1
V[1, 7] = max ( V[0, 7], v1 + V[0, 7 – w1] ) = max ( 0 , 10 + V[0, 7 - 5] )
= max ( 0 , 10 + 0 ) = 10 Keep [1, 7] = 1
V[1, 8] = max ( V[0, 8], v1 + V[0, 8 – w1] ) = max ( 0 , 10 + V[0, 8 - 5] )
= max ( 0 , 10 + 0 ) = 10 Keep [1, 8] = 1
V[1, 9] = max ( V[0, 9], v1 + V[0, 9 – w1] ) = max ( 0 , 10 + V[0, 9 - 5] )
= max ( 0 , 10 + 0 ) = 10 Keep [1, 9] = 1
V[1, 10] = max ( V[0, 10], v1 + V[0, 10 – w1] ) = max ( 0 , 10 + V[0, 10 - 5] )
= max ( 0 , 10 + 0 ) = 10 Keep [1, 10] = 1
0-1 Knapsack Problem
Dynamic Programming – Bottom Up Approach
6
Step 3: Computing the Optimal Value
V[2, 1] = V[2, 2] = V[2, 3] = 0
V[i, w] = max ( V[i-1, w], vi + V[i-1, w - wi] )
V[2, 4] = max ( V[1, 4], v2 + V[1, 4 – w2] ) = max ( 0 , 40 + V[1, 4 - 4] )
= max ( 0 , 40 + 0 ) = 40 Keep [2, 4] = 1
V[2, 5] = max ( V[1, 5], v2 + V[1, 5 – w2] ) = max ( 10 , 40 + V[1, 5 - 4] )
= max ( 10 , 40 + 0 ) = 40 Keep [2, 5] = 1
V[2, 6] = max ( V[1, 6], v2 + V[1, 6 – w2] ) = max ( 10 , 40 + V[1, 6 - 4] )
= max ( 10 , 40 + 0 ) = 40 Keep [2, 6] = 1
V[2, 7] = max ( V[1, 7], v2 + V[1, 7 – w2] ) = max ( 10 , 40 + V[1, 7 - 4] )
= max ( 10 , 40 + 0 ) = 40 Keep [2, 7] = 1
V[2, 8] = max ( V[1, 8], v2 + V[1, 8 – w2] ) = max ( 10 , 10 + V[1, 8 - 4] )
= max ( 10 , 40 + 0 ) = 10 Keep [2, 8] = 1
0-1 Knapsack Problem
Dynamic Programming – Bottom Up Approach
7
Step 3: Computing the Optimal Value
V[2, 1] = V[2, 2] = V[2, 3] = 0
V[i, w] = max ( V[i-1, w], vi + V[i-1, w - wi] )
V[2, 9] = max ( V[1, 9], v2 + V[1, 9 – w2] ) = max ( 10 , 40 + V[1, 9 - 4] )
= max ( 10 , 40 + 10 ) = 50 Keep [2, 9] = 1
V[2, 10] = max ( V[1, 10], v2 + V[1, 10 – w2] ) = max ( 10 , 40 + V[1, 10 - 4] )
= max ( 10 , 40 + 10 ) = 50 Keep [2, 10] = 1
0-1 Knapsack Problem
Dynamic Programming – Bottom Up Approach
8
Step 3: Computing the Optimal Value
V[3, 1] = V[3, 2] = V[3, 3] = 0
V[i, w] = max ( V[i-1, w], vi + V[i-1, w - wi] )
V[3, 4] = max ( V[2, 4], v3 + V[2, 4 – w3] ) = max ( 40 , 30 + V[2, 4 - 6] )
= max ( 40 , invalid ) = 40 Keep [3, 4] = 0
V[3, 5] = max ( V[2, 5], v3 + V[2, 5 – w3] ) = max ( 40 , 30 + V[2, 5 - 6] )
= max ( 40 , invalid ) = 40 Keep [3, 5] = 0
V[3, 6] = max ( V[2, 6], v3 + V[2, 6 – w3] ) = max ( 40 , 30 + V[2, 6 - 6] )
= max ( 40 , 30+0 ) = 40 Keep [3, 6] = 0
V[3, 7] = max ( V[2, 7], v3 + V[2, 7 – w3] ) = max ( 40 , 30 + V[2, 7 - 6] )
= max ( 40 , 30+0 ) = 40 Keep [3, 7] = 0
V[3, 8] = max ( V[2, 8], v3 + V[2, 8 – w3] ) = max ( 40 , 30 + V[2, 8 - 6] )
= max ( 40 , 30+0 ) = 40 Keep [3, 8] = 0
0-1 Knapsack Problem
Dynamic Programming – Bottom Up Approach
9
Step 3: Computing the Optimal Value
V[3, 1] = V[3, 2] = V[3, 3] = 0
V[i, w] = max ( V[i-1, w], vi + V[i-1, w - wi] )
V[3, 9] = max ( V[2, 9], v3 + V[2, 9 – w3] ) = max ( 50 , 30 + V[2, 9 - 6] )
= max ( 50 , 30+0 ) = 50 Keep [3, 9] = 0
V[3, 10] = max ( V[2, 10], v3 + V[2, 10 – w3] ) = max ( 50 , 30 + V[2, 10 - 6] )
= max ( 50 , 30+40 ) = 70 Keep [3, 10] = 1
0-1 Knapsack Problem
Dynamic Programming – Bottom Up Approach
10
Step 3: Computing the Optimal Value
V[4, 1] = V[4, 2] = 0
V[i, w] = max ( V[i-1, w], vi + V[i-1, w - wi] )
V[4, 3] = max ( V[3, 3], v4 + V[3, 3 – w4] ) = max ( 0 , 50 + V[3, 3 - 3] )
= max ( 0 , 50+0 ) = 50 Keep [4, 3] = 1
V[4, 4] = max ( V[3, 4], v4 + V[3, 4 – w4] ) = max ( 40 , 50 + V[3, 4 - 3] )
= max ( 40 , 50+0 ) = 50 Keep [4, 4] = 1
V[4, 5] = max ( V[3, 5], v4 + V[3, 5 – w4] ) = max ( 40 , 50 + V[3, 5 - 3] )
= max ( 40 , 50+0 ) = 50 Keep [4, 5] = 1
V[4, 6] = max ( V[3, 6], v4 + V[3, 6 – w4] ) = max ( 40 , 50 + V[3, 6 - 3] )
= max ( 40 , 50+0 ) = 50 Keep [4, 6] = 1
V[4, 7] = max ( V[3, 7], v4 + V[3, 7 – w4] ) = max ( 40 , 50 + V[3, 7 - 3] )
= max ( 40 , 50+40 ) = 90 Keep [4, 7] = 1
0-1 Knapsack Problem
Dynamic Programming – Bottom Up Approach
11
Step 3: Computing the Optimal Value
V[4, 1] = V[4, 2] = 0
V[i, w] = max ( V[i-1, w], vi + V[i-1, w - wi] )
V[4, 8] = max ( V[3, 8], v4 + V[3, 8 – w4] ) = max ( 50 , 50 + V[3, 8 - 3] )
= max ( 50 , 50+40 ) = 90 Keep [4, 8] = 1
V[4, 9] = max ( V[3, 9], v4 + V[3, 9 – w4] ) = max ( 50 , 50 + V[3, 9 - 3] )
= max ( 50 , 50+40 ) = 90 Keep [4, 9] = 1
V[4, 10] = max ( V[3, 10], v4 + V[3, 10 – w4] ) = max ( 70 , 50 + V[3, 10 - 3] )
= max ( 70 , 50+40 ) = 90 Keep [4, 10] = 1
0-1 Knapsack Problem
Dynamic Programming – Bottom Up Approach
1 2 3 4 5 6 7 8 9 10
0 0 0 0 0 0 0 0 0 0
0 0 0 0 10 10 10 10 10 10
0 0 0 40 40 40 40 40 50 50
0 0 0 40 40 40 40 40 50 70
0 0 50 50 50 50 90 90 90 90
W=0
0
0
0
0
0
W[i, w]
i = 0
i = 1
i = 2
i = 3
i = 4
12
Step 3: Pseudo Code to compute optimal value
KnapSack(v, w, n, W)
for (w = 0 to W), V[0, w] = 0; for (i = 1 to n), V[i, 0] = 0;
for (i = 1 to n)
for (w = 1 to W)
if ((w(i) ≤ w) and (vi + V[i-1,w – wi] > V[i-1,w]))
V[i, w] = (vi + V[i-1,w – wi];
keep[i, w] = 1;
else
V[i, w] = V[i-1,w];
keep[i, w] = 0;
K = W;
for (i = n down to 1)
if keep[i, K] = = 1
output i
K = K – wi
Return V[n, W]
0-1 Knapsack Problem
Dynamic Programming – Bottom Up Approach
13
Step 4: Construction of Optimal Solution
V[i, w] = max ( V[i-1, w], vi + [i-1, w – wi] );
i = 4
V[4, 10] = max (70, 50 + 40) = 90; Keep(4, 10) = 1
i = 3
V[3, 10 - 3] = V[3, 7] = max(40, 30) = 40; Keep(3, 7) = 0
i = 2
V[2, 7] = max(10, 40) = 40 Keep(2, 7) = 1
i = 1
V[1, 7-4] = V[1, 3] = 0 Keep(1, 3) = 0
0-1 Knapsack Problem
Dynamic Programming – Bottom Up Approach
Step 4: Pdeudo code to construct of optimal solution
K = W;
for (i = n down to 1)
if keep[i, K] = = 1
output i
K = K – wi
0-1 Knapsack Problem
Dynamic Programming – Bottom Up Approach

Knapsack Problem Analysis of Algorithm.ppt

  • 1.
    1 Problem Statement You haveprepared a list of n objects for which you are interested to buy, The items are numbered as i1, i2, . . ., in and Capacity of bag is W. Each item i has value vi, and weigh wi.We want to select a set of items among i1, i2, . . .,in which do not exceed capacity W of the bag. Total value of selected items must be maximum 0-1 Knapsack Problem Dynamic Programming – Bottom Up Approach
  • 2.
    2 Step 1: Characterizethe Structure of the problem Original Problem: V[n, W] = contains maximum value of the items selected from {1,2,…,n} that can fit into the bag with capacity W storage. Generalized Problem: V[i, w] = maximum value of items selected from {1, 2,. . ., i}, that can fit into a bag with capacity w, where 1 ≤ i ≤ n, 1 ≤ w ≤ W 0-1 Knapsack Problem Dynamic Programming – Bottom Up Approach
  • 3.
    3 Step 1: Characterizethe Structure of the problem To compute V[i, w], we have only two choices for i 1. Do not Select Item i Items left = {1,2,. . . , i - 1} and storage limit = w Hence, Max. value selected from {1,2, …,i} = V[i-1,w] 2. Select Item i (possible wi ≤ w) In this way, we gain value vi but use capacity wi Items left = {1,2,. . . , i-1} and storage limit = w - wi Max. value, from items {1,2, …,i-1} = V[i-1,w – wi] Total value = vi + V[i-1,w – wi] - if we select item i Finally, the solution will be optimal if we take the maximum of V[i-1,w] and vi + V[i-1,w – wi] 0-1 Knapsack Problem Dynamic Programming – Bottom Up Approach
  • 4.
    4 Step 2: Arecursive solution Base Case: V[0, w] = 0 for 0 ≤ w ≤ W, no items are available V[0, w] = -∞ for w < 0, invalid V[i, 0] = 0 for 0 ≤ i ≤ n, no capacity available Recursion: V[i, w] = max ( V[i-1, w], vi + V[i-1, w - wi] ) 1 ≤ i ≤ n , 0 ≤ w ≤ W 0-1 Knapsack Problem Dynamic Programming – Bottom Up Approach
  • 5.
    5 Step 3: Computingthe Optimal Value V[1, 1] = V[1, 2] = V[1, 3] = V[1, 4] = 0 V[i, w] = max ( V[i-1, w], vi + V[i-1, w - wi] ) V[1, 5] = max ( V[0, 5], v1 + V[0, 5 – w1] ) = max ( 0 , 10 + V[0, 5 - 5] ) = max ( 0 , 10 + 0 ) = 10 Keep [1, 5] = 1 V[1, 6] = max ( V[0, 6], v1 + V[0, 6 – w1] ) = max ( 0 , 10 + V[0, 6 - 5] ) = max ( 0 , 10 + 0 ) = 10 Keep [1, 6] = 1 V[1, 7] = max ( V[0, 7], v1 + V[0, 7 – w1] ) = max ( 0 , 10 + V[0, 7 - 5] ) = max ( 0 , 10 + 0 ) = 10 Keep [1, 7] = 1 V[1, 8] = max ( V[0, 8], v1 + V[0, 8 – w1] ) = max ( 0 , 10 + V[0, 8 - 5] ) = max ( 0 , 10 + 0 ) = 10 Keep [1, 8] = 1 V[1, 9] = max ( V[0, 9], v1 + V[0, 9 – w1] ) = max ( 0 , 10 + V[0, 9 - 5] ) = max ( 0 , 10 + 0 ) = 10 Keep [1, 9] = 1 V[1, 10] = max ( V[0, 10], v1 + V[0, 10 – w1] ) = max ( 0 , 10 + V[0, 10 - 5] ) = max ( 0 , 10 + 0 ) = 10 Keep [1, 10] = 1 0-1 Knapsack Problem Dynamic Programming – Bottom Up Approach
  • 6.
    6 Step 3: Computingthe Optimal Value V[2, 1] = V[2, 2] = V[2, 3] = 0 V[i, w] = max ( V[i-1, w], vi + V[i-1, w - wi] ) V[2, 4] = max ( V[1, 4], v2 + V[1, 4 – w2] ) = max ( 0 , 40 + V[1, 4 - 4] ) = max ( 0 , 40 + 0 ) = 40 Keep [2, 4] = 1 V[2, 5] = max ( V[1, 5], v2 + V[1, 5 – w2] ) = max ( 10 , 40 + V[1, 5 - 4] ) = max ( 10 , 40 + 0 ) = 40 Keep [2, 5] = 1 V[2, 6] = max ( V[1, 6], v2 + V[1, 6 – w2] ) = max ( 10 , 40 + V[1, 6 - 4] ) = max ( 10 , 40 + 0 ) = 40 Keep [2, 6] = 1 V[2, 7] = max ( V[1, 7], v2 + V[1, 7 – w2] ) = max ( 10 , 40 + V[1, 7 - 4] ) = max ( 10 , 40 + 0 ) = 40 Keep [2, 7] = 1 V[2, 8] = max ( V[1, 8], v2 + V[1, 8 – w2] ) = max ( 10 , 10 + V[1, 8 - 4] ) = max ( 10 , 40 + 0 ) = 10 Keep [2, 8] = 1 0-1 Knapsack Problem Dynamic Programming – Bottom Up Approach
  • 7.
    7 Step 3: Computingthe Optimal Value V[2, 1] = V[2, 2] = V[2, 3] = 0 V[i, w] = max ( V[i-1, w], vi + V[i-1, w - wi] ) V[2, 9] = max ( V[1, 9], v2 + V[1, 9 – w2] ) = max ( 10 , 40 + V[1, 9 - 4] ) = max ( 10 , 40 + 10 ) = 50 Keep [2, 9] = 1 V[2, 10] = max ( V[1, 10], v2 + V[1, 10 – w2] ) = max ( 10 , 40 + V[1, 10 - 4] ) = max ( 10 , 40 + 10 ) = 50 Keep [2, 10] = 1 0-1 Knapsack Problem Dynamic Programming – Bottom Up Approach
  • 8.
    8 Step 3: Computingthe Optimal Value V[3, 1] = V[3, 2] = V[3, 3] = 0 V[i, w] = max ( V[i-1, w], vi + V[i-1, w - wi] ) V[3, 4] = max ( V[2, 4], v3 + V[2, 4 – w3] ) = max ( 40 , 30 + V[2, 4 - 6] ) = max ( 40 , invalid ) = 40 Keep [3, 4] = 0 V[3, 5] = max ( V[2, 5], v3 + V[2, 5 – w3] ) = max ( 40 , 30 + V[2, 5 - 6] ) = max ( 40 , invalid ) = 40 Keep [3, 5] = 0 V[3, 6] = max ( V[2, 6], v3 + V[2, 6 – w3] ) = max ( 40 , 30 + V[2, 6 - 6] ) = max ( 40 , 30+0 ) = 40 Keep [3, 6] = 0 V[3, 7] = max ( V[2, 7], v3 + V[2, 7 – w3] ) = max ( 40 , 30 + V[2, 7 - 6] ) = max ( 40 , 30+0 ) = 40 Keep [3, 7] = 0 V[3, 8] = max ( V[2, 8], v3 + V[2, 8 – w3] ) = max ( 40 , 30 + V[2, 8 - 6] ) = max ( 40 , 30+0 ) = 40 Keep [3, 8] = 0 0-1 Knapsack Problem Dynamic Programming – Bottom Up Approach
  • 9.
    9 Step 3: Computingthe Optimal Value V[3, 1] = V[3, 2] = V[3, 3] = 0 V[i, w] = max ( V[i-1, w], vi + V[i-1, w - wi] ) V[3, 9] = max ( V[2, 9], v3 + V[2, 9 – w3] ) = max ( 50 , 30 + V[2, 9 - 6] ) = max ( 50 , 30+0 ) = 50 Keep [3, 9] = 0 V[3, 10] = max ( V[2, 10], v3 + V[2, 10 – w3] ) = max ( 50 , 30 + V[2, 10 - 6] ) = max ( 50 , 30+40 ) = 70 Keep [3, 10] = 1 0-1 Knapsack Problem Dynamic Programming – Bottom Up Approach
  • 10.
    10 Step 3: Computingthe Optimal Value V[4, 1] = V[4, 2] = 0 V[i, w] = max ( V[i-1, w], vi + V[i-1, w - wi] ) V[4, 3] = max ( V[3, 3], v4 + V[3, 3 – w4] ) = max ( 0 , 50 + V[3, 3 - 3] ) = max ( 0 , 50+0 ) = 50 Keep [4, 3] = 1 V[4, 4] = max ( V[3, 4], v4 + V[3, 4 – w4] ) = max ( 40 , 50 + V[3, 4 - 3] ) = max ( 40 , 50+0 ) = 50 Keep [4, 4] = 1 V[4, 5] = max ( V[3, 5], v4 + V[3, 5 – w4] ) = max ( 40 , 50 + V[3, 5 - 3] ) = max ( 40 , 50+0 ) = 50 Keep [4, 5] = 1 V[4, 6] = max ( V[3, 6], v4 + V[3, 6 – w4] ) = max ( 40 , 50 + V[3, 6 - 3] ) = max ( 40 , 50+0 ) = 50 Keep [4, 6] = 1 V[4, 7] = max ( V[3, 7], v4 + V[3, 7 – w4] ) = max ( 40 , 50 + V[3, 7 - 3] ) = max ( 40 , 50+40 ) = 90 Keep [4, 7] = 1 0-1 Knapsack Problem Dynamic Programming – Bottom Up Approach
  • 11.
    11 Step 3: Computingthe Optimal Value V[4, 1] = V[4, 2] = 0 V[i, w] = max ( V[i-1, w], vi + V[i-1, w - wi] ) V[4, 8] = max ( V[3, 8], v4 + V[3, 8 – w4] ) = max ( 50 , 50 + V[3, 8 - 3] ) = max ( 50 , 50+40 ) = 90 Keep [4, 8] = 1 V[4, 9] = max ( V[3, 9], v4 + V[3, 9 – w4] ) = max ( 50 , 50 + V[3, 9 - 3] ) = max ( 50 , 50+40 ) = 90 Keep [4, 9] = 1 V[4, 10] = max ( V[3, 10], v4 + V[3, 10 – w4] ) = max ( 70 , 50 + V[3, 10 - 3] ) = max ( 70 , 50+40 ) = 90 Keep [4, 10] = 1 0-1 Knapsack Problem Dynamic Programming – Bottom Up Approach 1 2 3 4 5 6 7 8 9 10 0 0 0 0 0 0 0 0 0 0 0 0 0 0 10 10 10 10 10 10 0 0 0 40 40 40 40 40 50 50 0 0 0 40 40 40 40 40 50 70 0 0 50 50 50 50 90 90 90 90 W=0 0 0 0 0 0 W[i, w] i = 0 i = 1 i = 2 i = 3 i = 4
  • 12.
    12 Step 3: PseudoCode to compute optimal value KnapSack(v, w, n, W) for (w = 0 to W), V[0, w] = 0; for (i = 1 to n), V[i, 0] = 0; for (i = 1 to n) for (w = 1 to W) if ((w(i) ≤ w) and (vi + V[i-1,w – wi] > V[i-1,w])) V[i, w] = (vi + V[i-1,w – wi]; keep[i, w] = 1; else V[i, w] = V[i-1,w]; keep[i, w] = 0; K = W; for (i = n down to 1) if keep[i, K] = = 1 output i K = K – wi Return V[n, W] 0-1 Knapsack Problem Dynamic Programming – Bottom Up Approach
  • 13.
    13 Step 4: Constructionof Optimal Solution V[i, w] = max ( V[i-1, w], vi + [i-1, w – wi] ); i = 4 V[4, 10] = max (70, 50 + 40) = 90; Keep(4, 10) = 1 i = 3 V[3, 10 - 3] = V[3, 7] = max(40, 30) = 40; Keep(3, 7) = 0 i = 2 V[2, 7] = max(10, 40) = 40 Keep(2, 7) = 1 i = 1 V[1, 7-4] = V[1, 3] = 0 Keep(1, 3) = 0 0-1 Knapsack Problem Dynamic Programming – Bottom Up Approach
  • 14.
    Step 4: Pdeudocode to construct of optimal solution K = W; for (i = n down to 1) if keep[i, K] = = 1 output i K = K – wi 0-1 Knapsack Problem Dynamic Programming – Bottom Up Approach