# What's the time complexity bound for the Knapsack with real weights?

Let $$W$$ be the total weight of our bag, $$1,...,n$$ be our elements, $$w_1,...,w_n$$ their corresponding weights, and $$v_1,...,v_n$$ their corresponding values.

As is known, the knapsack problem for integer weights can be solved by dynamic programming (or equivalently, using recursion + memoization), with time complexity of $$\mathcal O(nW)$$, where $$W$$ is the total weight our bag can hold, and $$n$$ is the number of items.

However, for real numbered weights, the algorithm fails (just imagine a billion items with weight $$<1$$, and $$W=1$$). We probably can round them in a clever way and then scale them up to be integers again, but that'd blow the size of the problem up in an inacceptable way.

Another approach would be to use recursion instead of dynamic programming, because the idea behind the algorithm still works out - either the $$n$$'th item is in the optimal solution, or not.

However, even with memoization, we can easily choose our weights in a way so that all sums \sum_{ \begin{align} i&\in M\\ M&\subseteq \{1,..,n\} \end{align}} w_i

are distinct, leaving us with $$2^n$$ weight-sums to memoize.

Is there any optimal algorithm that manages to keep the time complexity pseudopolynomial?

• It is taken for granted in the question that "pseudo-polynomial" time-complexity in case of real numbers is well-known. However, is it even defined anywhere? It looks like it is non-trivial to define that kind of time-complexity. – John L. Jan 1 '19 at 9:19

If you have n random real numbers, then you have 2^n possible real sums. If your knapsack has size S, you would expect 2k different sums in an interval $$S-k\cdot 2^{-n}, S+k\cdot 2^{-n}]$$, in other words calculating sums with a precision of $$k\cdot 2^{-n}$$ isn't enough to give the optimal sum.

With random integers, you can find an optimal sum because of the $$2^n$$ possible sums, there will be gazillions getting the same result. You wouldn't be able to list all choices that fill the knapsack exactly because there are to many of them, but you can find one.

You can most likely solve the problem "find items that fit in the knapsack, and have a total size at most eps less than the optimal size".

• So in other words, there might be an approximation algorithm running in polynomial time, but you doubt the existence of a general algorithm? – Sudix Dec 8 '18 at 22:02

I don't know if you read the book

Algorithm Design - Jon Kleinberg, Eva Tardos

you can read this(at Extension: The Knapsack Problem pag 271) :

Using this recurrence, we can write down a completely analogous dynamic programming algorithm, and this implies the following fact

Thats help to maintain the pseudopolinomial time in $$\Theta(nW)$$ time.