# Knapsack problem

__: Problem in combinatorial optimization__

**Short description**The **knapsack problem** is the following problem in combinatorial optimization:

*Given a set of items, each with a weight and a value, determine which items to include in the collection so that the total weight is less than or equal to a given limit and the total value is as large as possible.*

It derives its name from the problem faced by someone who is constrained by a fixed-size knapsack and must fill it with the most valuable items. The problem often arises in resource allocation where the decision-makers have to choose from a set of non-divisible projects or tasks under a fixed budget or time constraint, respectively.

The knapsack problem has been studied for more than a century, with early works dating as far back as 1897.^{[1]}

## Applications

Knapsack problems appear in real-world decision-making processes in a wide variety of fields, such as finding the least wasteful way to cut raw materials,^{[2]} selection of investments and portfolios,^{[3]} selection of assets for asset-backed securitization,^{[4]} and generating keys for the Merkle–Hellman^{[5]} and other knapsack cryptosystems.

One early application of knapsack algorithms was in the construction and scoring of tests in which the test-takers have a choice as to which questions they answer. For small examples, it is a fairly simple process to provide the test-takers with such a choice. For example, if an exam contains 12 questions each worth 10 points, the test-taker need only answer 10 questions to achieve a maximum possible score of 100 points. However, on tests with a heterogeneous distribution of point values, it is more difficult to provide choices. Feuerman and Weiss proposed a system in which students are given a heterogeneous test with a total of 125 possible points. The students are asked to answer all of the questions to the best of their abilities. Of the possible subsets of problems whose total point values add up to 100, a knapsack algorithm would determine which subset gives each student the highest possible score.^{[6]}

A 1999 study of the Stony Brook University Algorithm Repository showed that, out of 75 algorithmic problems related to the field of combinatorial algorithms and algorithm engineering, the knapsack problem was the 19th most popular and the third most needed after suffix trees and the bin packing problem.^{[7]}

## Definition

The most common problem being solved is the **0-1 knapsack problem**, which restricts the number *[math]\displaystyle{ x_i }[/math]* of copies of each kind of item to zero or one. Given a set of *[math]\displaystyle{ n }[/math]* items numbered from 1 up to *[math]\displaystyle{ n }[/math]*, each with a weight *[math]\displaystyle{ w_i }[/math]* and a value *[math]\displaystyle{ v_i }[/math]*, along with a maximum weight capacity *[math]\displaystyle{ W }[/math]*,

- maximize [math]\displaystyle{ \sum_{i=1}^n v_i x_i }[/math]
- subject to [math]\displaystyle{ \sum_{i=1}^n w_i x_i \leq W }[/math] and [math]\displaystyle{ x_i \in \{0,1\} }[/math].

Here *[math]\displaystyle{ x_i }[/math]* represents the number of instances of item *[math]\displaystyle{ i }[/math]* to include in the knapsack. Informally, the problem is to maximize the sum of the values of the items in the knapsack so that the sum of the weights is less than or equal to the knapsack's capacity.

The **bounded knapsack problem** (**BKP**) removes the restriction that there is only one of each item, but restricts the number [math]\displaystyle{ x_i }[/math] of copies of each kind of item to a maximum non-negative integer value [math]\displaystyle{ c }[/math]:

- maximize [math]\displaystyle{ \sum_{i=1}^n v_i x_i }[/math]
- subject to [math]\displaystyle{ \sum_{i=1}^n w_i x_i \leq W }[/math] and [math]\displaystyle{ x_i \in \{0,1,2,\dots,c\}. }[/math]

The **unbounded knapsack problem** (**UKP**) places no upper bound on the number of copies of each kind of item and can be formulated as above except that the only restriction on [math]\displaystyle{ x_i }[/math] is that it is a non-negative integer.

- maximize [math]\displaystyle{ \sum_{i=1}^n v_i x_i }[/math]
- subject to [math]\displaystyle{ \sum_{i=1}^n w_i x_i \leq W }[/math] and [math]\displaystyle{ x_i \in \mathbb{Z}, \ x_i \geq 0. }[/math]

One example of the unbounded knapsack problem is given using the figure shown at the beginning of this article and the text "if any number of each box is available" in the caption of that figure.

## Computational complexity

The knapsack problem is interesting from the perspective of computer science for many reasons:

- The decision problem form of the knapsack problem (
*Can a value of at least*V*be achieved without exceeding the weight*W*?*) is NP-complete, thus there is no known algorithm that is both correct and fast (polynomial-time) in all cases. - There is no known polynomial algorithm which can tell, given a solution, whether it is optimal (which would mean that there is no solution with a larger
*V*). This problem is co-NP-complete. - There is a pseudo-polynomial time algorithm using dynamic programming.
- There is a fully polynomial-time approximation scheme, which uses the pseudo-polynomial time algorithm as a subroutine, described below.
- Many cases that arise in practice, and "random instances" from some distributions, can nonetheless be solved exactly.

There is a link between the "decision" and "optimization" problems in that if there exists a polynomial algorithm that solves the "decision" problem, then one can find the maximum value for the optimization problem in polynomial time by applying this algorithm iteratively while increasing the value of k. On the other hand, if an algorithm finds the optimal value of the optimization problem in polynomial time, then the decision problem can be solved in polynomial time by comparing the value of the solution output by this algorithm with the value of k. Thus, both versions of the problem are of similar difficulty.

One theme in research literature is to identify what the "hard" instances of the knapsack problem look like,^{[8]}^{[9]} or viewed another way, to identify what properties of instances in practice might make them more amenable than their worst-case NP-complete behaviour suggests.^{[10]} The goal in finding these "hard" instances is for their use in public key cryptography systems, such as the Merkle-Hellman knapsack cryptosystem.

Furthermore, notable is the fact that the hardness of the knapsack problem depends on the form of the input. If the weights and profits are given as integers, it is weakly NP-complete, while it is strongly NP-complete if the weights and profits are given as rational numbers.^{[11]} However, in the case of rational weights and profits it still admits a fully polynomial-time approximation scheme.

### unit-cost models

The NP-hardness of the Knapsack problem relates to computational models in which the size of integers matters (such as the Turing machine). In contrast, decision trees count each decision as a single step. Dobkin and Lipton^{[12]} show an [math]\displaystyle{ {1\over 2}n^2 }[/math] lower bound on linear decision trees for the knapsack problem, that is, trees where decision nodes test the sign of affine functions.^{[13]} This was generalized to algebraic decision trees by Steele and Yao.^{[14]}
If the elements in the problem are real numbers or rationals, the decision-tree lower bound extends to the real random-access machine model with an instruction set that includes addition, subtraction and multiplication of real numbers, as well as comparison and either division or remaindering ("floor").^{[15]} This model covers more algorithms than the algebraic decision-tree model, as it encompasses algorithms that use indexing into tables. However, in this model all program steps are counted, not just decisions.
An *upper bound* for a decision-tree model was given by Meyer auf der Heide^{[16]} who showed that for every *n* there exists an *O*(*n*^{4})-deep linear decision tree that solves the subset-sum problem with *n* items. Note that this does not imply any upper bound for an algorithm that should solve the problem for *any given n*.

## Solving

Several algorithms are available to solve knapsack problems, based on the dynamic programming approach,^{[17]} the branch and bound approach^{[18]} or hybridizations of both approaches.^{[10]}^{[19]}^{[20]}^{[21]}

### Dynamic programming in-advance algorithm

The **unbounded knapsack problem** (**UKP**) places no restriction on the number of copies of each kind of item. Besides, here we assume that [math]\displaystyle{ x_i \gt 0 }[/math]

- [math]\displaystyle{ m[w']= \max \left( \sum_{i=1}^n v_i x_i \right) }[/math]
- subject to [math]\displaystyle{ \sum_{i=1}^n w_i x_i \leq w' }[/math] and [math]\displaystyle{ x_i \gt 0 }[/math]

Observe that [math]\displaystyle{ m[w] }[/math] has the following properties:

1. [math]\displaystyle{ m[0]=0\,\! }[/math] (the sum of zero items, i.e., the summation of the empty set).

2. [math]\displaystyle{ m[w]=\max (v_1+m[w-w_1],v_2+m[w-w_2],...,v_n+m[w-w_n]) }[/math] , [math]\displaystyle{ w_i \leq w }[/math], where [math]\displaystyle{ v_i }[/math] is the value of the [math]\displaystyle{ i }[/math]-th kind of item.

The second property needs to be explained in detail. During the process of the running of this method, how do we get the weight [math]\displaystyle{ w }[/math]? There are only [math]\displaystyle{ i }[/math] ways and the previous weights are [math]\displaystyle{ w-w_1, w-w_2,..., w-w_i }[/math] where there are total [math]\displaystyle{ i }[/math] kinds of different item (by saying different, we mean that the weight and the value are not completely the same). If we know each value of these [math]\displaystyle{ i }[/math] items and the related maximum value previously, we just compare them to each other and get the maximum value ultimately and we are done.

Here the maximum of the empty set is taken to be zero. Tabulating the results from [math]\displaystyle{ m[0] }[/math] up through [math]\displaystyle{ m[W] }[/math] gives the solution. Since the calculation of each [math]\displaystyle{ m[w] }[/math] involves examining at most [math]\displaystyle{ n }[/math] items, and there are at most [math]\displaystyle{ W }[/math] values of [math]\displaystyle{ m[w] }[/math] to calculate, the running time of the dynamic programming solution is [math]\displaystyle{ O(nW) }[/math]. Dividing [math]\displaystyle{ w_1,\,w_2,\,\ldots,\,w_n,\,W }[/math] by their greatest common divisor is a way to improve the running time.

Even if P≠NP, the [math]\displaystyle{ O(nW) }[/math] complexity does not contradict the fact that the knapsack problem is NP-complete, since [math]\displaystyle{ W }[/math], unlike [math]\displaystyle{ n }[/math], is not polynomial in the length of the input to the problem. The length of the [math]\displaystyle{ W }[/math] input to the problem is proportional to the number of bits in [math]\displaystyle{ W }[/math], [math]\displaystyle{ \log W }[/math], not to [math]\displaystyle{ W }[/math] itself. However, since this runtime is pseudopolynomial, this makes the (decision version of the) knapsack problem a weakly NP-complete problem.

#### 0-1 knapsack problem

A similar dynamic programming solution for the 0-1 knapsack problem also runs in pseudo-polynomial time. Assume [math]\displaystyle{ w_1,\,w_2,\,\ldots,\,w_n,\, W }[/math] are strictly positive integers. Define [math]\displaystyle{ m[i,w] }[/math] to be the maximum value that can be attained with weight less than or equal to [math]\displaystyle{ w }[/math] using items up to [math]\displaystyle{ i }[/math] (first [math]\displaystyle{ i }[/math] items).

We can define [math]\displaystyle{ m[i,w] }[/math] recursively as follows: **(Definition A)**

- [math]\displaystyle{ m[0,\,w]=0 }[/math]
- [math]\displaystyle{ m[i,\,w]=m[i-1,\,w] }[/math] if [math]\displaystyle{ w_i \gt w\,\! }[/math] (the new item is more than the current weight limit)
- [math]\displaystyle{ m[i,\,w]=\max(m[i-1,\,w],\,m[i-1,w-w_i]+v_i) }[/math] if [math]\displaystyle{ w_i \leqslant w }[/math].

The solution can then be found by calculating [math]\displaystyle{ m[n,W] }[/math]. To do this efficiently, we can use a table to store previous computations.

The following is pseudocode for the dynamic program:

// Input: // Values (stored in array v) // Weights (stored in array w) // Number of distinct items (n) // Knapsack capacity (W) // NOTE: The array "v" and array "w" are assumed to store all relevant values starting at index 1. array m[0..n, 0..W]; for j from 0 to W do: m[0, j] := 0 for i from 1 to n do: m[i, 0] := 0 for i from 1 to n do: for j from 1 to W do: if w[i] > j then: m[i, j] := m[i-1, j] else: m[i, j] := max(m[i-1, j], m[i-1, j-w[i]] + v[i])

This solution will therefore run in [math]\displaystyle{ O(nW) }[/math] time and [math]\displaystyle{ O(nW) }[/math] space. (If we only need the value m[n,W], we can modify the code so that the amount of memory required is O(W) which stores the recent two lines of the array "m".)

However, if we take it a step or two further, we should know that the method will run in the time between [math]\displaystyle{ O(nW) }[/math] and [math]\displaystyle{ O(2^n) }[/math]. From **Definition A**, we know that there is no need to compute all the weights when the number of items and the items themselves that we chose are fixed. That is to say, the program above computes more than necessary because the weight changes from 0 to W often. From this perspective, we can program this method so that it runs recursively.

// Input: // Values (stored in array v) // Weights (stored in array w) // Number of distinct items (n) // Knapsack capacity (W) // NOTE: The array "v" and array "w" are assumed to store all relevant values starting at index 1. Define value[n, W] Initialize all value[i, j] = -1 Define m:=(i,j) // Define function m so that it represents the maximum value we can get under the condition: use first i items, total weight limit is j { if i == 0 or j <= 0 then: value[i, j] = 0 return if (value[i-1, j] == -1) then: // m[i-1, j] has not been calculated, we have to call function m m(i-1, j) if w[i] > j then: // item cannot fit in the bag value[i, j] = value[i-1, j] else: if (value[i-1, j-w[i]] == -1) then: // m[i-1,j-w[i]] has not been calculated, we have to call function m m(i-1, j-w[i]) value[i, j] = max(value[i-1,j], value[i-1, j-w[i]] + v[i]) } Run m(n, W)

For example, there are 10 different items and the weight limit is 67. So, [math]\displaystyle{ \begin{align} &w[ 1]= 23 ,w[ 2]= 26,w[ 3]= 20,w[ 4]= 18,w[ 5]= 32,w[ 6]= 27,w[ 7]= 29,w[ 8]= 26,w[ 9]= 30,w[ 10]= 27 \\ &v[ 1]=505 ,v[ 2]=352,v[ 3]=458,v[ 4]=220,v[ 5]=354,v[ 6]=414,v[ 7]=498,v[ 8]=545,v[ 9]=473,v[ 10]=543 \\ \end{align} }[/math] If you use above method to compute for [math]\displaystyle{ m(10,67) }[/math], you will get this, excluding calls that produce [math]\displaystyle{ m(i,j) = 0 }[/math]: [math]\displaystyle{ \begin{align} &m(10, 67) = 1270\\ &m(9, 67) = 1270, m(9, 40) = 678\\ &m(8, 67) = 1270, m(8, 40) = 678, m(8, 37) = 545\\ &m(7, 67) = 1183, m(7, 41) = 725, m(7, 40) = 678, m(7, 37) = 505\\ &m(6, 67) = 1183, m(6, 41) = 725, m(6, 40) = 678, m(6, 38) = 678, m(6, 37) = 505\\ &m(5, 67) = 1183, m(5, 41) = 725, m(5, 40) = 678, m(5, 38) = 678, m(5, 37) = 505\\ &m(4, 67) = 1183, m(4, 41) = 725, m(4, 40) = 678, m(4, 38) = 678, m(4, 37) = 505, m(4, 35) = 505\\ &m(3, 67) = 963, m(3, 49) = 963, m(3, 41) = 505, m(3, 40) = 505, m(3, 38) = 505, m(3, 37) = 505, m(3, 35) = 505, m(3, 23) = 505, m(3, 22) = 458, m(3, 20) = 458\\ &m(2, 67) = 857, m(2, 49) = 857, m(2, 47) = 505, m(2, 41) = 505, m(2, 40) = 505, m(2, 38) = 505, m(2, 37) = 505, m(2, 35) = 505, m(2, 29) = 505, m(2, 23) = 505\\ &m(1, 67) = 505, m(1, 49) = 505, m(1, 47) = 505, m(1, 41) = 505, m(1, 40) = 505, m(1, 38) = 505, m(1, 37) = 505, m(1, 35) = 505, m(1, 29) = 505, m(1, 23) = 505\\ \end{align} }[/math]

Besides, we can break the recursion and convert it into a tree. Then we can cut some leaves and use parallel computing to expedite the running of this method.

To find the actual subset of items, rather than just their total value, we can run this after running the function above:

/** * Returns the indices of the items of the optimal knapsack. * i: We can include items 1 through i in the knapsack * j: maximum weight of the knapsack */ function knapsack(i: int, j: int): Set<int> { if i == 0 then: return {} if m[i, j] > m[i-1, j] then: return {i} ∪ knapsack(i-1, j-w[i]) else: return knapsack(i-1, j) } knapsack(n, W)

### Meet-in-the-middle

Another algorithm for 0-1 knapsack, discovered in 1974^{[22]} and sometimes called "meet-in-the-middle" due to parallels to a similarly named algorithm in cryptography, is exponential in the number of different items but may be preferable to the DP algorithm when [math]\displaystyle{ W }[/math] is large compared to *n*. In particular, if the [math]\displaystyle{ w_i }[/math] are nonnegative but not integers, we could still use the dynamic programming algorithm by scaling and rounding (i.e. using fixed-point arithmetic), but if the problem requires [math]\displaystyle{ d }[/math] fractional digits of precision to arrive at the correct answer, [math]\displaystyle{ W }[/math] will need to be scaled by [math]\displaystyle{ 10^d }[/math], and the DP algorithm will require [math]\displaystyle{ O(W10^d) }[/math] space and [math]\displaystyle{ O(nW10^d) }[/math] time.

algorithmMeet-in-the-middleisinput:A set of items with weights and values.output:The greatest combined value of a subset. partition the set {1...n} into two setsAandBof approximately equal size compute the weights and values of all subsets of each setfor eachsubsetofAdofind the subset ofBof greatest value such that the combined weight is less thanWkeep track of the greatest combined value seen so far

The algorithm takes [math]\displaystyle{ O(2^{n/2}) }[/math] space, and efficient implementations of step 3 (for instance, sorting the subsets of B by weight, discarding subsets of B which weigh more than other subsets of B of greater or equal value, and using binary search to find the best match) result in a runtime of [math]\displaystyle{ O(n2^{n/2}) }[/math]. As with the meet in the middle attack in cryptography, this improves on the [math]\displaystyle{ O(n2^n) }[/math] runtime of a naive brute force approach (examining all subsets of [math]\displaystyle{ \{1...n\} }[/math]), at the cost of using exponential rather than constant space (see also baby-step giant-step). The current state of the art improvement to the meet-in-the-middle algorithm, using insights from Schroeppel and Shamir's Algorithm for Subset Sum, provides as a corollary a randomized algorithm for Knapsack which preserves the [math]\displaystyle{ O^{*}(2^{n/2}) }[/math] (up to polynomial factors) running time and reduces the space requirements to [math]\displaystyle{ O^{*}(2^{0.249999n}) }[/math] (see ^{[23]} Corollary 1.4). In contrast, the best known deterministic algorithm runs in [math]\displaystyle{ O^{*}(2^{n/2}) }[/math] time with a slightly worse space complexity of [math]\displaystyle{ O^{*}(2^{n/4}) }[/math].^{[24]}

### Approximation algorithms

As for most NP-complete problems, it may be enough to find workable solutions even if they are not optimal. Preferably, however, the approximation comes with a guarantee of the difference between the value of the solution found and the value of the optimal solution.

As with many useful but computationally complex algorithms, there has been substantial research on creating and analyzing algorithms that approximate a solution. The knapsack problem, though NP-Hard, is one of a collection of algorithms that can still be approximated to any specified degree. This means that the problem has a polynomial time approximation scheme. To be exact, the knapsack problem has a fully polynomial time approximation scheme (FPTAS).^{[25]}

#### Greedy approximation algorithm

George Dantzig proposed a greedy approximation algorithm to solve the unbounded knapsack problem.^{[26]} His version sorts the items in decreasing order of value per unit of weight, [math]\displaystyle{ v_1/w_1\ge\cdots\ge v_n/w_n }[/math]. It then proceeds to insert them into the sack, starting with as many copies as possible of the first kind of item until there is no longer space in the sack for more. Provided that there is an unlimited supply of each kind of item, if [math]\displaystyle{ m }[/math] is the maximum value of items that fit into the sack, then the greedy algorithm is guaranteed to achieve at least a value of [math]\displaystyle{ m/2 }[/math].

For the bounded problem, where the supply of each kind of item is limited, the above algorithm may be far from optimal. Nevertheless, a simple modification allows us to solve this case: Assume for simplicity that all items individually fit in the sack ([math]\displaystyle{ w_i \le W }[/math] for all [math]\displaystyle{ i }[/math]). Construct a solution [math]\displaystyle{ S_1 }[/math] by packing items greedily as long as possible, i.e. [math]\displaystyle{ S_1=\left\{1,\ldots,k\right\} }[/math] where [math]\displaystyle{ k=\textstyle\max_{1\le k'\le n}\textstyle\sum_{i=1}^{k'} w_i\le W }[/math]. Furthermore, construct a second solution [math]\displaystyle{ S_2=\left\{k+1\right\} }[/math] containing the first item that did not fit. Since [math]\displaystyle{ S_1\cup S_2 }[/math] provides an upper bound for the LP relaxation of the problem, one of the sets must have value at least [math]\displaystyle{ m/2 }[/math]; we thus return whichever of [math]\displaystyle{ S_1 }[/math] and [math]\displaystyle{ S_2 }[/math] has better value to obtain a [math]\displaystyle{ 1/2 }[/math]-approximation.

It can be shown that the average performance converges to the optimal solution in distribution at the error rate [math]\displaystyle{ n^{-1/2} }[/math] ^{[27]}

#### Fully polynomial time approximation scheme

The fully polynomial time approximation scheme (FPTAS) for the knapsack problem takes advantage of the fact that the reason the problem has no known polynomial time solutions is because the profits associated with the items are not restricted. If one rounds off some of the least significant digits of the profit values then they will be bounded by a polynomial and 1/ε where ε is a bound on the correctness of the solution. This restriction then means that an algorithm can find a solution in polynomial time that is correct within a factor of (1-ε) of the optimal solution.^{[25]}

algorithmFPTASisinput:ε ∈ (0,1] a list A of n items, specified by their values, [math]\displaystyle{ v_i }[/math], and weightsoutput:S' the FPTAS solution P := max [math]\displaystyle{ \{v_i\mid 1 \leq i \leq n\} }[/math] // the highest item value K := ε [math]\displaystyle{ \frac{P}{n} }[/math]forifrom1tondo[math]\displaystyle{ v'_i }[/math] := [math]\displaystyle{ \left\lfloor \frac{v_i}{K} \right\rfloor }[/math]end forreturnthe solution, S', using the [math]\displaystyle{ v'_i }[/math] values in the dynamic program outlined above

**Theorem:** The set [math]\displaystyle{ S' }[/math] computed by the algorithm above satisfies [math]\displaystyle{ \mathrm{profit}(S') \geq (1-\varepsilon) \cdot \mathrm{profit}(S^*) }[/math], where [math]\displaystyle{ S^* }[/math] is an optimal solution.

### Dominance relations

Solving the unbounded knapsack problem can be made easier by throwing away items which will never be needed. For a given item [math]\displaystyle{ i }[/math], suppose we could find a set of items [math]\displaystyle{ J }[/math] such that their total weight is less than the weight of [math]\displaystyle{ i }[/math], and their total value is greater than the value of [math]\displaystyle{ i }[/math]. Then [math]\displaystyle{ i }[/math] cannot appear in the optimal solution, because we could always improve any potential solution containing [math]\displaystyle{ i }[/math] by replacing [math]\displaystyle{ i }[/math] with the set [math]\displaystyle{ J }[/math]. Therefore, we can disregard the [math]\displaystyle{ i }[/math]-th item altogether. In such cases, [math]\displaystyle{ J }[/math] is said to **dominate** [math]\displaystyle{ i }[/math]. (Note that this does not apply to bounded knapsack problems, since we may have already used up the items in [math]\displaystyle{ J }[/math].)

Finding dominance relations allows us to significantly reduce the size of the search space. There are several different types of dominance relations,^{[10]} which all satisfy an inequality of the form:

[math]\displaystyle{ \qquad \sum_{j \in J} w_j\,x_j \ \le \alpha\,w_i }[/math], and [math]\displaystyle{ \sum_{j \in J} v_j\,x_j \ \ge \alpha\,v_i\, }[/math] for some [math]\displaystyle{ x \in Z _+^n }[/math]

where [math]\displaystyle{ \alpha\in Z_+ \,,J\subsetneq N }[/math] and [math]\displaystyle{ i\not\in J }[/math]. The vector [math]\displaystyle{ x }[/math] denotes the number of copies of each member of [math]\displaystyle{ J }[/math].

- Collective dominance
- The [math]\displaystyle{ i }[/math]-th item is
**collectively dominated**by [math]\displaystyle{ J }[/math], written as [math]\displaystyle{ i\ll J }[/math], if the total weight of some combination of items in [math]\displaystyle{ J }[/math] is less than*w*and their total value is greater than_{i}*v*. Formally, [math]\displaystyle{ \sum_{j \in J} w_j\,x_j \ \le w_i }[/math] and [math]\displaystyle{ \sum_{j \in J} v_j\,x_j \ \ge v_i }[/math] for some [math]\displaystyle{ x \in Z _+^n }[/math], i.e. [math]\displaystyle{ \alpha=1 }[/math]. Verifying this dominance is computationally hard, so it can only be used with a dynamic programming approach. In fact, this is equivalent to solving a smaller knapsack decision problem where [math]\displaystyle{ V = v_i }[/math], [math]\displaystyle{ W = w_i }[/math], and the items are restricted to [math]\displaystyle{ J }[/math]._{i} - Threshold dominance
- The [math]\displaystyle{ i }[/math]-th item is
**threshold dominated**by [math]\displaystyle{ J }[/math], written as [math]\displaystyle{ i\prec\prec J }[/math], if some number of copies of [math]\displaystyle{ i }[/math] are dominated by [math]\displaystyle{ J }[/math]. Formally, [math]\displaystyle{ \sum_{j \in J} w_j\,x_j \ \le \alpha\,w_i }[/math], and [math]\displaystyle{ \sum_{j \in J} v_j\,x_j \ \ge \alpha\,v_i\, }[/math] for some [math]\displaystyle{ x \in Z _+^n }[/math] and [math]\displaystyle{ \alpha\geq 1 }[/math]. This is a generalization of collective dominance, first introduced in^{[17]}and used in the EDUK algorithm. The smallest such [math]\displaystyle{ \alpha }[/math] defines the**threshold**of the item [math]\displaystyle{ i }[/math], written [math]\displaystyle{ t_i =(\alpha-1)w_i }[/math]. In this case, the optimal solution could contain at most [math]\displaystyle{ \alpha-1 }[/math] copies of [math]\displaystyle{ i }[/math]. - Multiple dominance
- The [math]\displaystyle{ i }[/math]-th item is
**multiply dominated**by a single item [math]\displaystyle{ j }[/math], written as [math]\displaystyle{ i\ll_{m} j }[/math], if [math]\displaystyle{ i }[/math] is dominated by some number of copies of [math]\displaystyle{ j }[/math]. Formally, [math]\displaystyle{ w_j\,x_j \ \le w_i }[/math], and [math]\displaystyle{ v_j\,x_j \ \ge v_i }[/math] for some [math]\displaystyle{ x_j \in Z _+ }[/math] i.e. [math]\displaystyle{ J=\{j\}, \alpha=1, x_j=\lfloor \frac{w_i}{w_j}\rfloor }[/math]. This dominance could be efficiently used during preprocessing because it can be detected relatively easily. - Modular dominance
- Let [math]\displaystyle{ b }[/math] be the
*best item*, i.e. [math]\displaystyle{ \frac{v_b}{w_b}\ge\frac{v_i}{w_i}\, }[/math] for all [math]\displaystyle{ i }[/math]. This is the item with the greatest density of value. The [math]\displaystyle{ i }[/math]-th item is**modularly dominated**by a single item [math]\displaystyle{ j }[/math], written as [math]\displaystyle{ i\ll_\equiv j }[/math], if [math]\displaystyle{ i }[/math] is dominated by [math]\displaystyle{ j }[/math] plus several copies of [math]\displaystyle{ b }[/math]. Formally, [math]\displaystyle{ w_j+tw_b \le w_i }[/math], and [math]\displaystyle{ v_j +tv_b \ge v_i }[/math] i.e. [math]\displaystyle{ J=\{b,j\}, \alpha=1, x_b=t, x_j=1 }[/math].

## Variations

There are many variations of the knapsack problem that have arisen from the vast number of applications of the basic problem. The main variations occur by changing the number of some problem parameter such as the number of items, number of objectives, or even the number of knapsacks.

### Multi-objective knapsack problem

This variation changes the goal of the individual filling the knapsack. Instead of one objective, such as maximizing the monetary profit, the objective could have several dimensions. For example, there could be environmental or social concerns as well as economic goals. Problems frequently addressed include portfolio and transportation logistics optimizations.^{[28]}^{[29]}

As an example, suppose you ran a cruise ship. You have to decide how many famous comedians to hire. This boat can handle no more than one ton of passengers and the entertainers must weigh less than 1000 lbs. Each comedian has a weight, brings in business based on their popularity and asks for a specific salary. In this example, you have multiple objectives. You want, of course, to maximize the popularity of your entertainers while minimizing their salaries. Also, you want to have as many entertainers as possible.

### Multi-dimensional knapsack problem

In this variation, the weight of knapsack item [math]\displaystyle{ i }[/math] is given by a D-dimensional vector [math]\displaystyle{ \overline{w_i}=(w_{i1},\ldots,w_{iD}) }[/math] and the knapsack has a D-dimensional capacity vector [math]\displaystyle{ (W_1,\ldots,W_D) }[/math]. The target is to maximize the sum of the values of the items in the knapsack so that the sum of weights in each dimension [math]\displaystyle{ d }[/math] does not exceed [math]\displaystyle{ W_d }[/math].

Multi-dimensional knapsack is computationally harder than knapsack; even for [math]\displaystyle{ D=2 }[/math], the problem does not have EPTAS unless P[math]\displaystyle{ = }[/math]NP.^{[30]} However, the algorithm in^{[31]} is shown to solve sparse instances efficiently. An instance of multi-dimensional knapsack is sparse if there is a set [math]\displaystyle{ J=\{1,2,\ldots,m\} }[/math] for [math]\displaystyle{ m\lt D }[/math] such that for every knapsack item [math]\displaystyle{ i }[/math], [math]\displaystyle{ \exists z\gt m }[/math] such that [math]\displaystyle{ \forall j\in J\cup \{z\},\ w_{ij}\geq 0 }[/math] and [math]\displaystyle{ \forall y\notin J\cup\{z\}, w_{iy}=0 }[/math]. Such instances occur, for example, when scheduling packets in a wireless network with relay nodes.^{[31]} The algorithm from^{[31]} also solves sparse instances of the multiple choice variant, multiple-choice multi-dimensional knapsack.

The IHS (Increasing Height Shelf) algorithm is optimal for 2D knapsack (packing squares into a two-dimensional unit size square): when there are at most five square in an optimal packing.^{[32]}

### Multiple knapsack problem

This variation is similar to the Bin Packing Problem. It differs from the Bin Packing Problem in that a subset of items can be selected, whereas, in the Bin Packing Problem, all items have to be packed to certain bins. The concept is that there are multiple knapsacks. This may seem like a trivial change, but it is not equivalent to adding to the capacity of the initial knapsack. This variation is used in many loading and scheduling problems in Operations Research and has a Polynomial-time approximation scheme.^{[33]}

### Quadratic knapsack problem

The quadratic knapsack problem maximizes a quadratic objective function subject to binary and linear capacity constraints.^{[34]} The problem was introduced by Gallo, Hammer, and Simeone in 1980,^{[35]} however the first treatment of the problem dates back to Witzgall in 1975.^{[36]}

### Subset-sum problem

The subset sum problem is a special case of the decision and 0-1 problems where each kind of item, the weight equals the value: [math]\displaystyle{ w_i=v_i }[/math]. In the field of cryptography, the term *knapsack problem* is often used to refer specifically to the subset sum problem. The subset sum problem is one of Karp's 21 NP-complete problems.^{[37]}

A generalization of subset sum problem is called multiple subset-sum problem, in which multiple bins exist with the same capacity. It has been shown that the generalization does not have an FPTAS.^{[38]}

### Geometric knapsack problem

In the geometric knapsack problem, there is a set of rectangles with different values, and a rectangular knapsack. The goal is to pack the largest possible value into the knapsack.^{[39]}

## See also

- Bin packing problem
- Change-making problem
- Combinatorial auction
- Combinatorial optimization
- Continuous knapsack problem
- Cutting stock problem
- List of knapsack problems
- Packing problem

## Notes

- ↑ Mathews, G. B. (25 June 1897). "On the partition of numbers".
*Proceedings of the London Mathematical Society***28**: 486–490. doi:10.1112/plms/s1-28.1.486. http://plms.oxfordjournals.org/content/s1-28/1/486.full.pdf. - ↑ Kellerer, Hans; Pferschy, Ulrich; Pisinger, David (2004).
*Knapsack problems*. Berlin: Springer. p. 449. ISBN 978-3-540-40286-2. https://books.google.com/books?id=u5DB7gck08YC. Retrieved 5 May 2022. - ↑ Kellerer, Hans; Pferschy, Ulrich; Pisinger, David (2004).
*Knapsack problems*. Berlin: Springer. p. 461. ISBN 978-3-540-40286-2. https://books.google.com/books?id=u5DB7gck08YC. Retrieved 5 May 2022. - ↑ Kellerer, Hans; Pferschy, Ulrich; Pisinger, David (2004).
*Knapsack problems*. Berlin: Springer. p. 465. ISBN 978-3-540-40286-2. https://books.google.com/books?id=u5DB7gck08YC. Retrieved 5 May 2022. - ↑ Kellerer, Hans; Pferschy, Ulrich; Pisinger, David (2004).
*Knapsack problems*. Berlin: Springer. p. 472. ISBN 978-3-540-40286-2. https://books.google.com/books?id=u5DB7gck08YC. Retrieved 5 May 2022. - ↑ Feuerman, Martin; Weiss, Harvey (April 1973). "A Mathematical Programming Model for Test Construction and Scoring".
*Management Science***19**(8): 961–966. doi:10.1287/mnsc.19.8.961. - ↑ Skiena, S. S. (September 1999). "Who is Interested in Algorithms and Why? Lessons from the Stony Brook Algorithm Repository".
*ACM SIGACT News***30**(3): 65–74. doi:10.1145/333623.333627. ISSN 0163-5700. - ↑ Pisinger, D. 2003. Where are the hard knapsack problems? Technical Report 2003/08, Department of Computer Science, University of Copenhagen, Copenhagen, Denmark.
- ↑ Caccetta, L.; Kulanoot, A. (2001). "Computational Aspects of Hard Knapsack Problems".
*Nonlinear Analysis***47**(8): 5547–5558. doi:10.1016/s0362-546x(01)00658-7. - ↑
^{10.0}^{10.1}^{10.2}Poirriez, Vincent; Yanev, Nicola; Andonov, Rumen (2009). "A hybrid algorithm for the unbounded knapsack problem".*Discrete Optimization***6**(1): 110–124. doi:10.1016/j.disopt.2008.09.004. ISSN 1572-5286. - ↑ Wojtczak, Dominik (2018). "On Strong NP-Completeness of Rational Problems".
*International Computer Science Symposium in Russia*. Lecture Notes in Computer Science**10846**: 308–320. doi:10.1007/978-3-319-90530-3_26. ISBN 978-3-319-90529-7. - ↑ Dobkin, David; Lipton, Richard J. (1978). "A lower bound of ½
*n*^{2}on linear search programs for the Knapsack problem".*Journal of Computer and System Sciences***16**(3): 413–417. doi:10.1016/0022-0000(78)90026-0. https://www.sciencedirect.com/science/article/pii/0022000078900260. - ↑ In fact, the lower bound applies to the subset sum problem, which is a special case of Knapsack.
- ↑ Michael Steele, J; Yao, Andrew C (1982-03-01). "Lower bounds for algebraic decision trees" (in en).
*Journal of Algorithms***3**(1): 1–8. doi:10.1016/0196-6774(82)90002-5. ISSN 0196-6774. https://dx.doi.org/10.1016%2F0196-6774%2882%2990002-5. - ↑ Ben-Amram, Amir M.; Galil, Zvi (2001), "Topological Lower Bounds on Algebraic Random Access Machines",
*SIAM Journal on Computing***31**(3): 722–761, doi:10.1137/S0097539797329397. - ↑ auf der Heide, Meyer (1984), "A Polynomial Linear Search Algorithm for the
*n*-Dimensional Knapsack Problem",*Journal of the ACM***31**(3): 668–676, doi:10.1145/828.322450 - ↑
^{17.0}^{17.1}Andonov, Rumen; Poirriez, Vincent; Rajopadhye, Sanjay (2000). "Unbounded Knapsack Problem : dynamic programming revisited".*European Journal of Operational Research***123**(2): 168–181. doi:10.1016/S0377-2217(99)00265-9. - ↑ S. Martello, P. Toth, Knapsack Problems: Algorithms and Computer Implementations, John Wiley and Sons, 1990
- ↑ S. Martello, D. Pisinger, P. Toth, Dynamic programming and strong bounds for the 0-1 knapsack problem,
*Manag. Sci.*, 45:414–424, 1999. - ↑ Plateau, G.; Elkihel, M. (1985). "A hybrid algorithm for the 0-1 knapsack problem".
*Methods of Oper. Res.***49**: 277–293. - ↑ Martello, S.; Toth, P. (1984). "A mixture of dynamic programming and branch-and-bound for the subset-sum problem".
*Manag. Sci.***30**(6): 765–771. doi:10.1287/mnsc.30.6.765. - ↑ Horowitz, Ellis (1974), "Computing partitions with applications to the knapsack problem",
*Journal of the Association for Computing Machinery***21**(2): 277–292, doi:10.1145/321812.321823 - ↑ Nederlof, Jesper; Węgrzycki, Karol (2021-04-12). "Improving Schroeppel and Shamir's Algorithm for Subset Sum via Orthogonal Vectors". arXiv:2010.08576 [cs.DS].
- ↑ Schroeppel, Richard; Shamir, Adi (August 1981). "A $T = O(2^{n/2} )$, $S = O(2^{n/4} )$ Algorithm for Certain NP-Complete Problems" (in en).
*SIAM Journal on Computing***10**(3): 456–464. doi:10.1137/0210033. ISSN 0097-5397. http://epubs.siam.org/doi/10.1137/0210033. - ↑
^{25.0}^{25.1}Vazirani, Vijay. Approximation Algorithms. Springer-Verlag Berlin Heidelberg, 2003. - ↑ Dantzig, George B. (1957). "Discrete-Variable Extremum Problems".
*Operations Research***5**(2): 266–288. doi:10.1287/opre.5.2.266. - ↑ Calvin, James M.; Leung, Joseph Y. -T. (1 May 2003). "Average-case analysis of a greedy algorithm for the 0/1 knapsack problem".
*Operations Research Letters***31**(3): 202–210. doi:10.1016/S0167-6377(02)00222-5. - ↑ Chang, T. J., et al. Heuristics for Cardinality Constrained Portfolio Optimization. Technical Report, London SW7 2AZ, England: The Management School, Imperial College, May 1998
- ↑ Chang, C. S., et al. "Genetic Algorithm Based Bicriterion Optimization for Traction Substations in DC Railway System." In Fogel [102], 11-16.
- ↑ Kulik, A.; Shachnai, H. (2010). "There is no EPTAS for two dimensional knapsack".
*Inf. Process. Lett.***110**(16): 707–712. doi:10.1016/j.ipl.2010.05.031. https://www.cs.technion.ac.il/~hadas/PUB/multi_knap.pdf. - ↑
^{31.0}^{31.1}^{31.2}Cohen, R. and Grebla, G. 2014. "Multi-Dimensional OFDMA Scheduling in a Wireless Network with Relay Nodes". in*Proc. IEEE INFOCOM’14*, 2427–2435. - ↑ Yan Lan, György Dósa, Xin Han, Chenyang Zhou, Attila Benkő [1]:
*2D knapsack: Packing squares*, Theoretical Computer Science Vol. 508, pp. 35–40. - ↑ Chandra Chekuri and Sanjeev Khanna (2005). "A PTAS for the multiple knapsack problem".
*SIAM Journal on Computing***35**(3): 713–728. doi:10.1137/s0097539700382820. - ↑ Wu, Z. Y.; Yang, Y. J.; Bai, F. S.; Mammadov, M. (2011). "Global Optimality Conditions and Optimization Methods for Quadratic Knapsack Problems".
*J Optim Theory Appl***151**(2): 241–259. doi:10.1007/s10957-011-9885-4. - ↑ Gallo, G.; Hammer, P. L.; Simeone, B. (1980).
*Quadratic knapsack problems*.**12**. 132–149. doi:10.1007/BFb0120892. ISBN 978-3-642-00801-6. - ↑ Witzgall, C. (1975). "Mathematical methods of site selection for Electronic Message Systems (EMS)".
*NASA Sti/Recon Technical Report N*(NBS Internal report)**76**: 18321. Bibcode: 1975STIN...7618321W. - ↑ Richard M. Karp (1972). "Reducibility Among Combinatorial Problems". In R. E. Miller and J. W. Thatcher (editors). Complexity of Computer Computations. New York: Plenum. pp. 85–103
- ↑ Caprara, Alberto; Kellerer, Hans; Pferschy, Ulrich (2000). "The Multiple Subset Sum Problem".
*SIAM J. Optim.***11**(2): 308–319. doi:10.1137/S1052623498348481. http://www.or.deis.unibo.it/alberto/mssp_siam.ps. - ↑ Abed, Fidaa; Chalermsook, Parinya; Correa, José; Karrenbauer, Andreas; Pérez-Lantero, Pablo; Soto, José A.; Wiese, Andreas (2015).
*On Guillotine Cutting Sequences*. pp. 1–19. doi:10.4230/LIPIcs.APPROX-RANDOM.2015.1. ISBN 978-3-939897-89-7. https://kops.uni-konstanz.de/handle/123456789/33469.

## References

- {{Cite book

| first = Michael R. | last = Garey |author2=David S. Johnson | year = 1979 | title = Computers and Intractability: A Guide to the Theory of NP-Completeness | publisher = W.H. Freeman | isbn = 978-0-7167-1045-5

- Kellerer, Hans; Pferschy, Ulrich; Pisinger, David (2004).
*Knapsack Problems*. Springer. doi:10.1007/978-3-540-24777-7. ISBN 978-3-540-40286-2. - Martello, Silvano; Toth, Paolo (1990).
*Knapsack problems: Algorithms and computer implementations*. Wiley-Interscience. ISBN 978-0-471-92420-3. http://www.or.deis.unibo.it/knapsack.html.

## External links

- Lecture slides on the knapsack problem
- PYAsUKP: Yet Another solver for the Unbounded Knapsack Problem, with code taking advantage of the dominance relations in an hybrid algorithm, benchmarks and downloadable copies of some papers.
- Home page of David Pisinger with downloadable copies of some papers on the publication list (including "Where are the hard knapsack problems?")
- Knapsack Problem solutions in many languages at Rosetta Code
- Dynamic Programming algorithm to 0/1 Knapsack problem
- Knapsack Problem solver (online)
- Solving 0-1-KNAPSACK with Genetic Algorithms in Ruby
- Codes for Quadratic Knapsack Problem
- Optimizing Three-Dimensional Bin Packing
- Knapsack Integer Programming Solution in Python Gekko (optimization software)

Original source: https://en.wikipedia.org/wiki/Knapsack problem.
Read more |