8 Dynamic Programming¶
动态规划
说明
- 本文档仅涉及部分内容,仅可用于复习重点知识
- 本文档部分图片来源于教学课件
将一个问题拆分为一系列的 子问题,且这些子问题 相互重叠
通常按如下 4 个步骤来设计一个动态规划算法:
- 刻画一个最优解的结构特征
- 递归地定义最优解的值
- 计算最优解的值,通常采用自底向上的方法
- 利用计算出的信息构造一个最优解
如果仅需要一个最优解的值,可忽略步骤 4,如果需要步骤 4,有时就需要在执行步骤 3 的过程中维护一些额外信息,以便用来构造一个最优解
1 斐波那契数列¶
递归实现¶
在递归实现中,有很多已经算过了,但是第二次碰到的时候还是会再算一次,浪费时间
循环实现¶

称为 自底向上设计
还有一种处理方式便是 带备忘机制的自顶向下设计,采用递归算法,但是我算出来一个值我就存到一个数组里,下次遇到的时候就可以直接取出来,不需要重复计算
2 Shortest Path in DAGs¶

我们可以发现 dist(D) = min{dist(B) + 1, dist(C) + 3}

3 Maximum subsequence sum¶

设原始数组为 A
,设 D[i]
为以 A[i]
开头的最大子数组和
递推公式:
4 Optimal Binary Search Tree¶



5 0-1 knapsack¶



6 Product assembly¶

7 All-Pairs Shortest Path¶


PTA 8.1
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 = P_2 + P_6 = 5 + 17 = 22\). And if we are to sell a 3-inch rod, the best way is not to cut it at all.
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 \leqslant M\), we have \(R_N = \max\lbrace P_N, \max_{1 \leqslant i < N}\lbrace R_i + R_{N - i} \rbrace \rbrace\)
D. If \(N > M\), we have \(R_N = \max_{1 \leqslant i < N}\lbrace R_i + R_{N - M}\rbrace\)
PTA 8.2
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\lbrace i,j\rbrace - 1, \min\lbrace i,j\rbrace - 1), A(\max\lbrace i,j\rbrace - 1, \max\lbrace i,j\rbrace - 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))\)
PTA 8.3
Given a recurrence equation \(f_{i,j,k} = f_{i,j+1,k} + \min_{0 \leqslant l \leqslant k} \lbrace f_{i-1,j,l} + w_{j,l} \rbrace\). 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
答案
B
B 选项,计算 \(f_{i,j,k}\) 时,\(f_{i,j+1,k}\) 无法得知