Fundamentals of Algorithms Fall 2009 HW 4B Going to Turkey:

advertisement
Fundamentals of Algorithms Fall 2009 HW 4B
DUE: October 14 4pm, 2009
1. Going to Turkey: You are going for holiday in Turkey but you are having some problems packing. You
have made a list of all the items you might possibly take along with their weight and an estimate of their
subjective value:
Item
Weight (pounds)
Value
C++ Textbooks
12
4
Turkish phrasebook
1
2
Harry Potter VII
2
2
Tourist Guide
1
1
Laptop
4
10
The airline has a hard limit of 15 pounds. Use the Greedy algorithm for Knapsack to find the best set of
items to choose that maximize the value while still being less than 15 pounds.
SOLUTION
In the greedy algorithm we first compute and order the items by Value per Pound. We then
greedily add items until the limit is reached.
Item
C++ Textbooks
Turkish phrasebook
Harry Potter VII
Tourist Guide
Laptop
Weight (pounds)
12
1
2
1
4
Value
4
2
2
1
10
.33
2
1
1
2.5
We then add the items Laptop, Turkish Phrasebook, Harry Potter VII and Tourist Guide for a
total value of 15 and a total weight of 8. It turns out that this is the optimal solution in this case.
2. Turkish Departure Lounge:
You have an excellent holiday in Istanbul and are now preparing to return home. Your plane is leaving in
half an hour and you have exactly 12.10 Turkish Lira that you want to get rid of before you leave. At the
airport you encounter the following menu:
Chicken Sandwich
Cheese Burger
Super size Coke
Turkish Delight
Mystery Meat
Delightful Delight
2.15
2.75
3.35
3.55
4.20
5.80
Assuming you can only purchase one of each item, which items should you purchase in order to spend exactly
12.10 Lira? Use the enumerate all subsets algorithm to find the solution.
SOLUTION:
Pass
0
1
Sets of Size Pass
5.80
4.20
3.55
Items Added
5.80,4.20
4.20
5.80
5.80,2.75
Sum
10.00
10.00
10.00
12.10 (BINGO!)
3. Partition: On a random walk in DC you and your friend encounter a family of strangers who are facing
some difficulties. Being good Samaritans you help them out and they are so grateful they rewards with
several trinkets of substantial value. Unfortunately you are strapped for cash and want to sell your share so
that you may realize some profit. But first you have to divide up the trinkets with your friends. Here is a
list of the value of the items:
$127.00, $98.00, $158.00, $204.00, $142.00, $279.00
Use the enumerate all subsets algorithm to divide the list into two subsets that have items of equal value.
SOLUTION:
First we add up the numbers and divide by 2 to calculate the value of the limit which is 504.
Pass
0
1
Sets of Size Pass
279
204
158
142
127
Items Added
279,204
204
279
279
279
279,98
Sum
483
483
483
437
421
504 (BINGO!)
Download