This preview shows page 1. Sign up to view the full content.
Unformatted text preview: Introduction to Algorithms Massachusetts Institute of Technology Professors Shaﬁ Goldwasser and Silvio Micali September 12, 2003 6.046J/18.410J Handout 8 Problem Set 2
This problem set is due in lecture on Wednesday, September 24. Reading: Chapters 2.3, 5.1–5.3, 8.1–8.3, 28.2, 30.1. 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. You will often be called upon to “give an algorithm” to solve a certain problem. Your writeup should take the form of a short essay. A topic paragraph should summarize the problem you are solving and what your results are. The body of your essay should provide the following: 1. A description of the algorithm in English and, if helpful, pseudocode. English explanations should be used liberally in your pseudocode. For convenience, pseudocode may use standard arithemetic and logical operations, as well as loops and data structures like arrays. Pseudocode should be understandable to anyone who can program–it should not be ready to compile! 2. At least one worked example or diagram to show more precisely how your algorithm works. 3. A proof (or indication) of the correctness of the algorithm. 4. An analysis of the running time of the algorithm. Remember, your goal is to communicate. Graders will be instructed to take off points for convoluted and obtuse descriptions. 2 Problem 21. Goldilocks and the
¡ Handout 8: Problem Set 2
bears. Once upon a time, there was a little girl named Goldilocks. She went for a walk in the forest. Pretty soon, she came upon a house. She knocked and, when no one answered, she walked right in. At the table in the kitchen, there were three bowls of porridge. Goldilocks was hungry. She tasted the porridge from the ﬁrst bowl. “This porridge is too hot!” she exclaimed. So, she tasted the porridge from the second bowl. “This porridge is too cold,” she said. So, she tasted the last bowl of porridge. “Ahhh, this porridge is just right,” she said happily and she ate it all up.
In an unfortunate accident at a laboratory in Building 68, a little girl named Goldilocks wandered into a human cloning machine. As a result, there are now little Goldilocks (Goldilocks , Goldilocks , . . . ) wandering around the halls of MIT. Because cloning remains an imperfect technology, each Goldilock has a temperature preference distinct from that of all the other Goldilocks. Luckily, you also have managed to ﬁnd bowls of porridge, where bowl is kept at temperature . It is a great stroke of fortune that for each Goldilocks , there is exactly one bowl so that , and for each bowl there is exactly one Goldilocks whose temperature preference . That is, the two sets and are equal. Your job is to match Goldilocks to a bowl of porridge such that . When you give bowl to Goldilocks , she says:
©¨ B ¤ C6¦¥ ¤ ¤¥ © ¨ © ¨ ©¨ ¤ ¦¥ § § ¤ 0 ¡ % 3 % # ! ¤¥ 6)54&$12 ¤ ¤¥ © 47¨ ¡ ¤ 0¡ % § % #! © 1)('&$"¦¨ § § § ¢ ¡ ¤ ¦¥ ¤ £ Call any one such tasting a trial. You may only use trials to get information about temperatures of the bowls or temperature preferences of the Goldilocks. You may not directly compare the temperatures of two bowls or the temperature preferences of two Goldilocks.
X ¡ U SR ¡P YWVT"Q(I (a) Give a randomized algorithm for which the expected number of trials is
¤ ¥ ` © Fa¨ § ¤ (b) Suppose that when you give Goldilocks bowl with , she spits out a spoonful and says “Yuck!” (without indicating whether it’s too hot or too cold). Give the fastest algorithm you can to match Goldilocks to bowls in this scenario. G©¨ ¤ HFE¦¥ ©¨ D ¤ C6¦¥ 89 9A @ “This porridge is too hot!” if “This porridge is too cold!” if “This porridge is just right!” if . Handout 8: Problem Set 2
Problem 22. Hitting the target. 3 Give an algorithm for the following problem: Given a target integer and an array of integers such that for every , determine if there exists a pair such that . Problem 23. Beating the sorting lower bound? (a) You are a spammer. You have an array with entries, each containing information about a person. Each person is from one of different families, and there are exactly members of each family in the array. (You may assume that each family has a unique last name.) You would like to sort the entries in alphabetical order according to last name, ﬁrst name , using a comparisonbased sorting algorithm. You know how to sort using comparisons, but since is truly massive in size, you would like to sort the data faster. So you hire King Arthur Anderson Consulting to address your problem. The Anderson Consultant claims that he can sort using comparisons! He says he can do this because the array is partially sorted in the following sense: (i) Entries correspond to the members of one of the families, entries in correspond to the members of another family, etc. In other words, entries in for correspond to all the members of one of the famillies. (ii) The families are already in alphabetical order according to last name. In other words, the last name of the family comes alphabetically before the last name of the family , etc. In general, the last name of the family comes alphabetically before the last name of the family for all . You verify that (i) and (ii) are indeed correct. Should you believe his claim for all values of ? If not, for what values of is it possible to sort using comparisons? Prove your answer. (b) Anderson goes bankrupt, and loses all of your data. You have a scrambled backup copy of , so you can no longer assume that (i) and (ii) hold. You hire another consultant from ConsultingAgency.com to sort the data. She invents new algorithms that are not comparison based. She claims that she can sort your data in time. Should you believe her? Prove your answer. (c) After proceedings in bankruptcy court, you recover your partially sorted array. The ConsultingAgency.com consultant says that she can also use her algorithms on this partially sorted array for any values of . For what values of should you believe her claim? Prove your answer.
X ¡P Ycg e X ¡ U SR ¡P Y{VT"Qg e % 3 % # 4xwv3 B 3 % # w{wz3 q ¡ u 3 G G G # ¡ X # q 3Ph kVYi$jtwsVY¦Hrcpe q ¡ G G G #h kVfiVhe q ¡ m G G G # ¡h
VfoniVyVfe q ¡ X # # P G G G # ¡ u 3h kwf¦245¦HtV4vVY26e q ¡ u 3 G G G # ¡ X # q 3Ph kVfH6jtVzVY¦H)6cpe q ¡ m G G G # ¡h
VfonVlaVfe q ¡ G G G #h kwfjiVhe VY¡ X ¡ U SR ¡P fwT"cg e e ¡ ¡ e X ¡ U SR ¡P fVe"cd VY¡ e d ¡ gygx# %3 % d q h e q 3h )V§ e svuus VwVVtrcpig4f % q 3h e % 0 ¡GGG # § 6V
b3 X ¡P YcbI ¡ 4 Problem 24. Optimal investment strategies (with insider trading). Handout 8: Problem Set 2 You are given an array of integers. Entry is the stock price of BIM on day . Your goal is to make the most money that you can by buying BIM once and subsequently selling once during this day period.
q }h He } q h e q q }h t $Hpie qh i e For any , you can buy shares of BIM on day at price You need to ﬁnd two indices and such that and
~ s} X £ ¡P Q(I } } ~ y} and sell on day at price is maximized.
It is easy to ﬁnd the best and in time , simply by iterating over all possible pairs and . This question asks you to give a more efﬁcient algorithm.
e (a) Give an algorithm to solve this problem. Your algorithm should take input, and produce two indices and such that is maximized. (b) Optional extra credit: Give a linear time algorithm for this problem.
q h e q }h i q e ~ s} X ¡ U SR ¡P fVe"c(I as } 3 q 3h cpie ¡ e ¡ . ...
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.
 Fall '01
 CharlesE.Leiserson
 Algorithms

Click to edit the document details