BİL409_hm4.docx

advertisement
Homework - 4
Your fourth assignment is to create a greedy algorithm for the knapsack
problem. In the knapsack problem, the set is defined by the set of objects to be
packed. 𝑤𝑖 is the weight of the object i. 𝑢𝑖 is the utility of the object i. C is the
capacity of the knapsack. The greedy algorithm chooses objects that need to be
packed to knapsack according to following rules.
1. First calculate 𝒖𝒊 /𝒘𝒊 for each object. By this way you can find utility per unit
weight.
2. Then you should sort the ratios maximum to minimum (decreasingly).
3. In third step you start to pack beginning from the object that has maximum
ratio by checking the capacity.
4. Calculate remaining capacity of knapsack and update capacity
5. If an object does not fit skip this object and go on with next object
6. You should repeat these steps 3-4-5 until no object is remaining or the
capacity of the knapsack is zero.
You should take total object number, capacity of the knapsack, weights and utilities as
an input from the user.
Example solution:
PS: You are not allowed to use worksheet functions and macro record options.
Your assignment due to 18/04/2016-23:59. The assignments that do not follow
these rules will be graded as “zero”. Follow these steps;
http://www.endustri.anadolu.edu.tr/DersDuyuru.aspx?dersId=78&duyuruId=1
374
Download