From b4d54dd0f10a707eaffbea024830b9905805c0d8 Mon Sep 17 00:00:00 2001 From: hari-sikchi Date: Tue, 11 Oct 2016 00:39:09 +0530 Subject: [PATCH 1/2] 0-1 Knapsack problem --- .../Dynamic Programming/knapsack.cpp | 96 +++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 src/algorithm_practice/Dynamic Programming/knapsack.cpp diff --git a/src/algorithm_practice/Dynamic Programming/knapsack.cpp b/src/algorithm_practice/Dynamic Programming/knapsack.cpp new file mode 100644 index 00000000..c59a830d --- /dev/null +++ b/src/algorithm_practice/Dynamic Programming/knapsack.cpp @@ -0,0 +1,96 @@ + +/* +This is a implementation of the general (0,1) knapsack problem + +Problem statement:Given a set of items, each with a weight and a value, determine the number of each item +to include in a collection so that the total weight is less than or equal to a given limit and the total value is as large as possible. + +Code:- +To consider all subsets of items, there can be two cases for every item: (1) the item is included in the optimal subset, (2) not included in the optimal set. +Therefore, the maximum value that can be obtained from n items is max of following two values. +1) Maximum value obtained by n-1 items and W weight (excluding nth item). +2) Value of nth item plus maximum value obtained by n-1 items and W minus weight of the nth item (including nth item). + +We exploit this property for knapsack problem and build a 2-D array for storing value corresponding to certain weight and no of items + +Time Complexity: O(nW) where n is the number of items and W is the capacity of knapsack. + + + +*/ + + + + + + + + +#include + +using namespace std; + +//returns maximum of two numbers +int maxm(int a, int b) { return (a > b)? a : b; } + + +//a DP implementation of the famous knapsack problem +/* +Each element of the array k stores the optimal value for the corresponding weight size and number of items. +We see that the relation comes out to be (from the initial discussion): + +k[i][j]=maxm(k[i-1][j-w[i-1]]+v[i-1],k[i-1][j]); + +i.e >If you take the last weight find and optimal solution as if the weight didnt exist and If you dont the optimal +solution is just k[i-1][j]. +*/ +int knapsack(int v[],int w[],int n,int wmax) +{ + int k[n+1][wmax+1]; + + + for(int i=0;i<=n;i++) + { + for(int j=0;j<=wmax;j++) + { + if(i==0||j==0) + k[i][j]=0; + else if(w[i-1]<=j) + { + + k[i][j]=maxm(k[i-1][j-w[i-1]]+v[i-1],k[i-1][j]); + } + else + { + k[i][j]=k[i-1][j]; + } + + } + } + +return k[n][wmax]; + +} + +int main() + { + int n; + //Enter size of the input array + cout<<"Enter the size:"; + cin>>n; + int v[n]; + int w[n]; + int wmax; + // Enter the values + for(int i =0;i>v[i]; + //Enter the weights for each of the value + for(int i =0;i>w[i]; + + cin>>wmax; + + //Knapsack solution + cout< Date: Tue, 11 Oct 2016 15:16:31 +0530 Subject: [PATCH 2/2] algo:A implementation of the general knapsack problem --- .../Dynamic Programming/knapsack.cpp | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/algorithm_practice/Dynamic Programming/knapsack.cpp b/src/algorithm_practice/Dynamic Programming/knapsack.cpp index c59a830d..c1218cef 100644 --- a/src/algorithm_practice/Dynamic Programming/knapsack.cpp +++ b/src/algorithm_practice/Dynamic Programming/knapsack.cpp @@ -31,7 +31,9 @@ Time Complexity: O(nW) where n is the number of items and W is the capacity of k using namespace std; //returns maximum of two numbers -int maxm(int a, int b) { return (a > b)? a : b; } +int maxm(int a, int b) { + return (a > b)? a : b; +} //a DP implementation of the famous knapsack problem @@ -44,8 +46,8 @@ k[i][j]=maxm(k[i-1][j-w[i-1]]+v[i-1],k[i-1][j]); i.e >If you take the last weight find and optimal solution as if the weight didnt exist and If you dont the optimal solution is just k[i-1][j]. */ -int knapsack(int v[],int w[],int n,int wmax) -{ +int knapsack(int v[],int w[],int n,int wmax){ + int k[n+1][wmax+1]; @@ -55,13 +57,10 @@ int knapsack(int v[],int w[],int n,int wmax) { if(i==0||j==0) k[i][j]=0; - else if(w[i-1]<=j) - { - - k[i][j]=maxm(k[i-1][j-w[i-1]]+v[i-1],k[i-1][j]); + else if(w[i-1]<=j){ + k[i][j]=maxm(k[i-1][j-w[i-1]]+v[i-1],k[i-1][j]); } - else - { + else{ k[i][j]=k[i-1][j]; } @@ -72,8 +71,8 @@ return k[n][wmax]; } -int main() - { +int main(){ + int n; //Enter size of the input array cout<<"Enter the size:"; @@ -84,13 +83,14 @@ int main() // Enter the values for(int i =0;i>v[i]; + //Enter the weights for each of the value for(int i =0;i>w[i]; - cin>>wmax; + cin>>wmax; //Knapsack solution - cout<