ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
KnapsackVariants.h File Reference

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 \)

Detailed description

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:

  • rodCutting(): a rod of length \( L \) sold in pieces, \( \mathit{price}[ t - 1 ] \) the price of a piece of length \( t \), the largest gain \( R[ l ] = \max_{1 \le t \le \min( l , m )} \mathit{price}[ t - 1 ] + R[ l - t ] \), \( R[ 0 ] = 0 \), with the pieces read back; \( O( L m ) \) time and \( O( L ) \) space for \( m \) prices;
  • unboundedKnapsack(): the largest profit of objects of volumes \( v \) and profits \( p \) in unlimited supply in a knapsack of capacity \( C \), \( P[ c ] = \max( P[ c - 1 ] , \max_{v_i \le c} P[ c - v_i ] + p_i ) \), and how many copies of each object; \( O( n C ) \) time, \( O( C ) \) space;
  • minValueExactWeight(): the least value of coins of weights \( w \) and values \( v \) in unlimited supply that weigh exactly \( W \), nothing if no such coins exist; with all the values 1 it is the fewest coins of the change (optimalChange() of CoinChange.h); \( O( n W ) \) time, \( O( W ) \) space;
  • coinChangeWays(): the number of ways of paying \( T \) with coins of the given values in unlimited supply, the order not counting: the coins enter one value at a time, \( N[ x ] \mathrel{+}= N[ x - d ] \) for \( x \) from \( d \) up, so that each multiset is counted once; \( O( n T ) \) time, \( O( T ) \) space;
  • subsetSumCount(): the number of subsets of places of \( A \) of sum \( k \), the same scan with \( x \) going down, each item used at most once; \( O( n k ) \) time, \( O( k ) \) space;
  • minimumSquares(): the fewest squares of positive integers that sum to \( n \). The dynamic programming \( S[ i ] = 1 + \min_{t^2 \le i} S[ i - t^2 ] \) takes \( \Theta( n \sqrt{n} ) \) time; number theory answers in \( O( \sqrt{n} \log n ) \): 0 for \( n = 0 \), 1 for a square, 4 for \( n = 4^a ( 8 b + 7 ) \) (Legendre's three-square theorem), 2 if \( n - t^2 \) is a square for some \( t \), 3 otherwise; at most 4 by Lagrange's four-square theorem. Taking the largest square first is not optimal: \( 18 = 9 + 9 \), not \( 16 + 1 + 1 \).

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.

See also
A. Montresor, Algoritmi e Strutture Dati, 2025, section 13.6 and the Esercizi Programmazione dinamica, exercises B4 of 01/02/2012, B3 of 31/05/2012, B4 of 02/02/2015 and B4 of 26/01/2016, https://algoritmicamente.it/materiali/libro/capitoli/cap13.pdf
T. H. Cormen, C. E. Leiserson, R. L. Rivest, C. Stein, Introduction to Algorithms, 4th ed., MIT Press, 2022, section 14.1
S. Martello, P. Toth, Knapsack Problems: Algorithms and Computer Implementations, Wiley, 1990, chapters 3 and 5
M. O. Rabin, J. O. Shallit, Randomized algorithms in number theory, Communications on Pure and Applied Mathematics 39(S1), S239-S256, 1986, doi:10.1002/cpa.3160390713
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa