版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、Selected Solutions for Chapter 2: Getting StartedSolution to Exercise 2.2-2SELECTION-SORT.A/n D A: lengthfor j D 1 to n 1smallest D jfor i D j C 1 to nif Ai Amidlow D mid C 1else high D mid 1return NILRECURSIVE-BINARY-SEARCH.A; ; low; high/if low highreturn NILmid D b.low C high/=2cif= = Amidreturn
2、midelseif Amidreturn RECURSIVE-BINARY-SEARCH.A; ; mid C 1; high/else return RECURSIVE-BINARY-SEARCH.A; ; low; mid 1/Both procedures terminate the search unsuccessfully when the range is empty (i.e., low high) and terminate it successfully if the value has been found. Based on the comparison of to th
3、e middle element in the searched range, the search continues with the range halved. The recurrence for these procedures is therefore T.n/ D T.n=2/ C .1/, whose solution is T.n/ D .lg n/.Solution to Problem 2-4a.Theinversionsare .1; 5/; .2; 5/;.3; 4/; .3; 5/; .4; 5/. (Rememberthatinversions are speci
4、fied by indices rather than by the values in the array.)The array with elements from f1; 2; : : : ; ng with the most inversions isb.hn; n1; n2; : : : ; 2; 1i. For all 1ni jn, there is an inversion .i; j/.D n.n 1/=2.The number of such inversions is2c.Suppose that the array A starts out with an invers
5、ion .k; j/. Then k Aj. At the time that the outer for loop of lines 18 sets key D Aj, the value that started in Ak is still somewhere to the left of Aj. That is,its in Ai, where 1i y. Consider an inversion .i; j/, and let x D Ai and y D Aj, so that i y We claim that if we were to run merge sort, the
6、re would be exactly one merge- inversion involving x and y. To see why, observe that the only way in which array elements change their positions is within the MERGE procedure. More- over, since MERGE keeps elements within L in the same relative order to each other, and correspondingly for R, the onl
7、y way in which two elements can change their ordering relative to each other is for the greater one to appear in L and the lesser one to appear in R. Thus, there is at least one merge-inversion involving x and y. To see that there is exactly one such merge-inversion, ob- serve that after any call of
8、 MERGE that involves both x and y, they are in the same sorted subarray and will therefore both appear in L or both appear in R in any given call thereafter. Thus, we have proven the claim.We have shown that every inversion implies one merge-inversion. In fact, the correspondence between inversions
9、and merge-inversions is one-to-one. Sup- pose we have a merge-inversion involving values x and y, where x originally was Ai and y was originally Aj. Since we have a merge-inversion, x y And since x is in L and y is in R, x must be within a subarray preceding the subarray containing y. Therefore x st
10、arted out in a position i preceding ys original position j , and so .i; j/ is an inversion.Having shown a one-to-one correspondence between inversions and merge- inversions, it suffices for us to count merge-inversions.Consider a merge-inversion involving y in R. Let be the smallest value in L that
11、is greater than y. At some point during the merging process, and y will be the “exposed” values in L and R, i.e., we will have D Li and y D Rj in line 13 of MERGE. At that time, there will be merge-inversions involving yand Li; Li C 1; LiC 2; : : : ; Ln1, and these n1i C 1 merge-inversionswill be th
12、e only ones involving y. Therefore, we need to detect the first timethat and y become exposed during the MERGE procedure and add the valueof n1i C 1 at that time to our total count of merge-inversions.The following pseudocode, modeled on merge sort, works as we have just de- scribed. It also sorts t
13、he array A.COUNT-INVERSIONS.A; p; r/in ersions D 0if p rq D b.p C r/=2cin ersions D in ersions C COUNT-INVERSIONS.A; p; q/in ersions D in ersions C COUNT-INVERSIONS.A; q C 1; r/in ersions D in ersions C MERGE-INVERSIONS.A; p; q; r/return in ersions2-4Selected Solutions for Chapter 2: Getting Started
14、MERGE-INVERSIONS.A; p; q; r/n1 D q p C 1 n2 D r qlet L1 : : n1 C 1 and R1 : : n2 C 1 be new arraysfor i D 1 to n1Li D Ap C i1for j D 1 to n2Rj D Aq C j Ln1 C 1 D 1 Rn2 C 1 D 1i D 1j D 1in ersions D 0 counted D FALSE for k D p to rif counted = = FALSE and Rj 0 such that0 c1nb .n C a/b c2nb for all n
15、n0.Note thatn C an C jaj2nwhen jaj n ,andn C an jaj1when jaj 1 n2.n2Thus, when n 2 jaj,12n C a0n2n :Since b 0, the inequality still holds when all parts are raised to the power b:12b.n C a/b .2n/b ;0nb12nb .n C a/b 2bnb :0Thus, c1 D .1=2/b, c2 D 2b, and n0 D 2 jaj satisfy the definition.Solution to
16、Exercise 3.1-3Let the running time be T.n/. T.n/ O.n2/ means that T.n/ f.n/ for some function f.n/ in the set O.n2/. This statement holds for any running time T.n/, since the function g.n/ D 0 for all n is in O.n2/, and running times are always nonnegative. Thus, the statement tells us nothing about
17、 the running time.3-2Selected Solutions for Chapter 3: Growth of FunctionsSolution to Exercise 3.1-42nC1 D O.2n/, but 22n O.2n/.To show that 2nC1 D O.2n/, we must find constants c; n0 0 such that0 2nC1 c 2n for all n n0 :Since 2nC1 D 2 2n for all n, we can satisfy the definition with c D 2 and n0 D
18、1. To show that 22n 6D O.2n/, assume there exist constants c; n0 0 such that0 22n c 2n for all n n0 :Then 22n D 2nc 2n ) 2n c. But no constant is greater than all 2n, and so the assumption leads to a contradiction.2nSolution to Exercise 3.2-4dlg ne is not polynomially bounded, but dlg lg ne is.Provi
19、ngthat a function f.n/ is polynomiallybounded is equivalent to provingthat lg.f .n/ D O.lg n/ for the followingreasons.If f is polynomiallybounded, thenthereexistconstants c, k, n0 suchthatforall n n0, f.n/ cnk. Hence, lg.f .n/kc lg n, which, since c and k areconstants, means that lg.f .n/ D O.lg n/
20、.Similarly, i.f .n/ D O.lg n/, then f is polynomially bounded.In the following proofs, we will make use of the following two facts:1. lg.n/ D .n lg n/ (by equation (3.19).2. dlg ne D .lg n/, becausedlg ne lg ndlg ne 0, we have lgb n D o.na/. Substitute lg n for n, 2 for b, and 1 for a, giving lg2.lg
21、 n/ D o.lg n/.Therefore, lg.dlg lg ne/ D O.lg n/, and so dlg lg ne is polynomially bounded.Selected Solutions for Chapter 4: Divide-and-ConquerSolution to Exercise 4.2-4If you can multiply 3 3 matrices using k multiplications, then you can multiply n n matrices by recursively multiplying n=3 n=3 mat
22、rices, in time T.n/ D kT.n=3/ C .n2/.nlog3 kUsing the master method to solve this recurrence, consider the ratio of and n2:If log3 k D 2, case 2 applies and T.n/ D .n2 lg n/. In this case, k D 9 andT.n/ D o.nlg7/.If log3 k 2, case 3 applies and T.n/ D .n2/. In this case,T.n/ D o.nlg 7/. 2, case 1 ap
23、plies and T.n/ D .nlog3 k /. In this case, k 9.T.n/ D o.nlg 7/ when log3 k lg 7, i.e., when k 3lg 7 21:85. The largest such integer k is 21.Thus, k D 21 and the running time is .nlog3 k / D .nlog3 21/ D O.n2:80/ (since log3 21 2:77).Solution to Exercise 4.4-6The shortest path from the root to a leaf
24、 in the recursion tree is n ! .1=3/n !.1=3/2n ! ! 1. Since .1=3/kn D 1 when k D log3 n, the height of the part of the tree in which every node has two children is log3 n. Since the values at each of these levels of the tree add up to cn, the solution to the recurrence is at least cn log3 n D .n lg n
25、/.Solution to Exercise 4.4-9T.n/ D T.n/ C T.1/n/ C cnWe saw the solution to the recurrence T.n/ D T.n=3/ C T.2n=3/ C cn in the text. This recurrence can be similarly solved.4-2Selected Solutions for Chapter 4: Divide-and-ConquerWithout loss of generality, let , so that 0 11=2 and 1=2cn 0.T.n/DT.n/ C
26、 T.1/n/ C cndn lg.n/ C d.1/n lg.1/n/ C cnDDdn ln lg n C d.1/n lg.1/ C d.1/n lg n C cndn lg n C dn. lg C .1dn lgn ;/ lg.1/ C cnif dn. lg C .1/ lg.1/ C cnc :0. This condition is equivalent tod. lg C .1/ lg.1/Since 1=2 1 and 0 1 1=2, we have that lg 0 and lg.1 / 0. Thus, lg C .1 / lg.1 / 0. We can use
27、the same proof as for the upper bound, substituting for , and we get the requirement thatc0 d: lg .1 / lg.1 /Therefore, T.n/ D .n lg n/.Selected Solutions for Chapter 5: Probabilistic Analysis and Randomized AlgorithmsSolution to Exercise 5.2-1Since HIRE-ASSISTANT always hires candidate 1, it hires
28、exactly once ifandonly if no candidates other than candidate 1 are hired. This event occurs when candi- date 1 is the best candidate of the n, which occurs with probability 1=n.HIRE-ASSISTANT hires n times if each candidate is better than all those who were interviewed (and hired) before. This event
29、 occurs precisely when the list of ranks given to the algorithm is h1; 2; : : : ; ni, which occurs with probability 1=n.Solution to Exercise 5.2-4Another way to think of the hat-check problem is that we want to determine the expected number of fixed points in a random permutation. (A fixed point of
30、apermutation is a value ifor which .i/ D i.) We could enumerate all n per-mutations, count the total number of fixed points, and divide by n to determinethe average number of fixed points per permutation. This would be a painstak- ing process, and the answer would turn out to be 1. We can use indica
31、tor random variables, however, to arrive at the same answer much more easily.Define a random variable X that equals the number of customers that get back their own hat, so that we want to compute E X.For i D 1; 2; : : : ; n, define the indicator random variableXi D I fcustomer i gets back his own ha
32、tg :Then X D X1 C X2 C C Xn.Since the ordering of hats is random, each customer has a probability of 1=n of getting back his or her own hat. In other words, Pr fXi D 1g D 1=n, which, by Lemma 5.1, implies that E Xi D 1=n.5-2Selected Solutions for Chapter 5: Probabilistic Analysis and Randomized Algo
33、rithmsThus,#XnE XD EXiiD1XnDE Xi(linearity of expectation)iD1XnD1=ni D1D 1 ;and so we expect that exactly 1 customer gets back his own hat.Note that this is a situation in which the indicator random variables are not inde- pendent. For example, if n D 2 and X1 D 1, then X2 must also equal 1. Con- ve
34、rsely, if n D 2 and X1 D 0, then X2 must also equal 0. Despite the dependence, Pr fXi D 1g D 1=n for all i, and linearity of expectation holds. Thus, we can use thetechnique ofindicator randomvariables eveninthe presenceofdependence.Solution to Exercise 5.2-5Let Xij be an indicator random variable f
35、or the event where the pair Ai; Ajfor i Aj . More precisely, we defineDXijI fAi Aj g for 1i jn. We have Pr fXij D 1g D 1=2, becausegiven two distinct random numbers, the probability that the first is bigger than the second is 1=2. By Lemma 5.1, E Xij D 1=2.Let X be the the random variable denoting t
36、he total number of inverted pairs in the array, so thatXn 1 XnX DXij :iD1 jDiC1We want the expected number of inverted pairs, so we take the expectation of bothsides of the above equation to obtain#n 1XXnE X D EXij:iD1 jDiC1We use linearity of expectation to get#nX1 XnE XDEXiji D1 j Di C1Xn 1 XnDE X
37、ij i D1 j Di C1Xn 1 Xn1=2i D1 j Di C1DSelected Solutions for Chapter 5: Probabilistic Analysis and Randomized Algorithms5-3!n 12 2n.n 1/ 1DD2n.n 1/ 2D:4Thus the expected number of inverted pairs is n.n 1/=4.Solution to Exercise 5.3-2Although PERMUTE-WITHOUT-IDENTITY will not produce the identity per
38、muta- tion, there are other permutations that it fails to produce. For example, consider its operation when n D 3, when it should be able to produce the n 1 D 5 non- identity permutations. The for loop iterates for i D 1 and i D 2. When i D 1, the call to RANDOM returns one of two possible values (e
39、ither 2 or 3), and when i D 2, the call to RANDOM returns just one value (3). Thus, PERMUTE-WITHOUT- IDENTITY can produce only 2 1 D 2 possible permutations, rather than the 5 that are required.Solution to Exercise 5.3-4PERMUTE-BY-CYCLIC chooses offset as a random integer in the range 1offset n, and
40、 then it performs a cyclic rotation of the array.That is,B.i C offset1/ mod n/ C 1 D Ai for i D 1; 2; : : : ; n. (The subtractionand addition of 1 in the index calculation is due to the 1-origin indexing. If we had used 0-origin indexing instead, the index calculation would have simplied toB.i C off
41、set/ mod n D Ai for i D 0; 1; : : : ; n1.)Thus, once offset is determined, so is the entire permutation. Since each value of offset occurs with probability 1=n, each element Ai has a probability of ending up in position Bj with probability 1=n.This procedure does not produce a uniform random permutation, however, since it can produce only n different permutations. Thus, n permutations occur with probability 1=n, and the remaining n n permutations occur with probability 0.Selected Solutions for Chapter 6: HeapsortSolution to Exercise 6.1-1Since a hea
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026年青岛远洋船员职业学院单招综合素质考试题库带答案详解
- 2026年湖南省永州市高职单招综合素质考试题库带答案详解
- 2026年广西机电职业技术学院单招职业适应性测试题库及答案详细解析
- 2026年重庆市高职单招职业技能考试题库与答案详解
- 叉车安全之星会议演讲稿
- 五月演讲稿励志
- 领导国防教育的演讲稿
- 2026年吉林省四平市高职单招职业技能考试题库与答案详解
- 2026年兰州资源环境职业技术大学单招职业技能考试题库带答案详解
- 2026年上海商学院单招职业技能考试题库带答案详解
- 2026云南昆明巫家坝商业运营管理有限公司校园招聘8人考试参考题库及答案解析
- 紫菜养殖常见病虫害防治方法
- 药品市场营销技术
- (正式版)YST 1682-2024 镁冶炼行业绿色工厂评价要求
- 西门子变频器技术入门及实践- 课件 第5、6章 G120变频器的基本调试、G120变频器的操作与设置
- 部编人教版3三年级《道德与法治》下册电子课本课件
- 小学数学竞赛指导
- 通用电子嘉宾礼薄
- 机器人控制技术与实践 课程标准-教学大纲
- 室内无机防火涂料施工方案
- 安全意识培训课件 38、安全意识培训
评论
0/150
提交评论