《数据结构与算法分析C语言描述第二版》答案 英文版.pdf
《《数据结构与算法分析C语言描述第二版》答案 英文版.pdf》由会员分享,可在线阅读,更多相关《《数据结构与算法分析C语言描述第二版》答案 英文版.pdf(69页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、Data StructuresandAlgorithm Analysis in C(second edition)Solutions ManualMark Allen WeissFlorida International UniversityPrefaceIncluded in this manual are answers to most of the exercises in the textbook Data Structures andAlgorithm Analysis in C,second edition,published by Addison-Wesley.These ans
2、wers reflectthe state of the book in the first printing.Specifically omitted are likely programming assignments and any question whose solu-tion is pointed to by a reference at the end of the chapter.Solutions vary in degree of complete-ness;generally,minor details are left to the reader.For clarity
3、,programs are meant to bepseudo-C rather than completely perfect code.Errors can be reported to weissfiu.edu.Thanks to Grigori Schwarz and Brian Harveyfor pointing out errors in previous incarnations of this manual.Table of Contents1.Chapter 1:Introduction.12.Chapter 2:Algorithm Analysis.43.Chapter
4、3:Lists,Stacks,and Queues.74.Chapter 4:Trees.145.Chapter 5:Hashing.256.Chapter 6:Priority Queues(Heaps).297.Chapter 7:Sorting.368.Chapter 8:The Disjoint Set ADT.429.Chapter 9:Graph Algorithms.4510.Chapter 10:Algorithm Design Techniques.5411.Chapter 11:Amortized Analysis.6312.Chapter 12:Advanced Data
5、 Structures and Implementation.66-iii-Chapter 1:Introduction1.3Because of round-off errors,it is customary to specify the number of decimal places thatshould be included in the output and round up accordingly.Otherwise,numbers come outlooking strange.We assume error checks have already been performe
6、d;the routineSeparate?is left to the reader.Code is shown in Fig.1.1.1.4The general way to do this is to write a procedure with headingvoid ProcessFile(const char*FileName);which opens FileName,?does whatever processing is needed,and then closes it.If a line ofthe form#include SomeFileis detected,th
7、en the callProcessFile(SomeFile);is made recursively.Self-referential includes can be detected by keeping a list of files forwhich a call to ProcessFile?has not yet terminated,and checking this list before making anew call to ProcessFile.?1.5(a)The proof is by induction.The theorem is clearly true f
8、or 0 X?1,since it is true forX?=1,and for X?1,log X?is negative.It is also easy to see that the theorem holds for1 X?2,since it is true for X?=2,and for X?2,log X?is at most 1.Suppose the theoremis true for p?X?2p?(where p?is a positive integer),and consider any 2p?Y?4p?(p?1).Then log Y?=1+log(Y?/2)
9、1+Y?/2 Y?/2+Y?/2 Y?,where the first ine-quality follows by the inductive hypothesis.(b)Let 2X?=A?.Then A?B?=(2X?)B?=2XB?.Thus log A?B?=XB?.Since X?=log A?,thetheorem is proved.1.6(a)The sum is 4/3 and follows directly from the formula.(b)S?=41_+422_ _+433_ _+.4S?=1+42_ _+423_ _+.Subtracting the firs
10、t equation fromthe second gives 3S?=1+41 _+422_ _+.By part(a),3S?=4/3 so S?=4/9.(c)S?=41_+424_ _+439_ _+.4S?=1+44 _ _+429_ _+4316_ _+.Subtracting the first equa-tionfromthesecondgives3S?=1+43_ _+425_ _+437_ _+.Rewriting,weget3S?=2i?=04i?i_ _+i?=04i?1_ _.Thus 3S?=2(4/9)+4/3=20/9.Thus S?=20/27.(d)Let
11、SN?=i?=04i?i?N?_.Follow the same method as in parts(a)-(c)to obtain a formula for SN?in terms of SN?1,SN?2,.,S?0and solve the recurrence.Solving the recurrence is verydifficult.-1-_ _ _double RoundUp(double N,int DecPlaces)int i;double AmountToAdd=0.5;for(i=0;i DecPlaces;i+)AmountToAdd/=10;return N+
12、AmountToAdd;void PrintFractionPart(double FractionPart,int DecPlaces)int i,Adigit;for(i=0;i DecPlaces;i+)FractionPart*=10;ADigit=IntPart(FractionPart);PrintDigit(Adigit);FractionPart=DecPart(FractionPart);void PrintReal(double N,int DecPlaces)int IntegerPart;double FractionPart;if(N 0)putchar(.);Pri
13、ntFractionPart(FractionPart,DecPlaces);Fig.1.1._ _ _1.7i?=?N?/2?Ni1 _=i?=1Ni1 _ i?=1?N?/2 1?i1 _ ln N?ln N?/2 ln 2.-2-1.824=16 1(mod?5).(24)25 125(mod?5).Thus 2100 1(mod?5).1.9(a)Proof is by induction.The statement is clearly true for N?=1 and N?=2.Assume truefor N?=1,2,.,k?.Theni?=1k?+1Fi?=i?=1kFi?
14、+Fk?+1.By the induction hypothesis,the value of thesum on the right is Fk?+2 2+Fk?+1=Fk?+3 2,where the latter equality follows from thedefinition of the Fibonacci numbers.This proves the claim for N?=k?+1,and hence for allN?.(b)As in the text,the proof is by induction.Observe that +1=2.This implies
15、that1+2=1.For N?=1 and N?=2,the statement is true.Assume the claim is true forN?=1,2,.,k?.Fk?+1=Fk?+Fk?1by the definition and we can use the inductive hypothesis on the right-hand side,obtainingFk?+1 k?+k?1 1k?+1+2k?+1Fk?+1 (1+2)k?+1 k?+1and proving the theorem.(c)See any of the advanced math refere
16、nces at the end of the chapter.The derivationinvolves the use of generating functions.1.10(a)i?=1N(2i?1)=2i?=1Ni?i?=1N1=N?(N?+1)N?=N?2.(b)The easiest way to prove this is by induction.The case N?=1 is trivial.Otherwise,i?=1N?+1i?3=(N?+1)3+i?=1Ni?3=(N?+1)3+4N?2(N?+1)2_=(N?+1)2?4N?2_+(N?+1)?=(N?+1)2?4
17、N?2+4N?+4_?=22(N?+1)2(N?+2)2_ _=?2(N?+1)(N?+2)_?2=?i?=1N?+1i?2-3-Chapter 2:Algorithm Analysis2.12/N?,37,?N?,N?,N?log log N?,N?log N?,N?log(N?2),N?log2N?,N?1.5,N?2,N?2log N?,N?3,2N?/2,2N?.N?log N?and N?log(N?2)grow at the same rate.2.2(a)True.(b)False.A counterexample is T?1(N?)=2N?,T?2(N?)=N?,and?f?
18、(N?)=N?.(c)False.A counterexample is T?1(N?)=N?2,T?2(N?)=N?,and?f?(N?)=N?2.(d)False.The same counterexample as in part(c)applies.2.3We claim that N?log N?is the slower growing function.To see this,suppose otherwise.Then,N?/?log N?would grow slower than log N?.Taking logs of both sides,we find that,u
19、nder this assumption,/?log N?log N?grows slower than log log N?.But the first expres-sion simplifies to?log N?.If L?=log N?,then we are claiming that?L?grows slower thanlog L?,or equivalently,that 2L?grows slower than log2 L?.But we know thatlog2 L?=(L?),so the original assumption is false,proving t
20、he claim.2.4Clearly,logk?1N?=(logk?2N?)if k?1 k?2,so we need to worry only about positive integers.The claim is clearly true for k?=0 and k?=1.Suppose it is true for k?i?.Then,byLHospitals rule,N?limNlogi?N_ _=N?lim iNlogi?1N_The second limit is zero by the inductive hypothesis,proving the claim.2.5
21、Let?f?(N?)=1 when N?is even,and N?when N?is odd.Likewise,let g?(N?)=1 when N?isodd,and N?when N?is even.Then the ratio?f?(N?)/g?(N?)oscillates between 0 and.2.6For all these programs,the following analysis will agree with a simulation:(I)The running time is O?(N?).(II)The running time is O?(N?2).(II
22、I)The running time is O?(N?3).(IV)The running time is O?(N?2).(V)?j?can be as large as i?2,which could be as large as N?2.k?can be as large as?j?,which isN?2.The running time is thus proportional to N?.N?2.N?2,which is O?(N?5).(VI)The if?statement is executed at most N?3times,by previous arguments,b
23、ut it is trueonly O?(N?2)times(because it is true exactly i?times for each i?).Thus the innermost loop isonly executed O?(N?2)times.Each time through,it takes O?(?j?2)=O?(N?2)time,for a total ofO?(N?4).This is an example where multiplying loop sizes can occasionally give an overesti-mate.2.7(a)It sh
24、ould be clear that all algorithms generate only legal permutations.The first twoalgorithms have tests to guarantee no duplicates;the third algorithm works by shuffling anarray that initially has no duplicates,so none can occur.It is also clear that the first twoalgorithms are completely random,and t
25、hat each permutation is equally likely.The thirdalgorithm,due to R.Floyd,is not as obvious;the correctness can be proved by induction.-4-SeeJ.Bentley,Programming Pearls,Communications of the ACM 30(1987),754-757.Note that if the second line of algorithm 3 is replaced with the statementSwap(Ai,A Rand
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 数据结构与算法分析C语言描述第二版 数据结构与算法分析C语言描述第二版答案 英文版 数据结构 算法 分析 语言 描述 第二 答案 英文
限制150内