pset5

pset5 - Introduction to Algorithms Massachusetts Institute...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Introduction to Algorithms Massachusetts Institute of Technology Professors Shafi Goldwasser and Silvio Micali October 23, 2003 6.046J/18.410J Handout 17 Problem Set 5 This problem set is due in recitation on Friday, October 31. Reading: Chapters 15.1-15.5, 16.1-16.3. There are four problems. Each problem is to be done on a separate sheet (or sheets) of threehole punched paper. Mark the top of each sheet with your name, the course number, the problem number, your recitation section, the date, and the names of any students with whom you collaborated. Problem 2-1. Spam and Customer Service. You run a meat-processing plant in Cambridgeport. Unfortunately, due to a mixup with your suppliers in Peoria, you have accidentally produced a batch of spoiled sausages. When your customer service desk opens at 8am, there are customers waiting in the lobby to return the rotten sausages. Say that customer has sausages, and that all the ’s are distinct. Your underpaid customer service operator requires minutes to handle customer ’s complaint, for some constant . You cannot “partially process” a complaint—all of customer ’s sausage returns must be handled together—and only one customer can be handled at a time. Because you want to maintain the golden reputation of your company, you have decided to pay one dollar to each customer for every minute before ’s complaint is completely handled. For example, if customer 3 is handled first and customer 5 is handled second, you pay dollars to customer 3 and dollars to customer 5. Give an efficient algorithm to find the order in which the customers’ complaints should be processed so that the total cost to the company is minimized. Prove that your algorithm is optimal and analyze its running time. Problem 2-2. Splitsville. A split of (i) (ii) (iii) (iv) $ £ § ¢ ¢ "! #¥£   £ § § ¤ £ ¢ ¢ ¤ ¦£ ¡ ¤¦£¨©§ ¤ ¥£ ¢ ; ; ; and . p ¡'222'y 843¥E¥€i For example, the following are splits of banana: bnn ban banaa aaa ana n SH $' 2 2 2 ' CH $'AH TI4E¥E¥ERI4QPI$ F¤ $ ' 2 2 2 ' C¤ $ ' A¤ G643¥¥¥ED64#B1$ consists of two strings and 9 9 7¤ @86$ w u phc'2 2 2 '% ci r p a¢'2 2 2 '%¢ fv(t3¥¥¥(d(sqbQ3¥¥¥Pi u phc'2 2 2 '% ci x p a¢'2 2 2 '%¢ v(t3¥¥¥(d(sqbQ3¥¥¥Pi 5 $'222'0 $'% 643¥¥¥¥1)(&$ hc U ¨¨¨ U 0c U % (g`¦¦fX¥eVdc a¢ U ¨ ¨ ¨ U 0¢ U % bW`¦¦YXWVP¢ $ Let be a string , where for a finite alphabet . , where 2 Handout 17: Problem Set 5 Give an algorithm to determine if two strings and form a split of . You algorithm should take as input three strings, and , where is a string of symbols from the alphabet . If strings and form a split of , it should output “yes”. Otherwise, it should output “no”. Prove that your algorithm is correct and analyze its running time. (Your algorithm should run in time for some constant .) Problem 2-3. A Maki a Day ... To maintain your health, you have decided that you must eat a sushi meal a day for the remaining days, , of the fall semester. You have exactly two choices for acquiring a sushi meal: ¤ I’ ¡ 5’'222'0’'% 3¥¥¥3€Q(`’ "ˆ ¡ ‰‡†… 9 $ ¡ ‚  $ ‚ $ ' ' „Qƒ$ % `’ ‘ ‚  1. On day , you can buy a sushi meal (for consumption on day price . These prices are announced in advance, i.e. on day . % •’ % % –¤ ’ ' 2 2 2 ' 0 –¤ ’ ' % –¤ ’ ' ¤ t„GE¥E¥¥`G#„D“QR€’ ¤ “’ ¤ “’ ¤ `” ) from the supermarket at 2. On day , you can place an order with bulk-sushi.com to have a sushi meal delivered to you every evening for days . The cost of this contract is . Note that is fixed and does not vary from day to day. Since sushi is highly perishable, you cannot “stockpile” sushi meals; you must eat each sushi meal on the day that you get it. Also, because you hate to waste food and because you can only stomach exactly one sushi meal per day, you may not get two sushi meals on the same day. For example, you cannot order from bulk-sushi.com three days after placing a previous bulk-sushi.com order. Finally, you are not allowed to order from bulk-sushi.com on day if there are less than 12 days remaining (including day ), i.e. you can not have leftover sushi meals at the end of the day period. The problem is to find the cost of the optimal sushi-ordering schedule. (a) Consider the following greedy algorithm for this problem. This algorithm takes as input , the price of 12 sushi meals from bulk-sushi.com, and the prices , the price of a sushi meal from the supermarket on each of the days. At day , it checks if the total cost of buying a sushi meal from the supermarket for that day and ) is less than . If so, it buys each of the next 11 days (i.e. days through day a sushi meal for day at price and goes on to the next day. Otherwise, it orders 12 sushi meals (for day through day ) from bulk-sushi.com at price and goes on to day . Pseudocode for this algorithm is provided below. B UY S USHI G REEDILY ( ) 1 Let . 2 Go from day to day . 3 On day : 4 If is greater than the sum of through or if there are fewer than 12 days (meals) left, 5 Then buy a sushi meal from the supermarket, let and go to day . 6 Else and go to day . 7 Place an order with bulk-sushi.com, let 8 Output . % –¤ „G“’ 0% –¤ ‡„DI’ ¤”  f£d 1@Ee(‘ —  f£d k&Ed3‘ i f£d jEe(‘ i f£d jEd3‘ % % –¤ t„G`” ¤ 1” 5 ”' 2 2 2 ' % ”' `b3¥¥¥q™— 5 “’ h u f£d fgEe(‘ f£d Ee(‘ ¤ “’ — ¤ ’ 5 ”2 2 2 ' 0 ”'% `‡¥¥¥¥`b#ƒ” — — ¡ % % –¤ t„G“’ % % –¤ t„G“’ ¤ I’ ¤ `” ¤ “’ ¤ “’ 0% –¤ ‡„D€’ — ¤ ˜’ ¤ I’ ¡ — — Handout 17: Problem Set 5 Prove that B UY S USHI G REEDILY does not produce an optimal solution to the problem. (b) Give a dynamic programming algorithm to find the cost of an optimal sushi-ordering schedule. Prove your algorithm is correct and analyze its running time. (c) Modify your algorithm from part (b) to output an actual optimal sushi-ordering schedule. Problem 2-4. Printing Neatly. ¤ 3o ¤ l p 5 l'222'0 l'% nQ(¥¥¥¥4#ml 3 You are given a sequence of words out these words neatly in lines of total length contains its own whitespace. p , where word has length . You want to lay each, by choosing “nice” line breaks. Each word A neat line is one that contains close to characters (without going over). More precisely, A the badness—this is actually a technical term in LTEX—of a line is given by . The badness must be nonnegative; all lines must contain at most characters. For example with , one could lay out the following definition from Ambrose Bierce’s The Devil’s Dictionary in these ways: HATRED, n. A sentiment<->| appropriate to the<----->| occasion of another’s<-->| superiority.<----------->| or HATRED, n. A<----------->| sentiment appropriate to>| the occasion of<-------->| another’s superiority.<->| badness badness badness badness 13 1 10 3 ƒ u ‚€p p H l ' 2 2 2 ' % –¤ l '¤ q4E¥¥E#„GnQbl a ¤ } { p Eo Qa ~|zu H y xxv uts kwe“r badness badness badness badness 3 7 4 13 (a) The badness of a paragraph is the sum of the badnesses of each of the lines of the paragraph, except the last. Give the most efficient algorithm you can to lay out the given words in a single paragraph in a way that minimizes the badness of the paragraph. Prove your algorithm is correct and analyze its running time. (b) Consider the following alternative definition: the badness of a paragraph is the sum of the cubes of the badnesses of all lines by the paragraph, except the last. Is your algorithm from part (a) still optimal? Give a proof or a counterexample. (c) Consider a third definition: the badness of a paragraph is the maximum badness of any line in the paragraph other than the last. Give a dynamic programming algorithm to minimize the badness of a paragraph. Prove your algorithm is correct and analyze its running time. 5 l'222'% nQ3¥¥¥#©l ...
View Full Document

This note was uploaded on 01/01/2011 for the course CS 5503 taught by Professor Charlese.leiserson during the Fall '01 term at MIT.

Ask a homework question - tutors are online