算法导论第二版习题答案英文版.pdf
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_05.gif)
《算法导论第二版习题答案英文版.pdf》由会员分享,可在线阅读,更多相关《算法导论第二版习题答案英文版.pdf(53页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、算法导论第二版习题答案英文版Solutions for Introduction to algorithmssecond editionPhilip BilleThe author of this document takes absolutely no responsibility for the contents.This is merelya vague suggestion to a solution to some of the exercises posed in the book Introduction to algo-rithms by Cormen,Leiserson an
2、d Rivest.It is very likely that there are many errors and that thesolutions are wrong.If you have found an error,have a better solution or wish to contribute insome constructive way please send a message to beetleit.dk.It is important that you try hard to solve the exercises on your own.Use this doc
3、ument only asa last resort or to check if your instructor got it all wrong.Please note that the document is under construction and is updated only sporadically.Havefun with your algorithms.Best regards,Philip BilleLast update:December 9,20021.2 2Insertion sort beats merge sort when 8n2 64nlgn,n 8lgn
4、,2n/8 key to Ai s.Algorithm 2 SELECTION-SORT(A)Input:A=ha1,a2,.aniOutput:sorted A.for i 1 to n 1 doj FIND-MIN(A,i,n)Aj Aiend forAs a loop invariant we choose that A1,.,i1 are sorted and all other elements are greaterthan these.We only need to iterate to n 1 since according to the invariant the nth e
5、lement willthen the largest.The n calls of FIND-MINgives the following bound on the time complexity:nXi=1i!=(n2)This holds for both the best-and worst-case running time.2.2 3Given that each element is equally likely to be the one searched for and the element searched for ispresent in the array,a lin
6、ear search will on the average have to search through half the elements.This is because half the time the wanted element will be in the first half and half the time it willbe in the second half.Both the worst-case and average-case of LINEAR-SEARCHis(n).32.2 4One can modify an algorithm to have a bes
7、t-case running time by specializing it to handle a best-case input efficiently.2.3 5A recursive version of binary search on an array.Clearly,the worst-case running time is(lgn).Algorithm 3 BINARY-SEARCH(A,v,p,r)Input:A sorted array A and a value v.Output:An index i such that v=Ai or nil.if p r and v
8、 6=Ap thenreturn nilend ifj Ab(r p)/2cif v=Aj thenreturn jelseif v 0 and BINARY-SEARCH(A,Ai x,1,n)thenreturn trueend ifend forreturn falseClearly,this algorithm does the job.(It is assumed that nil cannot be true in the if-statement.)43.1 1Let f(n),g(n)be asymptotically nonnegative.Show that max(f(n
9、),g(n)=(f(n)+g(n).Thismeans that there exists positive constants c1,c2and n0such that,0 6 c1(f(n)+g(n)6 max(f(n),g(n)6 c2(f(n)+g(n)for all n n0.Selecting c2=1 clearly shows the third inequality since the maximum must be smaller thanthe sum.c1should be selected as 1/2 since the maximum is always grea
10、ter than the weightedaverage of f(n)and g(n).Note the significance of the“asymptotically nonnegative”assumption.The first inequality could not be satisfied otherwise.3.1 42n+1=O(2n)since 2n+1=2 2n6 2 2n!However 22nis not O(2n):by definition we have22n=(2n)2which for no constant c asymptotically may
11、be less than or equal to c 2n.3 4Let f(n)and g(n)be asymptotically positive functions.a.No,f(n)=O(g(n)does not imply g(n)=O(f(n).Clearly,n=O(n2)but n26=O(n).b.No,f(n)+g(n)is not(min(f(n),g(n).As an example notice that n+1 6=(min(n,1)=(1).c.Yes,if f(n)=O(g(n)then lg(f(n)=O(lg(g(n)provided that f(n)1
12、and lg(g(n)1are greater than or equal 1.We have that:f(n)6 cg(n)lgf(n)6 lg(cg(n)=lgc+lgg(n)To show that this is smaller than blgg(n)for some constant b we set lgc+lgg(n)=blgg(n).Dividing by lgg(n)yields:b=lg(c)+lgg(n)lgg(n)=lgclgg(n)+1 6 lgc+1The last inequality holds since lgg(n)1.d.No,f(n)=O(g(n)d
13、oes not imply 2f(n)=O(2g(n).If f(n)=2n and g(n)=n we have that2n 6 2 n but not 22n6 c2nfor any constant c by exercise 3.1 4.e.Yes and no,if f(n)1 for large n then f(n)2 1 and the statement is trivially true.f.Yes,f(n)=O(g(n)implies g(n)=(f(n).We have f(n)6 cg(n)for positive c and thus1/c f(n)6 g(n).
14、g.No,clearly 2n66 c2n/2=c2nfor any constant c if n is sufficiently large.h.By a small modification to exercise 3.11 we have that f(n)+o(f(n)=(max(f(n),o(f(n)=(f(n).54.1 1Show that T(n)=T(dn/2e)+1 is O(lgn).Using substitution we want to prove that T(n)6clg(n b).Assume this holds for dn/2e.We have:T(n
15、)6 clg(dn/2 be)+1 2 and c 1.4.2 1Determine an upper bound on T(n)=3T(bn/2c)+n using a recursion tree.We have that eachnode of depth i is bounded by n/2iand therefore the contribution of each level is at most(3/2)in.The last level of depth lgn contributes(3lg n)=(nlg 3).Summing up we obtain:T(n)=3T(b
16、n/2c)+n6 n+(3/2)n+(3/2)2n+(3/2)lg n1n+(nlg 3)=nlg n1Xi=0(3/2)i+(nlg 3)=n(3/2)lg n 1(3/2)1+(nlg 3)=2(n(3/2)lg n n)+(nlg 3)=2n3lg n2lg n 2n+(nlg 3)=2 3lg n 2n+(nlg 3)=2nlg 3 2n+(nlg 3)=(nlg 3)We can prove this by substitution by assumming that T(bn/2c)6 cbn/2clg 3 cbn/2c.Weobtain:T(n)=3T(bn/2c)+n6 3cb
17、n/2clg 3 cbn/2c+n63cnlg 32lg 3cn2+n6 cnlg 3cn2+n6 cnlg 3Where the last inequality holds for c 2.64.2 3Draw the recursion tree of T(n)=4T(bn/2c)+cn.The height of the tree is lgn,the out degreeof each node will be 4 and the contribution of the ith level will be 4ibcn/2ic.The last levelcontributes 4lg
18、n(1)=(n2).Hence we have a bound on the sum given by:T(n)=4T(bn/2c)+cn=lg n1Xi=04i bcn/2ic+(n2)6lg n1Xi=04i cn/2i+(n2)=cnlg n1Xi=02i+(n2)+(n2)=cn 2lg n 12 1+(n2)=(n2)Using the substitution method we can verify this bound.Assume the following clever inductionhypothesis.Let T(bn/2c)6 cbn/2c2 cbn/2c.We
19、have:T(n)=4T(bn/2c)+cn6 4(cbn/2c2 cbn/2c)+cn 4c(n/2)2 4cn/2+cn=cn2 2cn+cn=cn2 cn4.3 1Use the master method to find bounds for the following recursions.Note that a=4,b=4 andnlog24=n2 T(n)=4T(n/2)+n.Since n=O(n2?)case 1 applies and we get T(n)=(n2).T(n)=4T(n/2)+n2.Since n2=(n2)we have T(n)=(n2lgn).T(n
20、)=4T(n/2)+n3.Since n3=(n2+?)and 4(n/2)3=1/2n36 cn3for some c 1 wehave that T(n)=(n3).76.1 1There is a most 2h+11 vertices in a complete binary tree of height h.Since the lower level neednot be filled we may only have 2hvertices.6.1 2Since the height of an n-element heap must satisfy that 2h6 n 6 2h+
21、1 1 2h+1.we haveh 6 lgn 1 and APARENT(i).7.2 2If the elements in A are the same,then by exercise 7.1 2 the returned element from each callto PARTITION(A,p,r)is r 1 thus yielding the worst-case partitioning.The total running time iseasily seen to be(n2).7.2 3If the elements in A are distinct and sort
22、ed in decreasing order then,as in the previous exercise,we have worst-case partitioning.The running time is again(n2).7 4a.Clearly,the QUICKSORT does exactly the same as the original QUICKSORTand therefore workscorrectly.b.Worst-case partitioning can cause the stack depth of QUICKSORT to be(n).c.If
23、we recursively call QUICKSORT on the smallest subarray returned by PARTITIONwe willavoid the problem and retain a O(lgn)bound on the stack depth.118.2 3COUNTING-SORTwill work correctly no matter what order A is processed in,however it is notstable.The modification to the for loop actually causes num
24、bers with the same value to appear inreverse order in the output.This can seen by running a few examples.8.2 4Given n integers from 1 to k show how to count the number of elements from a to b in O(1)timewith O(n+k)preprocessing time.As shown in COUNTING-SORTwe can produce an array C suchthat Ci cont
25、ains the number of elements less than or equal to i.Clearly,Cb Ca gives thedesired answer.8.3 4Show how to sort n integers in the range 0 to n2 1 in O(n)time.Notice that the number ofdigits used to represent an n2different numbers in a k-ary number system is d=logk(n2).Thusconsidering the n2numbers
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 算法 导论 第二 习题 答案 英文
![提示](https://www.taowenge.com/images/bang_tan.gif)
限制150内