|
ADS
Algorithms and Data Structures
|
The relatives of the knapsack in which an item can be taken many times, and the counting versions of the knapsack, all solved by the dynamic programming on the capacity of the 0-1 knapsack of section 13.6, in pseudopolynomial time: More...
#include "ADS/Contract.h"#include <algorithm>#include <cstddef>#include <cstdint>#include <optional>#include <utility>#include <vector>#include "ADS/optimization/impl/KnapsackVariants.tpp"Go to the source code of this file.
Namespaces | |
| namespace | ADS |
Functions | |
| template<class T> | |
| std::pair< T, std::vector< std::size_t > > | ADS::rodCutting (const std::vector< T > &price, std::size_t L) |
| the largest gain of a rod of length \( L \) cut in pieces of lengths \( 1 , \ldots , m \), \( m = |\mathit{price}| \), \( \mathit{price}[ t - 1 ] \) the price of a piece of length \( t \), and the lengths of the pieces | |
| template<class T, class V> | |
| std::pair< T, std::vector< std::size_t > > | ADS::unboundedKnapsack (const std::vector< T > &p, const std::vector< V > &v, V C) |
| the largest profit of the objects of profits \( p \) and volumes \( v \), each in unlimited supply, in a knapsack of capacity \( C \), and the copies of each | |
| template<class T, class V> | |
| std::optional< T > | ADS::minValueExactWeight (const std::vector< V > &w, const std::vector< T > &v, V W) |
| the least value of coins of weights \( w \) and values \( v \), in unlimited supply, weighing exactly \( W \); nothing if there is none | |
| template<class C = std::uint64_t, class V> | |
| C | ADS::coinChangeWays (const std::vector< V > &coins, V T) |
| the number of multisets of the coins of sum \( T \), each coin in unlimited supply; equal values in coins count as distinct kinds | |
| template<class C = std::uint64_t, class V> | |
| C | ADS::subsetSumCount (const std::vector< V > &A, V k) |
| the number of subsets of the places of \( A \) whose items sum to \( k \) | |
| int | ADS::minimumSquares (std::int64_t n) |
| the fewest squares of positive integers summing to \( n \) | |
The relatives of the knapsack in which an item can be taken many times, and the counting versions of the knapsack, all solved by the dynamic programming on the capacity of the 0-1 knapsack of section 13.6, in pseudopolynomial time:
The running times are pseudopolynomial, exponential in the size of the numbers: the 0-1 decision versions are NP-complete [see Pseudopolynomial.h]. The bounded knapsack, each object available \( b_i \) times, reduces to the 0-1 one by splitting \( b_i \) in powers of two, and the counting versions are the coefficients of the generating functions \( \prod_d ( 1 - z^d )^{-1} \) and \( \prod_i ( 1 + z^{a_i} ) \).
The contracts [see Contract.h]: the capacities and the amounts are not negative, the volumes, the weights and the values of the coins positive, the items of subsetSumCount() not negative; the pieces of rodCutting() and the copies of unboundedKnapsack() fit and give the value found. The functions are defined in KnapsackVariants.tpp.