|
Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
|
Implementation of the Unbounded 0/1 Knapsack Problem. More...
#include <iostream>#include <vector>#include <cassert>#include <cstdint>Namespaces | |
| namespace | dynamic_programming |
| Dynamic Programming algorithms. | |
| namespace | Knapsack |
| Implementation of 0-1 Knapsack problem. | |
Functions | |
| std::uint16_t | dynamic_programming::unbounded_knapsack::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) |
| Recursive function to calculate the maximum value obtainable using an unbounded knapsack approach. | |
| std::uint16_t | dynamic_programming::unbounded_knapsack::unboundedKnapsack (std::uint16_t N, std::uint16_t W, const std::vector< std::uint16_t > &val, const std::vector< std::uint16_t > &wt) |
| Wrapper function to initiate the unbounded knapsack calculation. | |
| static void | tests () |
| self test implementation | |
| int | main () |
| main function | |
Implementation of the Unbounded 0/1 Knapsack Problem.
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.
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.
| std::uint16_t dynamic_programming::unbounded_knapsack::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 ) |
Recursive function to calculate the maximum value obtainable using an unbounded knapsack approach.
| i | Current index in the value and weight vectors. |
| W | Remaining capacity of the knapsack. |
| val | Vector of values corresponding to the items. |
| wt | Vector of weights corresponding to the items. |
| dp | 2D vector for memoization to avoid redundant calculations. |
| int main | ( | void | ) |
|
static |
self test implementation
| std::uint16_t dynamic_programming::unbounded_knapsack::unboundedKnapsack | ( | std::uint16_t | N, |
| std::uint16_t | W, | ||
| const std::vector< std::uint16_t > & | val, | ||
| const std::vector< std::uint16_t > & | wt ) |
Wrapper function to initiate the unbounded knapsack calculation.
| N | Number of items. |
| W | Maximum weight capacity of the knapsack. |
| val | Vector of values corresponding to the items. |
| wt | Vector of weights corresponding to the items. |