forked from samuel-stjean/algo_project
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathknapsack.cpp
More file actions
117 lines (99 loc) · 2.88 KB
/
Copy pathknapsack.cpp
File metadata and controls
117 lines (99 loc) · 2.88 KB
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
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int knapsack_variant(int n, int W, int l, vector<int> &weights, vector<int> &values)
{
vector<vector<vector<int>>> OPT(n + 1, vector<vector<int>>(W + 1, vector<int>(l + 1, 0)));
for (int i = 1; i <= n; ++i)
{
for (int w = 0; w <= W; ++w)
{
for (int k = 0; k <= l; ++k)
{
// Base cases
if (i < k || w == 0 || k == 0)
{
OPT[i][w][k] = 0;
}
else if (weights[i - 1] == w && k == 1)
{
OPT[i][w][k] = max(OPT[i - 1][w][k], values[i - 1]);
}
else if (weights[i - 1] <= w && k > 1)
{
OPT[i][w][k] = max(OPT[i - 1][w][k], values[i - 1] + OPT[i - 1][w - weights[i - 1]][k - 1]);
}
else
{
OPT[i][w][k] = OPT[i - 1][w][k];
}
}
}
}
// Check if a solution exists
if (OPT[n][W][l] == 0)
{
cout << "No solution exists." << endl;
return 0;
}
// Traceback to find the selected items
int i = n, w = W, k = l;
vector<int> selected_items;
while (i > 0 && w > 0 && k > 0)
{
if (weights[i - 1] == w && k == 1)
{
selected_items.push_back(i);
break;
}
else if (OPT[i][w][k] != OPT[i - 1][w][k])
{
selected_items.push_back(i);
w -= weights[i - 1];
k--;
}
i--;
}
// Output the selected items and their values and weights
cout << "Selected items:" << endl;
for (int i = selected_items.size() - 1; i >= 0; --i)
{
int item_index = selected_items[i] - 1;
cout << "Item " << selected_items[i] << ": Value = " << values[item_index]
<< ", Weight = " << weights[item_index] << endl;
}
// Output the maximum value of the subset
cout << "Maximum value of a subset: " << OPT[n][W][l] << endl;
return OPT[n][W][l];
}
int main()
{
while (true)
{
int n, W, l;
cout << "Enter the number of items (enter 0 to exit): ";
cin >> n;
if (n == 0)
{
break; // Exit the program if the user enters 0
}
vector<int> weights(n), values(n);
cout << "Enter the weights of items: ";
for (int i = 0; i < n; ++i)
{
cin >> weights[i];
}
cout << "Enter the values of items: ";
for (int i = 0; i < n; ++i)
{
cin >> values[i];
}
cout << "Enter the total weight (W): ";
cin >> W;
cout << "Enter the number of items in the subset (l): ";
cin >> l;
knapsack_variant(n, W, l, weights, values);
}
return 0;
}