TheAlgorithms/C++
1.0.0
All the algorithms implemented in C++
Toggle main menu visibility
Loading...
Searching...
No Matches
unbounded_0_1_knapsack.cpp
Go to the documentation of this file.
1
26
27
#include <cassert>
// For using assert function to validate test cases
28
#include <cstdint>
// For fixed-width integer types like std::uint16_t
29
#include <iostream>
// Standard input-output stream
30
#include <vector>
// Standard library for using dynamic arrays (vectors)
31
36
namespace
dynamic_programming
{
37
42
namespace
unbounded_knapsack {
43
58
std::uint16_t
KnapSackFilling
(std::uint16_t i, std::uint16_t W,
59
const
std::vector<std::uint16_t>& val,
60
const
std::vector<std::uint16_t>& wt,
61
std::vector<std::vector<int>>&
dp
) {
62
if
(i == 0) {
63
if
(wt[0] <= W) {
64
return
(W / wt[0]) *
65
val[0];
// Take as many of the first item as possible
66
}
else
{
67
return
0;
// Can't take the first item
68
}
69
}
70
if
(
dp
[i][W] != -1)
71
return
dp
[i][W];
// Return result if available
72
73
int
nottake =
74
KnapSackFilling
(i - 1, W, val, wt,
dp
);
// Value without taking item i
75
int
take = 0;
76
if
(W >= wt[i]) {
77
take = val[i] +
KnapSackFilling
(i, W - wt[i], val, wt,
78
dp
);
// Value taking item i
79
}
80
return
dp
[i][W] =
81
std::max(take, nottake);
// Store and return the maximum value
82
}
83
93
std::uint16_t
unboundedKnapsack
(std::uint16_t N, std::uint16_t W,
94
const
std::vector<std::uint16_t>& val,
95
const
std::vector<std::uint16_t>& wt) {
96
if
(N == 0)
97
return
0;
// Expect 0 since no items
98
std::vector<std::vector<int>>
dp
(
99
N, std::vector<int>(W + 1, -1));
// Initialize memoization table
100
return
KnapSackFilling
(N - 1, W, val, wt,
dp
);
// Start the calculation
101
}
102
103
}
// namespace unbounded_knapsack
104
105
}
// namespace dynamic_programming
106
111
static
void
tests
() {
112
// Test Case 1
113
std::uint16_t N1 = 4;
// Number of items
114
std::vector<std::uint16_t> wt1 = {1, 3, 4, 5};
// Weights of the items
115
std::vector<std::uint16_t> val1 = {6, 1, 7, 7};
// Values of the items
116
std::uint16_t W1 = 8;
// Maximum capacity of the knapsack
117
// Test the function and assert the expected output
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
(
122
N1, W1, val1, wt1)
123
<< std::endl;
124
125
// Test Case 2
126
std::uint16_t N2 = 3;
// Number of items
127
std::vector<std::uint16_t> wt2 = {10, 20, 30};
// Weights of the items
128
std::vector<std::uint16_t> val2 = {60, 100, 120};
// Values of the items
129
std::uint16_t W2 = 5;
// Maximum capacity of the knapsack
130
// Test the function and assert the expected output
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
(
135
N2, W2, val2, wt2)
136
<< std::endl;
137
138
// Test Case 3
139
std::uint16_t N3 = 3;
// Number of items
140
std::vector<std::uint16_t> wt3 = {2, 4, 6};
// Weights of the items
141
std::vector<std::uint16_t> val3 = {5, 11, 13};
// Values of the items
142
std::uint16_t W3 = 27;
// Maximum capacity of the knapsack
143
// Test the function and assert the expected output
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
(
148
N3, W3, val3, wt3)
149
<< std::endl;
150
151
// Test Case 4
152
std::uint16_t N4 = 0;
// Number of items
153
std::vector<std::uint16_t> wt4 = {};
// Weights of the items
154
std::vector<std::uint16_t> val4 = {};
// Values of the items
155
std::uint16_t W4 = 10;
// Maximum capacity of the knapsack
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
(
160
N4, W4, val4, wt4)
161
<< std::endl;
162
163
std::cout <<
"All test cases passed!"
<< std::endl;
164
}
165
170
int
main
() {
171
tests
();
// Run self test implementation
172
return
0;
173
}
main
int main()
Main function.
Definition
generate_parentheses.cpp:110
dp
for std::vector
Definition
partition_problem.cpp:39
dynamic_programming
Dynamic Programming algorithms.
tests
Testcases to check Union of Two Arrays.
dynamic_programming::unbounded_knapsack::unboundedKnapsack
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.
Definition
unbounded_0_1_knapsack.cpp:93
dynamic_programming::unbounded_knapsack::KnapSackFilling
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.
Definition
unbounded_0_1_knapsack.cpp:58
dynamic_programming
unbounded_0_1_knapsack.cpp
Generated by
1.18.0