1

Dynamic programming algorithm for Knapsack is stated to have complexity $\mathcal O (nW)$.

However, I've also seen the complexity stated as $\mathcal O (n^2V)$, where $V=\max v_i$.

(Here $n$ is the number of items and $W$ the weight limit).

I see from the algorithm that the first complexity measure is correct: http://en.wikipedia.org/wiki/Knapsack_problem

Can someone tell me, why the other complexity measure works ?

Raphael
  • 72,336
  • 29
  • 179
  • 389
Shuzheng
  • 511
  • 2
  • 16

1 Answers1

2

The first complexity measure is in terms of the target weight, the second in terms of the heaviest element. Since $W \leq nV$ (or rather, we can assume that $W \leq nV$), the first estimate $O(nW)$ implies the second $O(n^2V)$. So $O(nW)$ is a stronger estimate than $O(n^2V)$.

Yuval Filmus
  • 276,994
  • 27
  • 311
  • 503
  • Thank you, excellent answer. Now, the approximation algorithm (with $v_i$ scaled) runs in polynomial size in the input encoded as binary (not a pseudo polynomial), because the size $n$ and the size of the input is polynomially related ? – Shuzheng Jun 02 '15 at 13:59
  • I have no idea what "the approximation algorithm" is, but I'm sure you can answer your question yourself and don't need my help. – Yuval Filmus Jun 02 '15 at 14:02