TheAlgorithms/C++
1.0.0
All the algorithms implemented in C++
Toggle main menu visibility
Loading...
Searching...
No Matches
0_1_knapsack.cpp
Go to the documentation of this file.
1
24
25
#include <array>
26
#include <cassert>
27
#include <iostream>
28
#include <vector>
29
34
namespace
dynamic_programming
{
39
namespace
knapsack {
50
template
<
size_t
n>
51
int
maxKnapsackValue
(
const
int
capacity,
const
std::array<int, n> &weight,
52
const
std::array<int, n> &value) {
53
std::vector<std::vector<int> > maxValue(n + 1,
54
std::vector<int>(capacity + 1, 0));
55
// outer loop will select no of items allowed
56
// inner loop will select the capacity of the knapsack bag
57
int
items =
sizeof
(weight) /
sizeof
(weight[0]);
58
for
(
size_t
i = 0; i < items + 1; ++i) {
59
for
(
size_t
j = 0; j < capacity + 1; ++j) {
60
if
(i == 0 || j == 0) {
61
// if no of items is zero or capacity is zero, then maxValue
62
// will be zero
63
maxValue[i][j] = 0;
64
}
else
if
(weight[i - 1] <= j) {
65
// if the ith item's weight(in the actual array it will be at i-1)
66
// is less than or equal to the allowed weight i.e. j then we
67
// can pick that item for our knapsack. maxValue will be the
68
// obtained either by picking the current item or by not picking
69
// current item
70
71
// picking the current item
72
int
profit1 = value[i - 1] + maxValue[i - 1][j - weight[i - 1]];
73
74
// not picking the current item
75
int
profit2 = maxValue[i - 1][j];
76
77
maxValue[i][j] = std::max(profit1, profit2);
78
}
else
{
79
// as the weight of the current item is greater than the allowed weight, so
80
// maxProfit will be profit obtained by excluding the current item.
81
maxValue[i][j] = maxValue[i - 1][j];
82
}
83
}
84
}
85
86
// returning maximum value
87
return
maxValue[items][capacity];
88
}
89
}
// namespace knapsack
90
}
// namespace dynamic_programming
91
96
static
void
test
() {
97
// Test 1
98
const
int
n1 = 3;
// number of items
99
std::array<int, n1> weight1 = {10, 20, 30};
// weight of each item
100
std::array<int, n1> value1 = {60, 100, 120};
// value of each item
101
const
int
capacity1 = 50;
// capacity of carrying bag
102
const
int
max_value1 =
dynamic_programming::knapsack::maxKnapsackValue
(
103
capacity1, weight1, value1);
104
const
int
expected_max_value1 = 220;
105
assert(max_value1 == expected_max_value1);
106
std::cout <<
"Maximum Knapsack value with "
<< n1 <<
" items is "
107
<< max_value1 << std::endl;
108
109
// Test 2
110
const
int
n2 = 4;
// number of items
111
std::array<int, n2> weight2 = {24, 10, 10, 7};
// weight of each item
112
std::array<int, n2> value2 = {24, 18, 18, 10};
// value of each item
113
const
int
capacity2 = 25;
// capacity of carrying bag
114
const
int
max_value2 =
dynamic_programming::knapsack::maxKnapsackValue
(
115
capacity2, weight2, value2);
116
const
int
expected_max_value2 = 36;
117
assert(max_value2 == expected_max_value2);
118
std::cout <<
"Maximum Knapsack value with "
<< n2 <<
" items is "
119
<< max_value2 << std::endl;
120
}
121
126
int
main
() {
127
// Testing
128
test
();
129
return
0;
130
}
dynamic_programming::knapsack::maxKnapsackValue
int maxKnapsackValue(const int capacity, const std::array< int, n > &weight, const std::array< int, n > &value)
Picking up all those items whose combined weight is below the given capacity and calculating the valu...
Definition
0_1_knapsack.cpp:51
test
void test()
Definition
caesar_cipher.cpp:100
main
int main()
Main function.
Definition
generate_parentheses.cpp:110
dynamic_programming
Dynamic Programming algorithms.
dynamic_programming
0_1_knapsack.cpp
Generated by
1.18.0