5.《算法
与分析》试
库
《算法分析与设计》试题库(一)
一、 选择题
1.应用Johnson法则的流水作业调度采用的算法是(D)
A. 贪心算法 B. 分支限界法 C.分治法 D. 动态规划算法
2.Hanoi塔问题如下图所示。现要求将塔座A上的的所有圆盘移到塔座B上,并
仍按同样顺序叠置。移动圆盘时遵守Hanoi塔问题的移动规则。由此设计出解
Hanoi塔问题的递归算法正确的为:(B)
A. void hanoi(int n, int A, int C, int B)
{ if (n > 0)
Hanoi塔 {
hanoi(n-1,A,C, B); move(n,a,b);
hanoi(n-1, C, B, A);
} }
B. void hanoi(int n, int A, int B, int C)
{
if (n > 0)
{
hanoi(n-1, A, C, B);
move(n,a,b);
hanoi(n-1, C, B, A);
}
}
C. void hanoi(int n, int C, int B, int A)
{
if (n > 0)
{
hanoi(n-1, A, C, B);
move(n,a,b);
hanoi(n-1, C, B, A);
}
}
used after the removal of plastic-wrapped, concrete curing period of not less than 7 days. Curing agent emulsion curing agent should be used to avoid yellowing on the surface of the concrete. Detailed rules for the implementation of specific implementation requirements 8 compliance and excellence project 8.1 standard of excellence presided over by the Chief Engineer of the project, implemented in full. 8.2 professional
D. void hanoi(int n, int C, int A, int B)
{
if (n > 0)
{
hanoi(n-1, A, C, B);
move(n,a,b);
hanoi(n-1, C, B, A);
}
}
3. 动态规划算法的基本要素为(C)
A. 最优子结构性质与贪心选择性质
B(重叠子问题性质与贪心选择性质
C(最优子结构性质与重叠子问题性质
D. 预排序与递归调用
,,4. 算法分析中,记号O表示(B), 记号表示(A), 记号表示(D)。
A.渐进下界
B.渐进上界
C.非紧上界
D.紧渐进界
E.非紧下界
5. 以下关于渐进记号的性质是正确的有:(A) A. f(n)(g(n)),g(n)(h(n))f(n)(h(n)),,,,,,,
B. f(n)O(g(n)),g(n)O(h(n))h(n)O(f(n)),,,,
C. O(f(n))+O(g(n)) = O(min{f(n),g(n)})
D. f(n)O(g(n))g(n)O(f(n)),,,
6. 能采用贪心算法求最优解的问题,一般具有的重要性质为:(A)
A. 最优子结构性质与贪心选择性质
B(重叠子问题性质与贪心选择性质
C(最优子结构性质与重叠子问题性质 D. 预排序与递归调用
used after the removal of plastic-wrapped, concrete curing period of not less than 7 days. Curing agent emulsion curing agent should be used to avoid yellowing on the surface of the concrete. Detailed rules for the implementation of specific implementation requirements 8 compliance and excellence project 8.1 standard of excellence presided over by the Chief Engineer of the project, implemented in full. 8.2 professional
7. 回溯法在问题的解空间树中,按(D)策略,从根结点出发搜索解空间树。
A.广度优先 B. 活结点优先 C.扩展结点优先 D. 深度优先 8. 分支限界法在问题的解空间树中,按(A)策略,从根结点出发搜索解空间树。
A( 广度优先 B. 活结点优先 C.扩展结点优先 D. 深度优先 9. 程序块(A)是回溯法中遍历排列树的算法框架程序。
A. void backtrack (int t)
{
if (t>n) output(x); else
for (int i=t;i<=n;i++) {
swap(x[t], x[i]); if (legal(t)) backtrack(t+1);
swap(x[t], x[i]);
} }
B. void backtrack (int t)
{
if (t>n) output(x); else
for (int i=0;i<=1;i++) {
x[t]=i; if (legal(t)) backtrack(t+1);
}
}
void backtrack (int t) C.
{ if (t>n) output(x);
else
for (int i=0;i<=1;i++) { x[t]=i;
if (legal(t)) backtrack(t-1);
} }
D.
void backtrack (int t) {
if (t>n) output(x);
else for (int i=t;i<=n;i++) {
swap(x[t], x[i]); used after the removal of plastic-wrapped, concrete curing period of not less than 7 days. Curing agent emulsion curing agent should be used to avoid yellowing on the surface of the concrete. Detailed rules for the implementation of specific implementation requirements 8 compliance and excellence project 8.1 standard of excellence presided over by the Chief Engineer of the project, implemented in full. 8.2 professional
if (legal(t)) backtrack(t+1);
}
}
10. 回溯法的效率不依赖于以下哪一个因素,(C )
A. 产生x[k]的时间;
B. 满足显约束的x[k]值的个数;
C. 问题的解空间的形式;
D. 计算上界函数bound的时间;
11. 常见的两种分支限界法为(D)
A. 广度优先分支限界法与深度优先分支限界法;
B. 队列式(FIFO)分支限界法与堆栈式分支限界法;
C. 排列树法与子集树法;
D. 队列式(FIFO)分支限界法与优先队列式分支限界法;
12. k带图灵机的空间复杂性S(n)是指(B)
A. k带图灵机处理所有长度为n的输入时,在某条带上所使用过的最大方格数。 B. k带图灵机处理所有长度为n的输入时,在k条带上所使用过的方格数的总
和k带图灵机处理所有长度为n的输入时,在k条带上所使用过的平均方格
数。
D. k带图灵机处理所有长度为n的输入时,在某条带上所使用过的最小方格数。 13. NP类语言在图灵机下的定义为(D)
A. NP={L|L是一个能在非多项式时间内被一台NDTM所接受的语言}; B. NP={L|L是一个能在多项式时间内被一台NDTM所接受的语言}; C. NP={L|L是一个能在多项式时间内被一台DTM所接受的语言}; D. NP={L|L是一个能在多项式时间内被一台NDTM所接受的语言};14. 记号O的定义正确的是(A)。
A. O(g(n)) = { f(n) | 存在正常数c和n0使得对所有nn有:0 f(n) ,,,0
cg(n) };
B. O(g(n)) = { f(n) | 存在正常数c和n0使得对所有nn有:0 cg(n) ,,,0
f(n) };
used after the removal of plastic-wrapped, concrete curing period of not less than 7 days. Curing agent emulsion curing agent should be used to avoid yellowing on the surface of the concrete. Detailed rules for the implementation of specific implementation requirements 8 compliance and excellence project 8.1 standard of excellence presided over by the Chief Engineer of the project, implemented in full. 8.2 professional
C. O(g(n)) = { f(n) | 对于任何正常数c>0,存在正数和n>0使得对所有nn0 0,
有:0 f(n)
0,存在正数和n >0使得对所有0
nn有:0 cg(n) < f(n) }; 0,,
15. 记号的定义正确的是(B)。 ,
A. O(g(n)) = { f(n) | 存在正常数c和n0使得对所有nn有:0 f(n) 0,,,
cg(n) };
B. O(g(n)) = { f(n) | 存在正常数c和n0使得对所有nn有:0 cg(n) 0,,,
f(n) };
C. (g(n)) = { f(n) | 对于任何正常数c>0,存在正数和n>0使得对所有nn0 ,0
有:0 f(n)0,存在正数和n >0使得对所有nn,00
cg(n) < f(n) }; 有:0 ,
二、 填空题
21. 下面程序段的所需要的计算时间为( )。 O(n)
int MaxSum(int n, int *a, int &besti, int &bestj) {
int sum=0;
for(int i=1;i<=n;i++){
int thissum=0;
for(int j=i;j<=n;j++){
thissum+=a[j];
if(thissum>sum) {
sum=thissum;
besti=i;
bestj=j;
}
}
}
return sum;
}
used after the removal of plastic-wrapped, concrete curing period of not less than 7 days. Curing agent emulsion curing agent should be used to avoid yellowing on the surface of the concrete. Detailed rules for the implementation of specific implementation requirements 8 compliance and excellence project 8.1 standard of excellence presided over by the Chief Engineer of the project, implemented in full. 8.2 professional
2. 有11个待安排的活动,它们具有下表所示的开始时间与结束时间,如果
以贪心算法求解这些活动的最优安排(即为活动安排问题:在所给的活
动集合中选出最大的相容活动子集合),得到的最大相容活动子集合为活
动( {1,4,8,11} )。
i 1 2 3 4 5 6 7 8 9 10 11
S[i] 1 3 0 5 3 5 6 8 8 2 12
f[i] 4 5 6 7 8 9 10 11 12 13 14
3. 所谓贪心选择性质是指(所求问题的整体最优解可以通过一系列局部最
优的选择,即贪心选择来达到)。
4. 所谓最优子结构性质是指(问题的最优解包含了其子问题的最优解)。 5. 回溯法是指(具有限界函数的深度优先生成法)。
6. 用回溯法解题的一个显著特征是在搜索过程中动态产生问题的解空间。在
任何时刻,算法只保存从根结点到当前扩展结点的路径。如果解空间树
中从根结点到叶结点的最长路径的长度为h(n),则回溯法所需的计算空
间通常为(O(h(n)))。7. 回溯法的算法框架按照问题的解空间一般分为
(子集树)算法框架与(排列树)算法框架。
8. 用回溯法解0/1背包问题时,该问题的解空间结构为(子集树)结构。
used after the removal of plastic-wrapped, concrete curing period of not less than 7 days. Curing agent emulsion curing agent should be used to avoid yellowing on the surface of the concrete. Detailed rules for the implementation of specific implementation requirements 8 compliance and excellence project 8.1 standard of excellence presided over by the Chief Engineer of the project, implemented in full. 8.2 professional
9.用回溯法解批处理作业调度问题时,该问题的解空间结构为(排列树)结
构。
10.用回溯法解0/1背包问题时,计算结点的上界的函数如下所示,请在空格
中填入合适的内容:
Typep Knap::Bound(int i)
{// 计算上界
Typew cleft = c - cw; // 剩余容量 Typep b = cp; // 结点的上界
// 以物品单位重量价值递减序装入物品
while (i <= n && w[i] <= cleft) { cleft -= w[i];
b += p[i];
i++;
}
// 装满背包
if (i <= n) (b += p[i]/w[i] * cleft); 11. 用回溯法解布线问题时,求最优解的主要程序段如下。如果布线区域划分为
return b; 的方格阵列,扩展每个结点需O(1)的时间,L为最短布线路径的长度,则nm,}
算法共耗时 ( O(mn) ),构造相应的最短距离需要(O(L))时间。
for (int i = 0; i < NumOfNbrs; i++) {
nbr.row = here.row + offset[i].row;
nbr.col = here.col + offset[i].col;
if (grid[nbr.row][nbr.col] == 0) {
// 该方格未标记
grid[nbr.row][nbr.col]
= grid[here.row][here.col] + 1;
if ((nbr.row == finish.row) &&
(nbr.col == finish.col)) break; // 完成布线
Q.Add(nbr);}
}
12. 用回溯法解图的m着色问题时,使用下面的函数OK检查当前扩展结点的used after the removal of plastic-wrapped, concrete curing period of not less than 7 days. Curing agent emulsion curing agent should be used to avoid yellowing on the surface of the concrete. Detailed rules for the implementation of specific implementation requirements 8 compliance and excellence project 8.1 standard of excellence presided over by the Chief Engineer of the project, implemented in full. 8.2 professional
每一个儿子所相应的颜色的可用性,则需耗时(渐进时间上限)(O(mn))。 Bool Color::OK(int k)
{//
for(int j=1;j<=n;j++)
if((a[k][j]= =1)&&(x[j]= =x[k])) return false;
return true;
}
13. 旅行售货员问题的解空间树是(排列树)。解答题
1. 机器调度问题。
问题描述:现在有n件任务和无限多台的机器,任务可以在机器上得到处理。每件任务的开始时间为s,完成时间为f,s方案。
问题实例:若任务占用的时间范围是{[1,4],[2,5],[4,5],[2,6],[4,7]},则按时完成所有任务最少需要几台机器,(提示:使用贪心算法)
画出工作在对应的机器上的分配情况。
f(n)bf(n1)g(n),,,,2. 已知非齐次递归方程: ,其中,b、c是常数,,f(0)c,,
n,nnig(n)是n的某一个函数。则f(n)的非递归表达式为:。 f(n)cbbg(i),,,,i1
h(n)2h(n1)1,,,,现有Hanoi塔问题的递归方程为: ,求h(n)的非递归表,h(1)1,,
used after the removal of plastic-wrapped, concrete curing period of not less than 7 days. Curing agent emulsion curing agent should be used to avoid yellowing on the surface of the concrete. Detailed rules for the implementation of specific implementation requirements 8 compliance and excellence project 8.1 standard of excellence presided over by the Chief Engineer of the project, implemented in full. 8.2 professional
达式。
解:利用给出的关系式,此时有:b=2, c=1, g(n)=1, 从n递推到1,有:
n1,n1n1i,,,h(n)cbbg(i),,,i1,
n1n22,, ,,,,,,22...221
n,,21
3. 单源最短路径的求解。
问题的描述:给定带权有向图(如下图所示)G =(V,E),其中每条边的权是非负实数。另外,还给定V中的一个顶点,称为源。现在要计算从源到所有其它各顶点的最短路长度。这里路的长度是指路上各边权之和。这个问题通常称为单源最短路径问题。
解法:现采用Dijkstra算法计算从源顶点1到其它顶点间最短路径。请将此过程填入下表中。
迭代 S u dist[2] dist[3] dist[4] dist[5]
初始 {1} - 10 maxint 30 100
1 4. 请写出用回溯法解装载问题的函数。 2
装载问题:有一批共n个集装箱要装上2艘载重量分别为c1和c2的轮船,3
n4 wcc,,,i12,i1其中集装箱i的重量为wi,且。装载问题要求确定是否有一个合理的装载方案可将这n个集装箱装上这2艘轮船。如果有,找出一种装载方案。
解:void backtrack (int i)
{// 搜索第i层结点
if (i > n) // 到达叶结点
更新最优解bestx,bestw;return;
r -= w[i];
used after the removal of plastic-wrapped, concrete curing period of not less than 7 days. Curing agent emulsion curing agent should be used to avoid yellowing on the surface of the concrete. Detailed rules for the implementation of specific implementation requirements 8 compliance and excellence project 8.1 standard of excellence presided over by the Chief Engineer of the project, implemented in full. 8.2 professional
if (cw + w[i] <= c) {// 搜索左子树
x[i] = 1;
cw += w[i];
backtrack(i + 1);
cw -= w[i]; }
if (cw + r > bestw) {
x[i] = 0; // 搜索右子树
backtrack(i + 1); }
r += w[i];
}5. 用分支限界法解装载问题时,对算法进行了一些改进,下面的程序段给出了改进部分;试
斜线部分完成什么功能,以及这样做的原因,即采用这样的方式,算法在执行上有什么不同。
// 检查左儿子结点
Type wt = Ew + w[i]; // 左儿子结点的重量 if (wt <= c) { // 可行结点
if (wt > bestw) bestw = wt;
// 加入活结点队列 if (i < n) Q.Add(wt);
}
// 检查右儿子结点 if (Ew + r > bestw && i < n)
Q.Add(Ew); // 可能含最优解
Q.Delete(Ew); // 取下一扩展结点
解答:斜线标识的部分完成的功能为:提前更新bestw值;
这样做可以尽早的进行对右子树的剪枝。具体为:算法Maxloading初始时将bestw设置为0,直到搜索到第一个叶结点时才更新bestw。因此在算法搜索到第一个叶子结点之前,总有bestw=0,r>0 故Ew+r>bestw总是成立。也就是说,此时右子树测试不起作用。
为了使上述右子树测试尽早生效,应提早更新bestw。又知算法最终找到的最优值是所求问题的子集树中所有可行结点相应重量的最大值。而结点所相应得重量仅在搜索进入左子树是增加,因此,可以在算法每一次进入左子树时更新bestw的值。
7. 最长公共子序列问题:给定2个序列X={x1,x2,„,xm}和Y={y1,y2,„,yn},找出X和Y的最长公共子序列。
used after the removal of plastic-wrapped, concrete curing period of not less than 7 days. Curing agent emulsion curing agent should be used to avoid yellowing on the surface of the concrete. Detailed rules for the implementation of specific implementation requirements 8 compliance and excellence project 8.1 standard of excellence presided over by the Chief Engineer of the project, implemented in full. 8.2 professional
由最长公共子序列问题的最优子结构性质建立子问题最优值的递归关系。用c[i][j]记录序列Xi和Yj的最长公共子序列的长度。其中, Xi={x1,x2,„,xi};Yj={y1,y2,„,yj}。当i=0或j=0时,空序列是Xi和Yj的最长公共子序列。故此时C[i][j]=0。其它情况下,由最优子结构性质可建立
,00,0ij,,
,递归关系如下: cijcijijxy[][][1][1]1,0;,,,,,,,ij
,max{[][1],[1][]},0;cijcijijxy,,,,ij,
在程序中,b[i][j]记录C[i][j]的值是由哪一个子问题的解得到的。
(1) 请填写程序中的空格,以使函数LCSLength完成计算最优值的功能。
void LCSLength(int m,int n,char *x,char *y,int **c,int **b)
{
int i,j; for (i = 1; i <= m; i++) c[i][0] = 0;
for (i = 1; i <= n; i++) c[0][i] = 0;
for (i = 1; i <= m; i++)
for (j = 1; j <= n; j++) {
if (x[i]==y[j]) {
c[i][j]=c[i-1][j-1]+1; b[i][j]=1;} else if (c[i-1][j]>=c[i][j-1]) {
c[i][j]=c[i-1][j]; b[i][j]=2;}
else { c[i][j]=c[i][j-1]; b[i][j]=3; }
}
}
(2) 函数LCS实现根据b的内容打印出Xi和Yj的最长公共子序列。请填
写程序中的空格,以使函数LCS完成构造最长公共子序列的功能(请
将b[i][j]的取值与(1)中您填写的取值对应,否则视为错误)。
void LCS(int i,int j,char *x,int **b)
{
if (i ==0 || j==0) return;
if (b[i][j]== 1) {
LCS(i-1,j-1,x,b);
cout<
0 )
{ printf("%d\n ",k);
f(k-1);
f(k-1);
}
}
《算法分析与设计》试题库(二) 一、简要回答下列问题 :
1. 算法重要特性是什么,
2. 算法分析的目的是什么,
3. 算法的时间复杂性与问题的什么因素相关,
4. 算法的渐进时间复杂性的含义,
5. 最坏情况下的时间复杂性和平均时间复杂性有什么不同, used after the removal of plastic-wrapped, concrete curing period of not less than 7 days. Curing agent emulsion curing agent should be used to avoid yellowing on the surface of the concrete. Detailed rules for the implementation of specific implementation requirements 8 compliance and excellence project 8.1 standard of excellence presided over by the Chief Engineer of the project, implemented in full. 8.2 professional
6. 简述二分检索(折半查找)算法的基本过程。
7. 背包问题的目标函数和贪心算法最优化量度相同吗,
8. 采用回溯法求解的问题,其解如何表示,有什么规定,
9. 回溯法的搜索特点是什么,
10.n皇后问题回溯算法的判别函数place的基本流程是什么, 11.为什么用分治法设计的算法一般有递归调用,
12.为什么要分析最坏情况下的算法时间复杂性,
13.简述渐进时间复杂性上界的定义。
14.二分检索算法最多的比较次数,
15.快速排序算法最坏情况下需要多少次比较运算,
16.贪心算法的基本思想,
17.回溯法的解(x,x,„„x)的隐约束一般指什么, 12n
18.阐述归并排序的分治思路。
19.快速排序的基本思想是什么。
20.什么是直接递归和间接递归,消除递归一般要用到什么数据结构, 21.什么是哈密顿环问题,
22.用回溯法求解哈密顿环,如何定义判定函数,
23.请写出prim算法的基本思想。
参考答案:1. 确定性、可实现性、输入、输出、有穷性
2. 分析算法占用计算机资源的情况,对算法做出比较和评价,设计出额更好的算法。
3. 算法的时间复杂性与问题的规模相关,是问题大小n的函数。 4(当问题的规模n趋向无穷大时,影响算法效率的重要因素是T(n)的数量级,而其他因素仅是使时间复杂度相差常数倍,因此可以用T(n)的数量级(阶)评价算法。时间复杂度T(n)的数量级(阶)称为渐进时间复杂性。 5. 最坏情况下的时间复杂性和平均时间复杂性考察的是n固定时,不同输入实例下的算法所耗时间。最坏情况下的时间复杂性取的输入实例中最大的时间复杂度:
W(n) = max{ T(n,I) } , I?Dn
平均时间复杂性是所有输入实例的处理时间与各自概率的乘积和: A(n) =?P(I)T(n,I) I?Dn
. 设输入是一个按非降次序排列的元素表A[i:j] 和x,选取A[(i+j)/2]6
与x比较,如果A[(i+j)/2]=x,则返回(i+j)/2,如果A[(i+j)/2]标准 ;然后按这种量度标准对这n个输入排序,依次选择输入量加入部分解中。如果当前这个输入量的加入,不满足约束条件,则不把此输入加到这部分解中。
17(回溯法的解(x,x,„„x)的隐约束一般指个元素之间应满足的某种12n
关系。
18. 讲数组一分为二,分别对每个集合单独排序,然后将已排序的两个序列归并成一个含n个元素的分好类的序列。如果分割后子问题还很大,则继续分治,直到一个元素。
19.快速排序的基本思想是在待排序的N个记录中任意取一个记录,把该记录放在最终位置后,数据序列被此记录分成两部分。所有关键字比该记录关键字小的放在前一部分,所有比它大的放置在后一部分,并把该记录排在这两部分的中间,这个过程称作一次快速排序。之后重复上述过程,直到每一部分内只有一个记录为止。
20.在定义一个过程或者函数的时候又出现了调用本过程或者函数的成分,既调用它自己本身,这称为直接递归。如果过程或者函数P调用过程或者函数Q,Q又调用P,这个称为间接递归。消除递归一般要用到栈这种数据结构。
21.哈密顿环是指一条沿着图G的N条边环行的路径,它的访问每个节点一次并且返回它的开始位置。
22.当前选择的节点X[k]是从未到过的节点,即X[k]?X[i](i=1,2,„,k-1),且C(X[k-1], X[k])??,如果k=-1,则C(X[k], X[1]) ??。
23. 思路是:最初生成树T为空,依次向内加入与树有最小邻接边的n-1条边。处理过程:首先加入最小代价的一条边到T,根据各节点到T的邻接边排序,选择最小边加入,新边加入后,修改由于新边所改变的邻接边排序,再选择下一条边加入,直至加入n-1条边。
二、复杂性分析
1、MERGESORT(low,high)
if low