You are on page 1of 7

GREEDY ALGORITHM

Part 1:

 You are about to sit down to a meal


 But you have a calorie budget, e.g., you don't want to consume more than
1500 calories

 Choosing what to eat is a knapsack problem

Procedure: write the following program using python.


Step 1:

Step 2:
Step 3:

Step 4:
Step 5:

Step 6: Testing with a value of 750 calories


Run the program.
Explain what are the food were selected? Who wins greedy of value, cost or density?

The greedy chosen on Value has the lower food selected because it only finds the largest value within the
list of objects and does try to put all large values on the knapsack, its full not maximizing the number of
items that is able to be put in the knapsack while greedy by cost and density contains the same items
taken from the difference greedy by density does values outputted in descending order meaning the
algorithm is maximizing the total values it can carry in the knapsack. In conclusion the winner in the
greedy by density for it searches the items efficiently maximizing all the possibilities.

Part 2. Change the value of calories to 1500


Explain what are the food were selected? Who wins greedy of value, cost or density?

In conclusion of part 2, by greedy summing the value and density it has the same total value of items
taken. Greedy by value has a high constraint because greedy value would pick all the items starting from
highest to lowest value and put it all in the knapsack until it reaches the limit. A reason why the bigger
constraint the more items it will put in the knapsack but of course this depends on the values of the items.
The greedy by cost has the lowest total value of items taken for this is because this starts from the lowest
to highest values and try to put it all in the knapsack same as the greedy by value for they are both not
maximizing all the possibilities they are putting the items base on one choice only in this case they are
only picking items to put in the knapsack based on either value or cost. While the greedy by density is
maximizing all the possibilities from the items cost and calories it efficiently picks what to put in the
knapsack that’s a reason for greedy by density wins this portion.

You might also like