Given weights and values of n items, put these items in a knapsack of capacity W to get the maximum total value in the knapsack. In other words, given two integer arrays val[0..n-1] and wt[0..n-1] which represent values and weights associated with n items respectively. Also given an integer W which represents knapsack capacity, find out the items such that sum of the weights of those items of given subset is smaller than or equal to W. You cannot break an item, either pick the complete item, or don’t pick it (0-1 property).
intknapSack(intW,intwt[],intval[],intn){inti,w;intK[n+1][W+1];// Build table K[][] in bottom up mannerfor(i=0;i<=n;i++){for(w=0;w<=W;w++){if(i==0||w==0)K[i][w]=0;elseif(wt[i-1]<=w)K[i][w]=max(val[i-1]+K[i-1][w-wt[i-1]],K[i-1][w]);elseK[i][w]=K[i-1][w];}}returnK[n][W];}
Solution 2: DP using 1D array
1
2
3
4
5
6
7
8
9
10
11
12
13
14
intKnapSack(intval[],intwt[],intn,intW){intdp[W+1];memset(dp,0,sizeof(dp));// iterate through all itemsfor(inti=0;i<n;i++)//traverse dp array from bigger to small, since dp[small] is the "old" valuefor(intj=W;j>=wt[i];j--)dp[j]=max(dp[j],val[i]+dp[j-wt[i]]);returndp[W];}
Solution 3: Backtrack
With little modification, backtrack algorithm can be used to solve 0/1 knapsack Problem
intknapSack(intW,intwt[],intval[],intn){// Base Caseif(n==0||W==0)return0;// If weight of the nth item is more than Knapsack capacity W, then// this item cannot be included in the optimal solutionif(wt[n-1]>W)returnknapSack(W,wt,val,n-1);// Return the maximum of two cases:// (1) nth item included// (2) not includedelsereturnmax(val[n-1]+knapSack(W-wt[n-1],wt,val,n-1),knapSack(W,wt,val,n-1));}
Unbounded Knapsack Problem
Given a knapsack weight W and a set of n items with certain value val[i] and weight wt[i], we need to calculate the maximum amount that could make up this quantity exactly.
This is different from classical Knapsack problem, here we are allowed to use unlimited number of instances of an item.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
intunboundedKnapsack(intW,intn,intval[],intwt[]){// dp[i] is going to store maximum value with knapsack capacity i.intdp[W+1];memset(dp,0,sizeofdp);// Fill dp[] using above recursive formulafor(inti=0;i<=W;i++)for(intj=0;j<n;j++)if(wt[j]<=i)dp[i]=max(dp[i],dp[i-wt[j]]+val[j]);returndp[W];}