Extended Knapsack Problem - GeeksforGeeks

30 Dec, 2021
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 = 12Input: 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.
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-1]][k-1] + P[i])  
Below is the implementation of the above approach:  
C++
Java
Javascript
Python3
// C++ code for the extended// Knapsack Approach#include <bits/stdc++.h>using namespace std; // To store the dp valuesvector<vector<vector<int> > > dp; int maxProfit(int profit[], int weight[], int n, int max_W,              int max_E){     // for each element given    for (int i = 1; i <= n; i++) {         // For each possible        // weight value        for (int j = 1; j <= max_W; j++) {             // For each case where            // the total elements are            // less than the constraint            for (int k = 1; k <= max_E; k++) {                 // To ensure that we dont                // go out of the array                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];} // Driver Codeint 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 code for the extended// Knapsack Approachimport java.util.*;import java.lang.*;import java.io.*; class GFG{ // To store the dp valuesstatic int[][][] dp = new int[100][100][100]; static int maxProfit(int profit[],                     int weight[],                     int n, int max_W,                     int max_E){         // for each element given    for(int i = 1; i <= n; i++)    {                 // For each possible        // weight value        for(int j = 1; j <= max_W; j++)        {                         // For each case where            // the total elements are            // less than the constraint            for(int k = 1; k <= max_E; k++)            {                                 // To ensure that we dont                // go out of the array                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];}   // Driver codepublic 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));  }} // This code is contributed by offbeat
<script> // Javascript code for the extended// Knapsack Approach // To store the dp valuesvar 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 each element given    for (var i = 1; i <= n; i++)    {         // For each possible        // weight value        for (var j = 1; j <= max_W; j++)        {             // For each case where            // the total elements are            // less than the constraint            for (var k = 1; k <= max_E; k++)            {                 // To ensure that we dont                // go out of the array                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];} // Driver Codevar 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>
# Python3 code for the extended# Knapsack Approach # To store the dp valuesdp=[] def maxProfit(profit, weight, n, max_W,              max_E):     # for each element given    for i in range(1,n+1) :         # For each possible        # weight value        for j in range(1,max_W+1) :             # For each case where            # the total elements are            # less than the constra            for k in range(1, max_E+1) :                 # To ensure that we dont                # go out of the array                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]  # Driver Codeif __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))
12
 
Time Complexity: O(N * W * K) Auxiliary Space: O(N * W * K)
nidhi_biet
manujk881
offbeat
famously
ashutoshsinghgeeksforgeeks
amartyaghoshgfg
knapsack
Algorithms
Dynamic Programming
Greedy
Dynamic Programming
Greedy
Algorithms
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
DSA Sheet by Love Babbar
SCAN (Elevator) Disk Scheduling Algorithms
Rail Fence Cipher - Encryption and Decryption
Program for SSTF disk scheduling algorithm
Quadratic Probing in Hashing
0-1 Knapsack Problem | DP-10
Program for Fibonacci numbers
Largest Sum Contiguous Subarray
Longest Common Subsequence | DP-4
Bellman–Ford Algorithm | DP-23