Homework Help Question & Answers

You have a bag that can hold 100kg of fruit. You are in a shop that...

You have a bag that can hold 100kg of fruit. You are in a shop that sells (an unlimited supply of) pumpkins, watermelons, and squash, weighing 10, 2, and 1kg each, respectively. Give a greedy algorithm to fill your bag with the smallest number of fruits/vegetables (eg, buying a watermelon is preferable to buying two squash, etc). The total weight has to be exactly 100kg. Argue that this will return you the optimal solution. Argue that, given this fruit/vegetable collection, you can completely fill a bag of any weight capacity (ie, you can realize any weight, not just 100kg).

0 0
Add a comment
Answer #1

Solution:

As given in the problem, we have an unlimited amount of supply.

This means that we can take as many weighted items we can and then rest of the items with smaller weight in the decreasing order in order to produce the minimum number of fruits.

the algorithm is given below:

  1. Begin
  2. sort the weighing in the non-increasing order
  3. call this array as fruit array
  4. for i= 0 to n-1 // Where n is the number of fruits
  5. numberOfFruits += bagWeight/fruit[i]
  6. bagWeight= bagWeight%fruit[i];
  7. end
  8. end

The given algorithm is first filling all the most weighted fruits/vegetables in the bag then the next weighted item is placed in the bag, the given approach is a greedy approach and it will always produce an optimal solution.

Suppose the bag weight is 113 then our algorithm will return

113/10= 11

numberOfFruits = 11

then

113%10= 3

numberOfFruits = 11 + (3/2)= 11 + 1= 12

then 1 is remining

which means

numberOfFruits = 12 + 1= 13

Please, please upvote and ask your doubts in the comments.

Add a comment
Know the answer?
Add Answer to:
You have a bag that can hold 100kg of fruit. You are in a shop that...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coin

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Assume that you have 6 different types of vegetables and 9 different types of fruit

    Assume that you have 6 different types of vegetables and 9 different types of fruit. A vegetable salad consists of a mixture of any 3 types of vegetables, and a fruit salad consists of a mixture of 3 kinds of fruit.(1) In how many different ways can you prepare a vegetable salad and a fruit salad? (2) In how many different ways can you prepare a vegetable salad or a fruit salad but not both? (3) How many ways can...

  • Could you convert this PDF to HTML format using styles for massgeneral.org ?   Here’s a...

    Could you convert this PDF to HTML format using styles for massgeneral.org ?   Here’s a good example of a page you can copy HTML from:http://www.massgeneral.org/children/turner-syndrome/turner-syndrome-mosaic.aspx. When you’re done, you can send me the HTML and I’ll pop it into a site template and we can take a look. PS You don’t have to build anything from scratch for this—it’s just about tagging the subheads and the bulleted lists, etc. and getting that “Did you know?” box into the same format...

  • Need answers. thank you VOCABULARY BUILDER Misspelled Words Find the words below that are misspelled; circle...

    Need answers. thank you VOCABULARY BUILDER Misspelled Words Find the words below that are misspelled; circle them, and then correctly spell them in the spaces provided. Then fill in the blanks below with the correct vocabulary terms from the following list. amino acids digestion clectrolytes nutrients antioxident nutrition basal metabolic rate extracellulare oxydation calories fat-soluble presearvatives catalist glycogen processed foods cellulose homeostasis saturated fats major mineral coenzyeme trace minerals diaretics metabolism water-soluable 1. Artificial flavors, colors, and commonly added to...

Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.