算法导论第十九章答案
算法设计与分析知到章节答案智慧树2023年天津大学

算法设计与分析知到章节测试答案智慧树2023年最新天津大学第一章测试1.下列关于效率的说法正确的是()。
参考答案:提高程序效率的根本途径在于选择良好的设计方法,数据结构与算法;效率主要指处理机时间和存储器容量两个方面;效率是一个性能要求,其目标应该在需求分析时给出2.算法的时间复杂度取决于()。
参考答案:问题的规模;待处理数据的初态3.计算机算法指的是()。
参考答案:解决问题的有限运算序列4.归并排序法的时间复杂度和空间复杂度分别是()。
参考答案:O(nlog2n);O(n)5.将长度分别为m,n的两个单链表合并为一个单链表的时间复杂度为O(m+n)。
()参考答案:错6.用渐进表示法分析算法复杂度的增长趋势。
()参考答案:对7.算法分析的两个主要方面是时间复杂度和空间复杂度的分析。
()参考答案:对8.某算法所需时间由以下方程表示,求出该算法时间复杂度()。
参考答案:O(nlog2n)9.下列代码的时间复杂度是()。
参考答案:O(log2N)10.下列算法为在数组A[0,...,n-1]中找出最大值和最小值的元素,其平均比较次数为()。
参考答案:3n/2-3/2第二章测试1.可用Master方法求解的递归方程的形式为()。
参考答案:T(n)=aT(n/b)+f(n) , a≥1, b>1, 为整数, f(n)>0.2.参考答案:对3.假定,, 递归方程的解是. ( )参考答案:对4.假设数组A包含n个不同的元素,需要从数组A中找出n/2个元素,要求所找的n/2个元素的中点元素也是数组A的中点元素。
针对该问题的任何算法需要的时间复杂度的下限必为。
( )参考答案:错5.使用Master方法求解递归方程的解为().参考答案:6.考虑包含n个二维坐标点的集合S,其中n为偶数,且所有坐标点中的均不相同。
一条竖直的直线若能把S集合分成左右两部分坐标点个数相同的子集合,则称直线L为集合S的一条分界线。
若给定集合S,则可在时间内找到这条分界线L。
算法导论复习资料

算法导论复习资料一、选择题:第一章的概念、术语。
二、考点分析:1、复杂度的渐进表示,复杂度分析。
2、正确性证明。
考点:1)正确性分析(冒泡,归并,选择);2)复杂度分析(渐进表示O,Q,©,替换法证明,先猜想,然后给出递归方程)。
循环不变性的三个性质:1)初始化:它在循环的第一轮迭代开始之前,应该是正确的;2)保持:如果在循环的某一次迭代开始之前它是正确的,那么,在下一次迭代开始之前,它也应该保持正确;3)当循环结束时,不变式给了我们一个有用的性质,它有助于表明算法是正确的。
插入排序算法:INSERTION-SORT(A)1 for j ←2 to length[A]2 do key ←A[j]3 ▹Insert A[j] into the sorted sequence A[1,j - 1].4 i ←j - 15 while i > 0 and A[i] > key6 do A[i + 1] ←A[i]7 i ←i - 18 A[i + 1] ←key插入排序的正确性证明:课本11页。
归并排序算法:课本17页及19页。
归并排序的正确性分析:课本20页。
3、分治法(基本步骤,复杂度分析)。
——许多问题都可以递归求解考点:快速排序,归并排序,渐进排序,例如:12球里面有一个坏球,怎样用最少的次数找出来。
(解:共有24种状态,至少称重3次可以找出不同的球)不是考点:线性时间选择,最接近点对,斯特拉算法求解。
解:基本步骤:一、分解:将原问题分解成一系列的子问题;二、解决:递归地解各子问题。
若子问题足够小,则直接求解;三、合并:将子问题的结果合并成原问题的解。
复杂度分析:分分治算法中的递归式是基于基本模式中的三个步骤的,T(n)为一个规模为n的运行时间,得到递归式T(n)=Q(1) n<=cT(n)=aT(n/b)+D(n)+C(n) n>c附加习题:请给出一个运行时间为Q(nlgn)的算法,使之能在给定的一个由n个整数构成的集合S和另一个整数x时,判断出S中是否存在有两个其和等于x的元素。
《算法导论》习题答案12、13、14章

第9章 中位数和顺序统计学
9.3-2
大于x的数至少有3n/10-6, n≥140时,易证3n/10-6 ≥n/4 小于x的数同理。
9.3-4
通过比较得到第i小元素,每次保留比较信息。 在比较过程中比这个元素小的元素构成的集合即为i – 1个 小数集合,而比较过程中比这个元素大的元素则构成了n – i 个大元素集合。不需要增加比较次数。
Preprocessing(A,k) for i←0 to k do C[i]←0 for j←1 to length[A] do C[A[j]] ←C[A[j]]+1 for i←1 to k do C[i] ←C[i]+C[i-1] Query(C,k,a,b) if b<a or b<1 or a>k return 0 if a<1 then a=1 if b>k then b=k if a≠1 then return C[b]-C[a-1] else return C[b]
0 +1
k +1
k +1
( k +1) +1
第6章 堆排序
6.4-3 不论递增还是递减,时间均为O(nlgn) 6.4-4 最坏情况下,n-1次调用MAX-HEAPIFY,运 行时间为O(nlgn)
第6章 堆排序
6.5-3
HEAP-MINIMUM(A) if heap-size[A]<1 then error”heap underflow” else return A[1] HEAP-EXTRACT-MIN(A) if heap-size[A]<1 then error”heap underflow” min<-A[1] A[1]<-A[heap-size[A]] heap-size[A]<-heap-size[A]-1 MIN-HEAPIFY(A,1) return min HEAP-DECREASE-KEY(A,i,key) if key>A[i] then error A[i]<-key while i>1 and A[PARENT(i)>A[i] do exchange A[i]<->A[PARENT(i)] i<-PARENT(i) MIN-HEAP-INSERT(A,key) heap-size[A]<-heap-size[A]+1 A[heap-size[A]]<-+∞ HEAP-DECREASE-KEY(A,heap-size[A],key)
中科大算法导论第一二次和第四次作业答案PPT学习教案

取c 1/ 3,n 2时,有T (n) cn lg n,T (n) (n lg n)
所以T (n) (n lg n). 第5页/共18页
4.1-4证明合并排序算法的“准确”递归式(4.2)的解为
Θ(nlgn)
(1)
如果n 1
T (n) T (n / 2) T (n / 2) (n) 如果n 1
c(n
/
2)
lg(
n
/
2)
c(n/2)Fra biblioteklg(
n
/
2)
(n)
c
n 2
lg(
n 2
)
c
n
1 2
lg(
n
2
1)
(n)
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
14.
i++
15.
else do A[k]=R[j]
16.
j++
17.
k++
18. while(i<=n1) 19. do A[k++]=L[i++]
20. while(j<=n2)
21. do A[k++]=R[j++]
第1页/共18页
第二次作业
3.2-3 证明等式lg(n!)=Θ(nlgn)。并证明等式n!=ω(2n)和
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
算法导论习题答案 (5)

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.
算法导论作业5答案概要

6 Problem 5-2. Join operation on red-black trees Handout 22: Problem Set 5 The join operation takes two dynamic� sets �� and �� and an element � such that for any ��������� and ������� , we have key ������� key ����� key ����� . It returns a set ���������������� . In this problem, we investigate how to implement the join operation on red-black trees. (a Given a red-black tree � , we store its black-height as the field bh ��. Argue that this field can be maintained by RB-I NSERT and RB-D ELETE (as given in the textbook, on pages 280 and 288 respectively without requiring extra storage in the nodes of the tree and without increasing the asymptotic running times. Show that while descending through � , we can determine the black-height of each node we visit in ������� time per node visited. Solution: Starting at the root, we can proceed to a leaf, couting the number of black nodes on the path. This does not require any extra storage in the nodes of the tree and will take ����������� time. Since RB-I NSERT and RB-D ELETE also run in����������� time, the asymptotic running time is not increased. While descending through � , we decrement bh �� by � 1 everytime we encounter a blacknode. The black-height of a node, � , is then bh �� minus the number of black nodes encountered (excluding node � itself. This decrement can be done in ������� time per node visited. We wish to implement the operation RB-J OIN ������������� , which may destroy �� and �� and returns a red-black tree ���������������� . Let � be the total number of nodes in �� and �� . (b Assume that bh ������ bh ���� . Describe an ����������� -time algorithm that finds a black � node ����� in � with the largest key from among those nodes whose black-height is bh � . Solution: Since �� is a binary search tree, the largest element at any level is on the rightmost path. So, we decend down the rightmost path, calculating bh at each node (as described ���� in the previous part, until we reach the black node whose black-height is bh � , which is what we want. Thus the running time is at most the height of the tree, i.e. ����������� . (Calculating the black-height takes ������� per node, as shown in the previous part. ���� (c Let ��� be the subtree rooted at � . Describe how �������������� can replace ��� in ������� time without destroying the binary-search-tree property. Solution:Handout 22: Problem Set 5 Insert � into where � was in �� . Form �������������� by letting ��� be the left subtree� of � , and ��� be the right subtree of � . Given that this join operation is such that � key ������� key����� key ����� where ������� and ������� , the binary search tree property is maintained and this operation takes ������� time. Consider the following red-black properties: 7 � every node is either red or black � every leaf is black nodes (d What color should we make � so that the above red-black properties are maintained? Solution: We should make � red. Since ��� already has black-height = bh ������� , � must be red to �������������� = bh �������maintain the same black-height, bh ��� Consider the following red-black properties: �for each node, all paths from the node to descendant leaves contain the same number of black � the root is black � if a node is red, then both its children are black (e Describe how the above two properties can be enforced in ����������� time. Solution:Use RB-I NSERT-F IXUP on the new tree, to perform the recoloring and rotations nec-essary to enforce these two properties. We know that RB-I NSERT-F IXUP runs in����������� time, thus we conclude that the enforcement can be done in����������� time. (f Argue that the running time of RB-J OIN is����������� . Solution: RB-J OIN is implemented by using all the previous parts: The black-height can be calculated and maintained in ������� time. The required black node, � , can be found in ����������� time. Then, the join is done in�����������time, and finally, after assigning � the right color, the red-black tree properties can be enforced in ����������� time. So the total runing time is �����������。
算法导论参考问题详解
第二章算法入门由于时间问题有些问题没有写的很仔细,而且估计这里会存在不少不恰当之处。
另,思考题2-3 关于霍纳规则,有些部分没有完成,故没把解答写上去,我对其 c 问题有疑问,请有解答方法者提供个意见。
给出的代码目前也仅仅为解决问题,没有做优化,请见谅,等有时间了我再好好修改。
插入排序算法伪代码INSERTION-SORT(A)1 for j ←2 to length[A]2 do key ←A[j]3 Insert A[j] into the sorted sequence A[1..j-1]4 i ←j-15 while i > 0 and A[i] >6 do A[i+1]←A[i]7 i ←i − 18 A[i+1]←keyC#对揑入排序算法的实现:public static void InsertionSort<T>(T[] Input) where T:IComparable<T>{T key;int i;for (int j = 1; j < Input.Length; j++){key = Input[j];i = j - 1;for (; i >= 0 && Input[i].CompareTo(key)>0;i-- )Input[i + 1] = Input[i];Input[i+1]=key;}}揑入算法的设计使用的是增量(incremental)方法:在排好子数组A[1..j-1]后,将元素A[ j]揑入,形成排好序的子数组A[1..j]这里需要注意的是由于大部分编程语言的数组都是从0开始算起,这个不伪代码认为的数组的数是第1个有所丌同,一般要注意有几个关键值要比伪代码的小1.如果按照大部分计算机编程语言的思路,修改为:INSERTION-SORT(A)1 for j ← 1 to length[A]2 do key ←A[j]3 i ←j-14 while i ≥ 0 and A[i] >5 do A[i+1]←A[i]6 i ←i − 17 A[i+1]←key循环丌变式(Loop Invariant)是证明算法正确性的一个重要工具。
算法导论Exercises9.3-6
算法导论Exercises9.3-6Problem Description:The kth quantiles of an n-element set are the k - 1 order statistics that divide the sorted set intok equal-sized sets (to within 1). Give an O(n lg k)-time algorithm to list the kth quantiles of a set.问题描述:⼀个集合的第k分位数是指这样k - 1个数:若将⼀个⼤⼩为n的集合从⼩到⼤排好序,这k - 1个数能将这个有序的数列分为k组,并且每组元素的个数相差不超过1。
现给出⼀个长为n的数组(⽆序),要求以O(n lg k)的时间复杂度找到其 k 分位数。
⽐如:8 4 5 3 2 6 1 7 9 排序后是 1 2 3 4 5 6 7 8 9其 3 分位数是 3 6,将排序后的数列分成 1 2 3; 4 5 6; 7 8 9 三段,每段元素个数均为3。
2 934 3 3 9 1 3 8 1 排序后是 1 1 2 3 3 3 3 4 8 9 9其 4 分位数是 2 3 8,将排序后的数列分成 1 1 2; 3 3 3; 3 4 8; 9 9 四段,每段元素个数分别为 3 3 3 2。
解决⽅案:⾸先,我们知道在⼀个长度为n的⽆序数组⾥找到第 i 个数的时间复杂度是O(n),具体见⾥的算法ithSmallestLinear 。
所以最直观的⽅法是调⽤k次ithSmallestLinear,但这样总时间复杂度是O(kn)⽽不是题⽬要求的O(n lg k)。
为了实现从 k 到 lgk 的突破,必然采⽤分治的⽅法(我们先假定 n/k 刚好整除):0、如果 k == 1 ,return1、i = k / 2 ……O(1)将数组 a 划分成两部分 A 和 B,使得 A 中所有元素不⼤于 B,且 A 中元素的个数为 tmpSize = (n / k) * i。
算法导论第九章习题答案(第三版)IntroductiontoAlgorithm
算法导论第九章习题答案(第三版)IntroductiontoAlgorithm Exercise
9.1-1
对所有的元素,两个⼀组进⾏⽐较,共需n-1次⽐较,可以构成⼀棵⼆叉树,最⼩的元素在树的根结点上,接下来,画出⼆叉树,可以很容易的看出共需lgn-1次⽐较,所以共需n+lgn-2次⽐较才可以找出第⼆⼩的元素。
9.1-2
略。
9.2-1
在randomized-select中,对于长度为0的数组,此时p=r,直接返回A[p],所以不会进⾏递归调⽤。
9.2-2
略。
9.2-3
RANDOMIZED-SELECT(A,p,r,i){
while(true){
if(p==r)
return A[p];
q=RANDOMIZED-PARTITION(A,p,r);
k=q-p+1;
if(i==k)
return A[q];
else if(i<k)
q--;
else{
q++;
i-=k;
}
}
}
9.2-4
每次都以最⼤的元素进⾏划分即可。
9.3-1
数学计算,根据书中例题仿照分析即可。
9.3-3
随机化
9.3-5
类似主元划分,只要把⿊箱⼦输出的值作为主元划分去选择即可。
9.3-6
多重⼆分即可。
9.3-7
算出中位数,之后算出每⼀个数与中位数的差即可。
9.3-8
分别取两个数组的中位数进⾏⽐较,如果两个中位数相等,那么即为所求,否则,取中位数较⼩的⼀个的右边,取较⼤的⼀个的右边,直到就剩4个元素为⽌,这时候只要求这4个元素的中位数即可。
算法导论第二章答案
第二章算法入门由于时间问题有些问题没有写的很仔细,而且估计这里会存在不少不恰当之处。
另,思考题2-3 关于霍纳规则,有些部分没有完成,故没把解答写上去,我对其 c 问题有疑问,请有解答方法者提供个意见。
给出的代码目前也仅仅为解决问题,没有做优化,请见谅,等有时间了我再好好修改。
插入排序算法伪代码INSERTION-SORT(A)1 for j ←2 to length[A]2 do key ←A[j]3 Insert A[j] into the sorted sequence A[1..j-1]4 i ←j-15 while i > 0 and A[i] > key6 do A[i+1]←A[i]7 i ←i − 18 A[i+1]←keyC#对揑入排序算法的实现:public static void InsertionSort<T>(T[] Input) where T:IComparable<T>{T key;int i;for (int j = 1; j < Input.Length; j++){key = Input[j];i = j - 1;for (; i >= 0 && Input[i].CompareTo(key)>0;i-- )Input[i + 1] = Input[i];Input[i+1]=key;}}揑入算法的设计使用的是增量(incremental)方法:在排好子数组A[1..j-1]后,将元素A[ j]揑入,形成排好序的子数组A[1..j]这里需要注意的是由于大部分编程语言的数组都是从0开始算起,这个不伪代码认为的数组的数是第1个有所丌同,一般要注意有几个关键值要比伪代码的小1.如果按照大部分计算机编程语言的思路,修改为:INSERTION-SORT(A)1 for j ← 1 to length[A]2 do key ←A[j]3 i ←j-14 while i ≥ 0 and A[i] > key5 do A[i+1]←A[i]6 i ← i − 17 A[i+1]←key循环丌变式(Loop Invariant)是证明算法正确性的一个重要工具。