forked from JiauZhang/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1_knapsack.cpp
167 lines (141 loc) · 3.04 KB
/
1_knapsack.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
157
158
159
160
161
162
163
164
165
166
167
/*
* Copyright(c) 2019 Jiau Zhang
* For more information see <https://github.com/JiauZhang/algorithms>
*
* This repo is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation
*
* It is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with THIS repo. If not, see <http://www.gnu.org/licenses/>.
*/
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
/*
0-1 ±³°üÎÊÌâ
*/
// total weight
#define W (18)
// total stuff
#define N (7)
int table[N+1][W+1] = {0};
int value[N+1] = {0, 12, 10, 8, 11, 14, 7, 9};
int weight[N+1] = {0, 4, 6, 5, 7, 3, 1, 6};
void knapsack_stuff()
{
int k, w;
for (k=1; k<=N; k++) {
for (w=1; w<=W; w++) {
if (weight[k] > w) {
table[k][w] = table[k-1][w];
} else {
int value1 = table[k-1][w-weight[k]] + value[k];
int value2 = table[k-1][w];
if (value1 > value2) {
table[k][w] = value1;
} else {
table[k][w] = value2;
}
}
}
}
}
void knapsack_weight()
{
int k, w;
for (w=1; w<=W; w++) {
for (k=1; k<=N; k++) {
if (weight[k] > w) {
table[k][w] = table[k-1][w];
} else {
int value1 = table[k-1][w-weight[k]] + value[k];
int value2 = table[k-1][w];
if (value1 > value2) {
table[k][w] = value1;
} else {
table[k][w] = value2;
}
}
}
}
}
void knapsack_optimization()
{
int k, w;
for (k=1; k<=N; k++) {
for (w=W; w>=1; w--) {
if (weight[k] > w) {
table[0][w] = table[0][w];
} else {
int value1 = table[0][w-weight[k]] + value[k];
int value2 = table[0][w];
if (value1 > value2) {
table[0][w] = value1;
} else {
table[0][w] = value2;
}
}
}
}
}
void show_table()
{
for (int i=0; i<=N; i++) {
for (int j=0; j<=W; j++) {
cout << ' ' << table[i][j] << ' ';
}
cout << '\n';
}
cout << endl;
}
void show_track()
{
vector<pair<int, int>> track;
int i=N, j=W;
cout << "track is: ";
while (i>0) {
if (table[i][j] == table[i-1][j]) {
i--;
} else {
track.push_back(make_pair(i, j));
int w = weight[i];
j -= w;
i--;
}
}
for (int i=0; i<track.size(); i++) {
cout << track[i].first << ' ';
}
cout << endl;
}
void reset_table()
{
for (int i=0; i<=N; i++) {
for (int j=0; j<=W; j++) {
table[i][j] = 0;
}
}
}
int main()
{
knapsack_stuff();
cout << "knapsack stuff first" << endl;
show_table();
reset_table();
knapsack_weight();
cout << "knapsack weight first" << endl;
show_table();
show_track();
reset_table();
knapsack_optimization();
cout << "knapsack optimization" << endl;
show_table();
return 0;
}