Given N items, each item having a given weight Ci and a profit value Pi, the task is to maximize the profit by selecting a maximum of K items adding up to a maximum weight W.
Examples:
Input: N = 5, P[] = {2, 7, 1, 5, 3}, C[] = {2, 5, 2, 3, 4}, W = 8, K = 2.
Output: 12
Explanation:
Here, the maximum possible profit is when we take 2 items: item2 (P[1] = 7 and C[1] = 5) and item4 (P[3] = 5 and C[3] = 3).
Hence, maximum profit = 7 + 5 = 12
Input: N = 5, P[] = {2, 7, 1, 5, 3}, C[] = {2, 5, 2, 3, 4}, W = 1, K = 2
Output: 0
Explanation: All weights are greater than 1. Hence, no item can be picked.
Approach: The dynamic programming approach is preferred over the general recursion approach. Let us first verify that the conditions of DP are still satisfied.
- Overlapping sub-problems: When the recursive solution is tried, 1 item is added first and the solution set is (1), (2), …(n). In the second iteration we have (1, 2) and so on where (1) and (2) are recalculated. Hence there will be overlapping solutions.
- Optimal substructure: Overall, each item has only two choices, either it can be included in the solution or denied. For a particular subset of z elements, the solution for (z+1)th element can either have a solution corresponding to the z elements or the (z+1)th element can be added if it doesn’t exceed the knapsack constraints. Either way, the optimal substructure property is satisfied.
Let’s derive the recurrence. Let us consider a 3-dimensional table dp[N][W][K], where N is the number of elements, W is the maximum weight capacity and K is the maximum number of items allowed in the knapsack. Let’s define a state dp[i][j][k] where i denotes that we are considering the ith element, j denotes the current weight filled, and k denotes the number of items filled until now.
For every state dp[i][j][k], the profit is either that of the previous state (when the current state is not included) or the profit of the current item added to that of the previous state (when the current item is selected). Hence, the recurrence relation is:
dp[i][j][k] = max( dp[i-1][j][k], dp[i-1][j-W[i]][k-1] + P[i])
Note: In code we have used 1 based indexing so, we are doing i – 1 for weight and profit array.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
vector<vector<vector< int > > > dp;
int maxProfit( int profit[], int weight[], int n, int max_W,
int max_E)
{
for ( int i = 1; i <= n; i++) {
for ( int j = 1; j <= max_W; j++) {
for ( int k = 1; k <= max_E; k++) {
if (j >= weight[i - 1]) {
dp[i][j][k] = max(
dp[i - 1][j][k],
dp[i - 1][j - weight[i - 1]][k - 1]
+ profit[i - 1]);
}
else {
dp[i][j][k] = dp[i - 1][j][k];
}
}
}
}
return dp[n][max_W][max_E];
}
int main()
{
int n = 5;
int profit[] = { 2, 7, 1, 5, 3 };
int weight[] = { 2, 5, 2, 3, 4 };
int max_weight = 8;
int max_element = 2;
dp = vector<vector<vector< int > > >(
n + 1, vector<vector< int > >(
max_weight + 1,
vector< int >(max_element + 1, 0)));
cout << maxProfit(profit, weight, n, max_weight,
max_element)
<< "\n" ;
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG{
static int [][][] dp = new int [ 100 ][ 100 ][ 100 ];
static int maxProfit( int profit[],
int weight[],
int n, int max_W,
int max_E)
{
for ( int i = 1 ; i <= n; i++)
{
for ( int j = 1 ; j <= max_W; j++)
{
for ( int k = 1 ; k <= max_E; k++)
{
if (j >= weight[i - 1 ])
{
dp[i][j][k] = Math.max(dp[i - 1 ][j][k],
dp[i - 1 ][j -
weight[i - 1 ]][k - 1 ] +
profit[i - 1 ]);
}
else
{
dp[i][j][k] = dp[i - 1 ][j][k];
}
}
}
}
return dp[n][max_W][max_E];
}
public static void main(String[] args)
{
int n = 5 ;
int profit[] = { 2 , 7 , 1 , 5 , 3 };
int weight[] = { 2 , 5 , 2 , 3 , 4 };
int max_weight = 8 ;
int max_element = 2 ;
System.out.println(maxProfit(profit,
weight, n,
max_weight,
max_element));
}
}
|
Python3
dp = []
def maxProfit(profit, weight, n, max_W,
max_E):
for i in range ( 1 ,n + 1 ) :
for j in range ( 1 ,max_W + 1 ) :
for k in range ( 1 , max_E + 1 ) :
if (j > = weight[i - 1 ]) :
dp[i][j][k] = max (
dp[i - 1 ][j][k],
dp[i - 1 ][j - weight[i - 1 ]][k - 1 ]
+ profit[i - 1 ])
else :
dp[i][j][k] = dp[i - 1 ][j][k]
return dp[n][max_W][max_E]
if __name__ = = '__main__' :
n = 5
profit = [ 2 , 7 , 1 , 5 , 3 ]
weight = [ 2 , 5 , 2 , 3 , 4 ]
max_weight = 8
max_element = 2
dp = [[[ 0 for j in range (max_element + 1 )] for i in range (max_weight + 1 )] for k in range (n + 1 )]
print (maxProfit(profit, weight, n, max_weight,
max_element))
|
C#
using System;
using System.Collections;
using System.Collections.Generic;
class GFG
{
static int [][][] dp = new int [100][][];
static int maxProfit( int [] profit, int [] weight,
int n, int max_W, int max_E)
{
for ( int i = 1 ; i <= n ; i++)
{
for ( int j = 1 ; j <= max_W ; j++)
{
for ( int k = 1 ; k <= max_E ; k++)
{
if (j >= weight[i - 1])
{
dp[i][j][k] = Math.Max(dp[i - 1][j][k],
dp[i - 1][j - weight[i - 1]][k - 1] + profit[i - 1]);
}
else
{
dp[i][j][k] = dp[i - 1][j][k];
}
}
}
}
return dp[n][max_W][max_E];
}
public static void Main( string [] args){
int n = 5;
int [] profit = new int []{ 2, 7, 1, 5, 3 };
int [] weight = new int []{ 2, 5, 2, 3, 4 };
int max_weight = 8;
int max_element = 2;
for ( int i = 0 ; i < 100 ; i++){
dp[i] = new int [100][];
for ( int j = 0 ; j < 100 ; j++){
dp[i][j] = new int [100];
}
}
Console.WriteLine(maxProfit(profit, weight, n,
max_weight, max_element));
}
}
|
Javascript
<script>
var dp = Array.from(Array(100), ()=>Array(100));
for ( var i =0; i<100; i++)
for ( var j =0; j<100; j++)
dp[i][j] = new Array(100).fill(0);
function maxProfit(profit,weight, n, max_W, max_E)
{
for ( var i = 1; i <= n; i++)
{
for ( var j = 1; j <= max_W; j++)
{
for ( var k = 1; k <= max_E; k++)
{
if (j >= weight[i-1])
{
dp[i][j][k]
= Math.max(dp[i - 1][j][k],
dp[i - 1][j -
weight[i-1]][k - 1]+
profit[i-1]);
}
else
{
dp[i][j][k]
= dp[i - 1][j][k];
}
}
}
}
return dp[n][max_W][max_E];
}
var n = 5;
var profit = [2, 7, 1, 5, 3 ];
var weight = [2, 5, 2, 3, 4 ];
var max_weight = 8;
var max_element = 2;
document.write( maxProfit(profit,
weight, n,
max_weight,
max_element)
+ "<br>" );
</script>
|
Time Complexity: O(N * W * K)
Auxiliary Space: O(N * W * K)
Approach: 2 The above code takes O(N * W * K) extra space, however, this can be reduced to a 2d matrix. Note that only space would be reduced to O(K * W), and time complexity remains the same, i.e. O(N * W * K).
- The idea is to eliminate the array storage for items. If you see, we update a cell based on dp[i][j][k] = max( dp[i – 1][j][k], dp[i – 1][j – weight[i ]][k – 1] + profit[i]);
instead, we can only use a 2d matrix where the x-axis would represent knapsack capacity (0, 1, 2, 3…W) and y-axis represents k items picked (0, 1, 2, 3, … k).
- Note that a cell is updated keeping only two values in mind, the previous value dp[i – 1][j][k], and the value for remaining capacity with k – 1 items. So we only need the previous row to check for [i – 1 ] and [i – 1][j – weight[i ]].
- A cell dp[j][k] in the matrix represents the maximum profit we can get with capacity j, if at most k, items are allowed, with all the items till i. This way we can still iterate over items but we don’t need to keep storage for them.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
int maxProfit( int profit[], int weight[], int n,
int maxCapacity, int maxItems)
{
vector<vector< int > > dp(
maxItems + 1, vector< int >(maxCapacity + 1, 0));
for ( int i = 0; i < n; i++) {
for ( int k = 1; k < maxItems + 1; k++) {
for ( int j = maxCapacity; j >= 0; j--) {
if (weight[i] <= j) {
dp[k][j] = max(
dp[k][j],
profit[i]
+ dp[k - 1][j - weight[i]]);
}
}
}
}
return dp[maxItems][maxCapacity];
}
int main()
{
int n = 5;
int profit[] = { 2, 7, 1, 5, 3 };
int weight[] = { 2, 5, 2, 3, 4 };
int max_weight = 8;
int max_element = 2;
cout << maxProfit(profit, weight, n, max_weight,
max_element)
<< "\n" ;
return 0;
}
|
Java
public class Main {
static int maxProfit( int profit[], int weight[], int n,
int maxCapacity, int maxItems)
{
int [][] dp = new int [maxItems + 1 ][maxCapacity + 1 ];
for ( int i = 0 ; i < n; i++) {
for ( int k = 1 ; k < maxItems + 1 ; k++) {
for ( int j = maxCapacity; j >= 0 ; j--) {
if (weight[i] <= j) {
dp[k][j] = Math.max(
dp[k][j],
profit[i]
+ dp[k - 1 ][j - weight[i]]);
}
}
}
}
return dp[maxItems][maxCapacity];
}
public static void main(String[] args)
{
int n = 5 ;
int [] profit = { 2 , 7 , 1 , 5 , 3 };
int [] weight = { 2 , 5 , 2 , 3 , 4 };
int max_weight = 8 ;
int max_element = 2 ;
System.out.println(maxProfit(
profit, weight, n, max_weight, max_element));
}
}
|
Python3
def maxProfit(profit, weight, n, maxCapacity, maxItems):
dp = [[ 0 for j in range (maxCapacity + 1 )] for i in range (maxItems + 1 )]
for i in range (n):
for k in range ( 1 , maxItems + 1 ):
for j in range (maxCapacity, - 1 , - 1 ):
if weight[i] < = j:
dp[k][j] = max (dp[k][j], profit[i] + dp[k - 1 ][j - weight[i]])
return dp[maxItems][maxCapacity]
if __name__ = = "__main__" :
n = 5
profit = [ 2 , 7 , 1 , 5 , 3 ]
weight = [ 2 , 5 , 2 , 3 , 4 ]
max_weight = 8
max_element = 2
print (maxProfit(profit, weight, n, max_weight, max_element))
|
C#
using System;
class GFG {
static int MaxProfit( int [] profit, int [] weight, int n,
int maxCapacity, int maxItems)
{
int [, ] dp = new int [maxItems + 1, maxCapacity + 1];
for ( int i = 0; i < n; i++)
{
for ( int k = 1; k < maxItems + 1; k++) {
for ( int j = maxCapacity; j >= 0; j--) {
if (weight[i] <= j) {
dp[k, j] = Math.Max(
dp[k, j],
profit[i]
+ dp[k - 1, j - weight[i]]);
}
}
}
}
return dp[maxItems, maxCapacity];
}
public static void Main( string [] args)
{
int n = 5;
int [] profit = { 2, 7, 1, 5, 3 };
int [] weight = { 2, 5, 2, 3, 4 };
int max_weight = 8;
int max_element = 2;
Console.WriteLine(MaxProfit(
profit, weight, n, max_weight, max_element));
}
}
|
Javascript
function maxProfit(profit, weight, n, maxCapacity, maxItems) {
const dp = Array.from(Array(maxItems + 1), () => new Array(maxCapacity + 1).fill(0));
for (let i = 0; i < n; i++) {
for (let k = 1; k < maxItems + 1; k++) {
for (let j = maxCapacity; j >= 0; j--) {
if (weight[i] <= j) {
dp[k][j] = Math.max(
dp[k][j],
profit[i] + dp[k - 1][j - weight[i]]
);
}
}
}
}
return dp[maxItems][maxCapacity];
}
const n = 5;
const profit = [2, 7, 1, 5, 3];
const weight = [2, 5, 2, 3, 4];
const max_weight = 8;
const max_element = 2;
console.log(maxProfit(profit, weight, n, max_weight, max_element));
|
Time Complexity: O(N * W * K)
Auxiliary Space: O(W * K)
Efficient approach : Space optimization
In previous approach the current value dp[i][j] is only depend upon the current and previous row values of DP. So to optimize the space complexity we use a single 1D array to store the computations.
Implementation steps:
- Create a 1D vector dp of size maxCapacity+1.
- Set a base case by initializing the values of DP .
- Now iterate over subproblems by the help of nested loop and get the current value from previous computations.
- At last return and print the final answer stored in dp[maxCapacity].
Implementation:
C++
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
int maxProfit( int profit[], int weight[], int n, int maxCapacity, int maxItems)
{
vector< int > dp(maxCapacity + 1, 0);
for ( int i = 0; i < n; i++) {
for ( int j = maxCapacity; j >= weight[i]; j--) {
dp[j] = max(dp[j], profit[i] + dp[j - weight[i]]);
}
}
return dp[maxCapacity];
}
int main()
{
int n = 5;
int profit[] = { 2, 7, 1, 5, 3 };
int weight[] = { 2, 5, 2, 3, 4 };
int max_weight = 8;
int max_element = 2;
cout << maxProfit(profit, weight, n, max_weight, max_element)
<< "\n" ;
return 0;
}
|
Java
import java.util.Arrays;
class GFG {
public static int maxProfit( int [] profit, int [] weight, int n, int maxCapacity, int maxItems) {
int [] dp = new int [maxCapacity + 1 ];
Arrays.fill(dp, 0 );
for ( int i = 0 ; i < n; i++) {
for ( int j = maxCapacity; j >= weight[i]; j--) {
dp[j] = Math.max(dp[j], profit[i] + dp[j - weight[i]]);
}
}
return dp[maxCapacity];
}
public static void main(String[] args) {
int n = 5 ;
int [] profit = { 2 , 7 , 1 , 5 , 3 };
int [] weight = { 2 , 5 , 2 , 3 , 4 };
int max_weight = 8 ;
int max_element = 2 ;
System.out.println(maxProfit(profit, weight, n, max_weight, max_element));
}
}
|
Python3
def maxProfit(profit, weight, n, maxCapacity, maxItems):
dp = [ 0 ] * (maxCapacity + 1 )
for i in range (n):
for j in range (maxCapacity, weight[i] - 1 , - 1 ):
dp[j] = max (dp[j], profit[i] + dp[j - weight[i]])
return dp[maxCapacity]
if __name__ = = '__main__' :
n = 5
profit = [ 2 , 7 , 1 , 5 , 3 ]
weight = [ 2 , 5 , 2 , 3 , 4 ]
max_weight = 8
max_element = 2
print (maxProfit(profit, weight, n, max_weight, max_element))
|
C#
using System;
public class GFG
{
public static int MaxProfit( int [] profit, int [] weight, int n, int maxCapacity, int maxItems)
{
int [] dp = new int [maxCapacity + 1];
for ( int i = 0; i < n; i++)
{
for ( int j = maxCapacity; j >= weight[i]; j--)
{
dp[j] = Math.Max(dp[j], profit[i] + dp[j - weight[i]]);
}
}
return dp[maxCapacity];
}
public static void Main( string [] args)
{
int n = 5;
int [] profit = { 2, 7, 1, 5, 3 };
int [] weight = { 2, 5, 2, 3, 4 };
int maxWeight = 8;
int maxElement = 2;
Console.WriteLine(MaxProfit(profit, weight, n, maxWeight, maxElement));
}
}
|
Javascript
function maxProfit(profit, weight, n, maxCapacity, maxItems) {
const dp = new Array(maxCapacity + 1).fill(0);
for (let i = 0; i < n; i++) {
for (let j = maxCapacity; j >= weight[i]; j--) {
dp[j] = Math.max(dp[j], profit[i] + dp[j - weight[i]]);
}
}
return dp[maxCapacity];
}
const n = 5;
const profit = [2, 7, 1, 5, 3];
const weight = [2, 5, 2, 3, 4];
const maxWeight = 8;
const maxElement = 2;
console.log(maxProfit(profit, weight, n, maxWeight, maxElement));
|
Output
12
Time Complexity: O(N * W * K)
Auxiliary Space: O(K)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
23 Aug, 2023
Like Article
Save Article