From d0527489be1ea3a20d666e4b09a6a69b9c74a895 Mon Sep 17 00:00:00 2001 From: Sanskruti Pravin Yeole <129084275+yeolesanskruti@users.noreply.github.com> Date: Mon, 7 Oct 2024 21:37:06 +0530 Subject: [PATCH 01/19] Create Unbounded_knapsack.cpp --- dynamic_programming/Unbounded_knapsack.cpp | 54 ++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 dynamic_programming/Unbounded_knapsack.cpp diff --git a/dynamic_programming/Unbounded_knapsack.cpp b/dynamic_programming/Unbounded_knapsack.cpp new file mode 100644 index 00000000000..1a65d5a986e --- /dev/null +++ b/dynamic_programming/Unbounded_knapsack.cpp @@ -0,0 +1,54 @@ +/* +The Unbounded Knapsack problem allows you to take 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, you can pick any item as many times as needed, as long as the total weight stays within the knapsack's capacity. +The problem is commonly solved using dynamic programming. + +TASK - Given a set of N items, each with a weight and a value, represented by the array wt and val respectively. Also, a knapsack with weight limit W. +The task is to fill the knapsack in such a way that we can get the maximum profit. Return the maximum profit. +Note: Each item can be taken any number of times. + +Test Cases - +Input: +N = 4, W = 8, val[] = {6, 1, 7, 7}, wt[] = {1, 3, 4, 5} +Output: +48 +*/ + +// Code +#include +using namespace std; + +int KnapSackFilling(int i,int W, vector val,vector wt, vector> &dp){ + if(i==0){ + if(wt[0]<=W){ + return (W/wt[0])*val[0]; + }else{ + return 0; + } + } + if(dp[i][W]!=-1)return dp[i][W]; + int nottake=KnapSackFilling(i-1,W,val,wt,dp); + int take=0; + if(W>=wt[i]){ + take=val[i]+KnapSackFilling(i,W-wt[i],val,wt,dp); + } + return dp[i][W]=max(take,nottake); +} +int unboundedKnapsack(int N, int W, vector val, vector wt) +{ + vector> dp(N,vector(W+1,-1)); + return KnapSackFilling(N-1,W,val,wt,dp); +} +int main() { + int N; + N=4; + vector wt= {1, 3, 4, 5}; + vector val= {6, 1, 7, 7}; + int W = 8; + + cout << "The Maximum value of items is " << unboundedKnapsack(N, W, val, wt) << endl; + + return 0; +} + From 8b9f0c5895f628bc67912f477de9c134b1a238db Mon Sep 17 00:00:00 2001 From: Sanskruti Pravin Yeole <129084275+yeolesanskruti@users.noreply.github.com> Date: Tue, 8 Oct 2024 19:17:17 +0530 Subject: [PATCH 02/19] Update Unbounded_knapsack.cpp Documentation done. --- dynamic_programming/Unbounded_knapsack.cpp | 119 +++++++++++++-------- 1 file changed, 74 insertions(+), 45 deletions(-) diff --git a/dynamic_programming/Unbounded_knapsack.cpp b/dynamic_programming/Unbounded_knapsack.cpp index 1a65d5a986e..4e6cd9e82dc 100644 --- a/dynamic_programming/Unbounded_knapsack.cpp +++ b/dynamic_programming/Unbounded_knapsack.cpp @@ -1,54 +1,83 @@ -/* -The Unbounded Knapsack problem allows you to take 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, you can pick any item as many times as needed, as long as the total weight stays within the knapsack's capacity. -The problem is commonly solved using dynamic programming. +/** + * @file + * @brief Unbounded Knapsack problem using Dynamic Programming + * @details + * The Unbounded Knapsack problem allows you to take 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, you can pick any item as many times as needed, as long as the total weight stays within the knapsack's capacity. + * + * @task Given a set of N items, each with a weight and a value, represented by the array `wt` and `val` respectively, and a knapsack + * with a weight limit W, the task is to fill the knapsack in such a way that the maximum profit is obtained. Return the maximum profit. + * + * Note: Each item can be taken any number of times. + * + * @author [Sanskruti Yeole](https://github.com/yeolesanskruti) + * @see dynamic_programming/Unbounded_knapsack.cpp + */ -TASK - Given a set of N items, each with a weight and a value, represented by the array wt and val respectively. Also, a knapsack with weight limit W. -The task is to fill the knapsack in such a way that we can get the maximum profit. Return the maximum profit. -Note: Each item can be taken any number of times. +#include +#include +#include +#include // for std::uint16_t -Test Cases - -Input: -N = 4, W = 8, val[] = {6, 1, 7, 7}, wt[] = {1, 3, 4, 5} -Output: -48 -*/ - -// Code -#include -using namespace std; - -int KnapSackFilling(int i,int W, vector val,vector wt, vector> &dp){ - if(i==0){ - if(wt[0]<=W){ - return (W/wt[0])*val[0]; - }else{ +/** + * @brief Recursive function to calculate the maximum value that can be obtained + * using an unbounded knapsack approach. + * + * @param i Current index in the value and weight vectors. + * @param W Remaining capacity of the knapsack. + * @param val Vector of values corresponding to the items. + * @param wt Vector of weights corresponding to the items. + * @param dp 2D vector for memoization to avoid redundant calculations. + * @return The maximum value that can be obtained for the given index and capacity. + */ +int KnapSackFilling(std::uint16_t i, std::uint16_t W, const std::vector& val, const std::vector& wt, std::vector>& dp) { + if (i == 0) { + if (wt[0] <= W) { + return (W / wt[0]) * val[0]; + } else { return 0; } - } - if(dp[i][W]!=-1)return dp[i][W]; - int nottake=KnapSackFilling(i-1,W,val,wt,dp); - int take=0; - if(W>=wt[i]){ - take=val[i]+KnapSackFilling(i,W-wt[i],val,wt,dp); - } - return dp[i][W]=max(take,nottake); + } + if (dp[i][W] != -1) return dp[i][W]; + + int nottake = KnapSackFilling(i - 1, W, val, wt, dp); + int take = 0; + if (W >= wt[i]) { + take = val[i] + KnapSackFilling(i, W - wt[i], val, wt, dp); + } + return dp[i][W] = std::max(take, nottake); } -int unboundedKnapsack(int N, int W, vector val, vector wt) -{ - vector> dp(N,vector(W+1,-1)); - return KnapSackFilling(N-1,W,val,wt,dp); + +/** + * @brief Wrapper function to initiate the unbounded knapsack calculation. + * + * @param N Number of items. + * @param W Maximum weight capacity of the knapsack. + * @param val Vector of values corresponding to the items. + * @param wt Vector of weights corresponding to the items. + * @return The maximum value that can be obtained for the given capacity. + */ +int unboundedKnapsack(std::uint16_t N, std::uint16_t W, const std::vector& val, const std::vector& wt) { + std::vector> dp(N, std::vector(W + 1, -1)); + return KnapSackFilling(N - 1, W, val, wt, dp); } -int main() { - int N; - N=4; - vector wt= {1, 3, 4, 5}; - vector val= {6, 1, 7, 7}; - int W = 8; - - cout << "The Maximum value of items is " << unboundedKnapsack(N, W, val, wt) << endl; - return 0; +/** + * @brief Function to run test cases for the unbounded knapsack implementation. + */ +void test_cases() { + std::uint16_t N = 4; // Number of items + std::vector wt = {1, 3, 4, 5}; // Weights of the items + std::vector val = {6, 1, 7, 7}; // Values of the items + std::uint16_t W = 8; // Maximum capacity of the knapsack + + // Test the function and assert the expected output + assert(unboundedKnapsack(N, W, val, wt) == 48); + std::cout << "All test cases passed!" << std::endl; } +int main() { + test_cases(); + return 0; +} From a68f0e52502d385c60641f2bc393e7242ca31d9d Mon Sep 17 00:00:00 2001 From: Sanskruti Pravin Yeole <129084275+yeolesanskruti@users.noreply.github.com> Date: Tue, 8 Oct 2024 21:25:49 +0530 Subject: [PATCH 03/19] Update dynamic_programming/Unbounded_knapsack.cpp Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com> --- dynamic_programming/Unbounded_knapsack.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dynamic_programming/Unbounded_knapsack.cpp b/dynamic_programming/Unbounded_knapsack.cpp index 4e6cd9e82dc..0740c6619ba 100644 --- a/dynamic_programming/Unbounded_knapsack.cpp +++ b/dynamic_programming/Unbounded_knapsack.cpp @@ -66,7 +66,7 @@ int unboundedKnapsack(std::uint16_t N, std::uint16_t W, const std::vector wt = {1, 3, 4, 5}; // Weights of the items std::vector val = {6, 1, 7, 7}; // Values of the items From f641dd31116f12e07a7a676b8bcf945334e09606 Mon Sep 17 00:00:00 2001 From: Sanskruti Pravin Yeole <129084275+yeolesanskruti@users.noreply.github.com> Date: Tue, 8 Oct 2024 21:26:15 +0530 Subject: [PATCH 04/19] Update dynamic_programming/Unbounded_knapsack.cpp Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com> --- dynamic_programming/Unbounded_knapsack.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dynamic_programming/Unbounded_knapsack.cpp b/dynamic_programming/Unbounded_knapsack.cpp index 0740c6619ba..7f25a6cf9c8 100644 --- a/dynamic_programming/Unbounded_knapsack.cpp +++ b/dynamic_programming/Unbounded_knapsack.cpp @@ -9,7 +9,7 @@ * @task Given a set of N items, each with a weight and a value, represented by the array `wt` and `val` respectively, and a knapsack * with a weight limit W, the task is to fill the knapsack in such a way that the maximum profit is obtained. Return the maximum profit. * - * Note: Each item can be taken any number of times. + * @note Each item can be taken any number of times. * * @author [Sanskruti Yeole](https://github.com/yeolesanskruti) * @see dynamic_programming/Unbounded_knapsack.cpp From ff9f842a3171c950e418460f3e13deb910a081a4 Mon Sep 17 00:00:00 2001 From: Sanskruti Pravin Yeole <129084275+yeolesanskruti@users.noreply.github.com> Date: Tue, 8 Oct 2024 21:26:26 +0530 Subject: [PATCH 05/19] Update dynamic_programming/Unbounded_knapsack.cpp Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com> --- dynamic_programming/Unbounded_knapsack.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dynamic_programming/Unbounded_knapsack.cpp b/dynamic_programming/Unbounded_knapsack.cpp index 7f25a6cf9c8..fd9f58622b5 100644 --- a/dynamic_programming/Unbounded_knapsack.cpp +++ b/dynamic_programming/Unbounded_knapsack.cpp @@ -78,6 +78,6 @@ void tests() { } int main() { - test_cases(); + tests(); // run self test implementation return 0; } From 43bc2ce99c65d9d539b799a143f81113e0d88564 Mon Sep 17 00:00:00 2001 From: Sanskruti Pravin Yeole <129084275+yeolesanskruti@users.noreply.github.com> Date: Tue, 8 Oct 2024 21:26:43 +0530 Subject: [PATCH 06/19] Update dynamic_programming/Unbounded_knapsack.cpp Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com> --- dynamic_programming/Unbounded_knapsack.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/dynamic_programming/Unbounded_knapsack.cpp b/dynamic_programming/Unbounded_knapsack.cpp index fd9f58622b5..7bc6aa84275 100644 --- a/dynamic_programming/Unbounded_knapsack.cpp +++ b/dynamic_programming/Unbounded_knapsack.cpp @@ -77,6 +77,10 @@ void tests() { std::cout << "All test cases passed!" << std::endl; } +/** + * @brief main function + * @return 0 on successful exit + */ int main() { tests(); // run self test implementation return 0; From 5cc7245fec5d55746a61c333a96b4593c1b9d37f Mon Sep 17 00:00:00 2001 From: Sanskruti Pravin Yeole <129084275+yeolesanskruti@users.noreply.github.com> Date: Tue, 8 Oct 2024 21:59:32 +0530 Subject: [PATCH 07/19] Delete dynamic_programming/Unbounded_knapsack.cpp --- dynamic_programming/Unbounded_knapsack.cpp | 87 ---------------------- 1 file changed, 87 deletions(-) delete mode 100644 dynamic_programming/Unbounded_knapsack.cpp diff --git a/dynamic_programming/Unbounded_knapsack.cpp b/dynamic_programming/Unbounded_knapsack.cpp deleted file mode 100644 index 7bc6aa84275..00000000000 --- a/dynamic_programming/Unbounded_knapsack.cpp +++ /dev/null @@ -1,87 +0,0 @@ -/** - * @file - * @brief Unbounded Knapsack problem using Dynamic Programming - * @details - * The Unbounded Knapsack problem allows you to take 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, you can pick any item as many times as needed, as long as the total weight stays within the knapsack's capacity. - * - * @task Given a set of N items, each with a weight and a value, represented by the array `wt` and `val` respectively, and a knapsack - * with a weight limit W, the task is to fill the knapsack in such a way that the maximum profit is obtained. Return the maximum profit. - * - * @note Each item can be taken any number of times. - * - * @author [Sanskruti Yeole](https://github.com/yeolesanskruti) - * @see dynamic_programming/Unbounded_knapsack.cpp - */ - -#include -#include -#include -#include // for std::uint16_t - -/** - * @brief Recursive function to calculate the maximum value that can be obtained - * using an unbounded knapsack approach. - * - * @param i Current index in the value and weight vectors. - * @param W Remaining capacity of the knapsack. - * @param val Vector of values corresponding to the items. - * @param wt Vector of weights corresponding to the items. - * @param dp 2D vector for memoization to avoid redundant calculations. - * @return The maximum value that can be obtained for the given index and capacity. - */ -int KnapSackFilling(std::uint16_t i, std::uint16_t W, const std::vector& val, const std::vector& wt, std::vector>& dp) { - if (i == 0) { - if (wt[0] <= W) { - return (W / wt[0]) * val[0]; - } else { - return 0; - } - } - if (dp[i][W] != -1) return dp[i][W]; - - int nottake = KnapSackFilling(i - 1, W, val, wt, dp); - int take = 0; - if (W >= wt[i]) { - take = val[i] + KnapSackFilling(i, W - wt[i], val, wt, dp); - } - return dp[i][W] = std::max(take, nottake); -} - -/** - * @brief Wrapper function to initiate the unbounded knapsack calculation. - * - * @param N Number of items. - * @param W Maximum weight capacity of the knapsack. - * @param val Vector of values corresponding to the items. - * @param wt Vector of weights corresponding to the items. - * @return The maximum value that can be obtained for the given capacity. - */ -int unboundedKnapsack(std::uint16_t N, std::uint16_t W, const std::vector& val, const std::vector& wt) { - std::vector> dp(N, std::vector(W + 1, -1)); - return KnapSackFilling(N - 1, W, val, wt, dp); -} - -/** - * @brief Function to run test cases for the unbounded knapsack implementation. - */ -void tests() { - std::uint16_t N = 4; // Number of items - std::vector wt = {1, 3, 4, 5}; // Weights of the items - std::vector val = {6, 1, 7, 7}; // Values of the items - std::uint16_t W = 8; // Maximum capacity of the knapsack - - // Test the function and assert the expected output - assert(unboundedKnapsack(N, W, val, wt) == 48); - std::cout << "All test cases passed!" << std::endl; -} - -/** - * @brief main function - * @return 0 on successful exit - */ -int main() { - tests(); // run self test implementation - return 0; -} From 6a8ca41747a51ccf0b44a252f6e58fc12d41646c Mon Sep 17 00:00:00 2001 From: Sanskruti Pravin Yeole <129084275+yeolesanskruti@users.noreply.github.com> Date: Tue, 8 Oct 2024 22:11:43 +0530 Subject: [PATCH 08/19] Create Unbounded_0_1_Knapsack.cpp --- dynamic_programming/Unbounded_0_1_Knapsack.cpp | 1 + 1 file changed, 1 insertion(+) create mode 100644 dynamic_programming/Unbounded_0_1_Knapsack.cpp diff --git a/dynamic_programming/Unbounded_0_1_Knapsack.cpp b/dynamic_programming/Unbounded_0_1_Knapsack.cpp new file mode 100644 index 00000000000..8b137891791 --- /dev/null +++ b/dynamic_programming/Unbounded_0_1_Knapsack.cpp @@ -0,0 +1 @@ + From e7861298fc85c736f454163f312464a4c8735058 Mon Sep 17 00:00:00 2001 From: Sanskruti Pravin Yeole <129084275+yeolesanskruti@users.noreply.github.com> Date: Tue, 8 Oct 2024 23:02:00 +0530 Subject: [PATCH 09/19] Update Unbounded_0_1_Knapsack.cpp --- .../Unbounded_0_1_Knapsack.cpp | 135 ++++++++++++++++++ 1 file changed, 135 insertions(+) diff --git a/dynamic_programming/Unbounded_0_1_Knapsack.cpp b/dynamic_programming/Unbounded_0_1_Knapsack.cpp index 8b137891791..a827313fb22 100644 --- a/dynamic_programming/Unbounded_0_1_Knapsack.cpp +++ b/dynamic_programming/Unbounded_0_1_Knapsack.cpp @@ -1 +1,136 @@ +/** + * @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. + * + * ### Algorithm + * 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 ///< Standard input-output stream +#include ///< Standard library for using dynamic arrays (vectors) +#include ///< For using assert function to validate test cases +#include ///< For fixed-width integer types like std::uint16_t + +/** + * @namespace dynamic_programming + * @brief Namespace for dynamic programming algorithms + */ +namespace dynamic_programming { + +/** + * @namespace Knapsack + * @brief Implementation of unbounded 0-1 knapsack problem + */ +namespace unbounded_knapsack { + +/** + * @brief Recursive function to calculate the maximum value obtainable using + * an unbounded knapsack approach. + * + * @param i Current index in the value and weight vectors. + * @param W Remaining capacity of the knapsack. + * @param val Vector of values corresponding to the items. + * @param wt Vector of weights corresponding to the items. + * @param dp 2D vector for memoization to avoid redundant calculations. + * @return The maximum value that can be obtained for the given index and capacity. + */ +int KnapSackFilling(std::uint16_t i, std::uint16_t W, + const std::vector& val, + const std::vector& wt, + std::vector>& dp) { + if (i == 0) { + if (wt[0] <= W) { + return (W / wt[0]) * val[0]; // Take as many of the first item as possible + } else { + return 0; // Can't take the first item + } + } + 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 take = 0; + if (W >= wt[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 +} + +/** + * @brief Wrapper function to initiate the unbounded knapsack calculation. + * + * @param N Number of items. + * @param W Maximum weight capacity of the knapsack. + * @param val Vector of values corresponding to the items. + * @param wt Vector of weights corresponding to the items. + * @return The maximum value that can be obtained for the given capacity. + */ +int unboundedKnapsack(std::uint16_t N, std::uint16_t W, + const std::vector& val, + const std::vector& wt) { + std::vector> dp(N, std::vector(W + 1, -1)); // Initialize memoization table + return KnapSackFilling(N - 1, W, val, wt, dp); // Start the calculation +} + +/** + * @brief Function to run test cases for the unbounded knapsack implementation. + * + * @details This function tests the implementation with predefined test cases. + * It asserts the expected outcomes to ensure correctness of the algorithm. + */ +void tests() { + // Test Case 1 + std::uint16_t N1 = 4; // Number of items + std::vector wt1 = {1, 3, 4, 5}; // Weights of the items + std::vector 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; + + // Test Case 2 + std::uint16_t N2 = 3; // Number of items + std::vector wt2 = {10, 20, 30}; // Weights of the items + std::vector 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; + + // Test Case 3 + std::uint16_t N3 = 3; // Number of items + std::vector wt3 = {2, 4, 6}; // Weights of the items + std::vector 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; + + std::cout << "All test cases passed!" << std::endl; + +} + +int main() { + tests(); // Run test cases to validate implementation + return 0; +} + +} +} From d9c63edfe1c268df127766c9a60a11f3a2947f22 Mon Sep 17 00:00:00 2001 From: Sanskruti Pravin Yeole <129084275+yeolesanskruti@users.noreply.github.com> Date: Tue, 8 Oct 2024 23:04:24 +0530 Subject: [PATCH 10/19] Update Unbounded_0_1_Knapsack.cpp --- dynamic_programming/Unbounded_0_1_Knapsack.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dynamic_programming/Unbounded_0_1_Knapsack.cpp b/dynamic_programming/Unbounded_0_1_Knapsack.cpp index a827313fb22..39111f73d10 100644 --- a/dynamic_programming/Unbounded_0_1_Knapsack.cpp +++ b/dynamic_programming/Unbounded_0_1_Knapsack.cpp @@ -95,7 +95,7 @@ int unboundedKnapsack(std::uint16_t N, std::uint16_t W, * @details This function tests the implementation with predefined test cases. * It asserts the expected outcomes to ensure correctness of the algorithm. */ -void tests() { +static void tests() { // Test Case 1 std::uint16_t N1 = 4; // Number of items std::vector wt1 = {1, 3, 4, 5}; // Weights of the items From 541ea543319f10cf540a58e5e9844c50945b39c3 Mon Sep 17 00:00:00 2001 From: realstealthninja <68815218+realstealthninja@users.noreply.github.com> Date: Wed, 9 Oct 2024 06:07:04 +0530 Subject: [PATCH 11/19] docs: add docs for main --- dynamic_programming/Unbounded_0_1_Knapsack.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/dynamic_programming/Unbounded_0_1_Knapsack.cpp b/dynamic_programming/Unbounded_0_1_Knapsack.cpp index 39111f73d10..4d9f7105780 100644 --- a/dynamic_programming/Unbounded_0_1_Knapsack.cpp +++ b/dynamic_programming/Unbounded_0_1_Knapsack.cpp @@ -127,6 +127,10 @@ static void tests() { } +/** + * @brief main function + * @return 0 on successful exit + */ int main() { tests(); // Run test cases to validate implementation return 0; From 4d4c49c10ab068d146fb08f9cdc22306896d5029 Mon Sep 17 00:00:00 2001 From: Sanskruti Pravin Yeole <129084275+yeolesanskruti@users.noreply.github.com> Date: Wed, 9 Oct 2024 21:25:08 +0530 Subject: [PATCH 12/19] Update dynamic_programming/Unbounded_0_1_Knapsack.cpp Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com> --- dynamic_programming/Unbounded_0_1_Knapsack.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dynamic_programming/Unbounded_0_1_Knapsack.cpp b/dynamic_programming/Unbounded_0_1_Knapsack.cpp index 4d9f7105780..349efe8fd8c 100644 --- a/dynamic_programming/Unbounded_0_1_Knapsack.cpp +++ b/dynamic_programming/Unbounded_0_1_Knapsack.cpp @@ -132,7 +132,7 @@ static void tests() { * @return 0 on successful exit */ int main() { - tests(); // Run test cases to validate implementation + tests(); // Run self test implementation return 0; } From 17f398fa56598b9c2d19efdad1fc4cf9406ef682 Mon Sep 17 00:00:00 2001 From: Sanskruti Pravin Yeole <129084275+yeolesanskruti@users.noreply.github.com> Date: Wed, 9 Oct 2024 21:25:27 +0530 Subject: [PATCH 13/19] Update dynamic_programming/Unbounded_0_1_Knapsack.cpp Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com> --- dynamic_programming/Unbounded_0_1_Knapsack.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/dynamic_programming/Unbounded_0_1_Knapsack.cpp b/dynamic_programming/Unbounded_0_1_Knapsack.cpp index 349efe8fd8c..e6e466641e7 100644 --- a/dynamic_programming/Unbounded_0_1_Knapsack.cpp +++ b/dynamic_programming/Unbounded_0_1_Knapsack.cpp @@ -24,10 +24,10 @@ * @see dynamic_programming/0_1_knapsack.cpp */ -#include ///< Standard input-output stream -#include ///< Standard library for using dynamic arrays (vectors) -#include ///< For using assert function to validate test cases -#include ///< For fixed-width integer types like std::uint16_t +#include // Standard input-output stream +#include // Standard library for using dynamic arrays (vectors) +#include // For using assert function to validate test cases +#include // For fixed-width integer types like std::uint16_t /** * @namespace dynamic_programming From 122b4538087a19efa3b330a9f043660111debeb4 Mon Sep 17 00:00:00 2001 From: Sanskruti Pravin Yeole <129084275+yeolesanskruti@users.noreply.github.com> Date: Thu, 10 Oct 2024 21:23:36 +0530 Subject: [PATCH 14/19] Update Unbounded_0_1_Knapsack.cpp --- dynamic_programming/Unbounded_0_1_Knapsack.cpp | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/dynamic_programming/Unbounded_0_1_Knapsack.cpp b/dynamic_programming/Unbounded_0_1_Knapsack.cpp index e6e466641e7..a0234c277ad 100644 --- a/dynamic_programming/Unbounded_0_1_Knapsack.cpp +++ b/dynamic_programming/Unbounded_0_1_Knapsack.cpp @@ -13,7 +13,7 @@ * `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. + * @note weight and value of items is greater than zero * * ### Algorithm * The approach uses dynamic programming to build a solution iteratively. @@ -85,6 +85,7 @@ int KnapSackFilling(std::uint16_t i, std::uint16_t W, int unboundedKnapsack(std::uint16_t N, std::uint16_t W, const std::vector& val, const std::vector& wt) { + if(N==0)return 0; // Expect 0 since no items std::vector> dp(N, std::vector(W + 1, -1)); // Initialize memoization table return KnapSackFilling(N - 1, W, val, wt, dp); // Start the calculation } @@ -122,6 +123,14 @@ static void tests() { // 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; + + // Test Case 4 + std::uint16_t N4 = 0; // Number of items + std::vector wt4 = {}; // Weights of the items + std::vector 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; From e99d523ebd85b8a8781b018c1039c9d23736aa0f Mon Sep 17 00:00:00 2001 From: Sanskruti Pravin Yeole <129084275+yeolesanskruti@users.noreply.github.com> Date: Fri, 11 Oct 2024 22:44:10 +0530 Subject: [PATCH 15/19] Update Unbounded_0_1_Knapsack.cpp --- dynamic_programming/Unbounded_0_1_Knapsack.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/dynamic_programming/Unbounded_0_1_Knapsack.cpp b/dynamic_programming/Unbounded_0_1_Knapsack.cpp index a0234c277ad..4bd706c9c6a 100644 --- a/dynamic_programming/Unbounded_0_1_Knapsack.cpp +++ b/dynamic_programming/Unbounded_0_1_Knapsack.cpp @@ -90,6 +90,10 @@ int unboundedKnapsack(std::uint16_t N, std::uint16_t W, return KnapSackFilling(N - 1, W, val, wt, dp); // Start the calculation } +} // unbounded_knapsack + +} // dynamic_programming + /** * @brief Function to run test cases for the unbounded knapsack implementation. * @@ -145,5 +149,3 @@ int main() { return 0; } -} -} From 0cf4734e60c85406b76d4114c5decba9378a7c04 Mon Sep 17 00:00:00 2001 From: Sanskruti Pravin Yeole <129084275+yeolesanskruti@users.noreply.github.com> Date: Sun, 13 Oct 2024 12:03:01 +0530 Subject: [PATCH 16/19] Update Unbounded_0_1_Knapsack.cpp --- dynamic_programming/Unbounded_0_1_Knapsack.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/dynamic_programming/Unbounded_0_1_Knapsack.cpp b/dynamic_programming/Unbounded_0_1_Knapsack.cpp index 4bd706c9c6a..00472873a2f 100644 --- a/dynamic_programming/Unbounded_0_1_Knapsack.cpp +++ b/dynamic_programming/Unbounded_0_1_Knapsack.cpp @@ -48,7 +48,9 @@ namespace unbounded_knapsack { * @param i Current index in the value and weight vectors. * @param W Remaining capacity of the knapsack. * @param val Vector of values corresponding to the items. + * @note "val" data type can be changed according to the size of the input. * @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. */ From 6faf04d1f4aa1d034d816b8a04b13fb8617cf196 Mon Sep 17 00:00:00 2001 From: Sanskruti Pravin Yeole <129084275+yeolesanskruti@users.noreply.github.com> Date: Mon, 14 Oct 2024 19:15:52 +0530 Subject: [PATCH 17/19] Update dynamic_programming/Unbounded_0_1_Knapsack.cpp Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com> --- dynamic_programming/Unbounded_0_1_Knapsack.cpp | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/dynamic_programming/Unbounded_0_1_Knapsack.cpp b/dynamic_programming/Unbounded_0_1_Knapsack.cpp index 00472873a2f..4033d779535 100644 --- a/dynamic_programming/Unbounded_0_1_Knapsack.cpp +++ b/dynamic_programming/Unbounded_0_1_Knapsack.cpp @@ -97,10 +97,8 @@ int unboundedKnapsack(std::uint16_t N, std::uint16_t W, } // dynamic_programming /** - * @brief Function to run test cases for the unbounded knapsack implementation. - * - * @details This function tests the implementation with predefined test cases. - * It asserts the expected outcomes to ensure correctness of the algorithm. + * @brief self test implementation + * @return void */ static void tests() { // Test Case 1 From 783b156d94b46d54b2cca99d41e5d64af030a90a Mon Sep 17 00:00:00 2001 From: Sanskruti Pravin Yeole <129084275+yeolesanskruti@users.noreply.github.com> Date: Wed, 16 Oct 2024 01:11:17 +0530 Subject: [PATCH 18/19] Update Unbounded_0_1_Knapsack.cpp --- dynamic_programming/Unbounded_0_1_Knapsack.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dynamic_programming/Unbounded_0_1_Knapsack.cpp b/dynamic_programming/Unbounded_0_1_Knapsack.cpp index 4033d779535..558f9033528 100644 --- a/dynamic_programming/Unbounded_0_1_Knapsack.cpp +++ b/dynamic_programming/Unbounded_0_1_Knapsack.cpp @@ -54,7 +54,7 @@ namespace unbounded_knapsack { * @param dp 2D vector for memoization to avoid redundant calculations. * @return The maximum value that can be obtained for the given index and capacity. */ -int KnapSackFilling(std::uint16_t i, std::uint16_t W, +uint16_t KnapSackFilling(std::uint16_t i, std::uint16_t W, const std::vector& val, const std::vector& wt, std::vector>& dp) { @@ -84,7 +84,7 @@ int 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. */ -int unboundedKnapsack(std::uint16_t N, std::uint16_t W, +uint16_t unboundedKnapsack(std::uint16_t N, std::uint16_t W, const std::vector& val, const std::vector& wt) { if(N==0)return 0; // Expect 0 since no items From 20195f125a96127d9047576266efbd9c8969afac Mon Sep 17 00:00:00 2001 From: Sanskruti Pravin Yeole <129084275+yeolesanskruti@users.noreply.github.com> Date: Wed, 16 Oct 2024 01:18:26 +0530 Subject: [PATCH 19/19] Update Unbounded_0_1_Knapsack.cpp --- dynamic_programming/Unbounded_0_1_Knapsack.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dynamic_programming/Unbounded_0_1_Knapsack.cpp b/dynamic_programming/Unbounded_0_1_Knapsack.cpp index 558f9033528..96588fe3936 100644 --- a/dynamic_programming/Unbounded_0_1_Knapsack.cpp +++ b/dynamic_programming/Unbounded_0_1_Knapsack.cpp @@ -54,7 +54,7 @@ namespace unbounded_knapsack { * @param dp 2D vector for memoization to avoid redundant calculations. * @return The maximum value that can be obtained for the given index and capacity. */ -uint16_t KnapSackFilling(std::uint16_t i, std::uint16_t W, +std::uint16_t KnapSackFilling(std::uint16_t i, std::uint16_t W, const std::vector& val, const std::vector& wt, std::vector>& dp) { @@ -84,7 +84,7 @@ 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. */ -uint16_t unboundedKnapsack(std::uint16_t N, std::uint16_t W, +std::uint16_t unboundedKnapsack(std::uint16_t N, std::uint16_t W, const std::vector& val, const std::vector& wt) { if(N==0)return 0; // Expect 0 since no items