SlideShare a Scribd company logo
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

More Related Content

PPT
Knapsack problem
PPT
Knapsack problem using dynamic programming
PPTX
Longest common sub sequence & 0/1 Knapsack
PPT
Knapsack problem and Memory Function
PPT
DynProg_Knapsack.ppt
PPTX
Presentation of knapsack
PPTX
01 Knapsack using Dynamic Programming
PPTX
Knapsack Dynamic
Knapsack problem
Knapsack problem using dynamic programming
Longest common sub sequence & 0/1 Knapsack
Knapsack problem and Memory Function
DynProg_Knapsack.ppt
Presentation of knapsack
01 Knapsack using Dynamic Programming
Knapsack Dynamic

Similar to Knapsack Problem Analysis of Algorithm.ppt (16)

PPT
lecture 25
PPTX
Dynamic Programming-Knapsack Problem
PDF
13 - 06 Feb - Dynamic Programming
PPT
Dynamic Programming for 4th sem cse students
PDF
Johnson's algorithm
PPTX
Knapsack problem
PDF
ゲーム理論 BASIC 演習74 補足 -コアの存在, 線型計画問題-
PDF
CostFunctions.pdf
PDF
Vectors data frames
 
PDF
Re:ゲーム理論入門 第15回 - シャープレイ値 -
PPTX
Vector Space & Sub Space Presentation
PPT
Unit 3-Greedy Method
PPTX
Knapsack problem dynamicprogramming
PPTX
0/1 DYNAMIC PROGRAMMING KNAPSACK PROBLEM
PDF
Create a java project that - Draw a circle with three random init.pdf
PPT
0-1 knapsack.ppt
lecture 25
Dynamic Programming-Knapsack Problem
13 - 06 Feb - Dynamic Programming
Dynamic Programming for 4th sem cse students
Johnson's algorithm
Knapsack problem
ゲーム理論 BASIC 演習74 補足 -コアの存在, 線型計画問題-
CostFunctions.pdf
Vectors data frames
 
Re:ゲーム理論入門 第15回 - シャープレイ値 -
Vector Space & Sub Space Presentation
Unit 3-Greedy Method
Knapsack problem dynamicprogramming
0/1 DYNAMIC PROGRAMMING KNAPSACK PROBLEM
Create a java project that - Draw a circle with three random init.pdf
0-1 knapsack.ppt
Ad

Recently uploaded (20)

PPTX
Pharma ospi slides which help in ospi learning
PDF
Complications of Minimal Access Surgery at WLH
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
Pre independence Education in Inndia.pdf
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
master seminar digital applications in india
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
Sports Quiz easy sports quiz sports quiz
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPTX
GDM (1) (1).pptx small presentation for students
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
Classroom Observation Tools for Teachers
Pharma ospi slides which help in ospi learning
Complications of Minimal Access Surgery at WLH
O7-L3 Supply Chain Operations - ICLT Program
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Microbial diseases, their pathogenesis and prophylaxis
Pre independence Education in Inndia.pdf
Abdominal Access Techniques with Prof. Dr. R K Mishra
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Module 4: Burden of Disease Tutorial Slides S2 2025
master seminar digital applications in india
PPH.pptx obstetrics and gynecology in nursing
Sports Quiz easy sports quiz sports quiz
2.FourierTransform-ShortQuestionswithAnswers.pdf
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
FourierSeries-QuestionsWithAnswers(Part-A).pdf
GDM (1) (1).pptx small presentation for students
Supply Chain Operations Speaking Notes -ICLT Program
Classroom Observation Tools for Teachers
Ad

Knapsack Problem Analysis of Algorithm.ppt

  • 1. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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
  • 14. 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