TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
|
Implementation of the Unbounded 0/1 Knapsack Problem. More...
#include <cassert>
#include <cstdint>
#include <iostream>
#include <vector>
Go to the source code of this file.
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.
Definition in file unbounded_0_1_knapsack.cpp.
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. |
Definition at line 58 of file unbounded_0_1_knapsack.cpp.
int main | ( | void | ) |
main function
Definition at line 170 of file unbounded_0_1_knapsack.cpp.
|
static |
self test implementation
Definition at line 111 of file unbounded_0_1_knapsack.cpp.
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. |
Definition at line 93 of file unbounded_0_1_knapsack.cpp.