算法导论第十二章答案

合集下载

计算机科学导论第12章参考答案

计算机科学导论第12章参考答案

CHAPTER 12Abstract Data TypesReview Questions1.An abstract data type is a data declaration packaged together with the operationsthat are meaningful for the data type with the implementation hidden from the user.3.A linear list is a list in which each element has a unique successor.5.Two common implementations of a general list are an array and a linked list.7.A push operation adds an element to the top of the stack while a pop operationremoves an element from the top of the stack. A push can put the stack in an over-flow condition while a pop can put the stack in an underflow condition.9.The enqueue operation adds an element to the rear of a queue. The dequeue opera-tion removes the element at the front of the queue. The enqueue operation couldput the queue in an overflow state while the dequeue could put the queue in anunderflow state.11.A depth first traversal processes all of the nodes in one subtree before processingall of the nodes in the other subtree. In a breadth first traversal, all the nodes at onelevel are processed before moving on to the next level.13.In a depth-first traversal, all of a vertex's descendents are processed before movingto an adjacent vertex. In a breadth-first traversal, all adjacent vertices of a vertexare processed before going to the next level.15.A network is a graph with weighted lines.Multiple-Choice Questions17.d19.c21.a23.a25.b27.d29.a34CHAPTER 12ABSTRACT DATA TYPES31.c33.c35.d37.d39.c41.b43.a45.d47.a49.b51.b53.aExercises55.(top) 6 5 (bottom)57.moveStackInput: Source stack (s1) and destination stack (s2)1. While s1 is not empty1.1 push ( s2, pop(s1) )End loopEnd59.catStackInput: Source stack (s2) and destination stack (s1)1. While s2 is not empty1.1 push ( s1, pop(s2) )End loopEnd61.compareStackInput: The two stacks to compare (s1 and s2)1. Allocate memory for two temporary stacks (Temp1 and Temp2)2. copyStack ( s1, Temp1 ) (see #58)3. copyStack ( s2, Temp2 ) (see #58)4. While Temp1 is not empty AND Temp2 is not empty4.1 TempValue1 = pop ( Temp1 )4.2 TempValue2 = pop ( Temp2 )4.3 If TempValue1 is not equal to TempValue2SECTION 54.3.1 Return falseEnd ifEnd loop5. If Temp1 is not empty OR Temp2 is not empty5.1 Return falseEnd if6. Return trueEnd63.emptyQueueInput: Queue to empty (q3)1. While q3 is not empty1.1 dequeue( q3 )End loopEnd65.copyQueueInput: Source queue (q2) and destination queue (q3)1. Allocate memory for a temporary queue (Temp)2. While q2 is not empty2.1 enqueue ( Temp, dequeue(q2) )End loop3. While Temp is not empty3.1 TempValue = dequeue(Temp)3.2 enqueue ( q2, TempValue )3.3 enqueue ( q3, TempValue )End loopEnd67.compareQueueInput: The two queues to compare (q1 and q2)1. Allocate memory for two temporary queues (Temp1 and Temp2)2. copyQueue ( q1, Temp1 ) (see #65)3. copyQueue ( q2, Temp2 ) (see #65)4. While Temp1 is not empty AND Temp2 is not empty4.1 TempValue1 = dequeue ( Temp1 )4.2 TempValue2 = dequeue ( Temp2 )4.3 If TempValue1 is not equal to TempValue24.3.1 Return falseEnd if6CHAPTER 12ABSTRACT DATA TYPESEnd loop5. If Temp1 is not empty OR Temp2 is not empty5.1 Return falseEnd if6. Return trueEnd69. See Figure 12.1Figure 12.1Exercise 6971.This tree cannot be drawn because it is not a valid binary tree. Node C must be theroot because it is listed last in the postorder traversal. From the inorder traversal,we see that nodes A, B, and D must be in the left subtree (because they are listed tothe left of the root) and that nodes E, F, and G are in the right subtree. Lookingback at the postorder list, however, we see that nodes G and F are listed first,which is not possible.73.Assuming that arcs are stored in sequence by their destination, the traversal is: A,G, F, H, D, E, C, B75.See Figure 12.2.Figure 12.2Exercise 75SECTION 7 77.See Figure 12.3.Figure 12.3Exercise 778CHAPTER 12ABSTRACT DATA TYPES。

算法导论课程作业答案

算法导论课程作业答案

算法导论课程作业答案Introduction to AlgorithmsMassachusetts Institute of Technology 6.046J/18.410J Singapore-MIT Alliance SMA5503 Professors Erik Demaine,Lee Wee Sun,and Charles E.Leiserson Handout10Diagnostic Test SolutionsProblem1Consider the following pseudocode:R OUTINE(n)1if n=12then return13else return n+R OUTINE(n?1)(a)Give a one-sentence description of what R OUTINE(n)does.(Remember,don’t guess.) Solution:The routine gives the sum from1to n.(b)Give a precondition for the routine to work correctly.Solution:The value n must be greater than0;otherwise,the routine loops forever.(c)Give a one-sentence description of a faster implementation of the same routine. Solution:Return the value n(n+1)/2.Problem2Give a short(1–2-sentence)description of each of the following data structures:(a)FIFO queueSolution:A dynamic set where the element removed is always the one that has been in the set for the longest time.(b)Priority queueSolution:A dynamic set where each element has anassociated priority value.The element removed is the element with the highest(or lowest)priority.(c)Hash tableSolution:A dynamic set where the location of an element is computed using a function of the ele ment’s key.Problem3UsingΘ-notation,describe the worst-case running time of the best algorithm that you know for each of the following:(a)Finding an element in a sorted array.Solution:Θ(log n)(b)Finding an element in a sorted linked-list.Solution:Θ(n)(c)Inserting an element in a sorted array,once the position is found.Solution:Θ(n)(d)Inserting an element in a sorted linked-list,once the position is found.Solution:Θ(1)Problem4Describe an algorithm that locates the?rst occurrence of the largest element in a?nite list of integers,where the integers are not necessarily distinct.What is the worst-case running time of your algorithm?Solution:Idea is as follows:go through list,keeping track of the largest element found so far and its index.Update whenever necessary.Running time isΘ(n).Problem5How does the height h of a balanced binary search tree relate to the number of nodes n in the tree? Solution:h=O(lg n) Problem 6Does an undirected graph with 5vertices,each of degree 3,exist?If so,draw such a graph.If not,explain why no such graph exists.Solution:No such graph exists by the Handshaking Lemma.Every edge adds 2to the sum of the degrees.Consequently,the sum of the degrees must be even.Problem 7It is known that if a solution to Problem A exists,then a solution to Problem B exists also.(a)Professor Goldbach has just produced a 1,000-page proof that Problem A is unsolvable.If his proof turns out to be valid,can we conclude that Problem B is also unsolvable?Answer yes or no (or don’t know).Solution:No(b)Professor Wiles has just produced a 10,000-page proof that Problem B is unsolvable.If the proof turns out to be valid,can we conclude that problem A is unsolvable as well?Answer yes or no (or don’t know).Solution:YesProblem 8Consider the following statement:If 5points are placed anywhere on or inside a unit square,then there must exist two that are no more than √2/2units apart.Here are two attempts to prove this statement.Proof (a):Place 4of the points on the vertices of the square;that way they are maximally sepa-rated from one another.The 5th point must then lie within √2/2units of one of the other points,since the furthest from the corners it can be is the center,which is exactly √2/2units fromeach of the four corners.Proof (b):Partition the square into 4squares,each with a side of 1/2unit.If any two points areon or inside one of these smaller squares,the distance between these two points will be at most √2/2units.Since there are 5points and only 4squares,at least two points must fall on or inside one of the smaller squares,giving a set of points that are no more than √2/2apart.Which of the proofs are correct:(a),(b),both,or neither (or don’t know)?Solution:(b)onlyProblem9Give an inductive proof of the following statement:For every natural number n>3,we have n!>2n.Solution:Base case:True for n=4.Inductive step:Assume n!>2n.Then,multiplying both sides by(n+1),we get(n+1)n!> (n+1)2n>2?2n=2n+1.Problem10We want to line up6out of10children.Which of the following expresses the number of possible line-ups?(Circle the right answer.)(a)10!/6!(b)10!/4!(c) 106(d) 104 ·6!(e)None of the above(f)Don’t knowSolution:(b),(d)are both correctProblem11A deck of52cards is shuf?ed thoroughly.What is the probability that the4aces are all next to each other?(Circle theright answer.)(a)4!49!/52!(b)1/52!(c)4!/52!(d)4!48!/52!(e)None of the above(f)Don’t knowSolution:(a)Problem12The weather forecaster says that the probability of rain on Saturday is25%and that the probability of rain on Sunday is25%.Consider the following statement:The probability of rain during the weekend is50%.Which of the following best describes the validity of this statement?(a)If the two events(rain on Sat/rain on Sun)are independent,then we can add up the twoprobabilities,and the statement is true.Without independence,we can’t tell.(b)True,whether the two events are independent or not.(c)If the events are independent,the statement is false,because the the probability of no rainduring the weekend is9/16.If they are not independent,we can’t tell.(d)False,no matter what.(e)None of the above.(f)Don’t know.Solution:(c)Problem13A player throws darts at a target.On each trial,independentlyof the other trials,he hits the bull’s-eye with probability1/4.How many times should he throw so that his probability is75%of hitting the bull’s-eye at least once?(a)3(b)4(c)5(d)75%can’t be achieved.(e)Don’t know.Solution:(c),assuming that we want the probability to be≥0.75,not necessarily exactly0.75.Problem14Let X be an indicator random variable.Which of the following statements are true?(Circle all that apply.)(a)Pr{X=0}=Pr{X=1}=1/2(b)Pr{X=1}=E[X](c)E[X]=E[X2](d)E[X]=(E[X])2Solution:(b)and(c)only。

《算法导论》习题答案

《算法导论》习题答案

Chapter2 Getting Start2.1 Insertion sort2.1.2 将Insertion-Sort 重写为按非递减顺序排序2.1.3 计算两个n 位的二进制数组之和2.2 Analyzing algorithms2.2.1将函数32/10001001003n n n --+用符号Θ表示2.2.2写出选择排序算法selection-sort当前n-1个元素排好序后,第n 个元素已经是最大的元素了.最好时间和最坏时间均为2()n Θ2.3 Designing algorithms2.3.3 计算递归方程的解22()2(/2)2,1k if n T n T n n if n for k =⎧=⎨+ = >⎩ (1) 当1k =时,2n =,显然有()lg T n n n =(2) 假设当k i =时公式成立,即()lg 2lg22i i i T n n n i ===⋅,则当1k i =+,即12i n +=时,111111()(2)2(2)222(1)22lg(2)lg i i i i i i i i T n T T i i n n ++++++==+=⋅+=+== ()lg T n n n ∴ =2.3.4 给出insertion sort 的递归版本的递归式(1)1()(1)()1if n T n T n n if n Θ =⎧=⎨-+Θ >⎩2.3-6 使用二分查找来替代insertion-sort 中while 循环内的线性扫描,是否可以将算法的时间提高到(lg )n n Θ?虽然用二分查找法可以将查找正确位置的时间复杂度降下来,但是移位操作的复杂度并没有减少,所以最坏情况下该算法的时间复杂度依然是2()n Θ2.3-7 给出一个算法,使得其能在(lg )n n Θ的时间内找出在一个n 元素的整数数组内,是否存在两个元素之和为x首先利用快速排序将数组排序,时间(lg )n n Θ,然后再进行查找: Search(A,n,x)QuickSort(A,n);i←1; j←n;while A[i]+A[j]≠x and i<jif A[i]+A[j]<xi←i+1elsej←j -1if A[i]+A[j]=xreturn trueelsereturn false时间复杂度为)(n Θ。

《算法导论(第二版)》(中文版)课后答案

《算法导论(第二版)》(中文版)课后答案

5
《算法导论(第二版) 》参考答案 do z←y 调用之前保存结果 y←INTERVAL-SEARCH-SUBTREE(y, i) 如果循环是由于y没有左子树,那我们返回y 否则我们返回z,这时意味着没有在z的左子树找到重叠区间 7 if y≠ nil[T] and i overlap int[y] 8 then return y 9 else return z 5 6 15.1-5 由 FASTEST-WAY 算法知:
15
lg n
2 lg n1 1 2cn 2 cn (n 2 ) 2 1
4.3-1 a) n2 b) n2lgn c) n3 4.3-4
2
《算法导论(第二版) 》参考答案 n2lg2n 7.1-2 (1)使用 P146 的 PARTION 函数可以得到 q=r 注意每循环一次 i 加 1,i 的初始值为 p 1 ,循环总共运行 (r 1) p 1次,最 终返回的 i 1 p 1 (r 1) p 1 1 r (2)由题目要求 q=(p+r)/2 可知,PARTITION 函数中的 i,j 变量应该在循环中同 时变化。 Partition(A, p, r) x = A[p]; i = p - 1; j = r + 1; while (TRUE) repeat j--; until A[j] <= x; repeat i++; until A[i] >= x; if (i < j) Swap(A, i, j); else return j; 7.3-2 (1)由 QuickSort 算法最坏情况分析得知:n 个元素每次都划 n-1 和 1 个,因 为是 p<r 的时候才调用,所以为Θ (n) (2)最好情况是每次都在最中间的位置分,所以递推式是: N(n)= 1+ 2*N(n/2) 不难得到:N(n) =Θ (n) 7.4-2 T(n)=2*T(n/2)+ Θ (n) 可以得到 T(n) =Θ (n lgn) 由 P46 Theorem3.1 可得:Ω (n lgn)

高等数学第12章课后习题答案(科学出版社).

高等数学第12章课后习题答案(科学出版社).

习题 12.11. 判断下列方程是几阶微分方程:;)1(2y x dxdy +=;042)2(2=+-⎪⎭⎫⎝⎛x dx dy dx dy x;052)3(322=+⎪⎭⎫⎝⎛-xy dx dy dx y d x 2334(4)2()1xy x y x y x '''++=+.解 (1)是一阶线性微分方程; (2)是一阶非线性微分方程; (3)是二阶非线性微分方程; (4)是二阶非线性微分方程.2. 指出下列各题中的函数是否为所给微分方程的解:(1)2xy y '=,25y x =; (2)0y y ''+=,3sin 4cos y x x =-; (3)20y y y '''-+=,2e x y x =; (4)2()0xy x y yy ''''++=,y x =. 解 (1)是; (2)是; (3)不是; (4)不是二阶非线性微分方程.3. 验证函数x C x y sin )(2+=(C 为任意常数)是方程0sin 2cot =--x x x y dxdy的通解, 并求满足初始条件0|2==πx y 的特解.解 要验证一个函数是否是方程的通解,只要将函数代入方程,看是否恒等,再看函数式中所含的独立的任意常数的个数是否与方程的阶数相同.将x C x y sin )(2+=求一阶导数,得dxdy,cos )(sin 22x C x x x ++= 把y 和dxdy代入方程左边得 x x x y dxdysin 2cot --x x x x C x x C x x x sin 2cot sin )(cos )(sin 222-+-++=.0≡ 因方程两边恒等,且y 中含有一个任意常数,故x C x y sin )(2+=是题设方程的通解. 将初始条件02==πx y 代入通解x C x y sin )(2+=中,得C +=402π .42π-=C 从而所求特解为 .s i n422x x y ⎪⎪⎭⎫⎝⎛-=π 4.写出由下列条件确定的曲线所满足的微分方程.(1) 一曲线通过原点,并且它在(,)x y 处的切线斜率等于2x y +; (2) 一曲线通过点(2,3),它在两坐标轴间的任一切线段均被切点所平分.解:由题意,2y x y '=+,00x y==解:设该曲线的方程为()y f x =,(,)x y 为其上任意一点,该点处的切线斜率为y ',过该点的切线方程为()Y y y X x '-=-。

(word完整版)第十二章习题答案new

(word完整版)第十二章习题答案new

1、分析电子衍射与X 衍射有何异同? 答:相同点:① 都是以满足布拉格方程作为产生衍射的必要条件。

② 两种衍射技术所得到的衍射花样在几何特征上大致相似。

不同点:① 电子波的波长比x 射线短的多,在同样满足布拉格条件时,它的衍射角很小,约为10-2rad 。

而X 射线产生衍射时,其衍射角最大可接近2。

② 在进行电子衍射操作时采用薄晶样品,增加了倒易阵点和爱瓦尔德球相交截的机会,使衍射条件变宽。

③ 因为电子波的波长短,采用爱瓦尔德球图解时,反射球的半径很大,在衍射角θ较小的范围内反射球的球面可以近似地看成是一个平面,从而也可以认为电子衍射产生的衍射斑点大致分布在一个二维倒易截面内。

④ 原子对电子的散射能力远高于它对x 射线的散射能力,故电子衍射束的强度较大,摄取衍射花样时曝光时间仅需数秒钟。

2、倒易点阵与正点阵之间关系如何?倒易点阵与晶体的电子衍射斑点之间有何对应关系?答:倒易点阵是与正点阵相对应的量纲为长度倒数的一个三维空间点阵,通过倒易点阵可以把晶体的电子衍射斑点直接解释成晶体相对应晶面的衍射结果,可以认为电子衍射斑点就是与晶体相对应的倒易点阵某一截面上阵点排列的像。

关系:① 倒易矢量g hkl 垂直于正点阵中对应的(hkl )晶面,或平行于它的法向N hkl ② 倒易点阵中的一个点代表正点阵中的一组晶面③ 倒易矢量的长度等于点阵中的相应晶面间距的倒数,即g hkl =1/d hkl④ 对正交点阵有a *//a ,b *//b ,c *//c ,a *=1/a ,b *=1/b ,c *=1/c 。

⑤ 只有在立方点阵中,晶面法向和同指数的晶向是重合的,即倒易矢量g hkl 是与相应指数的晶向[hkl]平行 ⑥ 某一倒易基矢量垂直于正交点阵中和自己异名的二基矢所成平面。

3、用爱瓦尔德图解法证明布拉格定律。

证:如图,以入射X 射线的波长λ的倒数为半径作一球(厄瓦尔德球),将试样放在球心O 处,入射线经试样与球相交于O*;以O*为倒易原点,若任一倒易点G 落在厄瓦尔德球面上,则G 对应的晶面满足衍射条件产生衍射。

算法导论 第三版 第十二章 答案 英

算法导论 第三版 第十二章 答案 英

3
Algorithm 1 PREORDER-TREE-WALK(x) if x = N IL then print x PREORDER-TREE-WALK(x.left) PREORDER-TREE-WALK(x.right) end if return Algorithm 2 POSTORDER-TREE-WALK(x) if x = N IL then POSTORDER-TREE-WALK(x.left) POSTORDER-TREE-WALK(x.right) print x end if return
Exercise 12.2-3
Algorithm 5 TREE-PREDECESSOR(x) if x.lef t = N IL then return TREE-MAXIMUM(x.left) end if y = x.p while y = N IL and x == y.lef t do x=y y = y.p end while return y Exercise 12.2-4 Suppose we search for 10 in this tree. Then A = {9}, B = {8, 10} and C = ∅, and Professor Bunyan’s claim fails since 8 < 9. 8
Chapter 12
Michelle Bodnar, Andrew Lohr April 12, 2016
Exercise 12.1-1 Anytime that a node has a single child, treat it as the right child, with the left child being NIL 10

算法导论(第二版)习题答案(英文版)

算法导论(第二版)习题答案(英文版)

Last update: December 9, 2002
1.2 − 2 Insertion sort beats merge sort when 8n2 < 64n lg n, ⇒ n < 8 lg n, ⇒ 2n/8 < n. This is true for 2 n 43 (found by using a calculator). Rewrite merge sort to use insertion sort for input of size 43 or less in order to improve the running time. 1−1 We assume that all months are 30 days and all years are 365.
n
Θ
i=1
i
= Θ(n2 )
This holds for both the best- and worst-case running time. 2.2 − 3 Given that each element is equally likely to be the one searched for and the element searched for is present in the array, a linear 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 will be in the second half. Both the worst-case and average-case of L INEAR -S EARCH is Θ(n). 3
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
相关文档
最新文档