算法设计与分析基础课后习题答案solu3.docx
- 【下载声明】
1. 本站全部试题类文档,若标题没写含答案,则无答案;标题注明含答案的文档,主观题也可能无答案。请谨慎下单,一旦售出,不予退换。
2. 本站全部PPT文档均不含视频和音频,PPT中出现的音频或视频标识(或文字)仅表示流程,实际无音频或视频文件。请谨慎下单,一旦售出,不予退换。
3. 本页资料《算法设计与分析基础课后习题答案solu3.docx》由用户(最好的沉淀)主动上传,其收益全归该用户。163文库仅提供信息存储空间,仅对该用户上传内容的表现方式做保护处理,对上传内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知163文库(点击联系客服),我们立即给予删除!
4. 请根据预览情况,自愿下载本文。本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
5. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007及以上版本和PDF阅读器,压缩文件请下载最新的WinRAR软件解压。
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 算法 设计 分析 基础 课后 习题 答案 solu3
- 资源描述:
-
1、This le contains the exercises, hints, and solutions for Chapter 3 of the book ”Introduction to the Design and Analysis of Algorithms,” 2nd edition, byA. Levitin. The problems that might be challenging for at least some students are marked by ; those that might be dicult for a majority of students a
2、re marked by .Exercises 3.11. a. Give an example of an algorithm that should not be considered an application of the brute-force approach.b. Give an example of a problem that cannot be solved by a brute-force algorithm.2. a. What is the eciency of the brute-force algorithm for computing an as a func
3、tion of n? As a function of the number of bits in the binary representation of n?b. If you are to compute an mod m where a 1 and n is a large positive integer, how would you circumvent the problem of a very large magnitude of an?3. For each of the algorithms in Problems 4, 5, and 6 of Exercises 2.3,
4、 tell whether or not the algorithm is based on the brute-force approach.4. a. Design a brute-force algorithm for computing the value of a polynomialp(x)= anxn + an1xn1 + . + a1x + a0at a given point x0 and determine its worst-case eciency class.b. If the algorithm you designed is in (n2), design a l
5、inear algorithm for this problem.c. Is it possible to design an algorithm with a better than linear eciency for this problem?5. Sort the list E, X, A, M, P, L, E in alphabetical order by selection sort.6. Is selection sort stable? (The denition of a stable sorting algorithm was given in Section 1.3.
6、)7. Is it possible to implement selection sort for linked lists with the same(n2) eciency as the array version?8. Sort the list E, X, A, M, P, L, E in alphabetical order by bubble sort.9. a. Prove that if bubble sort makes no exchanges on its pass through a list, the list is sorted and the algorithm
7、 can be stopped.28b. Write a pseudocode of the method that incorporates this improve- ment.c. Prove that the worst-case eciency of the improved version is quadratic.10. Is bubble sort stable?11. Alternating disks You have a row of 2n disks of two colors, n dark and n light. They alternate: dark, lig
8、ht, dark, light, and so on. You want to get all the dark disks to the right-hand end, and all the light disks to the left-hand end. The only moves you are allowed to make are those which interchange the positions of two neighboring disks.Design an algorithm for solving this puzzle and determine the
9、number of moves it makes. Gar99, p.75Hints to Exercises 3.11. a. Think of algorithms that have impressed you with their eciency and/or sophistication. Neither characteristic is indicative of a brute- force algorithm.b. Surprisingly, it is not a very easy question to answer. Mathemati- cal problems (
10、including those you have studied in your secondary school and college courses) are a good source of such examples.2. a. The rst question was all but answered in the section. Expressing the answer as a function of the number of bits can be done by using the formula relating the two metrics.b. How can
11、 we compute (ab) mod m?3. It helps to have done the exercises in question.4. a. The most straightforward algorithm, which is based on substituting x0into the formula, is quadratic.b. Analyzing what unnecessary computations the quadratic algorithm does should lead you to a better (linear) algorithm.c
12、. How many coecients does a polynomial of degree n have? Can one compute its value at an arbitrary point without processing all of them?5. Just trace the algorithm on the input given. (It was done for another input in the section.)6. Although the majority of elementary sorting algorithms are stable,
13、 do not rush with your answer. A general remark about stability made in Section 1.3, where the notion of stability is introduced, could be helpful, too.7. Generally speaking, implementing an algorithm for a linked list poses prob- lems if the algorithm requires accessing the lists elements not in a
14、sequen- tial order.8. Just trace the algorithm on the input given. (See an example in the section.)9. a. A list is sorted if and only if all its adjacent elements are in a correct order. Why?b. Add a boolean ag to register the presence or absence of switches.c. Identify worst-case inputs rst.10. Can
15、 bubble sort change the order of two equal elements in its input?11. Thinking about the puzzle as a sorting-like problem may and may not lead you to the most simple and ecient solution.Solutions to Exercises 3.11. a. Euclids algorithm and the standard algorithm for nding the binary representation of
16、 an integer are examples from the algorithms previously mentioned in this book. There are, of course, many more examples in its other chapters.b. Solving nonlinear equations or computing denite integrals are ex- amples of problems that cannot be solved exactly (except for special in- stances) by any
17、 algorithm.2. a. M (n)= n 2b where M (n) is the number of multiplications made by the brute-force algorithm in computing an and b is the number of bits inthe ns binary representation. Hence, the eciency is linear as a function of n and exponential as a function of b.b. Perform all the multiplication
18、s modulo m, i.e.,ai mod m = (ai1 mod m a mod m) mod m for i = 1, ., n.13. Problem 4 (computes 艺 n i2): yesProblem 5 (computes the range of an arrays values): yes Problem 6 (checks whether a matrix is symmetric): yes4. a. Here is a pseudocode of the most straightforward version:Algorithm BruteForcePo
19、lynomialEvaluation(P 0.n, x)/The algorithm computes the value of polynomial P at a given point x/by the “highest-to-lowest term” brute-force algorithm/Input: Array P 0.n of the coecients of a polynomial of degree n,/stored from the lowest to the highest and a number x/Output: The value of the polyno
20、mial at the point x p 0.0for i n downto 0 dopower 1for j 1 to i dopower power x p p + P i powerreturn pWe will measure the inputs size by the polynomials degree n. The ba- sic operation of this algorithm is a multiplication of two numbers; the number of multiplications M (n) depends on the polynomia
21、ls degree only.Although it is not dicult to nd the total number of multiplications in this algorithm, we can count just the number of multiplications in the algorithms inner-most loop to nd the algorithms eciency class:2ninM (n)= 区区1= 区i = n(n + 1) (n2).i=0 j=1i=0b. The above algorithm is very ineci
22、ent: we recompute powers of x again and again as if there were no relationship among them. Thus, the obvious improvement is based on computing consecutive powers more eciently. If we proceed from the highest term to the lowest, we could computexi1 by using xi but this would require a division and he
展开阅读全文