59 const std::vector<std::uint16_t>& val,
60 const std::vector<std::uint16_t>& wt,
61 std::vector<std::vector<int>>&
dp) {
81 std::max(take, nottake);
94 const std::vector<std::uint16_t>& val,
95 const std::vector<std::uint16_t>& wt) {
98 std::vector<std::vector<int>>
dp(
99 N, std::vector<int>(W + 1, -1));
113 std::uint16_t N1 = 4;
114 std::vector<std::uint16_t> wt1 = {1, 3, 4, 5};
115 std::vector<std::uint16_t> val1 = {6, 1, 7, 7};
116 std::uint16_t W1 = 8;
118 assert(dynamic_programming::unbounded_knapsack::unboundedKnapsack(
119 N1, W1, val1, wt1) == 48);
120 std::cout <<
"Maximum Knapsack value "
121 << dynamic_programming::unbounded_knapsack::unboundedKnapsack(
126 std::uint16_t N2 = 3;
127 std::vector<std::uint16_t> wt2 = {10, 20, 30};
128 std::vector<std::uint16_t> val2 = {60, 100, 120};
129 std::uint16_t W2 = 5;
131 assert(dynamic_programming::unbounded_knapsack::unboundedKnapsack(
132 N2, W2, val2, wt2) == 0);
133 std::cout <<
"Maximum Knapsack value "
134 << dynamic_programming::unbounded_knapsack::unboundedKnapsack(
139 std::uint16_t N3 = 3;
140 std::vector<std::uint16_t> wt3 = {2, 4, 6};
141 std::vector<std::uint16_t> val3 = {5, 11, 13};
142 std::uint16_t W3 = 27;
144 assert(dynamic_programming::unbounded_knapsack::unboundedKnapsack(
145 N3, W3, val3, wt3) == 27);
146 std::cout <<
"Maximum Knapsack value "
147 << dynamic_programming::unbounded_knapsack::unboundedKnapsack(
152 std::uint16_t N4 = 0;
153 std::vector<std::uint16_t> wt4 = {};
154 std::vector<std::uint16_t> val4 = {};
155 std::uint16_t W4 = 10;
156 assert(dynamic_programming::unbounded_knapsack::unboundedKnapsack(
157 N4, W4, val4, wt4) == 0);
158 std::cout <<
"Maximum Knapsack value for empty arrays: "
159 << dynamic_programming::unbounded_knapsack::unboundedKnapsack(
163 std::cout <<
"All test cases passed!" << std::endl;
std::uint16_t unboundedKnapsack(std::uint16_t N, std::uint16_t W, const std::vector< std::uint16_t > &val, const std::vector< std::uint16_t > &wt)
Wrapper function to initiate the unbounded knapsack calculation.
std::uint16_t KnapSackFilling(std::uint16_t i, std::uint16_t W, const std::vector< std::uint16_t > &val, const std::vector< std::uint16_t > &wt, std::vector< std::vector< int > > &dp)
Recursive function to calculate the maximum value obtainable using an unbounded knapsack approach.