[题集]Lecture 8. Dynamic Programming

1.To solve a problem by dynamic programming instead of recursions, the key approach is to store the results of computations for the subproblems so that we only have to compute each different subproblem once. Those solutions can be stored in an array or a hash table.

T

2.We can tell that there must be a lot of redundant calculations during the exhaustive search for the matrix multiplication problem, because the search work load is the Catalan number, yet there are only ___ different sub-problems .

A.O(N)

B.O(N^2)

C.O(N^3)

D.O(N^4)

因为只要计算O(N2)个值,B

3.The root of an optimal binary search tree always contains the key with the highest search probability.

选择i到j的节点的根的时候,计算的cost是左边cost+右边cost+i到j的所有节点的可能性。后面一部分是个常数。(和解题无关)

如果这个highest search probability的点在端点的话,那么会导致另一侧的子树深度很深,导致搜索时间变长,所以不一定。

4.Why doesn’t Floyd algorithm work if there are negative-cost cycles?

A.Because Floyd didn’t like negative numbers.

B.Because Floyd algorithm will terminate after finite steps, yet the shortest distance is negative infinity if there is a negative-cost cycle.

C.Because Floyd algorithm will fall into infinite loops.

D.Because a negative-cost cycle will result in a negative D[i][i], yet Floyd algorithm can only accept positive weights

弗洛伊德算法的意思是:两个点最短距离是两个点当前最短距离和用第三个点相连后最短距离的最小值。

会一直走这条负的边,但是N-1步以后一定会停下来。

5.In dynamic programming, we derive a recurrence relation for the solution to one subproblem in terms of solutions to other subproblems. To turn this relation into a bottom up dynamic programming algorithm, we need an order to fill in the solution cells in a table, such that all needed subproblems are solved before solving a subproblem. Among the following relations, which one is impossible to be computed?

A.A(i,j)=min(A(i−1,j),A(i,j−1),A(i−1,j−1))

B.A(i,j)=F(A(min{i,j}−1,min{i,j}−1),A(max{i,j}−1,max{i,j}−1))

C.A(i,j)=F(A(i,j−1),A(i−1,j−1),A(i−1,j+1))

D.A(i,j)=F(A(i−2,j−2),A(i+2,j+2))

列出顺序

Ai-1ii+1
j-100
j01
j+1
Ci-1ii+1
j-100
j1
j+10
Di-2i-1ii+1i+2
j-20
j-1
j1
j+1
j+20

D显然是不行的。

找的顺序是先行后列,或者先列后行,上下各有一种,一共4种

6.Rod-cutting Problem: Given a rod of total length N inches and a table of selling prices P,L for lengths L=1,2,⋯,M. You are asked to find the maximum revenue R N obtainable by cutting up the rod and selling the pieces. For example, based on the following table of prices, if we are to sell an 8-inch rod, the optimal solution is to cut it into two pieces of lengths 2 and 6, which produces revenue R​8=P2 +P6=5+17=22. And if we are to sell a 3-inch rod, the best way is not to cut it at all.

Length L |1 | 2| 3| 4 | 5 | 6| 7| 8| 9| 10|
-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-
Price P/L| 1 | 5| 8| 9| 10| 17| 17| 20| 23| 28|

Which one of the following statements is FALSE?

A.This problem can be solved by dynamic programming

B.The time complexity of this algorithm is O(N​2​)

C.If N≤M, we have RN=max{PN,max​1≤i<N{Ri+RN−i}}

D.If N>M, we have RN=max1≤i<N{Ri+R​N−M}

关于木棍的题目,对于每一个长度都需要从1开始遍历到n,因此是N^2

而不管怎么样,最大值都是RN-i+Ri,不会是RN-M,因此错

7.Given a recurrence equation f​i,j,k=f​i,j+1,k​​ +min0≤l≤k​​ {f​i−1,j,l​ +wj,l​​ }. To solve this equation in an iterative way, we cannot fill up a table as follows:

A.for k in 0 to n: for i in 0 to n: for j in n to 0

B.for i in 0 to n: for j in 0 to n: for k in 0 to n

C.for i in 0 to n: for j in n to 0: for k in n to 0

D.for i in 0 to n: for j in n to 0: for k in 0 to n

范围的这种的意思:l换成0或者k都必须成立

可以列出相对顺序。黄色的是要算的必须在所有其他的之后出现

i-1ii+1
j-1
j0-kk
j+1k
A:对于k(想象成为高度),所有的有k的都已知了。对于i,i-1这列已知了。对于j,j+1这列已知了。

B:i-1已知了,j-1已知了,但是j+1未知,不行

C:i-1已知了,j+1已知了,k是已知了

D:可以。

8.To solve the optimal binary search tree problem, we have the recursive equation cij=mini≤l≤j{wij+c​i,l−1+c​l+1,j​​ }. To solve this equation in an iterative way, we must fill up a table as follows:

A.
for i= 1 to n-1 do;
for j= i to n do;
for l= i to j do

B.
for j= 1 to n-1 do;
for i= 1 to j do;
for l= i to j do

C.
for k= 1 to n-1 do;
for i= 1 to n-k do;
set j = i+k;
for l= i to j do

D.
for k= 1 to n-1 do;
for i= 1 to n do;
set j = i+k;
for l= i to j do

画出表

i-1ii+1j-1jj+1
i0
i+10
0
j-10
j100000
j+1

这题很特殊,因为cii是已知的。i和j之间的距离k必须放在最外面。D完全因为超出范围所以错了。A,B错在更新的不一定是最小的,因此必须固定两个之间的距离,遍历所有的ij

9.Which one of the following problems can be best solved by dynamic programming?

A.Mergesort

B.Closest pair of points problem

C.Quicksort

D.Longest common subsequence problem

前三个是分治。注意,DP必须记录下子问题的答案才行。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值