收藏 分享(赏)

《数据结构与算法分析C语言描述第二版》答案 英文版.pdf

上传人:精品资料 文档编号:10856766 上传时间:2020-01-15 格式:PDF 页数:69 大小:260.91KB
下载 相关 举报
《数据结构与算法分析C语言描述第二版》答案 英文版.pdf_第1页
第1页 / 共69页
《数据结构与算法分析C语言描述第二版》答案 英文版.pdf_第2页
第2页 / 共69页
《数据结构与算法分析C语言描述第二版》答案 英文版.pdf_第3页
第3页 / 共69页
《数据结构与算法分析C语言描述第二版》答案 英文版.pdf_第4页
第4页 / 共69页
《数据结构与算法分析C语言描述第二版》答案 英文版.pdf_第5页
第5页 / 共69页
点击查看更多>>
资源描述

1、Data Structures and Algorithm Analysis in C (second edition) Solutions Manual Mark Allen Weiss Florida International UniversityPreface Included in this manual are answers to most of the exercises in the textbook Data Structures and Algorithm Analysis in C, second edition, published by Addison-Wesley

2、. These answers reect the state of the book in the rst printing. Specically 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

3、. For clarity, programs are meant to be pseudo-C rather than completely perfect code. Errors can be reported to weissu.edu. Thanks to Grigori Schwarz and Brian Harvey for pointing out errors in previous incarnations of this manual.Table of Contents 1. Chapter 1: Introduction 1 2. Chapter 2: Algorith

4、m Analysis 4 3. Chapter 3: Lists, Stacks, and Queues . 7 4. Chapter 4: Trees . 14 5. Chapter 5: Hashing 25 6. Chapter 6: Priority Queues (Heaps) . 29 7. Chapter 7: Sorting 3 6 8. Chapter 8: The Disjoint Set ADT . 42 9. Chapter 9: Graph Algorithms . 45 10. Chapter 10: Algorithm Design Techniques 54 1

5、1. Chapter 11: Amortized Analysis 63 12. Chapter 12: Advanced Data Structures and Implementation 66 -iii-Chapter 1: Introduction 1.3 Because of round-off errors, it is customary to specify the number of decimal places that should be included in the output and round up accordingly. Otherwise, numbers

6、 come out looking strange. We assume error checks have already been performed; the routine Separate is left to the reader. Code is shown in Fig. 1.1. 1.4 The general way to do this is to write a procedure with heading void ProcessFile( const char *FileName ); which opens FileName, does whatever proc

7、essing is needed, and then closes it. If a line of the form #include SomeFile is detected, then the call ProcessFile( SomeFile ); is made recursively. Self-referential includes can be detected by keeping a list of les for which a call to ProcessFile has not yet terminated, and checking this list bef

8、ore making a new call to ProcessFile. 1.5 (a) The proof is by induction. The theorem is clearly true for 0 X 1, since it is true for X = 1, and for X 1, log X is negative. It is also easy to see that the theorem holds for 1 X 2, since it is true for X = 2, and for X 2, log X is at most 1. Suppose th

9、e theorem is 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) 1 + Y / 2 Y / 2 + Y / 2 Y , where the rst ine- quality follows by the inductive hypothesis. (b) Let 2 X = A . Then A B = (2 X ) B = 2 XB . Thus log A B = XB . Since X = log A ,

10、the theorem is proved. 1.6 (a) The sum is 4/3 and follows directly from the formula. (b) S = 4 1 _+ 4 2 2 _ _+ 4 3 3 _ _+ . . . .4 S = 1+ 4 2 _+ 4 2 3 _ _+ . . . . Subtracting the rst equation from the second gives 3S = 1 + 4 1 _+ 4 2 2 _ _+ . . . . By part (a), 3S = 4/ 3s oS = 4/ 9. (c) S = 4 1 _+

11、4 2 4 _ _+ 4 3 9 _ _+ . . . .4 S = 1 + 4 4 _+ 4 2 9 _ _+ 4 3 16 _ _+ . . . . Subtracting the rst equa- tion from the second gives 3S = 1+ 4 3 _+ 4 2 5 _ _+ 4 3 7 _ _+ . . . . Rewriting, we get 3S = 2 i =0 S 4 i i _ _+ i =0 S 4 i 1 _ _ . Thus 3S = 2(4/ 9) + 4/ 3 = 20/ 9. Thus S = 20/ 27. (d) Let S N

12、= i =0 S 4 i i N _ _ . Follow the same method as in parts (a) - (c) to obtain a formula for S N in terms of S N - 1 , S N - 2 , ., S 0 and solve the recurrence. Solving the recurrence is very difcult. -1-_ _ _ _ double RoundUp( double N, int DecPlaces ) int i; double AmountToAdd = 0.5; for( i = 0; i

13、 0 ) putchar(.); PrintFractionPart( FractionPart, DecPlaces ); Fig. 1.1. _ _ _ _ 1.7 i = N / 2 S N i 1 _= i =1 S N i 1 _- i =1 S N / 2 - 1 i 1 _ ln N - ln N / 2 ln 2. -2-1.8 2 4= 16 ” 1 (mod 5). (2 4 ) 25” 1 25(mod 5). Thus 2 100” 1 (mod 5). 1.9 (a) Proof is by induction. The statement is clearly tr

14、ue for N = 1 and N = 2. Assume true for N = 1, 2, ., k . Then i =1 S k +1 F i = i =1 S k F i +F k +1 . By the induction hypothesis, the value of the sum on the right is F k +2- 2 + F k +1 = F k +3- 2, where the latter equality follows from the denition of the Fibonacci numbers. This proves the claim

15、 for N = k + 1, and hence for all N . (b) As in the text, the proof is by induction. Observe that f + 1 = f 2 . This implies that f - 1+ f - 2= 1. For N = 1 and N = 2, the statement is true. Assume the claim is true for N = 1, 2, ., k . F k +1= F k + F k - 1 by the denition and we can use the induct

16、ive hypothesis on the right-hand side, obtaining F k +1 f k + f k - 1 f - 1 f k +1+ f - 2 f k +1 F k +1 (f - 1+ f - 2 )f k +1 f k +1 and proving the theorem. (c) See any of the advanced math references at the end of the chapter. The derivation involves the use of generating functions. 1.10 (a) i =1

17、S N (2i - 1) = 2 i =1 S N i - i =1 S N 1 = N (N +1) - N = N 2 . (b) The easiest way to prove this is by induction. The case N = 1 is trivial. Otherwise, i =1 S N +1 i 3= (N +1) 3+ i =1 S N i 3 = (N +1) 3+ 4 N 2 (N +1) 2 _ = (N +1) 2 4 N 2 _+ (N +1) = (N +1) 2 4 N 2+ 4N + 4 _ = 2 2 (N +1) 2 (N +2) 2

18、_ _ = 2 (N +1)(N +2) _ 2 = i =1 S N +1 i 2 -3-Chapter 2: Algorithm Analysis 2.1 2/N , 37, N , N , N log log N , N log N , N log (N 2 ), N log 2 N , N 1.5 , N 2 , N 2 log N , N 3 ,2 N / 2 , 2 N . 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 ) = 2

19、N , T 2 (N ) = N , and f (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.3 We claim that N log N is the slower growing function. To see this, suppose otherwise. Then, N e / log N would grow slow

20、er than log N . Taking logs of both sides, we nd that, under this assumption, e / log N log N grows slower than log log N . But the rst expres- sion simplies to e log N .I f L = log N , then we are claiming that e L grows slower than log L , or equivalently, that e 2 L grows slower than log 2L . But

21、 we know that log 2L = o (L ), so the original assumption is false, proving the claim. 2.4 Clearly, log k 1 N = o (log k 2 N )i fk 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, by LHospitals rule, N lim N l

22、og i N _ _= N lim i N log i - 1 N _ _ The second limit is zero by the inductive hypothesis, proving the claim. 2.5 Let f (N ) = 1 when N is even, and N when N is odd. Likewise, let g (N ) = 1 when N is odd, and N when N is even. Then the ratio f (N ) / g (N ) oscillates between 0 and . 2.6 For all t

23、hese programs, the following analysis will agree with a simulation: (I) The running time is O (N ). (II) The running time is O (N 2 ). (III) 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

24、 is N 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 3 times, by previous arguments, but it is true only O (N 2 ) times (because it is true exactly i times for each i ). Thus the innermost loop is only executed O (N 2 ) time

25、s. Each time through, it takes O (j 2 ) = O (N 2 ) time, for a total of O (N 4 ). This is an example where multiplying loop sizes can occasionally give an overesti- mate. 2.7 (a) It should be clear that all algorithms generate only legal permutations. The rst two algorithms have tests to guarantee n

26、o duplicates; the third algorithm works by shufing an array that initially has no duplicates, so none can occur. It is also clear that the rst two algorithms are completely random, and that each permutation is equally likely. The third algorithm, due to R. Floyd, is not as obvious; the correctness c

27、an be proved by induction. -4-See J. Bentley, “Programming Pearls,“ Communications of the ACM 30 (1987), 754-757. Note that if the second line of algorithm 3 is replaced with the statement Swap( Ai, A RandInt( 0, N-1 ) ); then not all permutations are equally likely. To see this, notice that for N =

28、 3, there are 27 equally likely ways of performing the three swaps, depending on the three random integers. Since there are only 6 permutations, and 6 does not evenly divide 27, each permutation cannot possibly be equally represented. (b) For the rst algorithm, the time to decide if a random number

29、to be placed in A i has not been used earlier is O (i ). The expected number of random numbers that need to be tried is N / (N - i ). This is obtained as follows: i of the N numbers would be duplicates. Thus the probability of success is (N - i ) / N . Thus the expected number of independent trials

30、is N / (N - i ). The time bound is thus i =0 S N - 1 N - i Ni _ i =0 S N - 1 N - i N 2 _ N 2 i =0 S N - 1 N - i 1 _ N 2 j =1 S N j 1 _= O (N 2 log N ) The second algorithm saves a factor of i for each random number, and thus reduces the time bound to O (N log N ) on average. The third algorithm is c

31、learly linear. (c, d) The running times should agree with the preceding analysis if the machine has enough memory. If not, the third algorithm will not seem linear because of a drastic increase for large N . (e) The worst-case running time of algorithms I and II cannot be bounded because there is al

32、ways a nite probability that the program will not terminate by some given time T . The algorithm does, however, terminate with probability 1. The worst-case running time of the third algorithm is linear - its running time does not depend on the sequence of random numbers. 2.8 Algorithm 1 would take

33、about 5 days for N = 10,000, 14.2 years for N = 100,000 and 140 centuries for N = 1,000,000. Algorithm 2 would take about 3 hours for N = 100,000 and about 2 weeks for N = 1,000,000. Algorithm 3 would use 1 1 2 minutes for N = 1,000,000. These calculations assume a machine with enough memory to hold

34、 the array. Algorithm 4 solves a problem of size 1,000,000 in 3 seconds. 2.9 (a) O (N 2 ). (b) O (N log N ). 2.10 (c) The algorithm is linear. 2.11 Use a variation of binary search to get an O (log N ) solution (assuming the array is preread). 2.13 (a) Test to see if N is an odd number (or 2) and is

35、 not divisible by 3, 5, 7, ., N . (b) O ( N ), assuming that all divisions count for one unit of time. (c) B = O (log N ). (d) O (2 B / 2 ). (e) If a 20-bit number can be tested in time T , then a 40-bit number would require about T 2 time. (f) B is the better measure because it more accurately repr

36、esents the size of the input. -5-2.14 The running time is proportional to N times the sum of the reciprocals of the primes less than N . This is O (N log log N ). See Knuth, Volume 2, page 394. 2.15 Compute X 2 , X 4 , X 8 , X 10 , X 20 , X 40 , X 60 , and X 62 . 2.16 Maintain an array PowersOfX tha

37、t can be lled in a for loop. The array will contain X , X 2 , X 4 ,u pt oX 2 log N . The binary representation of N (which can be obtained by testing even or odd and then dividing by 2, until all bits are examined) can be used to multiply the appropriate entries of the array. 2.17 For N = 0o rN = 1,

38、 the number of multiplies is zero. If b (N ) is the number of ones in the binary representation of N , then if N 1, the number of multiplies used is log N + b (N ) - 1 2.18 (a) A . (b) B . (c) The information given is not sufcient to determine an answer. We have only worst- case bounds. (d) Yes. 2.1

39、9 (a) Recursion is unnecessary if there are two or fewer elements. (b) One way to do this is to note that if the rst N - 1 elements have a majority, then the last element cannot change this. Otherwise, the last element could be a majority. Thus if N is odd, ignore the last element. Run the algorithm

40、 as before. If no majority element emerges, then return the N th element as a candidate. (c) The running time is O (N ), and satises T (N ) = T (N / 2) + O (N ). (d) One copy of the original needs to be saved. After this, the B array, and indeed the recur- sion can be avoided by placing each B i in

41、the A array. The difference is that the original recursive strategy implies that O (log N ) arrays are used; this guarantees only two copies. 2.20 Otherwise, we could perform operations in parallel by cleverly encoding several integers into one. For instance, if A = 001, B = 101, C = 111, D = 100, w

42、e could add A and B at the same time as C and D by adding 00A00C + 00B00D. We could extend this to add N pairs of numbers at once in unit cost. 2.22 No. If Low = 1, High = 2, then Mid = 1, and the recursive call does not make progress. 2.24 No. As in Exercise 2.22, no progress is made. -6-Chapter 3:

43、 Lists, Stacks, and Queues 3.2 The comments for Exercise 3.4 regarding the amount of abstractness used apply here. The running time of the procedure in Fig. 3.1 is O (L + P ). _ _ _ _ void PrintLots( List L, List P ) int Counter; Position Lpos, Ppos; Lpos = First( L ); Ppos = First( P ); Counter = 1

44、; while( Lpos != NULL Ppos = Next( Ppos, P ); Lpos = Next( Lpos, L ); Fig. 3.1. _ _ _ _ 3.3 (a) For singly linked lists, the code is shown in Fig. 3.2. -7-_ _ _ _ /* BeforeP is the cell before the two adjacent cells that are to be swapped. */ /* Error checks are omitted for clarity. */ void SwapWithNext( Position BeforeP, List L ) Position P, AfterP; P = BeforeP-Next; AfterP = P-Next; /* Both P and AfterP assumed no

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

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

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


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

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

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