@@ -39,21 +39,21 @@ namespace dynamic_programming {
39
39
namespace knapsack {
40
40
/* *
41
41
* @brief Picking up all those items whose combined weight is below
42
- * given capacity and calculating value of those picked items.Trying all
42
+ * the given capacity and calculating the value of those picked items. Trying all
43
43
* possible combinations will yield the maximum knapsack value.
44
44
* @tparam n size of the weight and value array
45
45
* @param capacity capacity of the carrying bag
46
- * @param weight array representing weight of items
47
- * @param value array representing value of items
48
- * @return maximum value obtainable with given capacity.
46
+ * @param weight array representing the weight of items
47
+ * @param value array representing the value of items
48
+ * @return maximum value obtainable with a given capacity.
49
49
*/
50
50
template <size_t n>
51
51
int maxKnapsackValue (const int capacity, const std::array<int , n> &weight,
52
52
const std::array<int , n> &value) {
53
53
std::vector<std::vector<int > > maxValue (n + 1 ,
54
54
std::vector<int >(capacity + 1 , 0 ));
55
55
// outer loop will select no of items allowed
56
- // inner loop will select capcity of knapsack bag
56
+ // inner loop will select the capacity of the knapsack bag
57
57
int items = sizeof (weight) / sizeof (weight[0 ]);
58
58
for (size_t i = 0 ; i < items + 1 ; ++i) {
59
59
for (size_t j = 0 ; j < capacity + 1 ; ++j) {
@@ -62,22 +62,22 @@ int maxKnapsackValue(const int capacity, const std::array<int, n> &weight,
62
62
// will be zero
63
63
maxValue[i][j] = 0 ;
64
64
} else if (weight[i - 1 ] <= j) {
65
- // if the ith item's weight(in actual array it will be at i-1)
65
+ // if the ith item's weight(in the actual array it will be at i-1)
66
66
// is less than or equal to the allowed weight i.e. j then we
67
67
// can pick that item for our knapsack. maxValue will be the
68
68
// obtained either by picking the current item or by not picking
69
69
// current item
70
70
71
- // picking current item
71
+ // picking the current item
72
72
int profit1 = value[i - 1 ] + maxValue[i - 1 ][j - weight[i - 1 ]];
73
73
74
- // not picking current item
74
+ // not picking the current item
75
75
int profit2 = maxValue[i - 1 ][j];
76
76
77
77
maxValue[i][j] = std::max (profit1, profit2);
78
78
} else {
79
- // as weight of current item is greater than allowed weight, so
80
- // maxProfit will be profit obtained by excluding current item.
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
81
maxValue[i][j] = maxValue[i - 1 ][j];
82
82
}
83
83
}
@@ -90,7 +90,7 @@ int maxKnapsackValue(const int capacity, const std::array<int, n> &weight,
90
90
} // namespace dynamic_programming
91
91
92
92
/* *
93
- * @brief Function to test above algorithm
93
+ * @brief Function to test the above algorithm
94
94
* @returns void
95
95
*/
96
96
static void test () {
0 commit comments