Dynamic Programming - Set 10 (0-1 Knapsack Problem) - GeeksforGeeks PDF
Dynamic Programming - Set 10 (0-1 Knapsack Problem) - GeeksforGeeks PDF
DynamicProgramming|Set10(01KnapsackProblem)GeeksforGeeks
DynamicProgramming|Set10(01
KnapsackProblem)
Givenweightsandvaluesofnitems,puttheseitemsinaknapsackofcapacity
Wtogetthemaximumtotalvalueintheknapsack.Inotherwords,giventwo
integerarraysval[0..n1]andwt[0..n1]whichrepresentvaluesandweights
associatedwithnitemsrespectively.AlsogivenanintegerWwhichrepresents
knapsackcapacity,findoutthemaximumvaluesubsetofval[]suchthatsumof
theweightsofthissubsetissmallerthanorequaltoW.Youcannotbreakan
item,eitherpickthecompleteitem,ordontpickit(01property).
Asimplesolutionistoconsiderallsubsetsofitemsandcalculatethetotalweight
andvalueofallsubsets.Considertheonlysubsetswhosetotalweightissmaller
thanW.Fromallsuchsubsets,pickthemaximumvaluesubset.
1)OptimalSubstructure:
Toconsiderallsubsetsofitems,therecanbetwocasesforeveryitem:(1)the
itemisincludedintheoptimalsubset,(2)notincludedintheoptimalset.
Therefore,themaximumvaluethatcanbeobtainedfromnitemsismaxof
followingtwovalues.
1)Maximumvalueobtainedbyn1itemsandWweight(excludingnthitem).
2)Valueofnthitemplusmaximumvalueobtainedbyn1itemsandWminus
weightofthenthitem(includingnthitem).
IfweightofnthitemisgreaterthanW,thenthenthitemcannotbeincludedand
case1istheonlypossibility.
2)OverlappingSubproblems
Followingisrecursiveimplementationthatsimplyfollowstherecursivestructure
mentionedabove.
/*ANaiverecursiveimplementationof01Knapsackproblem*/
#include<stdio.h>
//Autilityfunctionthatreturnsmaximumoftwointegers
data:text/htmlcharset=utf8,%3Cdiv%20class%3D%22posttitleinfo%22%20style%3D%22float%3A%20left%3B%20fontsize%3A%201.1em%3B%20width%
1/4
6/10/2015
DynamicProgramming|Set10(01KnapsackProblem)GeeksforGeeks
intmax(inta,intb){return(a>b)?a:b;}
//Returnsthemaximumvaluethatcanbeputinaknapsackofcapacity
intknapSack(intW,intwt[],intval[],intn)
{
//BaseCase
if(n==0||W==0)
return0;
//IfweightofthenthitemismorethanKnapsackcapacityW,then
//thisitemcannotbeincludedintheoptimalsolution
if(wt[n1]>W)
returnknapSack(W,wt,val,n1);
//Returnthemaximumoftwocases:(1)nthitemincluded(2)noti
elsereturnmax(val[n1]+knapSack(Wwt[n1],wt,val,n1),
knapSack(W,wt,val,n1)
);
}
//Driverprogramtotestabovefunction
intmain()
{
intval[]={60,100,120};
intwt[]={10,20,30};
intW=50;
intn=sizeof(val)/sizeof(val[0]);
printf("%d",knapSack(W,wt,val,n));
return0;
}
Itshouldbenotedthattheabovefunctioncomputesthesamesubproblems
againandagain.Seethefollowingrecursiontree,K(1,1)isbeingevaluated
twice.Timecomplexityofthisnaiverecursivesolutionisexponential(2^n).
Inthefollowingrecursiontree,K()referstoknapSack().Thetwo
parametersindicatedinthefollowingrecursiontreearenandW.
Therecursiontreeisforfollowingsampleinputs.
wt[]={1,1,1},W=2,val[]={10,20,30}
data:text/htmlcharset=utf8,%3Cdiv%20class%3D%22posttitleinfo%22%20style%3D%22float%3A%20left%3B%20fontsize%3A%201.1em%3B%20width%
2/4
6/10/2015
DynamicProgramming|Set10(01KnapsackProblem)GeeksforGeeks
K(3,2)>K(n,W)
/\
/\
K(2,2)K(2,1)
/\/\
/\/\
K(1,2)K(1,1)K(1,1)K(1,0)
/\/\/\
/\/\/\
K(0,2)K(0,1)K(0,1)K(0,0)K(0,1)K(0,0)
RecursiontreeforKnapsackcapacity2unitsand3itemsof1unitweight.
Sincesuproblemsareevaluatedagain,thisproblemhasOverlapping
Subprolemsproperty.Sothe01Knapsackproblemhasbothproperties
(seethisandthis)ofadynamicprogrammingproblem.Likeother
typicalDynamicProgramming(DP)problems,recomputationsofsame
subproblemscanbeavoidedbyconstructingatemporaryarrayK[][]inbottomup
manner.FollowingisDynamicProgrammingbasedimplementation.
//ADynamicProgrammingbasedsolutionfor01Knapsackproblem
#include<stdio.h>
//Autilityfunctionthatreturnsmaximumoftwointegers
intmax(inta,intb){return(a>b)?a:b;}
//Returnsthemaximumvaluethatcanbeputinaknapsackofcapacity
intknapSack(intW,intwt[],intval[],intn)
{
inti,w;
intK[n+1][W+1];
//BuildtableK[][]inbottomupmanner
for(i=0;i<=n;i++)
{
for(w=0;w<=W;w++)
{
if(i==0||w==0)
K[i][w]=0;
data:text/htmlcharset=utf8,%3Cdiv%20class%3D%22posttitleinfo%22%20style%3D%22float%3A%20left%3B%20fontsize%3A%201.1em%3B%20width%
3/4
6/10/2015
DynamicProgramming|Set10(01KnapsackProblem)GeeksforGeeks
elseif(wt[i1]<=w)
K[i][w]=max(val[i1]+K[i1][wwt[i1]],K[i1][w
else
K[i][w]=K[i1][w];
}
}
returnK[n][W];
}
intmain()
{
intval[]={60,100,120};
intwt[]={10,20,30};
intW=50;
intn=sizeof(val)/sizeof(val[0]);
printf("%d",knapSack(W,wt,val,n));
return0;
}
TimeComplexity:O(nW)wherenisthenumberofitemsandWisthecapacityof
knapsack.
References:
http://www.es.ele.tue.nl/education/5MC10/Solutions/knapsack.pdf
http://www.cse.unl.edu/~goddard/Courses/CSCE310J/Lectures/Lecture8
DynamicProgramming.pdf
data:text/htmlcharset=utf8,%3Cdiv%20class%3D%22posttitleinfo%22%20style%3D%22float%3A%20left%3B%20fontsize%3A%201.1em%3B%20width%
4/4