Skip to content

Commit bb5b955

Browse files
Merge branch 'master' into ctest
2 parents 84acc27 + 9b1d702 commit bb5b955

20 files changed

+2280
-247
lines changed

DIRECTORY.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
* [Count Of Set Bits](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/bit_manipulation/count_of_set_bits.cpp)
2020
* [Count Of Trailing Ciphers In Factorial N](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/bit_manipulation/count_of_trailing_ciphers_in_factorial_n.cpp)
2121
* [Find Non Repeating Number](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/bit_manipulation/find_non_repeating_number.cpp)
22+
* [Gray Code](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/bit_manipulation/gray_code.cpp)
2223
* [Hamming Distance](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/bit_manipulation/hamming_distance.cpp)
2324
* [Next Higher Number With Same Number Of Set Bits](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/bit_manipulation/next_higher_number_with_same_number_of_set_bits.cpp)
2425
* [Power Of 2](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/bit_manipulation/power_of_2.cpp)
@@ -121,6 +122,7 @@
121122
* [Subset Sum](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/dynamic_programming/subset_sum.cpp)
122123
* [Trapped Rainwater](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/dynamic_programming/trapped_rainwater.cpp)
123124
* [Tree Height](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/dynamic_programming/tree_height.cpp)
125+
* [Unbounded 0 1 Knapsack](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/dynamic_programming/unbounded_0_1_knapsack.cpp)
124126
* [Word Break](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/dynamic_programming/word_break.cpp)
125127

126128
## Games
@@ -159,8 +161,11 @@
159161
* [Spirograph](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/graphics/spirograph.cpp)
160162

161163
## Greedy Algorithms
164+
* [Binary Addition](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/greedy_algorithms/binary_addition.cpp)
162165
* [Boruvkas Minimum Spanning Tree](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/greedy_algorithms/boruvkas_minimum_spanning_tree.cpp)
166+
* [Digit Separation](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/greedy_algorithms/digit_separation.cpp)
163167
* [Dijkstra](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/greedy_algorithms/dijkstra.cpp)
168+
* [Gale Shapley](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/greedy_algorithms/gale_shapley.cpp)
164169
* [Huffman](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/greedy_algorithms/huffman.cpp)
165170
* [Jump Game](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/greedy_algorithms/jump_game.cpp)
166171
* [Knapsack](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/greedy_algorithms/knapsack.cpp)
@@ -298,7 +303,10 @@
298303
* [Iterative Tree Traversals](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/others/iterative_tree_traversals.cpp)
299304
* [Kadanes3](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/others/kadanes3.cpp)
300305
* [Kelvin To Celsius](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/others/kelvin_to_celsius.cpp)
306+
* [Lfu Cache](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/others/lfu_cache.cpp)
307+
* [Longest Substring Without Repeating Characters](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/others/longest_substring_without_repeating_characters.cpp)
301308
* [Lru Cache](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/others/lru_cache.cpp)
309+
* [Lru Cache2](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/others/lru_cache2.cpp)
302310
* [Matrix Exponentiation](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/others/matrix_exponentiation.cpp)
303311
* [Palindrome Of Number](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/others/palindrome_of_number.cpp)
304312
* [Paranthesis Matching](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/others/paranthesis_matching.cpp)
@@ -320,6 +328,7 @@
320328
* [Addition Rule](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/probability/addition_rule.cpp)
321329
* [Bayes Theorem](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/probability/bayes_theorem.cpp)
322330
* [Binomial Dist](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/probability/binomial_dist.cpp)
331+
* [Exponential Dist](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/probability/exponential_dist.cpp)
323332
* [Geometric Dist](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/probability/geometric_dist.cpp)
324333
* [Poisson Dist](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/probability/poisson_dist.cpp)
325334
* [Windowed Median](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/probability/windowed_median.cpp)
@@ -343,6 +352,7 @@
343352
* [Interpolation Search2](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/search/interpolation_search2.cpp)
344353
* [Jump Search](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/search/jump_search.cpp)
345354
* [Linear Search](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/search/linear_search.cpp)
355+
* [Longest Increasing Subsequence Using Binary Search](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/search/longest_increasing_subsequence_using_binary_search.cpp)
346356
* [Median Search](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/search/median_search.cpp)
347357
* [Median Search2](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/search/median_search2.cpp)
348358
* [Saddleback Search](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/search/saddleback_search.cpp)
@@ -367,6 +377,7 @@
367377
* [Gnome Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/sorting/gnome_sort.cpp)
368378
* [Heap Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/sorting/heap_sort.cpp)
369379
* [Insertion Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/sorting/insertion_sort.cpp)
380+
* [Insertion Sort Recursive](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/sorting/insertion_sort_recursive.cpp)
370381
* [Library Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/sorting/library_sort.cpp)
371382
* [Merge Insertion Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/sorting/merge_insertion_sort.cpp)
372383
* [Merge Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/sorting/merge_sort.cpp)
@@ -377,6 +388,7 @@
377388
* [Pigeonhole Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/sorting/pigeonhole_sort.cpp)
378389
* [Quick Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/sorting/quick_sort.cpp)
379390
* [Quick Sort 3](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/sorting/quick_sort_3.cpp)
391+
* [Quick Sort Iterative](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/sorting/quick_sort_iterative.cpp)
380392
* [Radix Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/sorting/radix_sort.cpp)
381393
* [Radix Sort2](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/sorting/radix_sort2.cpp)
382394
* [Random Pivot Quick Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/sorting/random_pivot_quick_sort.cpp)
@@ -396,6 +408,7 @@
396408
## Strings
397409
* [Boyer Moore](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/strings/boyer_moore.cpp)
398410
* [Brute Force String Searching](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/strings/brute_force_string_searching.cpp)
411+
* [Duval](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/strings/duval.cpp)
399412
* [Horspool](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/strings/horspool.cpp)
400413
* [Knuth Morris Pratt](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/strings/knuth_morris_pratt.cpp)
401414
* [Manacher Algorithm](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/strings/manacher_algorithm.cpp)
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)