《山东建筑大学计算机学院算法分析算法复习题.docx》由会员分享,可在线阅读,更多相关《山东建筑大学计算机学院算法分析算法复习题.docx(15页珍藏版)》请在三一办公上搜索。
1、山东建筑大学计算机学院算法分析算法复习题1. The O-notation provides an asymptotic upper bound. The W-notation provides an asymptotic lower bound. The -notation asymptotically a function form above and below. O型符号提供一个渐近的上限。符号提供一个渐近下界。 -符号渐近函数形式的上方和下方。 2. To represent a heap as an array,the root of tree is A1, and given t
2、he 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; . 代表一个堆中的一个数组,树的根节点是A1,并且给出一个节点i,那么该节点的父节点是 左孩子 右孩子 3. Because the heap of n elements is a binary tree, the height of any node is at most Q(lg n). 因为n个元素的堆是一个
3、二叉树,任意节点的树高最多是 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
4、. 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 xij = zj . Let X = and Y = be
5、sequences, and let Z = be any LCS of X and Y. (1). If xm = yn, then zk = xm = yn and Zk-1 is an LCS of Xm-1 and Yn-1. (2). If xm yn, then zk xm implies that Z is an LCS of Xm-1 and Y. (3). If xm yn, then zk yn implies that Z is an LCS of X and Yn-1. 7. A greedy algorithm always makes the choice that
6、 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 algorith
7、m. 贪心选择 和最优子结构是贪心算法的两个重要组成部分。 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
8、 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 (V3 lg V). 一个矩阵相乘问题的解决可以一个 时间复杂度算法的所有路径的最短路径问题,改进后的时间复杂度是 。 12. Floyd-Warsha
9、ll 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. 2快速排序的平均时间复杂度是 O(n lg n) ,最坏时间复杂度是 O(n) 。 14. The MERGE(A,p,q,r) procedure in merge sort takes time
10、 (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
11、(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 W(n lg n). 一个决策树代表一个比较类型,通过比较排序。N个元素的任意决策树的渐进高度是 。 True-false questions 1. An algorithm is said to be correct if, for some inp
12、ut 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
13、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.
14、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 W(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 theore
15、tical 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 su
16、bproblems 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
17、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
18、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:
19、AParent(i) Ai for all nodes i 1. F 19. For array of length n, all elements in range An/2 + 1 . n are heaps. T 20. The tighter bound of the running time to build a max-heap from an unordered array isnt in linear time. F 21. The call to BuildHeap takes O(n) time, Each of the n - 1 calls to Heapify tak
20、es 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 Ap.r is partitioned into two non-empty subarrays Ap.q and Aq+1.r, All elements in Ap.q are less than all elements in Aq+1.r, th
21、e 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
22、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. Kruskals algorithm uses a disjoint-set data structure to maintain several disjoint sets of elements. T 27. Optimal-subst
23、ructure property is a hallmark of the applicability of both dynamic programming. T 28. Dijkstras 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 =
24、(V, E) with weight function w : E R, let p = be a shortest path from vertex v1 to vertex vk and, for any i and j such that 1 i j k, let pij = be the subpath of p from vertex vi to vertex vj . Then, pij is a shortest path from vi to vj. T 31. Given a weighted, directed graph G = (V, E) with weight fu
25、nction 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 p
26、aths 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 Dijkstras algorithm and the shortest-paths algorithm for directed acyclic graphs, each edge is relaxed exactly once. In the Bellma
27、n-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 Bel
28、lman-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
29、algorithm indicates that no solution exists. If there is no such cycle, the algorithm produces the shortest paths and their weights. F 38. Dijkstras 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.
30、 F 39. Dijkstras 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 tim
31、e of Dijkstras 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 bot
32、tom-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 ith smallest element of n elements using
33、 divide and conquer strategy. 五Write the INSERT-SORT procedure to sort into non-decreasing order. Analyze the running time of it with RAM Model. Whats 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 ord
34、er. 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 gr
35、aph 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. 1421621425225162139919639 6八Given items of different values and w
36、eights, 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 sche
37、duling problem: A Motors Corporation produces automobiles that has two assembly lines, numbered i=1,2. Each line has n stations, numbered j=1,2n. We denote the jth station on line i by Sij. The following figure is an instance of the assembly-line problem with costs entry time ei, exit time xi, the a
38、ssembly time required at station Sij by aij, the time to transfer a chassis away from assembly line I after having gone through station Sij is tij. Please compute the fastest time and construct the fastest way through the factory of the instance. 7 9 3 4 8 4 2 3 2 3 1 3 4 entrance exit 2 1 2 2 1 4 2
39、 8 5 6 4 5 7 十. The matrix-chain multiplication problem can be stated as follows: given a chain of matrices, where for i=1,2,n, matrix Ai has dimension P i-1 Pi, fully parenthesize the product A1,A2,An in a way that minimizes the number of scalar multiplication. We pick as our subproblems the proble
40、ms of determining the minimum cost of a parenthesization of Ai Ai+1 Aj for 1 i j n. Let mi, j be the minimum number of scalar multiplications needed to compute the matrix Ai.j; for the full problem, the cost of a cheapest way to compute A1.n would thus be m1, n. Can you define mi, j recursively? Fin
41、d an optimal parenthesization of a matrix-chain product whose sequence of dimensions is 十一 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
42、of Sequence S1=ACTGATCG and sequence S2=CATGC. Please fill in the blanks in the table below. C A T G C A C T G A T C G 十二 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 appea
43、rs as some leaf) n! lAt most 2hleaves 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 v1 to vertex vk and, for any i and j such that 1 i j k, let pij = b
44、e the subpath of p from vertex vi to vertex vj . Then, pij is a shortest path from vi to vj. 十四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
45、for manipulating max-heaps. And analyze the running time of MAX-Heapify. 三 n! l 2h , which, by taking logarithms, implies h lg(n!) (since the lg function is monotonically increasing) = W(n lg n) 列式和求解5分 十三 Proof: If we decompose path p into v1 vi vj vk, then we have that w(p) = w(p1i) + w(pij) +w(pjk). Now, assume that there is a path pij from vi to vj with weight w(pij) w(pij) . Th