3

How would we solve the knapsack problem if we now have to fix the number of items in the knapsack by a constant $L$? This is the same problem (max weight of $W$, every item have a value $v$ and weight $w$), but you must add exactly $L$ item(s) to the knapsack (and obviously need to optimize the total value of the knapsack).

Every way I've thought of implementing this so far (dynamic programming, brute force) has resulted in either failure, or lifetime-of-universe level computation times. Any help or ideas are appreciated.

Edit: I am looking for pseudo-polynomial time solutions

Bryce219
  • 48
  • 1
  • 5

2 Answers2

4

You can transform this problem into an instance of Knapsack. Let $n$ be the number of items, $V$ be the maximum value of an item and suppose that each item weighs at most $W$ (otherwise it can be discarded).

To ensure that you select at least $L$ items:

  • Add $n(V+1)$ to the value of every item.
  • Now the problem is equivalent to that of maximizing the number of selected items, breaking ties in favor of set of items with largest original total value, subject to weight constraints. There is a solution that selects at least $L$ items iff the optimal solution has a value of at least $n(V+1)L$.

To ensure that you select at most $L$ items:

  • Add $(W+1)$ to the weight of every item.
  • Add $L(W+1)$ to $W$.
  • Now every subset of more than $L$ items weighs at least $(L+1)(W+1) = L(W+1) + (W+1) > L(W+1) + W$, and is hence not feasible. Every subset of $L$ items that had an overall weight of $w$, now weighs $L(W+1) + w$, and hence is feasible iff $w \le W$. A subset with less than $L$ items might be feasible even if its overall weight is more than $W$, however its total value will always be smaller than $n(V+1)L$.
Steven
  • 29,724
  • 2
  • 29
  • 49
2

Define a matrix $M$ of size $n \times W \times L$. The entry $M[i][j][k]$ denotes the maximum value of the knapsack when exactly $k$ items are chosen from $\{1,\dotsc,i\}$ and the allowed capacity of the knapsack is $j$.

Final Solution: The value of $M[n][W][L]$ (assuming 1 based indexing).

Induction or DP: $M[i][j][k]$ can be stated in terms of smaller subproblem as follows: $M[i][j][k] = \max\,\{\,M[i-1][j-w_i][k-1] + v_i, \, M[i-1][j][k]\}$.

The first term denotes the scenario when the $i^{th}$ item is chosen in the knapsack, and the second term states the scenario when the $i^{th}$ item is not chosen in the knapsack.

Base Case:

  1. For any entry with $k > i$, $M[i][j][k] = -\infty$ since there are only $i$ available items and the desired quantity is $k$.
  2. For every $j \in \{1,\dotsc,W\}$, $i \in \{1,\dotsc,n\}$, and $k = i$, the value $M[i][j][i] = \sum_{p = 1}^{i} v_p$ if $\sum_{p = 1}^i w_p \leq j$ else $M[p][j][p] = -\infty$. In other words, if there are only $i$ available items and the required quantity is also $i$, then pick all the items in the knapsack if and only if the items satisfy the capacity constraint, i.e., sum of weights of the items is at most $j$.

Running Time: $O(W\cdot L \cdot n)$

Inuyasha Yagami
  • 6,277
  • 1
  • 12
  • 23