|
Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
|
Classical knapsack problem variants (0/1, unbounded, bounded). More...
#include <algorithm>#include <concepts>#include <cstddef>#include <limits>#include <type_traits>#include <utility>#include <ah-errors.H>#include <tpl_array.H>Go to the source code of this file.
Classes | |
| struct | Aleph::Knapsack_Item< W, V > |
| An item for knapsack problems. More... | |
| struct | Aleph::Knapsack_Result< V > |
| Result of a knapsack computation. More... | |
Namespaces | |
| namespace | Aleph |
| Main namespace for Aleph-w library functions. | |
| namespace | Aleph::knapsack_detail |
Functions | |
| template<std::integral W> | |
| size_t | Aleph::knapsack_detail::to_size_checked (const W value, const char *fn_name, const char *field_name) |
| template<std::integral W, typename V > | |
| Array< size_t > | Aleph::knapsack_detail::extract_weights_checked (const Array< Knapsack_Item< W, V > > &items, const char *fn_name) |
| template<typename V > | |
| V | Aleph::knapsack_detail::dp_add (V a, V b) noexcept |
| template<typename V , std::integral W> | |
| Knapsack_Result< V > | Aleph::knapsack_01 (const Array< Knapsack_Item< W, V > > &items, W capacity) |
| Solve the 0/1 Knapsack problem with item reconstruction. | |
| template<typename V , std::integral W> | |
| V | Aleph::knapsack_01_value (const Array< Knapsack_Item< W, V > > &items, W capacity) |
| Solve the 0/1 Knapsack problem (value only, space-optimized). | |
| template<typename V , std::integral W> | |
| Knapsack_Result< V > | Aleph::knapsack_unbounded (const Array< Knapsack_Item< W, V > > &items, W capacity) |
| Solve the Unbounded Knapsack problem with reconstruction. | |
| template<typename V , std::integral W> | |
| Knapsack_Result< V > | Aleph::knapsack_bounded (const Array< Knapsack_Item< W, V > > &items, const Array< size_t > &counts, W capacity) |
| Solve the Bounded Knapsack problem with reconstruction. | |
Classical knapsack problem variants (0/1, unbounded, bounded).
The knapsack problem is a fundamental combinatorial optimization problem: given a set of items, each with a weight and a value, determine the number of each item to include in a collection so that the total weight is less than or equal to a given limit and the total value is as large as possible.
This header provides high-performance dynamic programming solutions for:
| Variant | Time | Space |
|---|---|---|
| 0/1 Knapsack | O(n * capacity) | O(capacity) |
| Unbounded | O(n * capacity) | O(capacity) |
| Bounded | O(capacity * sum(log c_i)) | O(capacity) |
Definition in file Knapsack.H.