Skip to content

Commit 80e27ba

Browse files
Merge branch 'master' into add/number_of_paths
2 parents ee6e56c + 649a145 commit 80e27ba

File tree

2 files changed

+223
-22
lines changed

2 files changed

+223
-22
lines changed
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+

math/sieve_of_eratosthenes.cpp

+72-22
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/**
22
* @file
3-
* @brief Get list of prime numbers using Sieve of Eratosthenes
3+
* @brief Prime Numbers using [Sieve of
4+
* Eratosthenes](https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes)
45
* @details
56
* Sieve of Eratosthenes is an algorithm that finds all the primes
67
* between 2 and N.
@@ -11,21 +12,39 @@
1112
* @see primes_up_to_billion.cpp prime_numbers.cpp
1213
*/
1314

14-
#include <cassert>
15-
#include <iostream>
16-
#include <vector>
15+
#include <cassert> /// for assert
16+
#include <iostream> /// for IO operations
17+
#include <vector> /// for std::vector
1718

1819
/**
19-
* This is the function that finds the primes and eliminates the multiples.
20+
* @namespace math
21+
* @brief Mathematical algorithms
22+
*/
23+
namespace math {
24+
/**
25+
* @namespace sieve_of_eratosthenes
26+
* @brief Functions for finding Prime Numbers using Sieve of Eratosthenes
27+
*/
28+
namespace sieve_of_eratosthenes {
29+
/**
30+
* @brief Function to sieve out the primes
31+
* @details
32+
* This function finds all the primes between 2 and N using the Sieve of
33+
* Eratosthenes algorithm. It starts by assuming all numbers (except zero and
34+
* one) are prime and then iteratively marks the multiples of each prime as
35+
* non-prime.
36+
*
2037
* Contains a common optimization to start eliminating multiples of
2138
* a prime p starting from p * p since all of the lower multiples
2239
* have been already eliminated.
23-
* @param N number of primes to check
24-
* @return is_prime a vector of `N + 1` booleans identifying if `i`^th number is a prime or not
40+
* @param N number till which primes are to be found
41+
* @return is_prime a vector of `N + 1` booleans identifying if `i`^th number is
42+
* a prime or not
2543
*/
2644
std::vector<bool> sieve(uint32_t N) {
27-
std::vector<bool> is_prime(N + 1, true);
28-
is_prime[0] = is_prime[1] = false;
45+
std::vector<bool> is_prime(N + 1, true); // Initialize all as prime numbers
46+
is_prime[0] = is_prime[1] = false; // 0 and 1 are not prime numbers
47+
2948
for (uint32_t i = 2; i * i <= N; i++) {
3049
if (is_prime[i]) {
3150
for (uint32_t j = i * i; j <= N; j += i) {
@@ -37,9 +56,10 @@ std::vector<bool> sieve(uint32_t N) {
3756
}
3857

3958
/**
40-
* This function prints out the primes to STDOUT
41-
* @param N number of primes to check
42-
* @param is_prime a vector of `N + 1` booleans identifying if `i`^th number is a prime or not
59+
* @brief Function to print the prime numbers
60+
* @param N number till which primes are to be found
61+
* @param is_prime a vector of `N + 1` booleans identifying if `i`^th number is
62+
* a prime or not
4363
*/
4464
void print(uint32_t N, const std::vector<bool> &is_prime) {
4565
for (uint32_t i = 2; i <= N; i++) {
@@ -50,23 +70,53 @@ void print(uint32_t N, const std::vector<bool> &is_prime) {
5070
std::cout << std::endl;
5171
}
5272

73+
} // namespace sieve_of_eratosthenes
74+
} // namespace math
75+
5376
/**
54-
* Test implementations
77+
* @brief Self-test implementations
78+
* @return void
5579
*/
56-
void tests() {
57-
// 0 1 2 3 4 5 6 7 8 9 10
58-
std::vector<bool> ans{false, false, true, true, false, true, false, true, false, false, false};
59-
assert(sieve(10) == ans);
80+
static void tests() {
81+
std::vector<bool> is_prime_1 =
82+
math::sieve_of_eratosthenes::sieve(static_cast<uint32_t>(10));
83+
std::vector<bool> is_prime_2 =
84+
math::sieve_of_eratosthenes::sieve(static_cast<uint32_t>(20));
85+
std::vector<bool> is_prime_3 =
86+
math::sieve_of_eratosthenes::sieve(static_cast<uint32_t>(100));
87+
88+
std::vector<bool> expected_1{false, false, true, true, false, true,
89+
false, true, false, false, false};
90+
assert(is_prime_1 == expected_1);
91+
92+
std::vector<bool> expected_2{false, false, true, true, false, true,
93+
false, true, false, false, false, true,
94+
false, true, false, false, false, true,
95+
false, true, false};
96+
assert(is_prime_2 == expected_2);
97+
98+
std::vector<bool> expected_3{
99+
false, false, true, true, false, true, false, true, false, false,
100+
false, true, false, true, false, false, false, true, false, true,
101+
false, false, false, true, false, false, false, false, false, true,
102+
false, true, false, false, false, false, false, true, false, false,
103+
false, true, false, true, false, false, false, true, false, false,
104+
false, false, false, true, false, false, false, false, false, true,
105+
false, true, false, false, false, false, false, true, false, false,
106+
false, true, false, true, false, false, false, false, false, true,
107+
false, false, false, true, false, false, false, false, false, true,
108+
false, false, false, false, false, false, false, true, false, false,
109+
false};
110+
assert(is_prime_3 == expected_3);
111+
112+
std::cout << "All tests have passed successfully!\n";
60113
}
61114

62115
/**
63-
* Main function
116+
* @brief Main function
117+
* @returns 0 on exit
64118
*/
65119
int main() {
66120
tests();
67-
68-
uint32_t N = 100;
69-
std::vector<bool> is_prime = sieve(N);
70-
print(N, is_prime);
71121
return 0;
72122
}

0 commit comments

Comments
 (0)