# Solving a knapsack problem with a lot of items

I am wondering what are the fastest ways(faster than classical dynamic programming) to solve the knapsack problem (to optimality) with $$n$$ items when $$n$$ is nearly equal to $$10000$$ ?

Apart from classic local search, is there any heuristics that are efficient ? Edit: I asked for both exact and approximation methods

• Hi Best_fit, I'm a bit puzzled (and judging from the comments below some other readers are as well) whether you need the optimal solution (in this case you can't use heuristics or approximation algorithms as they do not give you a optimality certificate) or whether "near optimal" solutions are ok? – JakobS Nov 20 '19 at 15:35
• Hi, I am asking for the two types of methods in fact, both approximate and exact. – Joffrey L. Nov 20 '19 at 17:50

For the knapsack problem, you just use the Pisinger's code. It implements an exact algorithm, it is the fastest algorithm known in the literature, and it is open-source: http://hjemmesider.diku.dk/~pisinger/codes.html

• Which of the algorithms in the linked page are you referring to? – David M. Nov 26 '19 at 4:20
• The COMBO algorithm is the standard problem. The rest are variation of it, more details are in the referenced papers/Pisinger's (et al) book on Knapsack Problems. – Tue Christensen Nov 26 '19 at 6:26

A comprehensive comparison of different approaches to solving the knapsack problem is given in the recent paper1 by Ezugwu et al., where the authors compare the performance of the following approaches both in small size and large size problems:

1. Genetic algorithms,
2. Simulated annealing,
3. Branch and bound,
4. Dynamic programming,
5. Greedy search algorithm,
6. Hybrid genetic algorithm-simulated annealing

This paper can be a good start point for your search.

(1) Ezugwu, Absalom E., et al. "A Comparative Study of Meta-Heuristic Optimization Algorithms for 0–1 Knapsack Problem: Some Initial Results." IEEE Access 7 (2019): 43979-44001.

• Because @Best_fit is interested in exact methods, points 3 and 4 seem most relevant. After checking out the paper, it is not yet clear to me if these algorithms are really used by Ezugwu et al. to find optimal solutions. This is because positive optimality gaps for both BB and DP are reported in some of the tables. – Kevin Dalmeijer Nov 20 '19 at 12:02
• @KevinDalmeijer the authors claimed that 6.approach is efficient and I mentioned in my answer that “this paper can be a good start point” to search for the actual answer. Also I tried to emphasize that some heuristics perform good in big size problems (as stated in the paper and asked in the question). But thank you very much for your effort in checking the paper and answer in detail. – Oguz Toragay Nov 20 '19 at 14:27
• I completely agree that this comparative study is a good starting point. I only wanted to point out a peculiarity that I found while reading it :) – Kevin Dalmeijer Nov 20 '19 at 14:51

You don't give that bit of information, but you might be able to use a far more efficient algorithm when knapsack size (let's call it $$S$$) is small enough (small enough to create an array of each possible value you could get) and all the items have positive (or zero) weight.

For example, if maximum knapsack size is $$10^7$$ units, you could easily create an array of that size.

This way you go from $$O(2^n)$$ to $$O(S\cdot n)$$. I believe that would be called memoization (I can paste my implementation if that's what you're looking for).