Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit db3f9d3

Browse files
Create Unbounded_knapsack.cpp (TheAlgorithms#2786)
* Create Unbounded_knapsack.cpp * Update Unbounded_knapsack.cpp Documentation done. * Update dynamic_programming/Unbounded_knapsack.cpp Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com> * Update dynamic_programming/Unbounded_knapsack.cpp Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com> * Update dynamic_programming/Unbounded_knapsack.cpp Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com> * Update dynamic_programming/Unbounded_knapsack.cpp Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com> * Delete dynamic_programming/Unbounded_knapsack.cpp * Create Unbounded_0_1_Knapsack.cpp * Update Unbounded_0_1_Knapsack.cpp * Update Unbounded_0_1_Knapsack.cpp * docs: add docs for main * Update dynamic_programming/Unbounded_0_1_Knapsack.cpp Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com> * Update dynamic_programming/Unbounded_0_1_Knapsack.cpp Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com> * Update Unbounded_0_1_Knapsack.cpp * Update Unbounded_0_1_Knapsack.cpp * Update Unbounded_0_1_Knapsack.cpp * Update dynamic_programming/Unbounded_0_1_Knapsack.cpp Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com> * Update Unbounded_0_1_Knapsack.cpp * Update Unbounded_0_1_Knapsack.cpp --------- Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com>
1 parent ecb8a33 commit db3f9d3

File tree

1 file changed

+151
-0
lines changed

1 file changed

+151
-0
lines changed
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
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+
*/
26+
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+
* @note "val" data type can be changed according to the size of the input.
52+
* @param wt Vector of weights corresponding to the items.
53+
* @note "wt" data type can be changed according to the size of the input.
54+
* @param dp 2D vector for memoization to avoid redundant calculations.
55+
* @return The maximum value that can be obtained for the given index and capacity.
56+
*/
57+
std::uint16_t KnapSackFilling(std::uint16_t i, std::uint16_t W,
58+
const std::vector<std::uint16_t>& val,
59+
const std::vector<std::uint16_t>& wt,
60+
std::vector<std::vector<int>>& dp) {
61+
if (i == 0) {
62+
if (wt[0] <= W) {
63+
return (W / wt[0]) * val[0]; // Take as many of the first item as possible
64+
} else {
65+
return 0; // Can't take the first item
66+
}
67+
}
68+
if (dp[i][W] != -1) return dp[i][W]; // Return result if available
69+
70+
int nottake = KnapSackFilling(i - 1, W, val, wt, dp); // Value without taking item i
71+
int take = 0;
72+
if (W >= wt[i]) {
73+
take = val[i] + KnapSackFilling(i, W - wt[i], val, wt, dp); // Value taking item i
74+
}
75+
return dp[i][W] = std::max(take, nottake); // Store and return the maximum value
76+
}
77+
78+
/**
79+
* @brief Wrapper function to initiate the unbounded knapsack calculation.
80+
*
81+
* @param N Number of items.
82+
* @param W Maximum weight capacity of the knapsack.
83+
* @param val Vector of values corresponding to the items.
84+
* @param wt Vector of weights corresponding to the items.
85+
* @return The maximum value that can be obtained for the given capacity.
86+
*/
87+
std::uint16_t unboundedKnapsack(std::uint16_t N, std::uint16_t W,
88+
const std::vector<std::uint16_t>& val,
89+
const std::vector<std::uint16_t>& wt) {
90+
if(N==0)return 0; // Expect 0 since no items
91+
std::vector<std::vector<int>> dp(N, std::vector<int>(W + 1, -1)); // Initialize memoization table
92+
return KnapSackFilling(N - 1, W, val, wt, dp); // Start the calculation
93+
}
94+
95+
} // unbounded_knapsack
96+
97+
} // dynamic_programming
98+
99+
/**
100+
* @brief self test implementation
101+
* @return void
102+
*/
103+
static void tests() {
104+
// Test Case 1
105+
std::uint16_t N1 = 4; // Number of items
106+
std::vector<std::uint16_t> wt1 = {1, 3, 4, 5}; // Weights of the items
107+
std::vector<std::uint16_t> val1 = {6, 1, 7, 7}; // Values of the items
108+
std::uint16_t W1 = 8; // Maximum capacity of the knapsack
109+
// Test the function and assert the expected output
110+
assert(unboundedKnapsack(N1, W1, val1, wt1) == 48);
111+
std::cout << "Maximum Knapsack value " << unboundedKnapsack(N1, W1, val1, wt1) << std::endl;
112+
113+
// Test Case 2
114+
std::uint16_t N2 = 3; // Number of items
115+
std::vector<std::uint16_t> wt2 = {10, 20, 30}; // Weights of the items
116+
std::vector<std::uint16_t> val2 = {60, 100, 120}; // Values of the items
117+
std::uint16_t W2 = 5; // Maximum capacity of the knapsack
118+
// Test the function and assert the expected output
119+
assert(unboundedKnapsack(N2, W2, val2, wt2) == 0);
120+
std::cout << "Maximum Knapsack value " << unboundedKnapsack(N2, W2, val2, wt2) << std::endl;
121+
122+
// Test Case 3
123+
std::uint16_t N3 = 3; // Number of items
124+
std::vector<std::uint16_t> wt3 = {2, 4, 6}; // Weights of the items
125+
std::vector<std::uint16_t> val3 = {5, 11, 13};// Values of the items
126+
std::uint16_t W3 = 27;// Maximum capacity of the knapsack
127+
// Test the function and assert the expected output
128+
assert(unboundedKnapsack(N3, W3, val3, wt3) == 27);
129+
std::cout << "Maximum Knapsack value " << unboundedKnapsack(N3, W3, val3, wt3) << std::endl;
130+
131+
// Test Case 4
132+
std::uint16_t N4 = 0; // Number of items
133+
std::vector<std::uint16_t> wt4 = {}; // Weights of the items
134+
std::vector<std::uint16_t> val4 = {}; // Values of the items
135+
std::uint16_t W4 = 10; // Maximum capacity of the knapsack
136+
assert(unboundedKnapsack(N4, W4, val4, wt4) == 0);
137+
std::cout << "Maximum Knapsack value for empty arrays: " << unboundedKnapsack(N4, W4, val4, wt4) << std::endl;
138+
139+
std::cout << "All test cases passed!" << std::endl;
140+
141+
}
142+
143+
/**
144+
* @brief main function
145+
* @return 0 on successful exit
146+
*/
147+
int main() {
148+
tests(); // Run self test implementation
149+
return 0;
150+
}
151+

0 commit comments

Comments
 (0)