ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
Knapsack.h

The 0-1 knapsack of section 13.6. More...

Files

file  ADS/optimization/Knapsack.h
 The 0-1 knapsack of section 13.6: \( n \) objects of profit \( p_i \) and volume \( v_i \), positive integers, and a knapsack of capacity \( C \); a set \( S \) of volume at most \( C \) and largest profit.

Functions

std::pair< long, std::vector< std::size_t > > ADS::knapsack (const std::vector< long > &p, const std::vector< long > &v, long C)
 the best profit and a set of objects, by places, of the 0-1 knapsack
long ADS::knapsackMemo (const std::vector< long > &p, const std::vector< long > &v, long C, long *solved=nullptr)
 the best profit by memoization in a hash table (the zaino() of the book); the number of subproblems solved goes to *solved, if given
long ADS::zaino (const std::vector< long > &p, const std::vector< long > &v, long C, long *solved=nullptr)
 knapsackMemo() under the name of the book

Detailed description

The 0-1 knapsack of section 13.6.

The definitions, the invariants and the costs of the area are in Theory of optimization.

Function documentation

◆ knapsack()

std::pair< long, std::vector< std::size_t > > ADS::knapsack ( const std::vector< long > & p,
const std::vector< long > & v,
long C )
inline

the best profit and a set of objects, by places, of the 0-1 knapsack

In pseudocode (section 13.6), the table filled by rows and read back:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{knapsack}( p , v , C )\quad \triangleright\ \mathit{DP}[ i ][ c ] \text{ the best with the first } i \text{ objects and capacity } c \cr 2 & \quad \mathit{DP}[ 0 ][ 0 .. C ] \gets 0 \cr 3 & \quad \textbf{for}\ i \gets 1\ \textbf{to}\ n\ \textbf{do} \cr 4 & \qquad \textbf{for}\ c \gets 0\ \textbf{to}\ C\ \textbf{do}\quad \triangleright\ \text{without the object } i - 1 \text{, or with it if it fits} \cr 5 & \qquad\quad \mathit{DP}[ i ][ c ] \gets \max\{ \mathit{DP}[ i - 1 ][ c ] , \mathit{DP}[ i - 1 ][ c - v_{i-1} ] + p_{i-1} \text{ if } v_{i-1} \le c \} \cr 6 & \quad S \gets \emptyset;\ c \gets C \cr 7 & \quad \textbf{for}\ i \gets n\ \textbf{downto}\ 1\ \textbf{do}\quad \triangleright\ \text{the object was taken iff the value changed} \cr 8 & \qquad \textbf{if}\ \mathit{DP}[ i ][ c ] \ne \mathit{DP}[ i - 1 ][ c ]\ \textbf{then}\ S \gets S \cup \{ i - 1 \};\ c \gets c - v_{i-1} \cr 9 & \quad \textbf{return}\ ( \mathit{DP}[ n ][ C ] , S ) \end{array}} \]

Precondition
\( |p| = |v| \), \( p_i , v_i \ge 0 \), \( C \ge 0 \), the \( C + 1 \) capacities within the largest vector
Postcondition
the objects fit and give the profit

◆ knapsackMemo()

long ADS::knapsackMemo ( const std::vector< long > & p,
const std::vector< long > & v,
long C,
long * solved = nullptr )
inline

the best profit by memoization in a hash table (the zaino() of the book); the number of subproblems solved goes to *solved, if given

In pseudocode (section 13.6), \( T_i \) the hash table of the object \( i \):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{knapsackMemo}( i , c )\quad \triangleright \ \text{the best with the first } i \text{ objects and capacity } c \cr 2 & \quad \textbf{if}\ c < 0\ \textbf{then return}\ -\infty\quad \triangleright\ \text{an object that did not fit} \cr 3 & \quad \textbf{if}\ i = 0\ \textbf{then return}\ 0 \cr 4 & \quad \textbf{if}\ T_i \text{ has no entry for } c\ \textbf{then} \cr 5 & \quad\quad T_i[ c ] \gets \max\{ \mathrm{knapsackMemo}( i - 1 , c - v_{i-1} ) + p_{i-1} , \mathrm{knapsackMemo}( i - 1 , c ) \} \cr 6 & \quad \textbf{return}\ T_i[ c ] \end{array}} \]

called as \( \mathrm{knapsackMemo}( n , C ) \).

Precondition
\( |p| = |v| \), \( p_i , v_i \ge 0 \), \( C \ge 0 \)

◆ zaino()

long ADS::zaino ( const std::vector< long > & p,
const std::vector< long > & v,
long C,
long * solved = nullptr )
inline

knapsackMemo() under the name of the book