-
Notifications
You must be signed in to change notification settings - Fork 0
/
knapsackdp.cpp
151 lines (140 loc) · 4.36 KB
/
knapsackdp.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
/*----------------------------------------------------------------
file: knapsackdp.cpp
-----------------------------------------------------------------*/
/*----------------------------------------------------------------
knapsackdp class
-----------------------------------------------------------------*/
/*----------------------------------------------------------------
All includes here
-----------------------------------------------------------------*/
#include "knapsackdp.h"
/*----------------------------------------------------------------
WRITE YOUR CODE BELOW
-----------------------------------------------------------------*/
void knapsackdp::_createNullMatrix(vector<vector<int>>& vec, int& _row, int& _col) {
for (int i = 0; i < _row; i++) {
vec[i] = vector<int>(_col);
for (int j = 0; j < _col; j++) {
vec[i][j] = 0;
}
}
}
void knapsackdp::_displayMatrix(vector<vector<int>>& vec, int& _row, int& _col) {
for (int i = 0; i < _row; i++) {
for (int j = 0; j < vec[i].size(); j++) {
cout << vec[i][j] << " ";
}
cout << endl;
}
}
void knapsackdp::_displayWVI(const int* w, const int* v, int& numItem) {
cout << "i = ";
for (int i = 1; i <= numItem; i++) {
cout << i << " ";
}
cout << endl;
cout << "w = ";
for (int i = 0; i < numItem; i++) {
cout << w[i] << " ";
}
cout << endl;
cout << "v = ";
for (int i = 0; i < numItem; i++) {
cout << v[i] << " ";
}
cout << endl;
}
void knapsackdp::_updateMatrix(vector<vector<int>>& vecValue, vector<vector<int>>& vecSteal, int& _row, int& _col, const int* w, const int* v) {
for (int i = 1; i < _row; i++) {
vecValue[i] = vector<int>(_col);
int weight = w[i-1];
int value = v[i-1];
for (int j = 1; j < _col; j++) {
//write here the function to build weight / value matrix
int currCol = j;
int currRow = i;
vecValue[i][j] = _buildWVMatrix(vecValue, vecSteal, weight, value, currRow, currCol);
}
}
}
int knapsackdp::_buildWVMatrix(vector<vector<int>>& vecValue, vector<vector<int>>& vecSteal, int& weight, int& value, int& currRow, int& currCol) {
if (currCol < weight) {
//cout << currRow - 1 << " " << currCol;
if ((currRow - 1) != 0) {
return vecValue[currRow - 1][currCol];
}
else { return 0; }
//return 0;
}
else {
int _stealVal = _steal(vecValue, weight, value, currRow, currCol);
int _notStealVal = _notSteal(vecValue, weight, value, currRow, currCol);
if (_stealVal > _notStealVal) {
vecSteal[currRow][currCol] = 1;
return _stealVal;
}
else {
return _notStealVal;
}
}
}
int knapsackdp::_steal(vector<vector<int>>& vecValue, int& weight, int& value, int& currRow, int& currCol) {
//int val = value;
//cout <<"currCOl " << currCol <<" weight " << weight << endl;
int newWeight = currCol - weight;
//cout <<"new weight "<< newWeight << endl;
int finalValue = value + vecValue[currRow - 1][newWeight];
return finalValue;
}
int knapsackdp::_notSteal(vector<vector<int>>& vecValue, int& weight, int& value, int& currRow, int& currCol) {
int finalValue = vecValue[currRow - 1][currCol];
return finalValue;
}
void knapsackdp::_displayFinal(vector<vector<int>>& vecSteal, const int* _w, const int* _v, int& _ans, int& numItem, int& bagSize) {
int value = 0;
int count = 0;
int row = numItem;
int col = bagSize;
vector<int> sumValues;
vector<int> indexValues;
while (row >= 0 && col >= 0) {
if (vecSteal[row][col] == 1) {
//cout << _v[row - 1] << " " << row << endl;
sumValues.push_back(_v[row - 1]);
indexValues.push_back(row);
value = value + _v[row - 1];
col = col - _w[row - 1];
row = row - 1;
}
else {
row = row - 1;
}
}
cout << "i = ";
for (int i = 1; i <= numItem; i++) {
cout << i << " ";
}
cout << endl;
cout << "w = ";
for (int i = 0; i < numItem; i++) {
cout << _w[i] << " ";
}
cout << endl;
cout << "v = ";
for (int i = 0; i < numItem; i++) {
cout << _v[i] << " ";
}
cout << endl;
cout << "Max Value of " << value << " can obtained from items {";
for (int i = 0; i < indexValues.size(); i++) {
cout << indexValues[i];
if (i != indexValues.size() - 1) { cout << ","; }
}
cout << "} that has values {";
for (int i = 0; i < sumValues.size(); i++) {
cout << sumValues[i];
if (i != sumValues.size() - 1) { cout << "+"; }
}
cout << "=" << value << "}" << endl;
}
//EOF