中科大算法导论第一,二次和第四次作业答案

合集下载

算法导论课程作业答案

算法导论课程作业答案

算法导论课程作业答案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。

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

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

26.1-2
26.2-1
26.2-4
14
《算法导论(第二版) 》参考答案 26.3-1 s–1–6–t s–2–8–t s–3–7–t 26.3-3
26.4-2 根据 26.22 对每个顶点执行 relabel 操作的次数最多为 2|V|-1 所以对每条边检测次数最多为 2*(2|V|-1)次 O(V) 在剩余网络中最多 2|E|条边 O(E) 所以为 O(VE)
f1[ j ] f 2 [ j 1] t2, j 1 a1, j f 2 [ j ] f1[ j 1] t1, j 1 a2, j
所以有:
f1[ j ] f 2 [ j ] f 2 [ j 1] t2, j 1 a1, j f1[ j 1] t1, j 1 a2, j
6
《算法导论(第二版) 》参考答案
最终答案:((A1A2)((A3A4)(A5A6))) 15.2-2
15.3-2 没有重叠的子问题存在 15.3-4 这题比较简单,由 P328 的(15.6)(15.7)展开两三步就可以看出来. 15.4-3 先初始化 c 数组元素为无穷大
7
《算法导论(第二版) 法, 最好是像书上画一颗递归树然后进行运算。
1
《算法导论(第二版) 》参考答案
注意题目中的要求使用递归树的方法,最好是像书上画一颗递归树然后进行运 算。 4.2.2 证略 4.2.3 由 2 i n 得 i=lgn
T (n) 2 i cn cn
i 0
由课本 P328(15.6) (15.7)式代入,可得:
min( f1[ j 1] a1, j , f 2 [ j 1] t2, j 1 a1, j ) min( f 2 [ j 1] a2, j , f1[ j 1] t1, j 1 a2, j ) min( f1[ j 1], f 2 [ j 1] t2, j 1 ) a1, j min( f 2 [ j 1], f1[ j 1] t1, j 1 ) a2, j f 2 [ j 1] t2, j 1 a1, j f1[ j 1] t1, j 1 a2, j

中科大算法导论期末试卷及答案

中科大算法导论期末试卷及答案
2. (卷 1)已知 f1(n)=Θ(g1(n)),f2(n)=Θ(g2(n)),请问是否有 f1(n)+ f2(n)= Θ( max( g1(n),g2(n) ) )?
解:存在������1(n) + ������2(������) = ������(max(������1(������), ������2(������) 证明: ������1(������) = ������(������1(n)) 则存在 a1>0,a2>0, n1>0 使得 n>n1 时有 ������1 ∗ ������1(n) < ������1(������) < ������2 ∗ ������1(n) ������2(������) = ������(������2(n)) 则存在 b1>0,b2>0, n2>0 使得 n>n2 时有 ������1 ∗ ������2(n) < ������2(������) < ������2 ∗ ������2(n) 取 c1=min(a1,b1) >0, c2=2*max(a2,b2) >0, n0=max(n1,n2)>0 当 n>n0 时,有 ������1(n) + ������2(������) > ������1 ∗ ������1(n) + ������1 ∗ ������2(n)
= ������2(������������ − 5 ������������������������) − 2������ > 2������3 − 2������ > 2������0(������02 − 1) =12 即当 c=7, n0=2 时,对 n>n0, 5������2������������������������ + 2������ < ������������3恒成立, 5������2������������������������ + 2������ = ������(������3)

算法导论习题答案 (5)

算法导论习题答案 (5)
c(j) = min {c(i − 1) + linecost(i, j)} .
Three-hole punch your paper on submissions. You will often be called upon to “give an algorithm” to solve a certain problem. Your write-up should take the form of a short essay. A topic paragraph should summarize the problem you are solving and what your results are. The body of the essay should provide the following:
(a) Argue that this problem exhibits optimal substructure.
Solution: First, notice that linecost(i, j) is defined to be � if the words i through j do not fit on a line to guarantee that no lines in the optimal solution overflow. (This relies on the assumption that the length of each word is not more than M .) Second, notice that linecost(i, j) is defined to be 0 when j = n, where n is the total number of words; only the actual last line has zero cost, not the recursive last lines of subprob­ lems, which, since they are not the last line overall, have the same cost formula as any other line.

中科院计算机算法设计与分析各章作业 历年习题.

中科院计算机算法设计与分析各章作业 历年习题.

设在新机器上用 t 秒时间能解决输入规模为 N 的问题,则
由于新复杂度
T新 ( N )

N2
,新机器的运行速度为 t新

t0 64

代入关系式T新(N) *t新 t ,得
N
2
*
t0 64

t

3* 2n
*t0

解得
N 8 3 2n
3)若进一步改进算法,最新的算法的时间复杂度为 T (n) 8 ,其余条件不
1).f (n) O(F(n)), g(n) O(G(n)) f (n) / g(n) O(F(n) / G(n)) ;错 2).f (n) O(F(n)), g(n) O(G(n)) f (n) / g(n) (F(n) / G(n)) ;错 3).f (n) O(F(n)), g(n) O(G(n)) f (n) / g(n) (F(n) / G(n)) ;错 4).f (n) (F(n)), g(n) (G(n)) f (n) / g(n) (F(n) / G(n)) ;错 5).f (n) (F(n)), g(n) (G(n)) f (n) / g(n) (F(n) / G(n)) 。错 6). f (n) (F(n)), g(n) (G(n)) f (n) / g(n) (F(n) / G(n)) 对
for(int i=0; i<m; i++) for(int j=0; j<p; j++){ T sum=0; for(int k=0; k<n; k++) Sum+=a[i][k]*b[k][j]; C[i][j]=sum; }

第四次作业答案

第四次作业答案

1.235。

由于本题求二叉树的结点数最多是多少,第7层共有27-1=64个结点,已知有10个叶子,其余54个结点均为分支结点。

它在第八层上有108个叶子结点。

所以该二叉树的结点数最多可达(27-1+108)=235。

(注意;本题并未明说完全二叉树的高度,但根据题意,只能8层。

)2.1023(=210-1)3.证明:设度为1和2 的结点数是n1和n2,则二叉树结点数n为n=m+n1+n2 (1)由于二叉树根结点没有分枝所指,度为1和2的结点各有1个和2个分枝,度为0 的结点没有分枝,故二叉树的结点数n与分枝数B有如下关系n=B+1=n1+2*n2+1 (2)由(1)和(2),得n2=m-1。

即n个结点的二叉树,若叶子结点数是m,则非叶子结点中有(m-1)个度为2,其余度为1。

4.根据顺序存储的完全二叉树的性质,编号为i的结点的双亲的编号是⎣i/2⎦,故A[i]和A[j]的最近公共祖先可如下求出:while(i/2!=j/2)if(i>j) i=i/2; else j=j/2;退出while后,若i/2=0,则最近公共祖先为根结点,否则最近公共祖先是i/2。

5. 结点个数在20到40的满二叉树且结点数是素数的数是31,其叶子数是16。

6.设分枝结点和叶子结点数分别是为n k和n0,因此有n=n0+n k (1) 另外从树的分枝数B与结点的关系有 n=B+1=K*n k +1 (2)由(1)和(2)有 n0=n-n k=(n(K-1)+1)/K7.(1)设n=1,则e=0+2*1=2(只有一个根结点时,有两个外部结点),公式成立。

设有n个结点时,公式成立,即E n=I n+2n (1)现在要证明,当有n+1个结点时公式成立。

增加一个内部结点,设路径长度为l,则I n+1=I n+l (2)该内部结点,其实是从一个外部结点变来的,即这时相当于也增加了一个外部结点(原外部结点变成内部结点时,增加两个外部结点),则E n+1=E n+l+2 (3)由(1)和(2),则(3)推导为E n+1=I n+2n+l+2=I n+1-l+2n+l+2=I n+1+2(n+1)故命题成立8.前序遍历是“根左右”,中序遍历是“左根右”,后序遍历是“左右根”。

中科大算法导论第一,二次和第四次作业答案

中科大算法导论第一,二次和第四次作业答案
第一次作业
2.2-3 再次考虑线性查找问题 (见练习2.1-3)。在平均情况 下,需要检查输入序列中的多 少个元素?假定待查找的元素 是数组中任何一个元素的可能 性是相等的。在最坏情况下有 怎样呢?用Θ形式表示的话,线 性查找的平均情况和最坏情况 运行时间怎样?对你的答案加 以说明。 • 线性查找问题 • 输入:一列数A=<a1,a2,…,an>和一 个值v。 • 输出:下标i,使得v=A[i],或者当 v不在A中出现时为NIL。 • 平均情况下需要查找 (1+2+…+n)/n=(n+1)/2 • 最坏情况下即最后一个元素为待 查找元素,需要查找n个。 • 故平均情况和最坏情况的运行时 间都为Θ(n)。
• 2.3-2改写MERGE过程,使之不使 用哨兵元素,而是在一旦数组L或R 中的所有元素都被复制回数组A后, 就立即停止,再将另一个数组中 余下的元素复制回数组A中。 • MERGE(A,p,q,r) 1. n1←q-p+1 2. n2 ←r-q 3. create arrays L[1..n1] and R[1..n2] 4. for i ←1 to n1 5. do L*i+ ←A*p+i-1] 6. for j ←1 to n2 7. do R*j+ ←A*q+j+ 8. i ←1 9. j ←1
10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21.
k ←p while((i<=n1) and (j<=n2)) do if L[i]<=R[j] do A[k]=L[i] i++ else do A[k]=R[j] j++ k++ while(i<=n1) do A[k++]=L[i++] while(j<=n2) do A[k++]=R[j++]

《算法导论》习题答案

《算法导论》习题答案
i 1
n/2
n! nn , n! o(nn )
3.2.4 是否多项式有界 lg n !与 lg lg n !
设lgn=m,则 m! 2 m ( )m e2m ( )m em(ln m1) mln m1 nln ln n
∴lgn!不是多项式有界的。
T (n) O(lg n)
4.1.2 证明 T (n) 2T (n) n 的解为 O(n lg n)
设 T (n) c n lg n
T (n) 2c n lg n n c lg n n n c(n 1) lg(n / 2) n cn lg n c lg n cn n cn(lg n 1) n c(lg n 2n)
虽然用二分查找法可以将查找正确位置的时间复杂度降下来,但 是移位操作的复杂度并没有减少, 所以最坏情况下该算法的时间复杂 度依然是 (n2 )
2.3-7 给出一个算法, 使得其能在 (n lg n) 的时间内找出在一个 n 元
素的整数数组内,是否存在两个元素之和为 x
首先利用快速排序将数组排序,时间 (n lg n) ,然后再进行查找:
sin(n / 2) 2 1,所以 af (n / b) cf (n) 不满足。 2(sin n 2)
4.1.6 计算 T (n) 2T (
令 m lg n, T (2 ) 2T (2
m m/ 2
n ) 1 的解
) 1
令 T(n)=S(m),则 S (m) 2S (m / 2) 1 其解为 S (m) (m),T (n) S (m) (lg n)
4.2 The recursion-tree method 4.2.1 4.2.2 4.2.3 4.2.5 略
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

15.
else do A[k]=R[j]
16.
j++
17.
k++
18. while(i<=n1) 19. do A[k++]=L[i++]
6. for j ←1 to n2 7. do R[j] ←A[q+j]
20. while(j<=n2) 21. do A[k++]=R[j++]
8. i ←1
9. j ←1
个递归的解也是Ω(nlgn)。得到解为Θ(nlgn)。
证明:假设当n 1时,T(n) 1是唯一的边界条件, T (n) cn lg n c 1 lg1 0,假设T(n) cn lg n对n / 2成立
T (n) 2T (n / 2) n 2cn / 2lg(n / 2) n
cn lg(n / 2) n cn lg n - (c 1)n cn lg n(当c 1时) 所以,T (n) O(n lg n),下面证T (n) (n lg n)
2
2
22
c n 1 lg( n 1) c n lg( n ) (n)
2
2
22
c n 1 lg(n 1) c n 1 c n lg(n) c n lg 2 (n)
2
2
2
2
cn lg(n 1) c lg(n 1) c n lg(n) c (2n 1) (n)
2
2
2
2
cn lg n c n(lg(n 1) lg n) c lg(n 1) c (2n 1) (n)
再证(n lg n),即要证T (n) cn lg n
设T (n / 2) c(n / 2) lg(n / 2)成立,
则T (n) T (n / 2) T (n / 2) (n)
c(n
/
2)
lg(n
/
2)
c(n
/
2)
lg(n
/
2)
(n)
c
n 2
lg(
n) 2
c
n
2
1
lg(=0,1,...n-1区间上,当q=0或q=n-1时,q2+(n-q-1)2取得最 大值。
• 求导,取极值。 • 或者进行变换
8.2-4在O(1)的时间内,回答出输入的整数中有多少个落在区 间[a...b]内。给出的算法的预处理时间为O(n+k)
• 利用计数排序,由于在计数排序中有一个存储数值个数的临时存储区 C[0...k],利用这个数组即可
则T (n) T (n / 2) T (n / 2) (n) c(n / 2) lg(n / 2) c(n / 2) lg(n / 2) (
c n 1 lg( n 1) c n lg( n ) (n)
2
2
22
c n 1 lg( n 1) c n lg( n ) (n) cn lg n
第二次作业
• 3.2-3 证明等式lg(n!)=Θ(nlgn)。并证明等式n!=ω(2n)和
n!=o(nn)。
证明:由于(
n
)
n 2
n! nn , 两边取对数可知:lg(n!) (n lg n)
2
3.2-4函数⌈lgn⌉! 是否多项式有界?函数⌈lglgn⌉! 呢
• 4.1-2 证明T (n) 2T (n / 2 n的解为O(n lg n). 证明这
c n lg( n ) c n 1 lg( n 1) (n)
22
2
2
c n lg n c n lg 2 c n 1 lg(n 1) c n 1 lg 2 (n)
2
2
2
2
c n lg n c (2n 1) c n lg(n 1) c lg(n 1) (n)
2
2
2
2
cn lg n c n lg n c n lg(n 1) c (2n 1) c lg(n 1) (n) 0
c n lg(n) c n 1lg(n 1) (n) cn lg n
22
2
2
c n lg(n) c n 1lg(n 1) (n) cn lg n 0
22
2
2
c n lg(1 1) c lg(n 1) 2n 1c (n) 0
2 n2
2
lg(1 1)是递增函数,取n 2,有lg(1 1) 1
或者修改PARTITION(A, p, r),增加对A[i]==x时的处理。对于A[i]==x的数据, 一半放在x左边,一半放在x右边 通过设置一个flag 初始为1 当flag>0时 才进行交换 交换flag=-flag
int Partition(int A[], int p, int r)
{ int x = A[r],i=p-1; int flag = 1; for (int j = p;j< r-1;j++) { if (x >=A[i]&&flag>0)//x=A[i]时,flag大于0和小于0的数量约为一半, { swap(A[i],A[j]); } if (x ==A[i]) { flag=-flag;//这样就能让i++次数减半。 } } swap(A[i + 1],A[r]); return i + 1
取c 1/ 3,n 2时,有T (n) cn lg n,T (n) (n lg n)
所以T (n) (n lg n).
• 4.1-4证明合并排序算法的“准确”递归式(4.2)的解为
Θ(nlgn)
(1)
如果n 1
T (n) T (n / 2) T (n / 2) (n) 如果n 1
n
n
c n lg(1 1) c lg(n 1) 2n 1c (n) (n) c lg(n 1) 3n 1c
2 n2
2
2
2
c (n 1 lg(n 1)) ((1) 2c)n 2
取n 3,有n 1 lg(n 1) 0,取c 1 (1),有((1) 2c) 0 2
c n lg(n) c n 1lg(n 1) (n) cn lg n
• [a..b]区间整数个数为C[b]-C[a-1]
• 题目已经提示运用桶排序,主要是正确设置桶
• 点均匀分布,则每个桶的尺寸应相等,即每个桶在圆中所占据的面积相等。 把圆分为n个部分,第一个部分是以原点为圆心的圆,其他部分是以原点为圆 心的圆环。
第一次作业
2.2-3 再次考虑线性查找问题 (见练习2.1-3)。在平均情况 下,需要检查输入序列中的多 少个元素?假定待查找的元素 是数组中任何一个元素的可能 性是相等的。在最坏情况下有 怎样呢?用Θ形式表示的话,线 性查找的平均情况和最坏情况 运行时间怎样?对你的答案加
以说明。
• 线性查找问题
• 输入:一列数A=<a1,a2,…,an>和一 个值v。
2
2
2
lg(n 1) lg n lg(1 1 )是递增函数,取n 1,有lg(1 1 ) 1
n
n
则有T (n) cn lg n c n c lg(n 1) 2n 1 c (n)
22
2
cn lg n c (lg(n 1) n 1) n (4(1) c)
2
2
4
取n 3,有lg(n 1) n 1 0, 取c 4(1),有4(1) c 0 2
2
n2
2
2
2
c (2 lg(n 1) n 2) n (4(1) c)
4
4
取n 3,有2 lg(n 1) n 2 0, 取c 4(1),有4(1) c 0
c n 1 lg( n 1) c n lg( n ) (n) cn lg n
2
2
22
即T (n) cn lg n
2
2
2
2
cn lg n c n(lg(n 1) lg n) c (2n 1) c lg(n 1) (n)
2
2
2
lg(n 1) lg n lg(1 1 )是递增函数,取n 2, 有 lg(1 1 ) 1
n
n
T (n) cn lg n c (3n 1) c lg(n 1) (n)
}
• 7.2-3 证明:当数组A包含不同的元素、且按降序排序时, QUICKSORT的运行时间为Θ(n2)。
• 证明:数组元素各不相同,且按降序排列时,每次递归调 用都会产生一个元素数目为n-1的分块和一个1的分块。 故有T(n)=T(n-1)+Θ(n) 有T(n)=T(n-1)+cn+d =T(n-2)+cn+c(n-1)+2d =T(n-3)+cn+c(n-1)+c(n-2)+3d =... =T(1)+cn+c(n-1)+c(n-2)+...+c(1)+nd =T(1)+cn(n+1)/2+nd =Θ(n2)
2
2
cn lg n 1 (1 c)n c lg(n 1) c 0 n
lg(n 1) lg(1 1)是递增函数,取n 2,则lg(n 1) 1
n
n
n
cn lg n 1 (1 c)n c lg(n 1) c (1 2c)n c lg(n 1) c n
取c 1/ 3,则(1 2c)n c lg(n 1) c c[n lg(n 1) 1] 0
用哨兵元素,而是在一旦数组L或R 中的所有元素都被复制回数组A后,
11. 12.
while((i<=n1) and (j<=n2)) do if L[i]<=R[j]
相关文档
最新文档