软工-2022 Algorithm A-最终
软工-2022 Algorithm A-最终
装 订 线
考试时间 120 分钟
试 题
题号 一 二 三 四 五 六 七 总分
分数 16 20 12 14 16 14 8
得分
1.考试形式:闭卷þ 开卷□
任课教师:
2.考试日期: 年 月 日(答案直接答在试卷上,不要超出装订线)
2 / 6
(3) T(n) = 2T(n/4) + n
装 订 线
original problem.
Fill in the procedure names for the blanks (a)~(c) in the pseudo code skeleton DC(…)
for a general Divide-and-Conquer algorithm:
DC (…){
装 订 线
(a) (…)
for each sub_problem
(b) (…)
学号:
(c) (…)
}
3 / 6
5. Dynamic Programming Strategy(16 points)
(1) (6 points) Describe the mainly steps of Dynamic Programming strategy for solving
optimal problems.
(2) (4 points) Describe the difference of ‘the value of optimal solution’ and ‘optimal
solution’ in Dynamic Programming algorithms, give their exact meanings in the
Floyd-Warshall algorithm for all-pairs shortest paths.
(3) (6 points) Given the optimal substructure used in Matrix Chain Multiplication
problem’s DP algorithm (Write down the recursive equation and explain it). What
should we do to construct the optimal solution when calculating the minimum
number of scalar multiplications?
4 / 6
6. Calculations (14 points)
(1) (5 points) Given the following integers in array A, find the maximum subarray
装 订 线
(write down the calculating steps, and the max sum and subarray respectively).
A = (13, -18, 20, -5, 8, 12, -15, -22, 25, -4)
(2) (4 points) Given the following set A of activities with start and finish times (si, fi),
1 ≤ i ≤ n, select a maximal set S of “non-overlapping” activities (the activities were
already sorted initially by their finish times) .
Index 1 2 3 4 5 6 7 8 9 10
si 1 4 2 5 3 5 7 9 2 12
fi 4 5 6 7 8 9 10 11 12 15
任课教师:
(3) (5 points) Given the following directed graph G, fill in the blanks (a)~(e) in
calculating the lengths of single-source shortest paths using Bellman-Ford
algorithm. Source vertex is A and the order of edge relaxation is marked by ①~
⑥, initial value d[A] is set to 0.
装 订 线
Edge(u,v) ① ② ③ ④ ⑤ ⑥
Target v C B C B C D
Initial(d[v]) ∞ ∞ ∞ ∞ ∞ ∞
Pass 1(d[v]) ∞ ∞ ∞ -1 (a) (b)
Pass 2(d[v]) (c) -1 (d) -1 -4 1
学号:
5 / 6
7. Algorithm Design(8 points)
There is a row of n houses, where each house can be painted one of three colors:
red, blue, or green. The cost of painting each house with a certain color is different.
You have to paint all the houses such that no two adjacent houses have the same color.
The cost of painting each house with a certain color is represented by an n*3 cost
matrix. For example, costs[1][1] is the cost of painting house 1 with the color red;
costs[2][3] is the cost of painting house 2 with color green, and so on...
Design an algorithm to find the minimum cost to paint all houses.
For Example, n=3:
Input: costs = [ [17,2,17],
[16,16,5],
[14,3,19] ]
Minimum cost should be : 10
By painting house 1 into blue, paint house 2 into green, paint house 3 into
blue, we can get minimum cost: 2 + 5 + 3 = 10.
6 / 6