算法导论作业3答案_第1页
算法导论作业3答案_第2页
算法导论作业3答案_第3页
算法导论作业3答案_第4页
算法导论作业3答案_第5页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、Introduction to Algorithm s Day 14 Massachusetts Institute of Technology 6.046J/18.410J Singapore-MIT Alliance SMA5503 Professors Erik Demaine, Lee Wee Sun, and Charles E. Leiserson Handout 17Problem Set 3 SolutionsMIT students: This problem set is due in lecture on Day 11.Reading: Chapters 8 and 9B

2、oth exercises and problems should be solved, but only the problems should be turned in. Exercises are intended to help you master the course material. Even though you should not turn in the exercise solutions, you are responsible for material covered by the exercises.Mark the top of each sheet with

3、your name, the course number, the problem number, your recitation instructor and time, the date, and the names of any students with whom you collaborated. MIT students: Each problem should be done on a separate sheet (or sheets of three-hole punched paper.You will often be called upon to “give an al

4、gorithm” to solve a certain problem. Your write-up 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, pseudo

5、code.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 co

6、nvoluted and obtuse descriptions.Exercise 3-1. Do exercise 8.1-2 on page 167 of CLRS.Exercise 3-2. Do exercise 8.1-3 on page 168 of CLRS.Exercise 3-3. Do exercise 8.2-3 on page 170 of CLRS.Exercise 3-4. Do exercise 8.4-2 on page 177 of CLRS.Exercise 3-5. Do exercise 9.3-1 on page 192 of CLRS.Exercis

7、e 3-6. Show that the second smallest of n elements can be found with n+(lg ncomparisons in the worst case. (Hint: Also nd the smallest element.Problem 3-1. Largest i numbers in sorted orderGiven a set of n numbers, we wish to nd the i largest in sorted order using a comparison-based algorithm. Find

8、the algorithm that implements each of the following methods with the best asymptotic worst-case running time, and analyze the running times of the algorithms in terms of n and i.(a Sort the numbers, and list the i largest.Solution:Use any optimal sorting algorithm, such as MergeSort or HeapSort. The

9、n this can bedone in (n lg n.(b Build a max-priority queue from the numbers, and call E XTRACT-M AX i times.Solution:Call Build-Heap, (n. Then call Extract-Max, (lg i, i times. So, total runningtime is (n+i lg i.(c Use an order-statistic algorithm to nd the i th largest number, partition around that

10、number, and sort the i largest numbers.Solution:Select the i-th largest number using SELECT, (n, call partition, (n, and then sortthe i largest numbers, (i lg i. So our algorithm takes (n+i lg i.Problem 3-2. At the wading poolYou work at a summer camp which holds regular outings for the n children w

11、hich attend. One of these outings is to a nearby wading pool which always turns out to be something of a nightmare at the end because there are n wet, cranky children and a pile of 2n shoes (n left shoes and n right shoes and it is not at all clear which kids go with which shoes. Not being particula

12、rly picky, all you care about is getting kids into shoes that t. The only way to determine if a shoe is a match for a child is to try the shoe on the childs foot. After trying on the shoe, you will know that it either ts, is too big, or is too small. It is important to note that you cannot accuratel

13、y compare childrens feet directly with each other, nor can you compare the shoes. You know that for every kid, there are at least two shoes (one left shoe and one right shoe that will t, and your task is to shoe all of the children efciently so that you can go home. There are enough shoes that each

14、child will nd a pair which ts her. Assume that each comparison (trying a shoe on a foot takes one time unit.(a Describe a deterministic algorithm that uses (n 2 comparisons to pair children withshoes.Solution:For each child, try on all the shoes until you nd the two shoes that t. T (n = T (n 2 + O (

15、n = (n 2.(b Prove a lower bound of (n lg n for the number of comparisons that must be madeby an algorithm solving this problem. (Hint: How many leaves does the decision tree have?Solution:There are n ! ways that left shoes can be assigned to children and n ! ways that right shoes can be assigned to

16、children. So the decision tree should have n !2 leaves. n !2 n ! h lg(n !h lg ( ne n by Stirlings Approximation = n lg n n lg e= (n lg n (c How might you partition the children into those with a smaller shoe size than a “pivot”child, and those with a larger shoe size than the pivot child?Solution:Ta

17、ke the pivot child and try on all the shoes until you nd one that ts. This should take (n time as there are 2n shoes. Then try the shoe on all the children. If the shoe is too small, then they have larger feet than the pivot child. If the shoe is too big, then they have smaller feet than the pivot c

18、hild. This should also take (n time making our partition algorithm run in linear time.(d Give a randomized algorithm whose expected number of comparisons is O (n lg n ,and prove that this bound is correct. What is the worst-case number of comparisons for your algorithm?Solution:This is similar to quicksort. Pick a random child. Partition the children around that child as in part (c. Then take the shoe you used to partition the children and partition the shoes around it. Take the

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论