rmykkänen

notes

Knapsack problem

date

Knapsack is a core problem in dynamic programming. It's easy to understand, but hard to solve.

Suppose you are given nn items (vi,wi)(v_i, w_i) where viv_i is the value of item ii and wiw_i is the weight of item ii. You have capacity WW. Your goal is to maximize the value of items subject to having the sum of the weights be less than or equal to the capacity.

Solution

Let's start with a two-dimensional solution. T[i,j]T[i, j] needs to be an intermediary version of some capacity and some item, such that at the end we could index T[W,n]T[W, n] for the maximum value. Let's define our table T[i,j]T[i,j] where T[i,j]T[i,j] is the maximum profit with a capacity ii, and only using items 1..j1..j. In this variant, there is only one copy of an item. We can iteratively build up the smaller cells in the table to bigger items. Let's consider the last possible step for our recurrence. Given one more item, if you don't pick the item, the solution is equivalent to the value with the same capacity except for the item (T[W,n]=T[W,n−1])(T[W, n] = T[W, n-1]). If you do pick the item, your capacity goes up by wnw_n, so your prior capacity should be W−wnW - w_n, hence your new value is T[W,n]=T[W−wn,n−1]+vnT[W, n] = T[W-w_n, n-1] + v_n. This implies that your solution to the optimal value at some weight is either:

  • the optimal solution at the capacity without the weight of some new item plus the value of that item, or
  • the weight ignoring that item
T[i,j]={T[i,j−1]if wj>imax(T[i,j−1],T[i−wj,j−1]+vj)if wj≤i\begin{equation} T[i, j] = \begin{cases} T[i, j-1] & \text{if } w_j \gt i \\ max(T[i, j-1], T[i-w_j, j-1] + v_j) & \text{if } w_j \leq i \end{cases} \end{equation}

In the chain matrix multiplication problem, we have a minimum over some things. Here, we have a maximum over some things. We're not going to end up finding the items we've used using this table directly. Like in the chain matrix, all it requires is storing pointers to choices we made. This variant of the knapsack is the 0-1 Knapsack since you can choose either 0 or 1 amounts of any item.

Pseudocode

def knapsack(W, weights, values):
	n = len(weights)
	dp = [0; W+1][0; n+1]

	for j in 1..(n+1):
		for i in 1..(W+1):
			if weights[j-1] <= i:
				dp[i, j] = max(
					dp[i, j-1],
					dp[i-weights[j-1], j-1] + values[j-1]
				)
			else:
				dp[i, j] = dp[i, j-1]

	return dp[W, n]

Both the space and the runtime are O(nW)O(nW).