收藏 分享(赏)

算法分析技巧与分析习题答案.doc

上传人:kpmy5893 文档编号:8137780 上传时间:2019-06-10 格式:DOC 页数:6 大小:79KB
下载 相关 举报
算法分析技巧与分析习题答案.doc_第1页
第1页 / 共6页
算法分析技巧与分析习题答案.doc_第2页
第2页 / 共6页
算法分析技巧与分析习题答案.doc_第3页
第3页 / 共6页
算法分析技巧与分析习题答案.doc_第4页
第4页 / 共6页
算法分析技巧与分析习题答案.doc_第5页
第5页 / 共6页
点击查看更多>>
资源描述

1、Page 541.16(a) The minimum number of element comparisons performed by the algorithm is n 1.This minimum is achieved when the input A1n is already sorted in nondecreasing order.(b) The maximum number of element comparisons performed by the algorithm is(n 1)+( n 2)+ 2+1=n(n 1)/2. This maximum is achie

2、ved when the input A1n is already sorted in decreasing order.(c) The minimum number of element assignments performed by the algorithm is 0.This minimum is achieved when the input A1n is already sorted in nondecreasing order.(d) The maximum number of element assignments performed by the algorithm is3

3、(n 1)+( n 2)+ 2+1=3n(n 1)/2.This maximum is achieved when the input A1n is already sorted in decreasing order.(e) The running time of Algorithm BUBBLESORT is (n2) in terms of the -notation, and (n) in terms of the -notation. (f)The running time cannot be expressed in terms of -notation, because the

4、running time ranges from the linear to quadratic.Page 992.16(a)On the one hand , j=1n jlog j j=1n nlog nn 2 log n.On the other hand, j=1n jlog j j= n/2 n n/2log n/2n/2 n/2log n/2(n-1)n /4log(n/2).Hence, j=1n jlog j= (n 2logn).(b)Let f(x)=x log x (x1). Since f(x) is an increasing function,we have j=1

5、n jlog j 1n+1 x log xdx(2(n+1) 2 ln(n+1)-(n+1)2+1) / (4ln2).Also, j=1n jlog j= j=2n jlog j 1n x log xdx(2 n2 lnn-n2+1) / (4ln2).Thus,有 j=1n jlog j= (n 2lnn)= (n 2logn).2.18(a) The characteristic equation isx=3.Thus, f(n)=c3n (n10) ,where c is determined by the initial value of the sequence : f(0).So

6、lving the equation f(0)=5=c, we obtain c=5.It follows that f(n)=53n (n10).(b) The characteristic equation isx=2.Thus,f(n)=c 2n (n10) ,where c is determined by the initial value of the sequence : f(0).Solving the equation f(0)=2=c, we obtain c=2.It follows that f(n)=2n+1 (n10).2.19(a) The characteris

7、tic equation is x2-5x +6=0, and hence x1=2 and x2=3.Thus, the solution to the recurrence is 于是, f(n)= c12n + c23n (n10) .To find the values of c1 and c2, we solve the two simultaneous equations:f(0)=1= c1 + c2 and f(1)=0=2c1 + 3c2.Solving the two simultaneous equations, we obtain c1=3 and c2 = -2. I

8、t follows thatf(n)=32n-23n (n10).(b) The characteristic equation is x2-4x +4=0, and hence x1=x2=2.Thus, the solution to the recurrence is 于是, f(n)= c12n + c2n 2n (n10).To find the values of c1 and c2, we solve the two simultaneous equations:f(0)=6= c1 and f(1)=8=2c1 +2c2.Solving the two simultaneous

9、 equations, we obtain c1=6 and c2 = -2. It follows thatf(n)=32n+1-n2n+1 (n10).2.20(a) f(n)= f(n-1)+ n2= f(n-2)+(n-1)2+n2= f(0)+12+22+(n-1)2+n2=0+12+22+(n-1)2+n2=n(n+1)(2n+1)/6 (n0).(b) Let f(n)= 2ng(n)(g(0)=f (0)=1). Then,2ng(n)= 22n-1g(n-1)+n,which simplifies tog(n)=g(n-1)+n2-n ,whose solution is g

10、(n) = i=1n i2-i +1= 2-(n+2)/2n+1=3-(n+2)/2n (n0).Hence, f(n)= 32n-n-2 (n0).(c) Let f(n)=3ng(n)(g(0)=f (0)=3). Then,3ng(n)=33n-1g(n-1)+ 2n,which simplifies tog(n)=g(n-1)+(2/3)n ,whose solution isg(n) = g(0)+ i=1n(2/3) i=3+ i=1n(2/3) i=5-2(2/3) n (n0).Hence, f(n)= 352n3n-n-22n+1 (n0).Page 156-1585.8so

11、lution: First, we note that the time complexity of RADIX is (kn), where n is the number of elements in array A, and k is the maximum size among elements in A. Thus,(1) when array A consists of n positive integers in the interval 1n, we can conclude that k=O(logn) and the time complexity can be expre

12、ssed as O(nlogn) in terms of n.(2) when array A consists of n positive integers in the interval 1 n2, we can conclude that k=O(logn) and the time complexity can be expressed as O(nlogn) in terms of n.(3) when array A consists of n positive integers in the interval 1 2n, we can conclude that k=O(n) a

13、nd the time complexity can be expressed as O(n2) in terms of n.5.9 solution:Since A1n is an array of positive integers in the interval 1 n!, it follows that k=O(nlog n) and the time complexity of RADIX is O(n2logn). Considering the time complexity of BOTTOMUPSORT is (nlogn), it is very likely that B

14、OTTOMUPSORT is faster.5.23solution:The only modification is to change “for j1 to n” to “for jn to 1 step -1” in Step 3 of Procedure perm2 in Algorithm PERMUTATIONS2 . 5.31disproof:Take the following instance as a counterexample: n=4, A1n=1,2,3,4. Obviously, if we run Algorithm MAJORITY on this insta

15、nce, then in step 7 of Procedure candidate j=n and count=0, but c=4 isnt the majority element.Exercise 66.39解答:当序列中的元素都相同时,每次执行算法 SPLIT,仅出现一次元素交换,即将序列的第一个元素与最后一个元素交换,且划分元素的新位置为该序列的最后一个位置。因此,在元素均相同的数组 A1n上,算法 QUICKSORT 的执行特点为:每次划分后只剩下一个非空子序列未处理的。第一次划分后剩下 A1n-1未处理,第二次划分后剩下 A1n-2未处理,第 n-1 次划分后剩下 A1(已有序

16、) 。在该实例上,算法的执行时间为:(n-1)+(n-2)+2+1=(n2),属于最坏的情况。且最后所得结果中各元素顺序如下:A2,A3,A4,An-1,A1(这里,Ai表示输入时的第 i 个元素,i=1,2,n) 。6.44解答:因为,(a+b)(c+d)=ac+ad+bc+bd,所以有,xy=(ac-bd)+(a+b)(c+d)-ac-bd)i.由此可见,这样计算 xy 只需要 3 次乘法。Exercise 7 (Page 220-223)7.5. 程序运行结果如下:Please input the string A: xzyzzyxPlease input the string B: z

17、xyyzxzThe length of a longest common subsequence of A and B is: 4A longest subsequence of A and B is: zyyx7.11. 程序运行结果如下:Please input the number of matrices: 5Please input r1N+1: 2 3 6 4 2 7The minimum number of scalar multiplications is: 124An optimal order is: (M1(M2(M3M4)M57.15. 程序运行结果如下:Please i

18、nput the number of vertices in graph G: 4Please input the cost matrix of G: 0 7 1 6 0 9 4 4 0 21 0Matrix D0:0 7 1 6 0 9 4 4 0 21 0Matrix D1:0 7 1 6 0 9 4 4 0 21 8 2 0Matrix D2:0 7 1 6 0 9 4 4 0 21 8 2 0Matrix D3:0 5 1 313 0 9 114 4 0 21 6 2 0Matrix D4:0 5 1 312 0 9 113 4 0 21 6 2 07.22. 程序运行结果如下:Ple

19、ase input the number of items: 5Please input the values of all these items P1N: 4 6 7 9 10Please input the sizes of all these items W1N: 3 5 7 8 9Please input the capacity of the knapsack M: 22V0N0M:0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 00 0 0 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 40 0 0 4 4 6

20、 6 6 10 10 10 10 10 10 10 10 10 10 10 10 10 10 100 0 0 4 4 6 6 7 10 10 11 11 13 13 13 17 17 17 17 17 17 17 170 0 0 4 4 6 6 7 10 10 11 13 13 15 15 17 19 19 20 20 22 22 220 0 0 4 4 6 6 7 10 10 11 13 14 15 16 17 19 20 20 21 23 23 25The maximum value is: 25An optimal solution is: 0 1 0 1 1Exercise 88.13

21、解答:计算过程如下表所示。迭代次数 集合 X 中的元素 1 2 3 4 5 6 最短路径初始化1234511,31,3,21,3,2,51,3,2,5,41,3,2,5,4,60 9 4 0 8 4 17 0 8 4 20 13 0 8 4 16 13 280 8 4 16 13 180 8 4 16 13 18131321325132541325468.25解答:算法 Prim 的时间复杂度为 (n2), 算法 Kruskal 的时间复杂度为 (mlogm) 。当 m=O(n1.99)时,Kruskal 算法的时间复杂度为 (n 1.99logn)。由于n1.99logn / n2 0(n) ,所以,算法 Kruskal 效率要高。此时应选择算法 Kruskal。8.31解:构造哈夫曼树如下。0 10 10 01 138f:9a:716 22e:12b: 5 5c: 3 d:2610由此可得到一种最优的编码方案为:a: 00, b: 100, c: 1010, d: 1011 , e: 11, f: 01。

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

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

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


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

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

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