Skip to content

Commit e786129

Browse files
Update Unbounded_0_1_Knapsack.cpp
1 parent 6a8ca41 commit e786129

File tree

1 file changed

+135
-0
lines changed

1 file changed

+135
-0
lines changed
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,136 @@
1+
/**
2+
* @file
3+
* @brief Implementation of the Unbounded 0/1 Knapsack Problem
4+
*
5+
* @details
6+
* The Unbounded 0/1 Knapsack problem allows taking unlimited quantities of each item.
7+
* The goal is to maximize the total value without exceeding the given knapsack capacity.
8+
* Unlike the 0/1 knapsack, where each item can be taken only once, in this variation,
9+
* any item can be picked any number of times as long as the total weight stays within
10+
* the knapsack's capacity.
11+
*
12+
* Given a set of N items, each with a weight and a value, represented by the arrays
13+
* `wt` and `val` respectively, and a knapsack with a weight limit W, the task is to
14+
* fill the knapsack to maximize the total value.
15+
*
16+
* @note weight and value of items is greater than zero.
17+
*
18+
* ### Algorithm
19+
* The approach uses dynamic programming to build a solution iteratively.
20+
* A 2D array is used for memoization to store intermediate results, allowing
21+
* the function to avoid redundant calculations.
22+
*
23+
* @author [Sanskruti Yeole](https://github.com/yeolesanskruti)
24+
* @see dynamic_programming/0_1_knapsack.cpp
25+
*/
126

27+
#include <iostream> ///< Standard input-output stream
28+
#include <vector> ///< Standard library for using dynamic arrays (vectors)
29+
#include <cassert> ///< For using assert function to validate test cases
30+
#include <cstdint> ///< For fixed-width integer types like std::uint16_t
31+
32+
/**
33+
* @namespace dynamic_programming
34+
* @brief Namespace for dynamic programming algorithms
35+
*/
36+
namespace dynamic_programming {
37+
38+
/**
39+
* @namespace Knapsack
40+
* @brief Implementation of unbounded 0-1 knapsack problem
41+
*/
42+
namespace unbounded_knapsack {
43+
44+
/**
45+
* @brief Recursive function to calculate the maximum value obtainable using
46+
* an unbounded knapsack approach.
47+
*
48+
* @param i Current index in the value and weight vectors.
49+
* @param W Remaining capacity of the knapsack.
50+
* @param val Vector of values corresponding to the items.
51+
* @param wt Vector of weights corresponding to the items.
52+
* @param dp 2D vector for memoization to avoid redundant calculations.
53+
* @return The maximum value that can be obtained for the given index and capacity.
54+
*/
55+
int KnapSackFilling(std::uint16_t i, std::uint16_t W,
56+
const std::vector<std::uint16_t>& val,
57+
const std::vector<std::uint16_t>& wt,
58+
std::vector<std::vector<int>>& dp) {
59+
if (i == 0) {
60+
if (wt[0] <= W) {
61+
return (W / wt[0]) * val[0]; // Take as many of the first item as possible
62+
} else {
63+
return 0; // Can't take the first item
64+
}
65+
}
66+
if (dp[i][W] != -1) return dp[i][W]; // Return result if available
67+
68+
int nottake = KnapSackFilling(i - 1, W, val, wt, dp); // Value without taking item i
69+
int take = 0;
70+
if (W >= wt[i]) {
71+
take = val[i] + KnapSackFilling(i, W - wt[i], val, wt, dp); // Value taking item i
72+
}
73+
return dp[i][W] = std::max(take, nottake); // Store and return the maximum value
74+
}
75+
76+
/**
77+
* @brief Wrapper function to initiate the unbounded knapsack calculation.
78+
*
79+
* @param N Number of items.
80+
* @param W Maximum weight capacity of the knapsack.
81+
* @param val Vector of values corresponding to the items.
82+
* @param wt Vector of weights corresponding to the items.
83+
* @return The maximum value that can be obtained for the given capacity.
84+
*/
85+
int unboundedKnapsack(std::uint16_t N, std::uint16_t W,
86+
const std::vector<std::uint16_t>& val,
87+
const std::vector<std::uint16_t>& wt) {
88+
std::vector<std::vector<int>> dp(N, std::vector<int>(W + 1, -1)); // Initialize memoization table
89+
return KnapSackFilling(N - 1, W, val, wt, dp); // Start the calculation
90+
}
91+
92+
/**
93+
* @brief Function to run test cases for the unbounded knapsack implementation.
94+
*
95+
* @details This function tests the implementation with predefined test cases.
96+
* It asserts the expected outcomes to ensure correctness of the algorithm.
97+
*/
98+
void tests() {
99+
// Test Case 1
100+
std::uint16_t N1 = 4; // Number of items
101+
std::vector<std::uint16_t> wt1 = {1, 3, 4, 5}; // Weights of the items
102+
std::vector<std::uint16_t> val1 = {6, 1, 7, 7}; // Values of the items
103+
std::uint16_t W1 = 8; // Maximum capacity of the knapsack
104+
// Test the function and assert the expected output
105+
assert(unboundedKnapsack(N1, W1, val1, wt1) == 48);
106+
std::cout << "Maximum Knapsack value " << unboundedKnapsack(N1, W1, val1, wt1) << std::endl;
107+
108+
// Test Case 2
109+
std::uint16_t N2 = 3; // Number of items
110+
std::vector<std::uint16_t> wt2 = {10, 20, 30}; // Weights of the items
111+
std::vector<std::uint16_t> val2 = {60, 100, 120}; // Values of the items
112+
std::uint16_t W2 = 5; // Maximum capacity of the knapsack
113+
// Test the function and assert the expected output
114+
assert(unboundedKnapsack(N2, W2, val2, wt2) == 0);
115+
std::cout << "Maximum Knapsack value " << unboundedKnapsack(N2, W2, val2, wt2) << std::endl;
116+
117+
// Test Case 3
118+
std::uint16_t N3 = 3; // Number of items
119+
std::vector<std::uint16_t> wt3 = {2, 4, 6}; // Weights of the items
120+
std::vector<std::uint16_t> val3 = {5, 11, 13};// Values of the items
121+
std::uint16_t W3 = 27;// Maximum capacity of the knapsack
122+
// Test the function and assert the expected output
123+
assert(unboundedKnapsack(N3, W3, val3, wt3) == 27);
124+
std::cout << "Maximum Knapsack value " << unboundedKnapsack(N3, W3, val3, wt3) << std::endl;
125+
126+
std::cout << "All test cases passed!" << std::endl;
127+
128+
}
129+
130+
int main() {
131+
tests(); // Run test cases to validate implementation
132+
return 0;
133+
}
134+
135+
}
136+
}

0 commit comments

Comments
 (0)