Given:

  • n items
  • Weights
  • Values

You can carry a total weight of at most . What is the most valuable combination of items that you can fit into your knapsack.

With Repetition

There is an unlimited supply of each item. We care about value per weight here. This is the shoplifter example.

Sub problem

  • Define , where
  • The goal is do compute
  • Suppose the optimal solution to includes item i
  • Then removing i from the solution must be an optimal solution to
  • Therefore,
  • The base case here is a knapsack of capacity has no value
K(0)=0
for w = 1...W:
	K(w) = max_{i:w_i<w}{K{w-w_i}+v_i}
return K(w)

The runtime here is due to the dynamic max operation within the loop. This problem scaled with the value of W so this is a pseudopolynomial algorithm.