In this repo you can see three different Knapsack Problem solving methods(backtracking, branch and bound, brute force). You can find the source codes in the codes directory.
You should run code of each solution separately. In each run you are asked to enter the following inputs:
- Number of items in knapsack
- Value and weight of each item
- Capacity of knapsack
And the outputs you will see are:
- Total time spent to run
- Max Profit
- Final selected itesms Inputs are read from the text file and also outputs are stored in the text file.
-
Time: when the number of items is large, the backtracking method works better in terms of time and the program runs in less time, while the brute force method has the longest run time. If the weight of the knapsack and the number of items is low, the branch and bound method will be the fastest way to conclude.
-
Memory: all methods need 2n of memory to store the weights and values of the items. So, we compare the memory on the taken memory in calculating the max profit. If the knapsack capacity is less than 2, backtracking method with W * N memory, gets the least amount of memory, otherwise, the brute force method with 2 * n memory, gets the least memory. In the branch and bound method, in the worst case, 4 * 2 ^ (n - 1) + 2 ^ n space is needed to store the tree created in the queue, which is the highest amount of memory compared to the previous two methods.