算法分析算法复习题中英文

算法分析算法复习题中英文
算法分析算法复习题中英文

(有翻译)

1.The O-notation provides an asymptotic upper bound. The Ω-notation provides an

asymptotic lower bound. The Θ-notation asymptotically a function form above

2.To represent a heap as an array,the root of tree is A[1], and given the index i of a

node, the indices of its parent Parent(i) { return ?i/2?; },left child, Left(i)

{ return 2*i; },right child, right(i) { return 2*i + 1; }.

代表一个堆中的一个数组,树的根节点是A[1],并且给出一个节点i,那么该节点的父节点是左孩子右孩子

3.Because the heap of n elements is a binary tree, the height of any node is at most

Θ(lg n).

因为n个元素的堆是一个二叉树,任意节点的树高最多是

4.In optimization problems, there can be many possible solutions. Each solution

has a value, and we wish to find a solution with the optimal (minimum or maximum) value. We call such a solution an optimal solution to the problem.

在最优化问题中,有很多可能的解,每个解都有一个值,我们希望找到一个最优解(最大或最小),我们称这个解为最优解问题。

5.optimal substructure if an optimal solution to the problem contains within it

optimal solutions to subproblems.

最优子结构中问题的最优解,至少包含它的最优解的子问题。

6. A subsequence of X if there exists a strictly increasing sequence of

indices of X such that for all j = 1, 2, ..., k, we have x i j= z j .

Let X = and Y = be sequences, and let Z =

z2, ..., z k> be any LCS of X and Y.

(1). If x m = y n, then z k = x m = y n and Z k-1 is an LCS of X m-1 and Y n-1.

(2). If x m ≠ y n, then z k ≠ x m implies that Z is an LCS of X m-1 and Y.

(3). If x m ≠ y n, then z k ≠ y n implies that Z is an LCS of X and Y n-1.

7. A greedy algorithm always makes the choice that looks best at the moment. That

is, it makes a locally optimal choice in the hope that this choice will lead to a

globally optimal solution.

贪心算法经常需要在某个时刻寻找最好的选择。正因如此,它在当下找到希望中的最优选择,以便引导出一个全局的最优解。

8.The greedy-choice property and optimal sub-structure are the two key ingredients

of greedy algorithm.

贪心选择和最优子结构是贪心算法的两个重要组成部分。

9.When a recursive algorithm revisits the same problem over and over again, we

say that the optimization problem has overlapping subproblems.

当一个递归算法一遍一遍的遍历同一个问题时,我们说这个最优化问题是重叠子问题。

10.greedy-choice property is a globally optimal solution can be arrived at by making

a locally optimal (greedy) choice.

贪心选择性质是一个全局的最优解,这个最优解可以做一个全局的最优选择。

11.An approach of Matrix multiplication can develope a Θ(V4)-time algorithm for

the all-pairs shortest-paths problem and then improve its running time to Θ(V3lg

V).

一个矩阵相乘问题的解决可以一个时间复杂度算法的所有路径的最短路径问题,改进后的时间复杂度是。

12.Floyd-Warshall algorithm, runs in Θ(V3) time to solve the all-pairs

shortest-paths problem.

FW算法在时间复杂度下可以解决最短路径问题。

13.The running time of Quick Sort is O(n2) in the worst case, and O(n lg n) in the

average case.

快速排序的平均时间复杂度是O(n lg n),最坏时间复杂度是O(n2)。

14.The MERGE(A,p,q,r) procedure in merge sort takes time Θ(n).

MERGE在归并排序中所花费的时间是。

15.Given a weighted, directed graph G = (V, E) with source s and weight function w :

E →R, the Bellman-Ford algorithm makes |V| - 1 passes over the edges of the

graph.

给一个带权重的有向图G = (V, E),权重关系w : E →R,则the Bellman-Ford算法需经过条边。

16.The Bellman-Ford algorithm runs in time O(V E).

Bellman ford 算法的时间复杂度是。

17.A decision tree represents the comparisons made by a comparison sort.The

asymptotic height of any decision tree for sorting n elements is Ω(n lg n).

一个决策树代表一个比较类型,通过比较排序。N个元素的任意决策树的渐进高度是。True-false questions

1.An algorithm is said to be correct if, for some input instance, it halts with the

correct output F

如果给一个算法输入一些实例,并且它给力正确的输出,则认识这个算法是正确的。

2.Insertion sort always best merge sort F

插入排序总是优越与归并排序。

3.Θ(n lg n) grows more slowly than Θ(n2). Therefore, merge sort asymptotically

beats insertion sort in the worst case. T

Θ(n lg n)

4.Currently computers are fast and computer memory is very cheap, we have no

reason to study algorithms. F

5.In RAM (Random-Access Machine) model, instructions are executed with

concurrent operations. F

6.The running time of an algorithm on a particular input is the number of primitive

operations or “steps” executed. T

7.Quick sorts, have no combining step: two subarrays form an already-sorted array.

T

8.The running time of Counting sort is O(n + k). But the running time of sorting is

Ω(n lg n). So this is contradiction. F

9.The Counting sort is stable. T

10.In the selection problem,there is a algorithm of theoretical interest only with O(n)

worst-case running time. T

11.Divide-and-conquer algorithms partition the problem into independent

subproblems, solve the subproblems recursively, and then combine their solutions to solve the original problem. In contrast, dynamic programming is applicable

when the subproblems are not independent, that is, when subproblems share

subsubproblems. T

12.In dynamic programming, we build an optimal solution to the problem from

optimal solutions to subproblems. T

13.The best-case running time is the longest running time for any input of size n.

F

14.When we analyze the running time of an algorithm, we actually interested on the

rate of growth (order of growth). T

15.The dynamic programming approach means that it break the problem into several

subproblems that are similar to the original problem but smaller in size, solve the subproblems recursively, and then combine these solutions to create a solution to the original problem. T

16.Insertion sort and merge sort both use divide-and-conquer approach. F

17.Θ(g(n)) = { f (n) : there exist positive constants c1, c2, and n0 such that 0 ≤ c1 g(n)

≤ f (n) ≤ c2 g(n) for all n ≥ n0 }

18.Min-Heaps satisfy the heap property: A[Parent(i)] ≥ A[i] for all nodes i > 1. F

19.For array of length n, all elements in range A[?n/2? + 1 .. n] are heaps. T

20.The tighter bound of the running time to build a max-heap from an unordered

array isn’t in linear time. F

21.The call to BuildHeap() takes O(n) time, Each of the n - 1 calls to Heapify()

takes O(lg n) time, Thus the total time taken by HeapSort() = O(n) + (n - 1) O(lg n)= O(n) + O(n lg n)= O(n lg n). T

22.Quick Sort is a dynamic programming algorithm. The array A[p..r] is partitioned

into two non-empty subarrays A[p..q] and A[q+1..r], All elements in A[p..q] are less than all elements in A[q+1..r], the subarrays are recursively sorted by calls to quicksort. F

23.Assume that we have a connected, undirected graph G = (V, E) with a weight

function w : E→R, and we wish to find a minimum spanning tree for G. Both

Kruskal and Prim algorithms use a dynamic programming approach to the

problem. F

24.A cut (S, V - S) of an undirected graph G = (V, E) is a partition of E. F

25.An edge is a light edge crossing a cut if its weight is the maximum of any edge

crossing the cut. F

26.Kruskal's algorithm uses a disjoint-set data structure to maintain several disjoint

sets of elements. T

27.Optimal-substructure property is a hallmark of the applicability of both dynamic

programming. T

28.Dijkstra's algorithm is a dynamic programming algorithm. F

29.Floyd-Warshall algorithm, which finds shortest paths between all pairs of vertices ,

is a greedy algorithm. F

30.Given a weighted, directed graph G = (V, E) with weight function w : E →R, let

p = be a shortest path from vertex v1 to vertex v k and, for any i and j

such that 1 ≤i ≤j ≤k, let p ij = be the subpath of p from vertex v i to vertex v j . Then, p ij is a shortest path from v i to v j. T

31.Given a weighted, directed graph G = (V, E) with weight function w : E →R,If

there is a negative-weight cycle on some path from s to v , there exists a

shortest-path from s to v. F

32.Since any acyclic path in a graph G = (V, E) contains at most |V| distinct vertices,

it also contains at most |V| - 1 edges. Thus, we can restrict our attention to shortest paths of at most |V| - 1 edges. T

33.The process of relaxing an edge (u, v) tests whether we can improve the shortest

path to v found so far by going through u. T

34.In Dijkstra's algorithm and the shortest-paths algorithm for directed acyclic graphs,

each edge is relaxed exactly once. In the Bellman-Ford algorithm, each edge is also relaxed exactly once . F

35.The Bellman-Ford algorithm solves the single-source shortest-paths problem in

the general case in which edge weights must be negative. F

36.Given a weighted, directed graph G = (V, E) with source s and weight function w :

E →R, the Bellman-Ford algorithm can not return a Boolean value indicating

whether or not there is a negative-weight cycle that is reachable from the source.

F

37.Given a weighted, directed graph G = (V, E) with source s and weight function w :

E →R, for the Bellman-Ford algorithm, if there is such a cycle, the algorithm

indicates that no solution exists. If there is no such cycle, the algorithm produces the shortest paths and their weights. F

38.Dijkstra's algorithm solves the single-source shortest-paths problem on a weighted,

directed graph G = (V, E) for the case in which all edge weights are negative.

F

39.Dijkstra's algorithm solves the single-source shortest-paths problem on a weighted,

directed graph G = (V, E) for the case in which all edge weights are nonnegative.

Bellman-Ford algorithm solves the single-source shortest-paths problem on a

weighted, directed graph G = (V, E), the running time of Dijkstra's algorithm is lower than that of the Bellman-Ford algorithm. T

40.The steps for developing a dynamic-programming algorithm:1. Characterize the

structure of an optimal solution. 2. Recursively define the value of an optimal

solution. 3. Compute the value of an optimal solution in a bottom-up fashion. 4.

Construct an optimal solution from computed information. T

三Each of n input elements is an integer in the range 0 to k, Design a linear running time algorithm to sort n elements.

四Design a expected linear running time algorithm to find the i th smallest element of n elements using divide and conquer strategy.

五Write the INSERT-SORT procedure to sort into non-decreasing order. Analyze the running time of it with RAM Model. What’s the best-case running time, the worst-case running time and average case running time. Write the MERGE-SORT procedure to sort into non-decreasing order. Give the recurrence for the worst-case

running time T(n) of Merge sort and find the solution to the recurrence.

六 What is an optimal Huffman code for the following set of frequencies,

七 The traveling-salesman problem (TSP): in the traveling-salesman problem, we are given a complete undirected graph G=(V ,E) that has a nonnegative integer cost c(u,v) associated with each edge (u,v)∈E , and we must find a tour of G with minimum cost. The following is an instance TSP. Please compute a tour with minimum cost with greedy algorithm. ∞

∞∞∞∞69326911699252312514216214 八Given items of different values and weights, find the most valuable set of items that fit in a knapsack of fixed weight C .For an instance of knapsack problem, n =8, C =110,value V ={11,21,31,33,43,53,55,65} weight W ={1,11,21,23,33,43,45,55}. Use greedy algorithms to solve knapsack problem.

九Use dynamic programming to solve Assembly-line scheduling problem: A Motors Corporation produces automobiles that has two assembly lines, numbered i =1,2. Each line has n stations, numbered j =1,2…n . We denote the j th station on line i by S ij . The following figure is an instance of the assembly-line problem with costs entry time e i , exit time x i , the assembly time required at station S ij by a ij , the time to transfer a chassis away from assembly line I after having gone through station S ij is t ij . Please compute the fastest time and construct the fastest way through the factory of the instance.

十. The matrix-chain multiplication problem can be stated as follows: given a chain

of matrices, where for i=1,2…,n, matrix A i has dimension

P i-1 P i, fully parenthesize the product A1,A2,…,A n in a way that minimizes the number of scalar multiplication. We pick as our subproblems the problems of determining the minimum cost of a parenthesization of A i A i+1 A j for 1 ≤i ≤j ≤n. Let m[i, j] be the minimum number of scalar multiplications needed to compute the matrix A i..j; for the full problem, the cost of a cheapest way to compute A1..n would thus be m[1, n]. Can you define m[i, j] recursively? Find an optimal parenthesization of a matrix-chain product whose sequence of dimensions is <4,3,5,2,3>

十一In the longest-common-subsequence (LCS) problem, we are given two sequences X = and Y = and wish to find a maximum-length common subsequence of X and Y. Please write its recursive formula and determine an LSC of Sequence S1=ACTGATCG and sequence S2=CATGC. Please fill in the blanks in the table below.

十二Proof: Any comparison sort algorithm requires Ω(nlgn) comparisons in the worst case.

How many leaves does the tree have? (叶节点的数目)

–At least n! (each of the n!permutations if the input appears as some leaf) ?n! ≤l (至少n! 个,排列)–At most 2hleaves (引理,至多2h个)?n! ≤l ≤2h

?

h ≥lg(n!) = ?(nlgn)

十三Proof: Subpaths of shortest paths are shortest paths.

Given a weighted, directed graph G = (V, E) with weight function w : E →R, let p = be a shortest path from vertex v1to vertex v k and, for any i and j such that 1 ≤i ≤j ≤k, let p ij= be the subpath of p from vertex v i to vertex v j . Then, p ij is a shortest path from v i to v j.

十四Proof : The worst case running time of quicksort is Θ(n2)

十五Compute shortest paths with matrix multiplication and the Floyd-Warshall algorithm for the following graph.

十六Write the MAX-Heapify() procedure to for manipulating max-heaps. And analyze the running time of MAX-Heapify().

三(10分)

1 CountingSort(A, B, k)

2 for i=1 to k

3 C[i]= 0;

4 for j=1 to n

5 C[A[j]] += 1;

6 for i=2 to k

7 C[i] = C[i] + C[i-1];

8 for j=n downto 1

9 B[C[A[j]]] = A[j];

10 C[A[j]] -= 1;

算法描述3分

The best-case running time is T(n) = c1n + c2(n - 1) + c4(n - 1) + c5(n - 1) + c8(n - 1) = (c1 + c2 + c4 + c5 + c8)n - (c2+ c4 + c5 + c8). This running time can be expressed as an + b for constants a and b that depend on the statement costs c i; it is thus a linear function of n.

This worst-case running time can be expressed as an2 + bn + c for constants a, b, and c that again depend on the statement costs c i; it is thus a quadratic function of n.

分析2分

算法描述2分

Θ(1) if n = 1

T(n) =

2T(n/2) + Θ(n) if n > 1.

递归方程和求解3分

7 RAND-SELECT(A, p, r, i) (5分)

if p = r then return A[p]

q ←RAND-PARTITION(A, p, r)

k ←q – p + 1

if i = k then return A[q]

if i < k

then return RAND-SELECT(A, p, q – 1, i )

else return RAND-SELECT(A, q + 1, r, i – k ) Randomized RANDOMIZED-PARTITION(A; p; r) (5分) { i ←RANDOM(p, r)

exchange A[r] ←A[i]

return PARTITION(A; p; r)}

PARTITION(A; p; r)

{ x←A[r]

i ←p-1

for j ←p to r-1

do if A[j] ≤x

then i ←i+1

exchange A[i] ?A[j]

exchange A[i+1] ? A[r]

return i+1 } 六

首先画出它对应的图,加上标号,假设从1出发,每次贪心选择一个权重最小的顶点作为下一个要去的城市。(算法策略5分)

求解过程5分 七

55

30

25

d:1630

14 f:5

e:9

c:12 b:13

a:45

100 a:1 b:100 c:101 d:111 e:1100 f:1101

八 V ={11,21,31,33,43,53,55,65} weight W ={1,11,21,23,33,43,45,55}

按照单位重量的价值排序,1121313343535565

111212333434555

>>>>>>>,然后按照该顺

序往背包中放。 九

递归方程4分

f1[1]=9 f2[1]=12 f1[2]=18 f2[2]=16 f1[3]=20 f2[3]=22 f1[4]=24 f2[4]=25 f1[5]=32 f2[5]=30 f1[6]=35 f2[6]=37

the fastest time is 38 and the fastest way is: station 1:line 1 station 2:line 2 station 3:line 1 station 4:line 2 station 5: line 2 station 6: line 1 求解过程6分 十

递归方程4分

m[1,1]=0 m[2,2]=0 m[3,3]=0 m[4,4]=0 m[1,2]=m[1,1]+m[2,3]+p0*p1*p2=60 m[2,3]=m[2,2]+m[3,3]+p1*p2*p3=30 m[3,4]=m[3,3]+m[4,4]+p2*p3*p4=30

m[1,3]=min{m[1,2]+m[3,3]+p0*p2*p3, m[1,1]+m[2,3]+p0*p1*p3}=54 m[2,4]=min{m[2,3]+m[4,4]+p1*p3*p4, m[2,2]+m[3,4]+p0*p2*p4}=48 m[1,4]=min{m[1,1] +m[2,4]+p0*p1*p4, m[1,2]+m[3,4]+p0*p2*p4, m[1,3]+m[4,4]+p0*p3*p4}=78

((A1(A2A3))A4) 求解过程6分 十一

??

?--=+--=otherwise ]),1[],1,[max(],[][ if 1

]1,1[],[j i c j i c j y i x j i c j i c

递归方程4分

最长公共子序列长度为4 AGTC

求解过程6分

十二From the preceding discussion, it suffices to determine the height of a decision

tree in which each permutation appears as a reachable leaf. Consider a decision tree of height h with l reachable leaves corresponding to a comparison sort on n elements.

从前面讨论,它可以确定一个决策树的高度,每个排列显示为一个可到达的叶子。考虑一个决策树的高度h 和l可及的叶子在n个元素对应于一种比较。

Because each of the n! permutations of the input appears as some leaf,

因为每个n !排列的输入出现一些叶子,

we have n! ≤l.

Since a binary tree of height h has no more than 2h leaves, 因为一个二叉树的高度h没有超过2 h叶子

we have(分析5分)

n! ≤l≤2h,

which, by taking logarithms, implies

h ≥ lg(n!) (since the lg function is monotonically increasing)

= Ω(n lg n)

列式和求解5分

十三

Proof:If we decompose path p into v1→ v i→ v j→ v k, then we have that w(p) = w(p1i) + w(p ij) +w(p jk). Now, assume that there is a path p’ij from v i to v j with weight w(p’ij)< w(p ij) . Then, v1→ v i→ v j→ v k is a path from v1 to v k whose weight w(p1i) + w(p’ij)

+w(p jk)is less than w(p), which contradicts the assumption that p is a shortest path from v1 to v k.

反证法假设5分,分析5分

十四

列式5分,求解5分

十五

matrix multiplication:

5分

Floyd-Warshall algorithm:

十六

Heapify(A, i)

{

l = Left(i); r = Right(i);

if (l <= heap_size(A) && A[l] > A[i])

largest = l;

else

largest = i;

if (r <= heap_size(A) && A[r] > A[largest])

largest = r;

if (largest != i)

Swap(A, i, largest);

Heapify(A, largest);

}

Fixing up relationships between i, l, and r takes Θ(1) time,If the heap at i has n elements, the subtrees at l or r can have 2n/3 elements. So time taken by Heapify() is given by T(n) ≤ T(2n/3) + Θ(1) ,by recursive tree, the solution is T(n) = O(lg n)

.算法描述4分列递归方程3分,求解3分

算法设计与分析考试题及答案

算法设计与分析考试题 及答案 Company number:【WTUT-WT88Y-W8BBGB-BWYTT-19998】

一、填空题(20分) 1.一个算法就是一个有穷规则的集合,其中之规则规定了解决某一特殊类型问题的一系列运算,此外,算法还应具有以下五个重要特性:确定性 有穷性 可行性 0个或多个输入 一个或多个输出 2.算法的复杂性有时间复杂性 空间复杂性之分,衡量一个算法好坏的标准是 时间复杂度高低 3.某一问题可用动态规划算法求解的显着特征是 该问题具有最优子结构性质 4.若序列X={B,C,A,D,B,C,D},Y={A,C,B,A,B,D,C,D},请给出序列X 和Y 的一个最长公共子序列{BABCD}或{CABCD}或{CADCD } 5.用回溯法解问题时,应明确定义问题的解空间,问题的解空间至少应包含一个(最优)解 6.动态规划算法的基本思想是将待求解问题分解成若干_子问题 ,先求解_子问题 ,然后从这些子问题 的解得到原问题的解。 7.以深度优先方式系统搜索问题解的算法称为回溯法 背包问题的回溯算法所需的计算时间为o(n*2n ) ,用动态规划算法所需的计算时间为o(min{nc,2n }) 9.动态规划算法的两个基本要素是最优子结构 _和重叠子问题 10.二分搜索算法是利用动态规划法实现的算法。 二、综合题(50分) 1.写出设计动态规划算法的主要步骤。 ①问题具有最优子结构性质;②构造最优值的递归关系表达式; ③最优值的算法描述;④构造最优解; 2. 流水作业调度问题的johnson 算法的思想。 ①令N 1={i|a i =b i };②将N 1中作业按a i 的非减序排序得到N 1’,将N 2中作业按b i 的非增序排序得到N 2’;③N 1’中作业接N 2’中作业就构成了满足Johnson 法则的最优调度。 3. 若n=4,在机器M1和M2上加工作业i 所需的时间分别为a i 和b i ,且 (a 1,a 2,a 3,a 4)=(4,5,12,10),(b 1,b 2,b 3,b 4)=(8,2,15,9)求4个作业的最优调度方案,并计算最优值。 步骤为:N1={1,3},N2={2,4}; N 1’={1,3}, N 2’={4,2}; 最优值为:38 4. 使用回溯法解0/1背包问题:n=3,C=9,V={6,10,3},W={3,4,4},其解空间有长度为3的0-1向量组成,要求用一棵完全二叉树表示其解空间(从根出发,左1右0),并画出其解空间树,计算其最优值及最优解。 解空间为{(0,0,0),(0,1,0),(0,0,1),(1,0,0),(0,1,1),(1,0,1), (1,1,0),(1,1,1)}。 解空间树为: 该问题的最优值为:16 最优解为:(1,1,0) 5. 设S={X 1,X 2,···,X n }是严格递增的有序集,利用二叉树的结点来存储S 中的元素,在表示S 的二叉搜索树中搜索一个元素X ,返回的结果有两种情形,(1)在二叉搜索树的内结点中找到X=X i ,其概率为b i 。(2)在二叉搜索树的叶结点中确定X ∈(X i ,X i+1),其概率为a i 。在表示S 的二叉搜索树T 中,设存储元素X i 的结点深度为C i ;叶结点(X i ,X i+1)的结点深度为d i ,则二叉搜索树T 的平均路长p 为多少假设二叉搜索树T[i][j]={X i ,X i+1,···,X j }最优值为m[i][j],W[i][j]= a i-1+b i +···+b j +a j ,则m[i][j](1<=i<=j<=n)递归关系表达式为什么 .二叉树T 的平均路长P=∑=+n i 1 Ci)(1*bi +∑=n j 0 dj *aj

算法分析与设计试卷

《算法分析与设计》试卷(A) (时间90分钟满分100分) 一、填空题(30分,每题2分)。 1.最长公共子序列算法利用的算法是( B )。 A、分支界限法 B、动态规划法 C、贪心法 D、回溯法2.在对问题的解空间树进行搜索的方法中,一个活结点最多有一次机会成为活结点的是( B ). A.回溯法 B.分支限界法 C.回溯法和分支限界法 D.回溯法求解子集树问题 3.实现最大子段和利用的算法是( B )。 A、分治策略 B、动态规划法 C、贪心法 D、回溯法4..广度优先是( A )的一搜索方式。 A、分支界限法 B、动态规划法 C、贪心法 D、回溯法5.衡量一个算法好坏的标准是( C )。 A 运行速度快 B 占用空间少 C 时间复杂度低 D 代码短 6.Strassen矩阵乘法是利用( A)实现的算法。 A、分治策略 B、动态规划法 C、贪心法 D、回溯法 7. 使用分治法求解不需要满足的条件是( A )。 A 子问题必须是一样的 B 子问题不能够重复 C 子问题的解可以合并 D 原问题和子问题使用相同的方法解 8.用动态规划算法解决最大字段和问题,其时间复杂性为( B ). A.logn B.n C.n2 D.nlogn 9.解决活动安排问题,最好用( B )算法 A.分治 B.贪心 C.动态规划 D.穷举 10.下面哪种函数是回溯法中为避免无效搜索采取的策略( B ) A.递归函数 B.剪枝函数C。随机数函数 D.搜索函数11. 从活结点表中选择下一个扩展结点的不同方式将导致不同的分支限界法,以下除( C )之外都是最常见的方式. A.队列式分支限界法 B.优先队列式分支限界法 C.栈式分支限界法 D.FIFO分支限界法 12. .回溯算法和分支限界法的问题的解空间树不会是( D ). A.有序树 B.子集树 C.排列树 D.无序树 13.优先队列式分支限界法选取扩展结点的原则是( C )。 A、先进先出 B、后进先出 C、结点的优先级 D、随机14.下面是贪心算法的基本要素的是( C )。 A、重叠子问题 B、构造最优解 C、贪心选择性质 D、定义最优解15.回溯法在解空间树T上的搜索方式是( A ). A.深度优先 B.广度优先 C.最小耗费优先 D.活结点优先 二、填空题(20分,每空1分)。 1.算法由若干条指令组成的又穷序列,且满足输入、输出、 确定性和有限性四个特性。 2.分支限界法的两种搜索方式有队列式(FIFO)分支限界法、优先队列式分支限界法,用一个队列来存储结点的表叫活节点表。

2015年算法分析与设计期末考试试卷B卷

西南交通大学2015 — 2016学年第(一)学期考试试卷 课程代码 3244152课程名称 算法分析与设计 考试时间 120分钟 阅卷教师签字: __________________________________ 填空题(每空1分,共15分) 1、 程序是 (1) 用某种程序设计语言的具体实现。 2、 矩阵连乘问题的算法可由 (2) 设计实现。 3、 从分治法的一般设计模式可以看出,用它设计出的程序一般是 (3) 4、 大整数乘积算法是用 (4) 来设计的。 5、 贪心算法总是做出在当前看来 (5) 的选择。也就是说贪心算法并不从整体最优 考虑,它所做出的选择只是在某种意义上的 (6) o 6、 回溯法是一种既带有 (7) 又带有 (8) 的搜索算法。 7、 平衡二叉树对于查找算法而言是一种变治策略,属于变治思想中的 (9) 类型 8、 在忽略常数因子的情况下,0、门和0三个符号中, (10) 提供了算法运行时 间的一个上界。 9、 算法的“确定性”指的是组成算法的每条 (11) 是清晰的,无歧义的。 10、 冋题的(12) 是该冋题可用动态规划算法或贪心算法求解的关键特征。 11、 算法就是一组有穷 (13),它们规定了解决某一特定类型问题的 (14) o 12、 变治思想有三种主要的类型:实例化简,改变表现, (15) o 、 ___________________________________________________________________________________ L 线订装封密 线订装封密 、 __________________ 二 线订装封密 级班 选择题(每题2分,共20 分)

算法设计与分析考试题(自测)

1.一个算法就是一个有穷规则的集合,其中之规则规定了解决某一特殊类型问题的一系列运算,此外,算法还应具有以下五个重要特性:_有穷性__,_确定性_,_可行性_,_ (0个或多个)输入__,_ (1个或多个)_输出_。 2.算法的复杂性有__时间复杂性__和__空间复杂性__之分,衡量一个 算法好坏的标准是__时间复杂度高低___。 3.某一问题可用动态规划算法求解的显著特征是___该问题具有最优 子结构性质___。 4.若序列X={B,C,A,D,B,C,D},Y={A,C,B,A,B,D,C,D},请给出序列X和Y的一个最长公共子序列_{A,B,C,D}_。{BABCD}或{CABCD}或{CADCD} 5.用回溯法解问题时,应明确定义问题的解空间,问题的解空间至少应包含_问题的一个(最优)解_。 6.动态规划算法的基本思想是将待求解问题分解成若干_子问题_,先求解_子问题__,然后从这些_子问题_的解得到原问题的解。 7.以深度优先方式系统搜索问题解的算法称为__回溯法__。 背包问题的回溯算法所需的计算时间为__O(n2n)__,用动态规划算法所需的计算时间为_O(n)__。o(min{nc,2n}) 9.动态规划算法的两个基本要素是_最优子结构_和_重叠子问题___。 10.二分搜索算法是利用__动态规划法__实现的算法。 二、综合题(50分)

1.写出设计动态规划算法的主要步骤。 1、解:(1)找出最优解的性质,并刻画其结构特征; (2)递归地定义最优值; (3)以自底向上的方式计算出最优值; (4)根据计算最优值时得到的信息,构造最优解。 ①问题具有最优子结构性质;②构造最优值的递归关系表达式; ③最优值的算法描述;④构造最优解 2.流水作业调度问题的johnson算法的思想。 2、解:①令N1={i|a i=b i};②将N1中作业按a i的非减序排序得到N1’,将N2中作业按b i的非增序排序得到N2’; ③N1’中作业接N2’中作业就构成了满足Johnson法则的最优调度。 3.若n=4,在机器M1和M2上加工作业i所需的时间分别为a i和b i,且(a1,a2,a3,a4)=(4,5,12,10),(b1,b2,b3,b4)=(8,2,15,9)求4个作业的最优调度方案,并计算最优值。 3、解:步骤为:N1={1,3},N2={2,4}; N1’={1,3},N2’={4,2}; 最优值为:38 4.使用回溯法解0/1背包问题:n=3(3种物品),C=9(背包的容量

算法设计与分析试卷A及答案

考试课程: 班级: 姓名: 学号: ------------------------------------------------- 密 ---------------------------------- 封 ----------------------------- 线 ---------------------------------------------------------

考试课程: 班级: 姓名: 学号: ------------------------------------------------- 密 ---------------------------------- 封 ----------------------------- 线 ---------------------------------------------------------

参考答案 一、填空 1、空间复杂度 时间复杂度 2、回溯法 3、递归算法 4、渐进确界或紧致界 5、原问题的较小模式 递归技术 6、问题的计算复杂性分析有一个共同的客观尺度 7、②③④① 8、问题的最优解包含其子问题的最优解 9、局部最优 10、正确的 三、简答题 1、高级语言更接近算法语言,易学、易掌握,一般工程技术人员只需要几周时间的培训就可以胜任程序员的工作; 高级语言为程序员提供了结构化程序设计的环境和工具,使得设计出来的程序可读性好,可维护性强,可靠性高; 高级语言不依赖于机器语言,与具体的计算机硬件关系不大,因而所写出来的程序可植性好、重用率高; 把繁杂琐碎的事务交给编译程序,所以自动化程度高,开发周期短,程序员可以集中时间和精力从事更重要的创造性劳动,提高程序质量。 2、 ①不能保证最后求得的解是最佳的;即多半是近似解。(少数问题除外) ②策略容易发现(关键:提取清楚问题中的维度), 而且运用简单,被广泛运用。 ③策略多样,结果也多样。 ④算法实现过程中,通常用到辅助算法:排序 3、解:① 因为:;01 -10n n )1-10n n (lim 22 2=+-+→∞n n 由渐近表达式的定义易知: 1-10n n 2 2+是n ;的渐近表达式。 ② 因为:;0n 1/ 5/n 1414)n 1/ 5/n 14(lim 22=++-++∞→n 由渐近表达式的定义易知: 14是14+5/n+1/ n 2的渐近表达式。 4、 找出最优解的性质,并刻划其结构特征。 递归地定义最优值。 以自底向上的方式计算出最优值。 根据计算最优值时得到的信息,构造最优解。 四、算法设计题 1、按照单位效益从大到小依次排列这7个物品为:FBGDECA 。将它们的序号分别记为1~7。则可生产如下的状态空间搜索树。其中各个节点处的限界函数值通过如下方式求得:【排序1分】 5x =6x =7x =

(完整版)算法设计与分析期末考试卷及答案a

一.填空题(每空 2 分,共30分) 1.算法的时间复杂性指算法中的执行次数。 2.在忽略常数因子的情况下,O、和三个符号中,提供了算法运行时间的一个上界。 3.设D n表示大小为n的输入集合,t(I)表示输入为I时算法的运算时间, p(I)表示输入 I 出现的概率,则算法的平均情况下时间复杂性A(n)= 。 4.分治算法的时间复杂性常常满足如下形式的递归方程: f (n) d , n n0 f(n) af(n/c) g(n) , n n0 其中,g(n)表示。 5. 分治算法的基本步骤包括。6.回溯算法的基本思想是。 7.动态规划和分治法在分解子问题方面的不同点是。 8.贪心算法中每次做出的贪心选择都是最优选择。 9.PQ 式的分支限界法中,对于活结点表中的结点,其下界函数值越小,优先级 10.选择排序、插入排序和归并排序算法中,算法是分治算法。 11.随机算法的一个基本特征是对于同一组输入,不同的运行可能得到的结果。12. 对于下面的确定性快速排序算法,只要在步骤3 前加入随机 化步骤,就可得到一个随机化快速排序算法,该随机化步骤的功能是。 算法QUICKSORT 输入:n 个元素的数组A[1..n] 。 输出:按非降序排列的数组 A 中的元素

1. quicksort(1, n) end QUICKSORT _ _ 过程 quicksort(A, low, high) _ _ // 对 A[low..high] 中的元素按非降序排序。 _ 号 学 2. if low

5.《算法设计与分析》试题库

《算法分析与设计》试题库 (一) 一、 选择题 1.应用Johnson 法则的流水作业调度采用的算法是(D ) A. 贪心算法 B.分支限界法 C.分治法 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); 2.Hanoi 塔问题如下图所示。现要求将塔座A 上的的所有圆盘移到塔座 B 上,并 D.动态规划算法

3. 动态规划算法的基本要素为(C) A. 最优子结构性质与贪心选择性质 B ?重叠子问题性质与贪心选择性质 C.最优子结构性质与重叠子问题性质

D.预排序与递归调用 4. 算法分析中,记号0表示(B),记号0表示(A),记号。表示(D) A. 渐进下界 B. 渐进上界 C. 非紧上界 D. 紧渐进界 E. 非紧下界 5. 以下关于渐进记号的性质是正确的有:(A) A. f(n) - P(g(n)),g(n) - 心(h(n))二f(n) - P(h(n)) B. f(n) =0(g(n)),g(n) =0(h(n))二h(n) =0(f(n)) C. O(f(n ))+0(g( n)) = O(mi n{f(n ),g( n)}) D. f(n) =0(g(n)) = g(n) -0(f (n)) 6?能采用贪心算法求最优解的问题,一般具有的重要性质为:(A) A. 最优子结构性质与贪心选择性质 B ?重叠子问题性质与贪心选择性质 C. 最优子结构性质与重叠子问题性质 D. 预排序与递归调用 7.回溯法在问题的解空间树中,按(D)策略,从根结点出发搜索解空间树。 A. 广度优先 B.活结点优先 C.扩展结点优先 D.深度优先

算法设计与分析试卷(2010)

内部资料,转载请注明出处,谢谢合作。 算法设计与分析试卷(A 卷) 一、 选择题 ( 选择1-4个正确的答案, 每题2分,共20分) (1)计算机算法的正确描述是: A .一个算法是求特定问题的运算序列。 B .算法是一个有穷规则的集合,其中之规则规定了一个解决某一特定类型的问题的运算序列。 C .算法是一个对任一有效输入能够停机的图灵机。 D .一个算法,它是满足5 个特性的程序,这5个特性是:有限性、确定性、能 行性、有0个或多个输入且有1个或多个输出。 (2)影响程序执行时间的因素有哪些? A .算法设计的策略 B .问题的规模 C .编译程序产生的机器代码质量 D .计算机执行指令的速度 (3)用数量级形式表示的算法执行时间称为算法的 A .时间复杂度 B .空间复杂度 C .处理器复杂度 D .通信复杂度 (4)时间复杂性为多项式界的算法有: A .快速排序算法 B .n-后问题 C .计算π值 D .prim 算法 (5)对于并行算法与串行算法的关系,正确的理解是: A .高效的串行算法不一定是能导出高效的并行算法 B .高效的串行算法不一定隐含并行性 C .串行算法经适当的改造有些可以变化成并行算法 D. 用串行方法设计和实现的并行算法未必有效 (6)衡量近似算法性能的重要标准有: A .算法复杂度 B .问题复杂度 C .解的最优近似度 D .算法的策略 (7)分治法的适用条件是,所解决的问题一般具有这些特征: A .该问题的规模缩小到一定的程度就可以容易地解决; B .该问题可以分解为若干个规模较小的相同问题; C .利用该问题分解出的子问题的解可以合并为该问题的解 D .该问题所分解出的各个子问题是相互独立的。 (8)具有最优子结构的算法有: A .概率算法 B .回溯法 C .分支限界法 D .动态规划法 (9)下列哪些问题是典型的NP 完全问题: A .排序问题 B .n-后问题 C .m-着色问题 D .旅行商问题 (10)适于递归实现的算法有: A .并行算法 B .近似算法 C .分治法 D .回溯法 二、算法分析题(每小题5分,共10分) (11)用展开法求解递推关系: (12)分析当输入数据已经有序时快速排序算法的不足,提出算法的改进方案。 ???>+-==1 1)1(211)(n n T n n T

算法分析期末试题集答案

1.应用Johnson 法则的流水作业调度采用的算法是(D ) A. 贪心算法 B. 分支限界法 C.分治法 D. 动态规划算法 2.Hanoi 塔问题如下图所示。现要求将塔座A 上的的所有圆盘移到塔座B 上,并仍按同样顺序叠置。移动圆盘时遵守Hanoi 塔问题的移动规则。由此设计出解Hanoi 塔问题的递归算确的为:(B ) 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. 预排序与递归调用 7. 回溯法在问题的解空间树中,按(D )策略,从根结点出发搜索解空间树。 A . 广度优先 B. 活结点优先 C.扩展结点优先 D. 深度优先 Hanoi 塔 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卷考试时量:120分钟 题号一二三四五总分统分人 得分 阅卷人 复查人 一、填空题(每小题3 分,共计30 分) 1、用O、Ω与θ表示函数f与g之间得关系______________________________。 2、算法得时间复杂性为,则算法得时间复杂性得阶为__________________________。 3、快速排序算法得性能取决于______________________________。 4、算法就是_______________________________________________________。 5、在对问题得解空间树进行搜索得方法中,一个活结点最多有一次机会成为活结点得就是_________________________。 6、在算法得三种情况下得复杂性中,可操作性最好且最有实际价值得就是_____情况下得时间复杂性。 7、大Ω符号用来描述增长率得下限,这个下限得阶越___________,结果就越有价值。。 8、____________________________就是问题能用动态规划算法求解得前提。 9、贪心选择性质就是指____________________________________________________________________________________________________________________。 10、回溯法在问题得解空间树中,按______________策略,从根结点出发搜索解空间树。 二、简答题(每小题10分,共计30分) 1、试述回溯法得基本思想及用回溯法解题得步骤。 2、有8个作业{1,2,…,8}要在由2台机器M1与M2组成得流水线上完成加工。每个作业加工得顺序都就是先在M1上加工,然后在M2上加工。M1与M2加工作业i所需得时间分别为: M110 2 8 12 6 9414

算法分析与设计复习题及答案

算法分析与设计复习题及答案一、单选题 1.D 2.B 3.C 4.D 5.D 6.D 7.C 8.D 9.B 10.C 11.D 12.B 13.D 14.C 15.C 16.D 17.D 18.D 19.D 20.C 1.与算法英文单词algorithm具有相同来源的单词是()。 A logarithm B algiros C arithmos D algebra 2.根据执行算法的计算机指令体系结构,算法可以分为()。 A精确算法与近似算法B串行算法语并行算法 C稳定算法与不稳定算法D32位算法与64位算法 3.具有10个节点的完全二叉树的高度是()。 A6B5C3D 2 4.下列函数关系随着输入量增大增加最快的是()。 Alog2n B n2 C 2n D n! 5.下列程序段的S执行的次数为( )。 for i ←0 to n-1 do for j ←0 to i-1 do s //某种基本操作 A.n2 B n2/2 C n*(n+1) D n(n+1)/2 6.Fibonacci数列的第十项为( )。 A 3 B 13 C 21 D 34 7.4个盘子的汉诺塔,至少要执行移动操作的次数为( )。 A 11次 B 13次 C 15次 D 17次 8.下列序列不是堆的是()。 A 99,85,98,77,80,60,82,40,22,10,66 B 99,98,85,82,80,77,66,60,40,22,10 C 10,22,40,60,66,77,80,82,85,98,99 D 99,85,40,77,80,60,66,98,82,10,22 9.Strassen矩阵乘法的算法复杂度为()。 AΘ(n3)BΘ(n2.807) CΘ(n2) DΘ(n) 10.集合A的幂集是()。 A.A中所有元素的集合 B. A的子集合 C. A 的所有子集合的集合 D. 空集 11.与算法英文单词algorithm具有相同来源的单词是()。 A logarithm B algiros C arithmos D algebra 12.从排序过程是否完全在内存中显示,排序问题可以分为()。 A稳定排序与不稳定排序B内排序与外排序 C直接排序与间接排序D主排序与辅助排序 13.下列()不是衡量算法的标准。 A时间效率B空间效率 C问题难度D适应能力 14.对于根树,出度为零的节点为()。 A0节点B根节点C叶节点D分支节点 15.对完全二叉树自顶向下,从左向右给节点编号,节点编号为10的父节点编号为()。 A0B2C4D6 16.下列程序段的算法时间的复杂度为()。 for i ←0 to n do for j ←0 to m do

《算法分析与设计》期末试题及参考答案

《算法分析与设计》期末试题及参考答案 一、简要回答下列问题: 1.算法重要特性是什么? 1.确定性、可行性、输入、输出、有穷性 2. 2.算法分析的目的是什么? 2.分析算法占用计算机资源的情况,对算法做出比较和评价,设计出额更好的算法。 3. 3.算法的时间复杂性与问题的什么因素相关? 3. 算法的时间复杂性与问题的规模相关,是问题大小n的函数。 4.算法的渐进时间复杂性的含义? 4.当问题的规模n趋向无穷大时,影响算法效率的重要因素是T(n)的数量级,而其他因素仅是使时间复杂度相差常数倍,因此可以用T(n)的数量级(阶)评价算法。时间复杂度T(n)的数量级(阶)称为渐进时间复杂性。 5.最坏情况下的时间复杂性和平均时间复杂性有什么不同? 5. 最坏情况下的时间复杂性和平均时间复杂性考察的是n固定时,不同输入实例下的 算法所耗时间。最坏情况下的时间复杂性取的输入实例中最大的时间复杂度: W(n) = max{ T(n,I) } , I∈Dn 平均时间复杂性是所有输入实例的处理时间与各自概率的乘积和: A(n) =∑P(I)T(n,I) I∈Dn 6.简述二分检索(折半查找)算法的基本过程。 6. 设输入是一个按非降次序排列的元素表A[i:j] 和x,选取A[(i+j)/2]与x比较, 如果A[(i+j)/2]=x,则返回(i+j)/2,如果A[(i+j)/2]

《算法分析与设计》期末复习题

一、选择题 1.一个.java文件中可以有()个public类。 A.一个B.两个C.多个D.零个 2.一个算法应该是() A.程序B.问题求解步骤的描述 C.要满足五个基本特性D.A和C 3.用计算机无法解决“打印所有素数”的问题,其原因是解决该问题的算法违背了算法特征中的()A.唯一性B.有穷性C.有0个或多个输入D.有输出 4.某校有6位学生参加学生会主席竞选,得票数依次为130,20,98,15,67,3。若采用冒泡排序算法对其进行排序,则完成第二遍时的结果是() A.3,15,130,20,98,67B.3,15,20,130,98,67 C.3,15,20,67,130,98 D.3,15,20,67,98,130 5.下列关于算法的描述,正确的是() A.一个算法的执行步骤可以是无限的B.一个完整的算法必须有输出 C.算法只能用流程图表示D.一个完整的算法至少有一个输入 6.Java Application源程序的主类是指包含有()方法的类。 A、main方法 B、toString方法 C、init方法 D、actionPerfromed方法 7.找出满足各位数字之和等于5的所有三位数可采用的算法思路是() A.分治法B.减治法C.蛮力法D.变治法 8.在编写Java Application程序时,若需要使用到标准输入输出语句,必须在程序的开头写上( )语句。 A、import java.awt.* ; B、import java.applet.Applet ; C、import java.io.* ; D、import java.awt.Graphics ; 9.计算某球队平均年龄的部分算法流程图如图所示,其中:c用来记录已输入球员的人数,sum用来计算有效数据之和,d用来存储从键盘输入的球员年龄值,输入0时表示输入结束。

《算法分析与设计》期末考试复习题纲(完整版)

《算法分析与设计》期末复习题 一、选择题 1.算法必须具备输入、输出和( D )等4个特性。 A.可行性和安全性 B.确定性和易读性 C.有穷性和安全性 D.有穷性和确定性 2.算法分析中,记号O表示( B ),记号Ω表示( A ) A.渐进下界 B.渐进上界 C.非紧上界 D.紧渐进界 3.假设某算法在输入规模为n时的计算时间为T(n)=3*2^n。在某台计算机上实现并 完成概算法的时间为t秒。现有另一台计算机,其运行速度为第一台的64倍,那么在这台新机器上用同一算法在t秒内能解输入规模为多大的问题( B )解题方法:3*2^n*64=3*2^x A.n+8 B.n+6 C.n+7 D.n+5 4.设问题规模为N时,某递归算法的时间复杂度记为T(N),已知T(1)=1, T(N)=2T(N/2)+N/2,用O表示的时间复杂度为( C )。 A.O(logN) B.O(N) C.O(NlogN) D.O(N2logN) 5.直接或间接调用自身的算法称为( B )。 A.贪心算法 B.递归算法 C.迭代算法 D.回溯法 6.Fibonacci数列中,第4个和第11个数分别是( D )。 A.5,89 B.3,89 C.5,144 D.3,144 7.在有8个顶点的凸多边形的三角剖分中,恰有( B )。

A.6条弦和7个三角形 B.5条弦和6个三角形 C.6条弦和6个三角形 D.5条弦和5个三角形 8.一个问题可用动态规划算法或贪心算法求解的关键特征是问题的( B )。 A.重叠子问题 B.最优子结构性质 C.贪心选择性质 D.定义最优解 9.下列哪个问题不用贪心法求解( C )。 A.哈夫曼编码问题 B.单源最短路径问题 C.最大团问题 D.最小生成树问题 10.下列算法中通常以自底向上的方式求解最优解的是( B )。 A.备忘录法 B.动态规划法 C.贪心法 D.回溯法 11.下列算法中不能解决0/1背包问题的是( A )。 A.贪心法 B.动态规划 C.回溯法 D.分支限界法 12.下列哪个问题可以用贪心算法求解( D )。 A.LCS问题 B.批处理作业问题 C.0-1背包问题 D.哈夫曼编码问题 13.用回溯法求解最优装载问题时,若待选物品为m种,则该问题的解空间树的结点 个数为()。 A.m! B.2m+1 C.2m+1-1 D.2m 14.二分搜索算法是利用( A )实现的算法。 A.分治策略 B.动态规划法 C.贪心法 D.回溯法 15.下列不是动态规划算法基本步骤的是( B )。P44 A.找出最优解的性质 B.构造最优解 C.算出最优解(应该是最优值) D.定义最优解

算法分析考试题

1. )(n T 给定数组a[0:n-1],试设计一个算法,在最坏情况下用n+[logn]-2次比较找出 a[0:n-1] 中的元素的最大值和次大值. (算法分析与设计习题 2.16 ) (分治法) a 、 算法思想 用分治法求最大值和次大值首先将问题划分,即将划分成长度相等的两个序列,递归求出左边的最大值次大值,再求出右边的的最大值次大值,比较左右两边,最后得出问题的解。 b 、复杂度分析: 把问题划分为左右两种的情况,需要分别递归求解,时间复杂度可如下计算: 有递推公式为: T(n)=1 n=1 T(n)= 2T(n/2)+1 n>1 所以,分治算法的时间复杂度是n+[logn]-2,当n 为奇数时,logn 取上线,当n 为偶数时,logn 取下线。//不知道为什么会-2! C 、代码实现: #include int a[100]; void maxcmax(int i,int j,int &max,int &cmax) { int lmax,lcmax,rmax,rcmax; int mid; if (i==j) { max=a[i]; cmax=a[i]; } else if (i==j-1) if (a[i]rmax)

if(lcmax>rmax) { max=lmax; cmax=lcmax; } else { max=lmax; cmax=rmax; } else if(rcmax>lmax) { if(rmax==rcmax) { max=rmax; cmax=lmax; } else { max=rmax; cmax=rcmax; } } else { max=rmax; cmax=lmax; } } } int main() { int n; int max,cmax; printf("输入数组长度"); scanf("%d",&n); printf("输入数组:\n"); for(int i=0;i

算法设计与分析期末试题-考试版(汇编)

1、用计算机求解问题的步骤:1、问题分析 2、数学模型建立 3、算法设计与选择 4、算法指标 5、算法分析 6、算法实现 7、程序调试 8、结果整理文档编制 2、算法定义:算法是指在解决问题时,按照某种机械步骤一定可以得到问题结果的处理过程 3、算法的三要素 1、操作 2、控制结构 3、数据结构 算法具有以下5个属性: 有穷性: 确定性: 可行性: 输入: 输出: 算法设计的质量指标: 正确性:算法应满足具体问题的需求; 可读性:算法应该好读,以有利于读者对程序的理解; 健壮性:算法应具有容错处理,当输入为非法数据时,算法应对其作出反应,而不是产生莫名其妙的输出结果。 效率与存储量需求:效率指的是算法执行的时间;存储量需求指算法执行过程中所需要的最大存储空间。一般这两者与问题的规模有

关。 复杂性的渐近性态 设T(N)是算法A的复杂性函数,使得当N→∞时有: (T(N)-T’(N))/T(N) → 0 那么,我们就说T’(N)是T(N)当N→∞时的渐近性态,或叫T’(N)为算法A当N→∞的渐近复杂性而与T(N)相区别。 P = NP 经常采用的算法主要有迭代法、分而治之法、贪婪法、动态规划法、回溯法、分支限界法 分而治之法 1、基本思想 将一个难以直接解决的大问题,分割成一些规模较小的相同问题,以

便各个击破,分而治之。 分治法所能解决的问题一般具有以下几个特征: (1)该问题的规模缩小到一定的程度就可以容易地解决; (2)该问题可以分解为若干个规模较小的相同问题,即该问题具有最优子结构性质; (3)利用该问题分解出的子问题的解可以合并为该问题的解; (4)该问题所分解出的各个子问题是相互独立的,即子问题之间不包含公共的子子问题。 3、分治法的基本步骤 (1)分解:将原问题分解为若干个规模较小,相互独立,与原问题形式相同的子问题; (2)解决:若子问题规模较小而容易被解决则直接解,否则递归地解各个子问题; (3)合并:将各个子问题的解合并为原问题的解。 递归: 直接或间接的调用自身的算法,叫做递归算法。 1·期盘覆盖 用分治策略,可以设计解棋盘问题的一个简捷的算法。 当k>0时,将2^k * 2^k棋盘分割为4个2^(k-1) * 2^(k-1)子棋盘 特殊方格必位于4个较小子棋盘之一中,其余3个子棋盘中无特殊方格。为了将这3个无特殊方格的子棋盘转化为特殊棋盘,我们可以用一个L型骨牌覆盖这3个较小的棋盘的汇合处,如下图所示,这3个子棋盘上被L型骨牌覆盖的方格就成为该棋盘上的特殊方格,从而将原问题化为4个较小规模的棋盘覆盖问题。递归的使用这种分割,直至棋盘简化为1x1棋盘。

算法设计与分析试题与答案

一、填空题(20分) 1.一个算法就是一个有穷规则的集合,其中之规则规定了解决某一特殊类型问题的一系列运算,此外,算法还应具有以下五个重要特性: 确定性,有穷性,可行性,0个或多个输入,一个或多个输出。 2.算法的复杂性有时间复杂性和空间复杂性之分,衡量一个算法好坏的标准是时间复杂度高低。 3.某一问题可用动态规划算法求解的显著特征是该问题具有最优子结构性质。 4.若序列X={B,C,A,D,B,C,D},Y={A,C,B,A,B,D,C,D},请给出序列X和Y的一个最长公共子序列{BABCD}或{CABCD}或{CADCD}。 5.用回溯法解问题时,应明确定义问题的解空间,问题的解空间至少应包含一个(最优)解。 6.动态规划算法的基本思想是将待求解问题分解成若干子问题,先求解子问题,然后从这些子问题的解得到原问题的解。 7.以深度优先方式系统搜索问题解的算法称为回溯法。 8.0-1背包问题的回溯算法所需的计算时间为o(n*2n) ,用动态规划算法所需的计算时间为o(min{nc,2n})。 9.动态规划算法的两个基本要素是最优子结构和重叠子问题。 10.二分搜索算法是利用动态规划法实现的算法。 二、综合题(50分) 1.写出设计动态规划算法的主要步骤。 ①问题具有最优子结构性质;

②构造最优值的递归关系表达式; ③最优值的算法描述; ④构造最优解; 2.流水作业调度问题的johnson算法的思想。 ②N1={i|ai=bi}; ②将N1中作业按ai的非减序排序得到N1’,将N2中作业按bi的非增序排序得到N2’; ③N1’中作业接N2’中作业就构成了满足Johnson法则的最优调度。 3.若n=4,在机器M1和M2上加工作业i所需的时间分别为ai和bi,且 (a1,a2,a3,a4)=(4,5,12,10),(b1,b2,b3,b4)=(8,2,15,9)求4个作业的最优调度方案,并计算最优值。 步骤为:N1={1,3},N2={2,4}; N1’={1,3}, N2’={4,2}; 最优值为:38 4.使用回溯法解0/1背包问题:n=3,C=9,V={6,10,3},W={3,4,4},其解空间有长度为3 的0-1向量组成,要求用一棵完全二叉树表示其解空间(从根出发,左1右0),并画出其解空间树,计算其最优值及最优解。 解空间为{(0,0,0),(0,1,0),(0,0,1),(1,0,0),(0,1,1),(1,0,1), (1,1,0),(1,1,1)}。 解空间树为:

算法分析与设计试题

一、选择题(20分) 1.最长公共子序列算法利用的算法是( B )。 A、分支界限法 B、动态规划法 C、贪心法 D、回溯法2.实现棋盘覆盖算法利用的算法是( A )。 A、分治法 B、动态规划法 C、贪心法 D、回溯法 3.下面是贪心算法的基本要素的是( C )。 A、重叠子问题 B、构造最优解 C、贪心选择性质 D、定义最优解 4.回溯法的效率不依赖于下列哪些因素( D ) A.满足显约束的值的个数 B. 计算约束函数的时间 C. 计算限界函数的时间 D. 确定解空间的时间 5.下面哪种函数是回溯法中为避免无效搜索采取的策略( B ) A.递归函数 B.剪枝函数C。随机数函数 D.搜索函数6.采用最大效益优先搜索方式的算法是( A )。 A、分支界限法 B、动态规划法 C、贪心法 D、回溯法7.贪心算法与动态规划算法的主要区别是( B )。 A、最优子结构 B、贪心选择性质 C、构造最优解 D、定义最优解 8. 实现最大子段和利用的算法是( B )。 A、分治策略 B、动态规划法 C、贪心法 D、回溯法 9.优先队列式分支限界法选取扩展结点的原则是( C )。 A、先进先出 B、后进先出 C、结点的优先级 D、随机 10.下列算法中通常以广度优先方式系统搜索问题解的是( A )。 A、分支限界法 B、动态规划法 C、贪心法 D、回溯法

二、填空题(22分每空2分) 1.算法是由若干条指令组成的有穷序列,且要满足输入、输出、确定性和有限性四条性质。 2、大整数乘积算法是用分治法来设计的。 3、以广度优先或以最小耗费方式搜索问题解的算法称为分支限界法。 4、舍伍德算法总能求得问题的一个解。 5、贪心选择性质是贪心算法可行的第一个基本要素,也是贪心算法与动态规划算法的主要区别。 6.快速排序 template void QuickSort (Type a[], int p, int r) { if (p using namespace std; int Gcd(int m,int n)

相关文档
最新文档