-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathCS_49_01KnapsackProblem.cpp
156 lines (141 loc) · 4.08 KB
/
CS_49_01KnapsackProblem.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include <bits/stdc++.h>
using namespace std;
// printing dp table
void print(vector<vector<int>> &dp)
{
for (auto row : dp)
{
for (auto col : row)
{
cout << col << " ";
}
cout << endl;
}
}
// tc: O(2^n) sc: O(n)
// int solvesingRecursion(int capacity, vector<int> &wt, vector<int> &profit, int i, int &n){
// // base case
// if(i >= n){
// return 0;
// }
// // inc/exc
// int inc = 0;
// if(wt[i] <= capacity){
// inc = profit[i] + solvesingRecursion(capacity-wt[i], wt, profit, i+1, n);
// }
// int exc = 0 + solvesingRecursion(capacity, wt, profit, i+1, n);
// return max(inc, exc);
// }
// tc: O(n*capacity) sc: O(n*capacity)
// int solvesingMemo(int capacity, vector<int> &wt, vector<int> &profit, int i, int &n, vector<vector<int>> &dp)
// {
// // base case
// if (i >= n)
// {
// return 0;
// }
// // check if already calculated
// if (dp[capacity][i] != -1)
// {
// return dp[capacity][i];
// }
// // inc/exc
// int inc = 0;
// if (wt[i] <= capacity)
// {
// inc = profit[i] + solvesingMemo(capacity - wt[i], wt, profit, i + 1, n, dp);
// }
// int exc = 0 + solvesingMemo(capacity, wt, profit, i + 1, n, dp);
// dp[capacity][i] = max(inc, exc);
// return dp[capacity][i];
// }
// tc: O(n*capacity) sc: O(n*capacity)
// int solveUsingTabu(int capacity, vector<int> &wt, vector<int> &profit, vector<vector<int>> &dp, int &n)
// {
// for (int row = 0; row <= capacity; row++)
// {
// dp[row][n] = 0;
// }
// for (int i = 0; i <= capacity; i++)
// {
// for (int j = n-1; j >= 0; j--)
// {
// // inc/exc
// int inc = 0;
// if (wt[j] <= i)
// {
// inc = profit[j] + dp[i - wt[j]][j + 1];
// }
// int exc = 0 + dp[i][j + 1];
// dp[i][j] = max(inc, exc);
// }
// }
// return dp[capacity][0];
// }
// tc: O(n*capacity) sc: O(capacity)
// int solveUsingTabuSpaceOpti(int capacity, vector<int> &wt, vector<int> &profit, int &n)
// {
// vector<int> nextCol(capacity + 1, 0);
// vector<int> currCol(capacity + 1, -1);
// for (int j = n - 1; j >= 0; j--)
// {
// for (int i = 0; i <= capacity; i++)
// {
// // inc/exc
// int inc = 0;
// if (wt[j] <= i)
// {
// inc = profit[j] + nextCol[i - wt[j]];
// }
// int exc = 0 + nextCol[i];
// currCol[i] = max(inc, exc);
// }
// nextCol = currCol;
// }
// return currCol[capacity];
// }
// tc: O(n*capacity) sc: O(capacity)
int solveUsingTabuSpaceOpti2(int capacity, vector<int> &wt, vector<int> &profit, int &n)
{
vector<int> nextCol(capacity + 1, 0);
// vector<int> currCol(capacity + 1, -1);
for (int j = n - 1; j >= 0; j--)
{
for (int i = capacity; i >= 0; i--)
{
// inc/exc
int inc = 0;
if (wt[j] <= i)
{
inc = profit[j] + nextCol[i - wt[j]];
}
int exc = 0 + nextCol[i];
nextCol[i] = max(inc, exc);
}
}
return nextCol[capacity];
}
int knapsack(vector<int> weight, vector<int> profit, int n, int capacity)
{
int i = 0;
// return solvesingRecursion(capacity, weight, profit, i, n);
vector<vector<int>> dp(capacity + 1, vector<int>(n + 1, -1));
// return solveUsingMemo(capacity, weight, profit, i, n, dp);
// int ans = solveUsingTabu(capacity, weight, profit, dp, n);
// int ans = solveUsingTabuSpaceOpti(capacity, weight, profit, n);
int ans = solveUsingTabuSpaceOpti2(capacity, weight, profit, n);
cout << "DP table:" << endl;
print(dp);
cout << endl;
cout << "Max profit:" << endl;
return ans;
}
int main()
{
vector<int> weight = {1, 2, 3};
vector<int> profit = {10, 15, 40};
int n = weight.size();
int capacity = 6;
cout << knapsack(weight, profit, n, capacity) << endl;
return 0;
}