The 0/1 Knapsack problem is an optimisation problem. The idea is to select an optimal (most valuable) subset of the set of all items (each characterised by its value and weight), knowing the maximum weight limit. Solutions tothis problem are widely used for example in loading/delivery applications.There are several existing approaches/algorithms to this problem. Obviously, and it will be proved later, all of them led to the same result, but the methods used are different. In this report the focus will be on the followingfour 0/1 Knapsack algorithms:
Brute Force – find optimal result by trying all existing item combinations,
Backtracking – use a State Space concept – find optimal solution by traversing the State Space tree andbacktracking when a node is not promising,
Best-First Branch and Bound – use a State Space concept – expand the nodes with highest bound first (best-first) to find the optimal solution,
Dynamic Programming – create and use the dynamic programming matrix to find the optimal solution.Obviously, using a different method implies difference in performance (usually also range of application). 0/1Knapsack algorithms are no different. All four presented algorithmic approaches should be different both interms of run-time and resources consumption.My approach to verify this statement and to find out which algorithm is the best (in given circumstances) was toprepare a computer program (using an object-oriented programming language) that allows to run all algorithmson a particular set of data and measure the algorithm performance.This program would be run in order to collect necessary data (run-time, array sizes, queue sizes, etc.) which willbe further used to analyse the overall performance of 0/1 Knapsack algorithms.Due to its nature, the Brute Force algorithm is expected to perform the worst [O(2
number of items
)]. On the other sidethe performance of Backtracking and Best-First Branch and Bound algorithms should be much better, howeverits performance is strongly dependant upon the input data set. Similar remark applies also to the DynamicProgramming approach, however here memory utilisation is predictable (more specific the matrix size ispredictable). Of course the cost of increased performance is in memory utilisation (recursive calls stack, queuesize or dynamic programming array).
Materials and methods
The implementation was prepared using Visual C++ object-oriented environment. Aside of four 0/1 Knapsack algorithms it includes an easy to use menu, data set generation, performance measurement and file loadingmechanisms. The menu allows specifying whether the user wants to load a ready made data file or create a newone (by specifying number of items, range of weights, type of data and seed). Once a file is prepared (either justgenerated or already existing) another menu allows choosing an algorithm to use. Furthermore the number of trials can be adjusted to eliminate zero-length run-times and increase the accuracy.Algorithm implementation:
Brute Force – this algorithm checks all the possible combinations of items by means of a simple for loop(from 1 to 2
number of items
). It utilises the binary representation of every combination/number from 1 to 2
number of items
(computed recursively) to calculate total profit and weight. At the same time total weight is beingverified whether it exceeds the Weight Limit or not. If so, total profit is 0. Once a better combination isdiscovered it is stored as a result.
Backtracking – this algorithm utilises the idea of virtual State Space tree. Every branch in this treecorresponds to one combination. Each node is expanded only if it is promising (additional function isimplemented for this purpose) otherwise the algorithm backtracks. It is a recursive algorithm – recursivecalls are executed only if the node is promising. Total weight and profit is computed accordingly and theoptimal solution stored similarly like in Brute Force algorithm.
Best-First Branch and Bound – another State Space tree algorithm, however its implementation and ideasare different. There is no recursion in my implementation of this algorithm. Instead it uses a priority queueto traverse along the State Space tree (and to keep several states in memory – unlike Brute Force andBacktracking algorithms, which keep only one state at the time). Here the nodes are enqueued into the queue