Skip to content

Commit

Permalink
knapsack updated
Browse files Browse the repository at this point in the history
  • Loading branch information
zarif98sjs committed Aug 11, 2020
1 parent 71377b2 commit adcd234
Showing 1 changed file with 101 additions and 59 deletions.
160 changes: 101 additions & 59 deletions Dynamic Programming/15 0-1 Knapsack.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@

/**
Problem : 0-1 Knapsack
F(i,W) = max( include ith item , exclude ith item )
In 0-1 Knapsack problem, we are given a set of items, each with a weight and a value and
we need to 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.
Here ,
include ith item = F(i+1,W+w[i]) + v[i]
exclude ith item = F(i+1,W)
Base case:
F(i,W>MAX_WEIGHT) = INT_MIN
F(i>n,W<=MAX_WIGHT) = 0
F(i>n,W>MAX_WIGHT) = INT_MIN
**/

/**Which of the favors of your Lord will you deny ?**/
/** Which of the favors of your Lord will you deny ? **/

#include<bits/stdc++.h>
using namespace std;
Expand All @@ -20,116 +26,152 @@ using namespace std;
#define MP make_pair
#define F first
#define S second
#define INF INT_MAX

#define ALL(x) (x).begin(), (x).end()
#define DBG(x) cerr << __LINE__ << " says: " << #x << " = " << (x) << endl
#define READ freopen("alu.txt", "r", stdin)
#define WRITE freopen("vorta.txt", "w", stdout)

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

template<class TIn>
using indexed_set = tree<
TIn, null_type, less<TIn>,
rb_tree_tag, tree_order_statistics_node_update>;
template<class TIn>using indexed_set = tree<TIn, null_type, less<TIn>,rb_tree_tag, tree_order_statistics_node_update>;

/**
/*
PBDS
-------------------------------------------------
1) insert(value)
2) erase(value)
3) order_of_key(value) // 0 based indexing
4) *find_by_order(position) // 0 based indexing
*/
**/

template<class T1, class T2>
ostream &operator <<(ostream &os, pair<T1,T2>&p);
template <class T>
ostream &operator <<(ostream &os, vector<T>&v);
template <class T>
ostream &operator <<(ostream &os, set<T>&v);

inline void optimizeIO()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
}

const int nmax = 100+7;
const int nmax = 1e2+7;
const int nmax2 = 1e5+7;
const LL LINF = 1e17;

string to_str(LL x)
template <class T>
string to_str(T x)
{
stringstream ss;
ss<<x;
return ss.str();
}

//bool cmp(const PII &A,const PII &B)
//{
//
//}

vector<int> vara = { -1, 20, 5, 10, 40, 15, 25 };
vector<int> wara = { -1, 1, 2, 3, 8, 7, 4 };
int max_cap = 10;
int n,MAX_WEIGHT;

/** 1 based indexing **/
LL w[nmax];
LL v[nmax];
LL dp[nmax][nmax2];

/**
dp[i][j] = max(include the item , exclude the item)
**/

int dp[nmax][nmax];

int solve(int i,int w)
LL solve(int pos,int W) /** maximum value to get using maximum weight W **/
{
if(w<0) return INT_MIN; /** **/

if(i==0 || w==0) return 0;
if(W>MAX_WEIGHT) return INT_MIN; /** if anytime W>MAX_HEIGHT , not possible . This is used just to avoid RTE **/

int &ret = dp[i][w];
if(pos>n) /** Done with n items and now check condition **/
{
return W<=MAX_WEIGHT ? 0 : INT_MIN;
}

LL &ret = dp[pos][W];
if(ret!=-1) return ret;

int inc = vara[i] + solve(i-1,w-wara[i]);
int exc = solve(i-1,w);
LL inc = solve(pos+1,W+w[pos]) + v[pos];
LL exc = solve(pos+1,W);

return ret = max(inc,exc);
}

string solve_print(int i,int w)
string solve_print(int pos,int W)
{
if(w<0) return ""; /** **/
if(W>MAX_WEIGHT) return "";
if(pos>n) return "";

if(i==0 || w==0) return "";
LL inc = solve(pos+1,W+w[pos]) + v[pos];
LL exc = solve(pos+1,W);

int inc = vara[i] + dp[i-1][w-wara[i]];
int exc = dp[i-1][w];

if(inc>exc)
return to_str(vara[i]) + " " + solve_print(i-1,w-wara[i]);
else
return solve_print(i-1,w);
if(inc>exc) return to_str(v[pos]) + " " + solve_print(pos+1,W+w[pos]);
else return solve_print(pos+1,W);
}


int main()
{
optimizeIO();

memset(dp,-1,sizeof dp);
cin>>n>>MAX_WEIGHT;

int n = vara.size()-1;
for(int i=1;i<=n;i++)
cin>>w[i]>>v[i];

cout<<solve(n,max_cap)<<endl;
cout<<solve_print(n,max_cap)<<endl;
memset(dp,-1,sizeof dp);

// for(int i=1; i<=n; i++)
// {
// for(int j=1; j<=max_cap; j++)
// cout<<dp[i][j]<<" ";
// cout<<endl;
// }
cout<<solve(0,0)<<endl;
cout<<solve_print(0,0)<<endl;

return 0;
}

/**
**/

template<class T1, class T2>
ostream &operator <<(ostream &os, pair<T1,T2>&p)
{
os<<"{"<<p.first<<", "<<p.second<<"} ";
return os;
}
template <class T>
ostream &operator <<(ostream &os, vector<T>&v)
{
os<<"[ ";
for(int i=0; i<v.size(); i++)
{
os<<v[i]<<" " ;
}
os<<" ]";
return os;
}

template <class T>
ostream &operator <<(ostream &os, set<T>&v)
{
os<<"[ ";
for(T i:v)
{
os<<i<<" ";
}
os<<" ]";
return os;
}

//string solve_print(int i,int w)
//{
// if(w<0) return ""; /** **/
//
// if(i==0 || w==0) return "";
//
// int inc = vara[i] + dp[i-1][w-wara[i]];
// int exc = dp[i-1][w];
//
// if(inc>exc)
// return to_str(vara[i]) + " " + solve_print(i-1,w-wara[i]);
// else
// return solve_print(i-1,w);
//}

0 comments on commit adcd234

Please sign in to comment.