|
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...
Files | |
| file | ADS/optimization/KnapsackVariants.h |
| 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: | |
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 definitions, the invariants and the costs of the area are in Theory of optimization.
| 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
In pseudocode (exercise B3 of 31/05/2012, Resto, of the Esercizi Programmazione dinamica of the book), the coins entering one kind at a time:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{coinChangeWays}( D , T )\quad \triangleright\ N[ x ] \text{ the ways to pay } x \text{ with the coins so far} \cr 2 & \quad N \gets ( 1 , 0 , \ldots , 0 ) \cr 3 & \quad \textbf{foreach}\ d \in D\ \textbf{do}\quad \triangleright \ \text{one coin at a time: each multiset counted once} \cr 4 & \qquad N[ x ] \gets N[ x ] + N[ x - d ] \text{ for } x = d , \ldots , T \text{, by increasing } x \cr 5 & \quad \textbf{return}\ N[ T ] \end{array}} \]
|
inline |
the fewest squares of positive integers summing to \( n \)
In pseudocode (the theorems of Lagrange and Legendre, beyond the book, whose exercise B4 of 26/01/2016, Somma di quadrati, of the Esercizi Programmazione dinamica asks the dynamic programming):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{minimumSquares}( n ) \cr 2 & \quad \textbf{if}\ n = 0\ \textbf{then return}\ 0 \cr 3 & \quad \textbf{if}\ n \text{ is a square}\ \textbf{then return}\ 1 \cr 4 & \quad m \gets n \text{ with its factors 4 removed} \cr 5 & \quad \textbf{if}\ m \bmod 8 = 7\ \textbf{then return}\ 4 \cr 6 & \quad \textbf{for}\ t \gets 1\ \textbf{to}\ \lfloor \sqrt n \rfloor \ \textbf{do} \cr 7 & \qquad \textbf{if}\ n - t^2 \text{ is a square}\ \textbf{then return} \ 2 \cr 8 & \quad \textbf{return}\ 3 \end{array}} \]
| 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
In pseudocode (exercise B4 of 02/02/2015, Piggy bank, of the Esercizi Programmazione dinamica of the book), \( D[ x ] = \textbf{nil} \) for a weight that no coins make:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{minValueExactWeight}( w , v , W )\quad \triangleright\ \textbf{nil} \text{: no exact weight} \cr 2 & \quad D[ 0 ] \gets 0;\ D[ 1 .. W ] \gets \textbf{nil} \cr 3 & \quad \textbf{for}\ x \gets 1\ \textbf{to}\ W\ \textbf{do}\quad \triangleright\ \text{each coin in any number} \cr 4 & \qquad D[ x ] \gets \min\{ D[ x - w_i ] + v_i : w_i \le x \text{, } D[ x - w_i ] \ne \textbf{nil} \} \text{, or } \textbf{nil} \text{ if no such } i \cr 5 & \quad \textbf{return}\ D[ W ] \end{array}} \]
| 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
In pseudocode (exercise B4 of 01/02/2012, Best cut, of the Esercizi Programmazione dinamica of the book), \( \mathit{cut}[ l ] \) the first piece of a best cut of the length \( l \):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{rodCutting}( \mathit{price} , L )\quad \triangleright\ R[ l ] \text{ the best of a rod of length } l \cr 2 & \quad R[ 0 ] \gets 0 \cr 3 & \quad \textbf{for}\ l \gets 1\ \textbf{to}\ L\ \textbf{do}\quad \triangleright\ \text{the first piece of length } t \le m \cr 4 & \qquad \mathit{cut}[ l ] \gets \text{the least } t \in [ 1 , \min( l , m ) ] \text{ that maximizes } \mathit{price}[ t - 1 ] + R[ l - t ] \cr 5 & \qquad R[ l ] \gets \mathit{price}[ \mathit{cut}[ l ] - 1 ] + R[ l - \mathit{cut}[ l ] ] \cr 6 & \quad \textbf{return}\ R[ L ] \text{ and the pieces } \mathit{cut}[ L ] , \mathit{cut}[ L - \mathit{cut}[ L ] ] , \ldots \end{array}} \]
| C ADS::subsetSumCount | ( | const std::vector< V > & | A, |
| V | k ) |
the number of subsets of the places of \( A \) whose items sum to \( k \)
In pseudocode (beyond the book), the sums scanned downwards so that each item is used once:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{subsetSumCount}( A , k ) \cr 2 & \quad N[ 0 ] \gets 1;\ N[ 1 .. k ] \gets 0 \cr 3 & \quad \textbf{foreach}\ a \in A\ \textbf{with}\ a \le k\ \textbf{do} \cr 4 & \qquad \textbf{for}\ x \gets k\ \textbf{downto}\ a\ \textbf{do}\ N[ x ] \gets N[ x ] + N[ x - a ] \cr 5 & \quad \textbf{return}\ N[ k ] \end{array}} \]
| 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
In pseudocode (beyond the book, after Martello and Toth), \( \mathit{last}[ c ] \) the last object added for the capacity \( c \), \( \textbf{nil} \) for a unit left empty:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{unboundedKnapsack}( p , v , C ) \cr 2 & \quad P[ 0 ] \gets 0 \cr 3 & \quad \textbf{for}\ c \gets 1\ \textbf{to}\ C\ \textbf{do} \cr 4 & \qquad P[ c ] \gets P[ c - 1 ];\ \mathit{last}[ c ] \gets \textbf{nil} \cr 5 & \qquad \textbf{foreach}\ i\ \textbf{with}\ v_i \le c \text{ and } P[ c - v_i ] + p_i > P[ c ]\ \textbf{do} \cr 6 & \qquad\quad P[ c ] \gets P[ c - v_i ] + p_i;\ \mathit{last}[ c ] \gets i \cr 7 & \quad \text{read the copies back from } \mathit{last}[ C ] \text{, } \text{a step of } v_i \text{ or of 1 for } \textbf{nil} \cr 8 & \quad \textbf{return}\ P[ C ] \text{ and the copies} \end{array}} \]