0 - 1 Knapsack Problem
PROBLEM :
You are 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. Note that we have only one quantity of each item, 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 maximum value subset of val[] such that sum of the weights of this 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).
Input:
The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. Each test case consists of four lines. The first line consists of N the number of items. The second line consists of W, the maximum capacity of the knapsack. In the next line are N space separated positive integers denoting the values of the N items and in the fourth line are N space separated positive integers denoting the weights of the corresponding items.
Output:
Print the maximum possible value you can get with the given conditions that you can obtain for each test case in a new line.
Constraints:
1=T=100
1=N=100
1=W=100
1=wt[i]=100
1=v[i]=100
Example:
Input:
1
3
4
1 2 3
4 5 1
Output:
3
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :(Using Auxilary arry)
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
int max(int ,int ) ;
int Knapsack_Problem(int [],int [],int ,int ) ;
int main()
{
int t,no,w,val[100],weight[100],i ;
cin>>t ;
while(t--)
{
cin>>no ;
cin>>w ;
for(i=0;i<no;i++)
cin>>val[i] ;
for(i=0;i<no;i++)
cin>>weight[i] ;
no=Knapsack_Problem(val,weight,no,w) ;
cout<<no<<endl ;
}
return 0;
}
int Knapsack_Problem(int val[],int weight[],int no,int w)
{
int i,j ;
int mat[no+1][w+1]={0} ;
for(i=0;i<=no;i++)
mat[i][0]=0 ;
for(i=0;i<=w;i++)
mat[0][i]=0 ;
for(i=1;i<=no;i++)
{
for(j=1;j<=w;j++)
{
if(j>=weight[i-1])
{
mat[i][j]=max(mat[i-1][j],val[i-1]+mat[i-1][j-weight[i-1]]) ;
}
else if(i>1)
mat[i][j]=mat[i-1][j] ;
}
}
return mat[no][w] ;
}
int max(int a,int b)
{
return a>b?a:b ;
}
---------------------------------------------------------------------------------
You are 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. Note that we have only one quantity of each item, 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 maximum value subset of val[] such that sum of the weights of this 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).
Input:
The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. Each test case consists of four lines. The first line consists of N the number of items. The second line consists of W, the maximum capacity of the knapsack. In the next line are N space separated positive integers denoting the values of the N items and in the fourth line are N space separated positive integers denoting the weights of the corresponding items.
Output:
Print the maximum possible value you can get with the given conditions that you can obtain for each test case in a new line.
Constraints:
1=T=100
1=N=100
1=W=100
1=wt[i]=100
1=v[i]=100
Example:
Input:
1
3
4
1 2 3
4 5 1
Output:
3
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :(Using Auxilary arry)
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
int max(int ,int ) ;
int Knapsack_Problem(int [],int [],int ,int ) ;
int main()
{
int t,no,w,val[100],weight[100],i ;
cin>>t ;
while(t--)
{
cin>>no ;
cin>>w ;
for(i=0;i<no;i++)
cin>>val[i] ;
for(i=0;i<no;i++)
cin>>weight[i] ;
no=Knapsack_Problem(val,weight,no,w) ;
cout<<no<<endl ;
}
return 0;
}
int Knapsack_Problem(int val[],int weight[],int no,int w)
{
int i,j ;
int mat[no+1][w+1]={0} ;
for(i=0;i<=no;i++)
mat[i][0]=0 ;
for(i=0;i<=w;i++)
mat[0][i]=0 ;
for(i=1;i<=no;i++)
{
for(j=1;j<=w;j++)
{
if(j>=weight[i-1])
{
mat[i][j]=max(mat[i-1][j],val[i-1]+mat[i-1][j-weight[i-1]]) ;
}
else if(i>1)
mat[i][j]=mat[i-1][j] ;
}
}
return mat[no][w] ;
}
int max(int a,int b)
{
return a>b?a:b ;
}
---------------------------------------------------------------------------------
Comments
Post a Comment