I recently wrote a program to solve the backpack problem using python. It works great and generally follows the greedy algorithm (i.e. find out the best solution for every step until it’s end)
But I need to optimise it based on the greedy algorithm
(That is a part of my homework)
So could you please provide me some fundamental idea(s) to improve it?
Item Name Weight Profit
Ammunition 3.00 95.00
Bread 3.60 90.00
Firewood 2.50 56.00
Olive Oil 2.40 45.00
Water 3.70 67.00
Weapon 4.80 79.73
This is the output of my current project. The bag capacity has been limited to 20kg, the data cannot be changed, but I need a better idea to improve it. Thank you!
I am not sure about the code or the solution, but I think it is all related to “efficiency”
Here I will use the terms “space” and “weight” synonymously.
Once thing you can do is calculate the ratio
profit/weightfor each item. The difference in ratios times some space is the best possible improvement for that space. For example if you had empty space and might be able to squeeze some other item Z in if you rearranged, the maximum profit you could achieve from that space would be (Zratio-0ratio)*weight. You can thus generate a candidate solution based on the greedy algorithm, then use this to bound possible improvements. In general though you will want to approach this from a dynamic programming standpoint.