Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Standard view
Full view
of .
Look up keyword
Like this
0 of .
Results for:
No results containing your search query
P. 1


Ratings: (0)|Views: 966|Likes:
Published by skimdad

More info:

Published by: skimdad on Apr 19, 2010
Copyright:Attribution Non-commercial


Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less





0/1 Knapsack algorithm comparison
Jacek DzikowskiIllinois Institute of Technologye-mail: dzikjac@iit.eduAbstract:
The goal of this report was to implement compare four different 0/1 Knapsack algorithms (brute force,backtracking, best-first branch and bound, and dynamic programming) in terms of their performance on varioussets of data. All algorithms were first programmed using object-oriented programming language (C++) and thencarefully tested with specially generated data sets. The results that were gathered during these tests served as abasis for further analysis, discussion and drawing conclusions concerning the performance characteristics of allfour algorithms. Final discussion leads to the conclusion that backtracking method seems to be the best in certainconditions, while Brute Force is evidently the least efficient algorithm as expected.
www.iit.edu/~dzikjace-mail: dzikjac@iit.edu2
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
www.iit.edu/~dzikjace-mail: dzikjac@iit.edu3only if their bound (computed by means of additional function) is satisfactory/promising (highest bound isthe priority). The implementation utilises a simple while loop [while (Queue not empty)] to obtain the result.Optimal solution is computed similarly as in previous algorithms.
Dynamic Programming – here a two-dimensional dynamic programming is used (of size number of items*weight limit). It stores solutions to all sub-problems that may constitute an optimal solution. Thealgorithm starts by filling first row and column with zeros and then filling it until it finds the optimalsolution as in other dynamic programming algorithms. Filling the array is realised by means of two nestedloops and two if statements. Optimal profit will be then the last (bottom right) entry in the array andcorresponding weight is then extracted by a single while loop.The implementation of main functions representing these algorithms is presented in the appendices. Pseudo-codethat was a basis for the implementation of Backtracking and Best-First Branch and Bound algorithm can befound in [1].ObjectsThe program defines several objects:
item – a structure for storing item information (value, weight, value/weight ratio). Items are further stored inan Item_Array,
node – a class defining a State Space tree node for Backtracking and Best-First Branch and Boundalgorithms. It defines such variables as Profit, Weight, Level and Bound together with functions to set andextract these values. Additionally it defines overloading of relational operators for queueing purposes,
test – main object containing the array of items and implementation of all four algorithms together with theirsupporting functions and other auxiliary functions (loading data, sorting, etc,).
generator – a class defining the data set generator,
userInterface – a class defining the user interface (menus, timer).SortingAlgorithms other than Brute Force require its input (item) array to be sorted according to the value/weight ratioof the item. I implemented Quick Sort algorithm for this task. Sorting is done before the run-time is computed,hence it does not influence the overall performance measurement.Data sets:The generator mechanism that is included in the program generates a data file of the following format:Number_Of_ItemsItem_Number Item_Value Item_Weight (one to a line)Weight_Limit
101 12 432 8 483 62 464 2 115 7 486 87 977 27 58 77 599 24 8510 60 6145
The generator allows specifying the size of the sample and the range of values. Additionally, data can eitherstrongly correlated or uncorrelated.Measurements:All algorithms were tested using the same sets of data (generated with parameters: number of items n, range of values 100, type of data 1/3, seed 300). Number of items (5, 10, 15, 20, 30, 40, 80, 160) and type of data(Uncorrelated/Strongly Correlated) - variable. I decided to expand the range of number of items due to the fact,that Brute Force algorithm was not working for inputs with n>30 (my decimal to binary algorithm was notworking with data structures better than long).All tests (excluding Brute Force for n>10) were repeated 100 000 times to obtain better accuracy.

Activity (14)

You've already reviewed this. Edit your review.
1 hundred reads
1 thousand reads
Prabesh Luitel liked this
Ethan Liu liked this
Sridhar Sri liked this
velikeTezave liked this
MANIKANDANK liked this
Imran Shaikh liked this
phdahmad liked this
m_hashemian liked this

You're Reading a Free Preview

/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->