feat: update to CXX standard 17 and add CMakeLists file to directories without them (#2746)

* chore: add cache and build comment to git ignore

* fix: add cmakelists to dynamic programming

* fix: add cmakelists to greedy_algorithms

* fix: add cmakelists to operations_on_datastructures

* fix: add cmakelists to range_queries

* fix: add `dynamic_programmin`, `greedy_algorithms`, `range_queries` and `operations_on_datastructures` subdirectories to cmakelists.txt

* fix: init of transform_reduce in dynamic_programming

* fix: add an include for functional in catalan_numbers

* chore: bump CXX standard to 20

* revert: bump CXX standard to 20

* chore: bump c++ version to 17 and add justification

Arm supports c++ 17
Esp32 supports c++ 23
decision was made to be 17 because it seemed to offer the best combatability

* fix: compilation error in catalan numbers

* fix: add <set> header to longest increasing subsequence nlogn

* fix: add cmath & algorithm header to mo.cpp

* fix: remove register key word from fast integer

* fix: replace using namespace std with std::cin and std::cout

* docs: typo in c++17

* fix: memory leak in bellman_ford

* fix: typo in bellman_ford

* fix: typo in word_break

* fix: dynamic array in coin_change

* fix dynamic array in egg_dropping puzzle

* chore: remove unnecessary comment

* fix: add vla to be an error

* chore: add extra warnings

* fix: use add_compile options instead of set()

* fix: compile options are not strings

* fix: vla in floyd_warshall

* fix: vla in egg_dropping_puzzel

* fix: vla in coin_change

* fix: vla in edit_distance

* fix: vla in floyd_warshall

* feat: remove kadane and replace it with kadane2

* fix: vla in longest_common_subsequence

* fix: int overflow in floyd_warshall

* fix: vla in lisnlogn

* fix: use const vector& instead of array

* fix: use dynamic array instead of vla in knapsack

* fix: use of and in msvc is unsupported by default adding permissive flag fixes it

* test: make executables the tests themselves

* Revert "test: make executables the tests themselves"

This reverts commit 7a16c31c4e.

* fix: make dist constant in print

* fix: namespace issue in unbounded_0_1

* fix: include cstdint to fix compilation
This commit is contained in:
realstealthninja
2024-11-04 18:00:20 +05:30
committed by GitHub
parent c6af943508
commit 0d766b0f8a
35 changed files with 377 additions and 268 deletions

View File

@@ -1,33 +1,33 @@
/**
* @file
* @brief Implementation of the Unbounded 0/1 Knapsack Problem
*
* @details
* The Unbounded 0/1 Knapsack problem allows taking unlimited quantities of each item.
* The goal is to maximize the total value without exceeding the given knapsack capacity.
* Unlike the 0/1 knapsack, where each item can be taken only once, in this variation,
* any item can be picked any number of times as long as the total weight stays within
* the knapsack's capacity.
*
* Given a set of N items, each with a weight and a value, represented by the arrays
* `wt` and `val` respectively, and a knapsack with a weight limit W, the task is to
* fill the knapsack to maximize the total value.
*
* @note weight and value of items is greater than zero
* @details
* The Unbounded 0/1 Knapsack problem allows taking unlimited quantities of each
* item. The goal is to maximize the total value without exceeding the given
* knapsack capacity. Unlike the 0/1 knapsack, where each item can be taken only
* once, in this variation, any item can be picked any number of times as long
* as the total weight stays within the knapsack's capacity.
*
* Given a set of N items, each with a weight and a value, represented by the
* arrays `wt` and `val` respectively, and a knapsack with a weight limit W, the
* task is to fill the knapsack to maximize the total value.
*
* @note weight and value of items is greater than zero
*
* ### Algorithm
* The approach uses dynamic programming to build a solution iteratively.
* A 2D array is used for memoization to store intermediate results, allowing
* The approach uses dynamic programming to build a solution iteratively.
* A 2D array is used for memoization to store intermediate results, allowing
* the function to avoid redundant calculations.
*
*
* @author [Sanskruti Yeole](https://github.com/yeolesanskruti)
* @see dynamic_programming/0_1_knapsack.cpp
*/
#include <cassert> // For using assert function to validate test cases
#include <cstdint> // For fixed-width integer types like std::uint16_t
#include <iostream> // Standard input-output stream
#include <vector> // Standard library for using dynamic arrays (vectors)
#include <cassert> // For using assert function to validate test cases
#include <cstdint> // For fixed-width integer types like std::uint16_t
#include <vector> // Standard library for using dynamic arrays (vectors)
/**
* @namespace dynamic_programming
@@ -42,7 +42,7 @@ namespace dynamic_programming {
namespace unbounded_knapsack {
/**
* @brief Recursive function to calculate the maximum value obtainable using
* @brief Recursive function to calculate the maximum value obtainable using
* an unbounded knapsack approach.
*
* @param i Current index in the value and weight vectors.
@@ -52,27 +52,33 @@ namespace unbounded_knapsack {
* @param wt Vector of weights corresponding to the items.
* @note "wt" data type can be changed according to the size of the input.
* @param dp 2D vector for memoization to avoid redundant calculations.
* @return The maximum value that can be obtained for the given index and capacity.
* @return The maximum value that can be obtained for the given index and
* capacity.
*/
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) {
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) {
if (i == 0) {
if (wt[0] <= W) {
return (W / wt[0]) * val[0]; // Take as many of the first item as possible
return (W / wt[0]) *
val[0]; // Take as many of the first item as possible
} else {
return 0; // Can't take the first item
return 0; // Can't take the first item
}
}
if (dp[i][W] != -1) return dp[i][W]; // Return result if available
if (dp[i][W] != -1)
return dp[i][W]; // Return result if available
int nottake = KnapSackFilling(i - 1, W, val, wt, dp); // Value without taking item i
int nottake =
KnapSackFilling(i - 1, W, val, wt, dp); // Value without taking item i
int take = 0;
if (W >= wt[i]) {
take = val[i] + KnapSackFilling(i, W - wt[i], val, wt, dp); // Value taking item i
take = val[i] + KnapSackFilling(i, W - wt[i], val, wt,
dp); // Value taking item i
}
return dp[i][W] = std::max(take, nottake); // Store and return the maximum value
return dp[i][W] =
std::max(take, nottake); // Store and return the maximum value
}
/**
@@ -84,17 +90,19 @@ std::uint16_t KnapSackFilling(std::uint16_t i, std::uint16_t W,
* @param wt Vector of weights corresponding to the items.
* @return The maximum value that can be obtained for the given capacity.
*/
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) {
if(N==0)return 0; // Expect 0 since no items
std::vector<std::vector<int>> dp(N, std::vector<int>(W + 1, -1)); // Initialize memoization table
return KnapSackFilling(N - 1, W, val, wt, dp); // Start the calculation
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) {
if (N == 0)
return 0; // Expect 0 since no items
std::vector<std::vector<int>> dp(
N, std::vector<int>(W + 1, -1)); // Initialize memoization table
return KnapSackFilling(N - 1, W, val, wt, dp); // Start the calculation
}
} // unbounded_knapsack
} // namespace unbounded_knapsack
} // dynamic_programming
} // namespace dynamic_programming
/**
* @brief self test implementation
@@ -102,42 +110,57 @@ std::uint16_t unboundedKnapsack(std::uint16_t N, std::uint16_t W,
*/
static void tests() {
// Test Case 1
std::uint16_t N1 = 4; // Number of items
std::vector<std::uint16_t> wt1 = {1, 3, 4, 5}; // Weights of the items
std::vector<std::uint16_t> val1 = {6, 1, 7, 7}; // Values of the items
std::uint16_t W1 = 8; // Maximum capacity of the knapsack
std::uint16_t N1 = 4; // Number of items
std::vector<std::uint16_t> wt1 = {1, 3, 4, 5}; // Weights of the items
std::vector<std::uint16_t> val1 = {6, 1, 7, 7}; // Values of the items
std::uint16_t W1 = 8; // Maximum capacity of the knapsack
// Test the function and assert the expected output
assert(unboundedKnapsack(N1, W1, val1, wt1) == 48);
std::cout << "Maximum Knapsack value " << unboundedKnapsack(N1, W1, val1, wt1) << std::endl;
assert(dynamic_programming::unbounded_knapsack::unboundedKnapsack(
N1, W1, val1, wt1) == 48);
std::cout << "Maximum Knapsack value "
<< dynamic_programming::unbounded_knapsack::unboundedKnapsack(
N1, W1, val1, wt1)
<< std::endl;
// Test Case 2
std::uint16_t N2 = 3; // Number of items
std::vector<std::uint16_t> wt2 = {10, 20, 30}; // Weights of the items
std::vector<std::uint16_t> val2 = {60, 100, 120}; // Values of the items
std::uint16_t W2 = 5; // Maximum capacity of the knapsack
std::uint16_t N2 = 3; // Number of items
std::vector<std::uint16_t> wt2 = {10, 20, 30}; // Weights of the items
std::vector<std::uint16_t> val2 = {60, 100, 120}; // Values of the items
std::uint16_t W2 = 5; // Maximum capacity of the knapsack
// Test the function and assert the expected output
assert(unboundedKnapsack(N2, W2, val2, wt2) == 0);
std::cout << "Maximum Knapsack value " << unboundedKnapsack(N2, W2, val2, wt2) << std::endl;
assert(dynamic_programming::unbounded_knapsack::unboundedKnapsack(
N2, W2, val2, wt2) == 0);
std::cout << "Maximum Knapsack value "
<< dynamic_programming::unbounded_knapsack::unboundedKnapsack(
N2, W2, val2, wt2)
<< std::endl;
// Test Case 3
std::uint16_t N3 = 3; // Number of items
std::vector<std::uint16_t> wt3 = {2, 4, 6}; // Weights of the items
std::vector<std::uint16_t> val3 = {5, 11, 13};// Values of the items
std::uint16_t W3 = 27;// Maximum capacity of the knapsack
std::uint16_t N3 = 3; // Number of items
std::vector<std::uint16_t> wt3 = {2, 4, 6}; // Weights of the items
std::vector<std::uint16_t> val3 = {5, 11, 13}; // Values of the items
std::uint16_t W3 = 27; // Maximum capacity of the knapsack
// Test the function and assert the expected output
assert(unboundedKnapsack(N3, W3, val3, wt3) == 27);
std::cout << "Maximum Knapsack value " << unboundedKnapsack(N3, W3, val3, wt3) << std::endl;
assert(dynamic_programming::unbounded_knapsack::unboundedKnapsack(
N3, W3, val3, wt3) == 27);
std::cout << "Maximum Knapsack value "
<< dynamic_programming::unbounded_knapsack::unboundedKnapsack(
N3, W3, val3, wt3)
<< std::endl;
// Test Case 4
std::uint16_t N4 = 0; // Number of items
std::vector<std::uint16_t> wt4 = {}; // Weights of the items
std::vector<std::uint16_t> val4 = {}; // Values of the items
std::uint16_t W4 = 10; // Maximum capacity of the knapsack
assert(unboundedKnapsack(N4, W4, val4, wt4) == 0);
std::cout << "Maximum Knapsack value for empty arrays: " << unboundedKnapsack(N4, W4, val4, wt4) << std::endl;
std::cout << "All test cases passed!" << std::endl;
std::uint16_t N4 = 0; // Number of items
std::vector<std::uint16_t> wt4 = {}; // Weights of the items
std::vector<std::uint16_t> val4 = {}; // Values of the items
std::uint16_t W4 = 10; // Maximum capacity of the knapsack
assert(dynamic_programming::unbounded_knapsack::unboundedKnapsack(
N4, W4, val4, wt4) == 0);
std::cout << "Maximum Knapsack value for empty arrays: "
<< dynamic_programming::unbounded_knapsack::unboundedKnapsack(
N4, W4, val4, wt4)
<< std::endl;
std::cout << "All test cases passed!" << std::endl;
}
/**
@@ -145,7 +168,6 @@ static void tests() {
* @return 0 on successful exit
*/
int main() {
tests(); // Run self test implementation
tests(); // Run self test implementation
return 0;
}