收藏 分享(赏)

算法设计与分析基础课后习题答案solu2.pdf

上传人:精品资料 文档编号:10088358 上传时间:2019-10-08 格式:PDF 页数:51 大小:292.83KB
下载 相关 举报
算法设计与分析基础课后习题答案solu2.pdf_第1页
第1页 / 共51页
算法设计与分析基础课后习题答案solu2.pdf_第2页
第2页 / 共51页
算法设计与分析基础课后习题答案solu2.pdf_第3页
第3页 / 共51页
算法设计与分析基础课后习题答案solu2.pdf_第4页
第4页 / 共51页
算法设计与分析基础课后习题答案solu2.pdf_第5页
第5页 / 共51页
点击查看更多>>
资源描述

1、This file contains the exercises, hints, and solutions for Chapter 2 of thebook ”Introduction to the Design and Analysis of Algorithms,” 2nd edition, byA. Levitin. The problems that might be challenging for at least some studentsare marked by triangleright; those that might be dicult for a majority

2、of students aremarked by trianglerightsld.Exercises 2.11. For each of the following algorithms, indicate (i) a natural size metric forits inputs; (ii) its basic operation; (iii) whether the basic operation countcan be dierent for inputs of the same size:a. computing the sum of n numbersb. computing

3、n!c. finding the largest element in a list of n numbersd. Euclids algorithme. sieve of Eratosthenesf. pen-and-pencil algorithm for multiplying two n-digit decimal integers2. a. Consider the definition-based algorithm for adding two n-by-n matri-ces. What is its basic operation? How many times is it

4、performed asa function of the matrix order n? As a function of the total number ofelements in the input matrices?b. Answer the same questions for the definition-based algorithm for matrixmultiplication.3. Consider a variation of sequential search that scans a list to return thenumber of occurrences

5、of a given search key in the list. Will its eciencydier from the eciency of classic sequential search?4. a. Glove selection There are 22 gloves in a drawer: 5 pairs of red gloves,4 pairs of yellow, and 2 pairs of green. You select the gloves in the darkand can check them only after a selection has b

6、een made. What is thesmallest number of gloves you need to select to have at least one matchingpair in the best case? in the worst case? (after Mos01, #18)b. Missing socks Imagine that after washing 5 distinct pairs of socks,you discover that two socks are missing. Of course, you would like to havet

7、he largest number of complete pairs remaining. Thus, you are left with4 complete pairs in the best-case scenario and with 3 complete pairs inthe worst case. Assuming that the probability of disappearance for each1of the 10 socks is the same, find the probability of the best-case scenario;the probabi

8、lity of the worst-case scenario; the number of pairs you shouldexpect in the average case. (after Mos01, #48)5. a.trianglerightProve formula (2.1) for the number of bits in the binary representationof a positive integer.b. What would be the analogous formula for the number of decimal digits?c. Expla

9、in why, within the accepted analysis framework, it does not mat-ter whether we use binary or decimal digits in measuring ns size.6. Suggest how any sorting algorithm can be augmented in a way to makethe best-case count of its key comparisons equal to just n1 (n is a listssize, of course). Do you thi

10、nk it would be a worthwhile addition to anysorting algorithm?7. Gaussian elimination, the classic algorithm for solving systems of n linearequations in n unknowns, requires about13n3multiplications, which is thealgorithms basic operation.a. How much longer should you expect Gaussian elimination to w

11、orkon a system of 1000 equations versus a system of 500 equations?b. You are considering buying a computer that is 1000 times faster thanthe one you currently have. By what factor will the faster computer in-crease the sizes of systems solvable in the same amount of time as on theold computer?8. For

12、 each of the following functions, indicate how much the functions valuewill change if its argument is increased fourfold.a. log2n b.n c. n d. n2e. n3f. 2n9. Indicate whether the first function of each of the following pairs has asmaller, same, or larger order of growth (to within a constant multiple

13、)than the second function.a. n(n+1)and 2000n2b. 100n2and 0.01n3c. log2n and lnn d. log22n and log2n2e. 2n1and 2nf. (n1)! and n!10. Invention of chess According to a well-known legend, the game of chesswas invented many centuries ago in northwestern India by a sage namedShashi. When he took his inven

14、tion to his king, the king liked the game2so much that he oered the inventor any reward he wanted. Sashi askedfor some grain to be obtained as follows: just a single grain of wheat wasto be placed on the first square of the chess board, two on the second, fouron the third, eight on the fourth, and s

15、o on, until all 64 squares had beenfilled. What would the ultimate result of this algorithm have been?3Hints to Exercises 2.11. The questions are indeed as straightforward as they appear, though someof them may have alternative answers. Also, keep in mind the caveatabout measuring an integers size.2

16、. a. The sum of two matrices is defined as the matrix whose elements arethe sums of the corresponding elements of the matrices given.b. Matrix multiplication requires two operations: multiplication and ad-dition. Which of the two would you consider basic and why?3. Will the algorithms eciency vary o

17、n dierent inputs of the same size?4. a. Gloves are not socks: they can be right-handed and left-handed.b. You have only two qualitatively dierent outcomes possible. Countthe number of ways to get each of the two.5. a. Prove first that if a positive decimal integer n has b digits in its binaryreprese

18、ntation then2b1 n 0 belongs to (nk).b. Prove that exponential functions anhave dierent orders of growthfor dierent values of base a0.87. Prove (by using the definitions of the notations involved) or disprove (bygiving a specific counterexample) the following assertions.a. If t(n) O(g(n), then g(n) (

19、t(n).b. (g(n) = (g(n), where 0.c. (g(n) = O(g(n)(g(n).d.triangleright For any two nonnegative functions t(n) and g(n) defined on the set ofnonnegative integers, either t(n) O(g(n), or t(n) (g(n), or both.8. triangleright Prove the sections theorem fora. notation.b. notation.9. We mentioned in this s

20、ection that one can check whether all elements of anarray are distinct by a two-part algorithm based on the arrays presorting.a. If the presorting is done by an algorithm with the time eciency in(nlogn), what will be the time eciency class of the entire algorithm?b. If the sorting algorithm used for

21、 presorting needs an extra array ofsize n, what will be the space eciency class of the entire algorithm?10. triangleright Door in a wall You are facing a wall that stretches infinitely in bothdirections. There is a door in the wall, but you know neither how faraway nor in which direction. You can se

22、e the door only when you areright next to it. Design an algorithm that enables you to reach the doorby walking at most O(n) steps where n is the (unknown to you) numberof steps between your initial position and the door. Par95, #6529Hints to Exercises 2.21. Use the corresponding counts of the algori

23、thms basic operation (see Sec-tion 2.1) and the definitions of O, , and .2.Establishtheorderofgrowthofn(n+1)/2 first and then use the informaldefinitions of O, , and . (Similar examples were given in the section.)3. Simplify the functions given to single out the terms defining their ordersof growth.

24、4. a. Check carefully the pertinent definitions.b. Compute the ratio limits of every pair of consecutive functions onthe list.5. First simplify some of the functions. Then use thelist of functions in Table2.2 to “anchor” each of the functions given. Prove their final placementby computing appropriat

25、e limits.6. a. You can prove this assertion either by computing an appropriate limitor by applying mathematical induction.b. Compute limnan1/an2.7. Prove the correctness of (a), (b), and (c) by using the appropriate de-finitions; construct a counterexample for (d) (e.g., by constructing twofunctions

26、 behaving dierently for odd and even values of their arguments).8. The proof of part (a) is similar to the one given for the theorems assertionin Section 2.2. Of course, dierent inequalities need to be used to boundthe sum from below.9. Follow the analysis plan used in the text when the algorithm wa

27、s men-tioned for the first time.10. You should walk intermittently left and right from your initial positionuntil the door is reached.10Solutions to Exercises 2.21. a. Since Cworst(n)=n, Cworst(n) (n).b. Since Cbest(n)=1,Cbest(1) (1).c. Since Cavg(n)=p(n+1)2+ n(1 p)=(1p2)n +p2where 0 p 1,Cavg(n) (n)

28、.2. n(n+1)/2 n2/2 is quadratic. Thereforea. n(n +1)/2 O(n3) is true. b. n(n+1)/2 O(n2) is true.c. n(n+1)/2 (n3) is false. d. n(n+1)/2 (n) is true.3. a. Informally, (n2+1)10 (n2)10= n20 (n20) Formally,limn(n2+1)10n20= limn(n2+1)10(n2)10=limnparenleftBign2+1n2parenrightBig10= limnparenleftbig1+1n2pare

29、nrightbig10=1.Hence (n2+1)10 (n20).Note: An alternative proof can be based on the binomial formula andthe assertion of Exercise 6a.b. Informally,10n2+7n +310n2=10n (n). Formally,limn10n2+7n+3n=limnradicalBig10n2+7n+3n2= limnradicalBig10 +7n+3n2=10.Hence10n2+7n +3 (n).c. 2nlg(n +2)2+(n +2)2lgn2=2n2lg

30、(n +2)+(n +2)2(lgn 1) (nlgn)+(n2lgn)=(n2lgn).d. 2n+1+3n1=2n2+3n13 (2n)+(3n)=(3n).e. Informally, floorleftlog2nfloorrightlog2n (logn). Formally, by using the in-equalities x1 log2n1 log2n12log2n (for every n 4) =12log2n.Hence floorleftlog2nfloorright(log2n) = (logn).114. a. The order of growth and th

31、e related notations O, ,and deal withthe asymptotic behavior of functions as n goes to infinity. Therefore nospecific values of functions within a finite range of ns values, suggestiveas they might be, can establish their orders of growth with mathematicalcertainty.b. limnlog2nn=limn(log2n)prime(n)p

32、rime=limn1nlog2e1=log2e limn1n=0.limnnnlog2n= limn1log2n=0.limnnlog2nn2= limnlog2nn= (see the first limit of this exercise) =0.limnn2n3= limn1n=0.limnn32n= limn(n3)prime(2n)prime= limn3n22nln2=3ln2limnn22n=3ln2limn(n2)prime(2n)prime=3ln2limn2n2nln2=6ln22limnn2n=6ln22limn(n)prime(2n)prime=6ln22limn12

33、nln2=6ln32limn12n=0.limn2nn!= (see Example 3 in the section) 0.5. (n2)! (n2)!), 5lg(n+100)10=50lg(n+100) (logn), 22n=(22)n (4n), 0.001n4+3n3+1 (n4), ln2n (log2n),3n (n13), 3n (3n). The list of these functions ordered in increasingorder of growth looks as follows:5lg(n + 100)10, ln2n,3n, 0.001n4+3n3+

34、1, 3n, 22n, (n2)!6. a. limnp(n)nk= limnaknk+ak1nk1+.+a0nk= limn(ak+ak1n+. +a0nk)= ak 0.Hence p(n) (nk).b.limnan1an2=limnparenleftbigga1a2parenrightbiggn=0 if a1a2 an2 o(an1)7. a. The assertion should be correct because it states that if the order ofgrowth of t(n) is smaller than or equal to the orde

35、r of growth of g(n), then12the order of growth of g(n) is larger than or equal to the order of growthof t(n). The formal proof is immediate, too:t(n) cg(n) for all n n0, where c0,implies(1c)t(n) g(n) for all n n0.b. The assertion that (g(n) = (g(n) should be true because g(n)and g(n) dier just by a

36、positive constant multiple and, hence, by thedefinition of , must have the same order of growth. The formal proofhas to show that (g(n) (g(n) and (g(n) (g(n). Letf(n) (g(n); well show that f(n) (g(n). Indeed,f(n) cg(n) for all n n0(where c0)can be rewritten asf(n) c1g(n) for all n n0(where c1= c 0),

37、i.e., f(n) (g(n).Let now f(n) (g(n); well show that f(n) (g(n) for 0.Indeed, if f(n) (g(n),f(n) cg(n) for all n n0(where c0)and thereforef(n) cag(n)=c1g(n) for all n n0(where c1=c 0),i.e., f(n) (g(n).c. The assertion is obviously correct (similar to the assertion that a = bif and only if a b and a b

38、). The formal proof should show that(g(n) O(g(n) (g(n) and that O(g(n) (g(n) (g(n),which immediately follow from the definitions of O, ,and.d. The assertion is false. The following pair of functions can serve asa counterexamplet(n)=n if n is evenn2if n is oddand g(n)=n2if n is evenn if n is odd138.

39、a. We need to prove that if t1(n) (g1(n) and t2(n) (g2(n), thent1(n)+t2(n) (maxg1(n),g2(n).Proof Since t1(n) (g1(n), there exist some positive constant c1and some nonnegative integer n1such thatt1(n) c1g1(n) for all n n1.Since t2(n) (g2(n), there exist some positive constant c2and somenonnegative in

40、teger n2such thatt2(n) c2g2(n) for all n n2.Let us denote c =minc1,c2 and consider n maxn1,n2 so that wecan use both inequalities. Adding the two inequalities above yields thefollowing:t1(n)+t2(n) c1g1(n)+c2g2(n) cg1(n)+cg2(n)=cg1(n)+g2(n) cmaxg1(n),g2(n).Hence t1(n)+t2(n) (maxg1(n),g2(n), with the

41、constants c andn0required by the O definition being minc1,c2 and maxn1,n2, re-spectively.b. The proof follows immediately from the theorem proved in the text(the O part), the assertion proved in part (a) of this exercise (the part),and the definition of (see Exercise 7c).9. a. Since the running time

42、 of the sorting part of the algorithm will stilldominate the running time of the second, its the former that will deter-mine the time eciency of the entire algorithm. Formally, it follows fromequality(nlogn)+O(n)=(nlogn),whose validity is easy to prove in the same manner as that of the sectionstheor

43、em.b. Since the second part of the algorithm will use no extra space, thespace eciency class will be determined by that of the first (sorting) part.Therefore, it will be in (n).10. The key idea here is to walk intermittently right and left going each timeexponentially farther from the initial positi

44、on. A simple implementationof this idea is to do the following until the door is reached: For i =0,1,.,make 2isteps to the right, return to the initial position, make 2isteps to14the left, and return to the initial position again. Let 2k1maxvalmaxval Aireturn maxval minvalAnswer questions ae of Prob

45、lem 4 about this algorithm.6. Consider the following algorithm.Algorithm Enigma(A0n1,0n1)/Input: A matrix A0n1,0n1 of real numbersfor i 0 to n2 dofor j i +1to n1 doif Ai,j negationslash= Aj,ireturn falsereturn trueAnswer the questions ae of Problem 4 about this algorithm.7. Improve the implementatio

46、n of the matrix multiplication algorithm (seeExample 3) by reducing the number of additions made by the algorithm.What eect will this change have on the algorithms eciency?8. Determine the asymptotic order of growth for the total number of times allthe doors are toggled in the Locker Doors puzzle (P

47、roblem 11 in Exercises1.1).9. Prove the formulansummationdisplayi=1i =1+2+. +n =n(n +1)2either by mathematical induction or by following the insight of a 10-yearold schoolboy named Karl Friedrich Gauss (17771855) who grew up tobecome one of the greatest mathematicians of all times.1710. Consider the

48、 following version of an important algorithm that we willstudy later in the book.Algorithm GE(A0n1,0n)/Input: An n-by-n +1matrix A0n1,0n of real numbersfor i 0 to n2 dofor j i +1to n1 dofor k i to n doAj,k Aj,kAi,kAj,i /Ai,ia.triangleright Find the time eciency class of this algorithm.b.triangleright What glaring ineciency does this pseudocode contain and how canit be eliminated to speed the algorithm up?

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 企业管理 > 管理学资料

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报