Dynamic Programming III:Parenthesization、Edit Distance 与 Knapsack
Algorithms:Dynamic Programming III:Parenthesization, Edit Distance and Knapsack
本文按 MIT 6.006 Fall 2011 Lecture 21《Dynamic Programming III:Parenthesization, Edit Distance, Knapsack》的顺序整理。重点是 sequence/string subproblem 的选择、matrix parenthesization、edit distance、longest common subsequence、0/1 knapsack,以及 pseudopolynomial time 的边界。
TL;DR
- Sequence subproblems:常见 state 是 suffix、prefix 或 substring;对两个 sequences,state space 通常是两个维度的乘积。
- Parenthesization:猜最后一次 multiplication 的 split point,得到 interval DP,典型复杂度 Θ(n³)。
- Edit distance:对两个 suffixes x[i:] 和 y[j:],猜 delete、insert 或 replace,复杂度 Θ(|x||y|)。
- LCS:当 insert/delete cost 为 1、replace matching cost 为 0 时,edit distance 与 longest common subsequence 有直接关系。
- 0/1 Knapsack:state 通常包含 item position 和 remaining capacity,复杂度 Θ(nW);W 是数值容量,不是输入 bit length。
- Pseudopolynomial:Θ(nW) 对数值 W 是 polynomial,但对 log W 表示的输入长度可能是 exponential。
目录
| 章节 | 说明 |
|---|---|
| Lecture 21:Dynamic Programming III | string / sequence states 与三类问题 |
| Subproblems for strings and sequences | suffix、prefix、substring |
| Parenthesization | matrix-chain multiplication 的 interval DP |
| Edit distance | insert、delete、replace |
| Longest common subsequence | edit distance 的关联 |
| 0/1 Knapsack | capacity state 与 item choice |
| Pseudopolynomial time | 输入编码长度与复杂度 |
| 小结 | 本讲要点 |
Lecture 21:Dynamic Programming III
本讲从上一讲的 text justification 和 Blackjack 继续推进 DP 设计。那些问题的输入是 words 或 cards;本讲转向更典型的 strings 和 sequences:
- 如何给多个 characters 或 matrices 定义合理的 interval state;
- 如何通过一次 split 或一次 edit 把问题拆成更小的 subproblems;
- 如何识别 algorithm 的 time 是 polynomial 还是 pseudopolynomial。
DP 的难点往往不是写 recurrence,而是选对 subproblem。state 太小无法表达剩余问题,state 太大则会产生不必要的高维 table。
Subproblems for strings and sequences
给定 sequence x,常见的 subproblem 有:
| 类型 | 表示 | 典型规模 |
|---|---|---|
| suffix | x[i:] | Θ(n) |
| prefix | x[:i] | Θ(n) |
| substring | x[i:j] | Θ(n²) |
如果问题同时涉及两个 sequences x、y,就可以组合这些 states。例如两个 suffixes x[i:]、y[j:] 形成 Θ(|x||y|) 个 states,仍然是 polynomial。
选择原则是:state 应保留未来 choices 所需要的信息,而不必保存已经处理过的全部历史。
Parenthesization
问题
给定一串 matrices:
A₁ A₂ ... Aₙ
matrix multiplication 满足 associativity:
(A₁A₂)A₃ = A₁(A₂A₃)
但不同的 parenthesization 可能产生极其不同的中间 matrix dimensions 和 multiplication cost。目标是找到 cost 最低的 parenthesization。
Subproblem
令 M[i,j] 表示计算矩阵链 Aᵢ...Aⱼ 的最小 multiplication cost。
- base case:M[i,i] = 0;
- 长度为 1 的 chain 不需要 multiplication;
- 更长的 chain 需要选择一个 split point k。
Guess split point
最后一次 multiplication 会把 chain 拆成:
(Aᵢ...Aₖ)(Aₖ₊₁...Aⱼ)
因此:
M[i,j]
= min over i ≤ k < j
{ M[i,k] + M[k+1,j] + cost of multiplying the two results }
如果矩阵 dimensions 为 p₀ × p₁、p₁ × p₂、...、pₙ₋₁ × pₙ,则最后一步的 scalar multiplication cost 是:
pᵢ₋₁ × pₖ × pⱼ
Evaluation order 与 complexity
M[i,j] 依赖更短的 intervals,因此按 chain length 从短到长填 table:
for length = 1 to n:
for i = 1 to n − length + 1:
j ← i + length − 1
try every split k in [i, j − 1]
共有 Θ(n²) 个 intervals;每个 interval 尝试 O(n) 个 split points:
time = Θ(n³)
space = Θ(n²)
保存每个 interval 的 best split k,就能用 parent pointers 恢复最优 parenthesization。
Edit distance
Edit distance 用于 DNA comparison、diff、version-control comparison、spellchecking 和 plagiarism detection 等场景。
给定两个 strings x、y,目标是找到把 x 变成 y 的最低成本 edit sequence。允许的 edits 包括:
- delete 一个 character;
- insert 一个 character;
- 把一个 character replace 成另一个 character。
每种 edit 的 cost 可以依赖字符。例如 DNA 中某些 mutation 比其他 mutation 更常见,可以设置更低 cost。
Subproblem
令 C(i,j) 表示把 suffix x[i:] 变成 suffix y[j:] 的最小 edit cost:
C(i,j) = edit-distance(x[i:], y[j:])
共有 Θ(|x||y|) 个 states。
base case:
C(|x|, |y|) = 0
如果 x 已经结束,只能 insert y 的剩余部分;如果 y 已经结束,只能 delete x 的剩余部分。
Guess 三种 edits
对当前 characters x[i]、y[j],最优序列的第一步可以是:
- delete x[i],进入 C(i+1,j);
- insert y[j],进入 C(i,j+1);
- replace x[i] by y[j],进入 C(i+1,j+1)。
recurrence 为:
C(i,j) = min(
delete-cost(x[i]) + C(i+1,j),
insert-cost(y[j]) + C(i,j+1),
replace-cost(x[i], y[j]) + C(i+1,j+1)
)
不存在的 choice 在边界处省略。
每个 state 只有 O(1) choices,每次工作为 Θ(1),所以:
time = Θ(|x||y|)
space = Θ(|x||y|)
如果只需要 edit distance value,可以只保留相邻两行,把 space 降到 Θ(min(|x|, |y|));如果要恢复具体 edits,就需要 parent pointers 或重新追踪 table。
Longest common subsequence
subsequence 保持原有顺序,但不要求 characters 连续。例如两个 sequence 可能共享 subsequence HELLO,即使中间插入了其他 characters。
当 insert 与 delete 的 cost 为 1,并把 matching characters 的 replace cost 设为 0 时,edit distance 与 longest common subsequence 有紧密关系:保留共同 subsequence,删除其余部分,再插入目标 sequence 中缺失部分。
LCS 的标准 state 可以写成 prefixes:
L[i,j] = x[:i] 与 y[:j] 的最长 common subsequence 长度
如果 x[i−1] = y[j−1]:
L[i,j] = L[i−1,j−1] + 1
否则:
L[i,j] = max(L[i−1,j], L[i,j−1])
这同样产生 Θ(|x||y|) 的 DP table。保存产生 max 的方向,就能恢复实际 subsequence。
0/1 Knapsack
问题
有 n 个 items。每个 item i 有:
- weight wᵢ;
- value vᵢ。
knapsack capacity 为 W。每个 item 最多选择一次,目标是最大化总 value,同时总 weight 不超过 W。
Subproblem
令 K(i,c) 表示只考虑 items i、i+1、...,capacity 为 c 时可得到的最大 value。
也可以使用 prefix 形式 K(i,c),关键是 state 必须记录:
- 当前考虑到哪个 item;
- 剩余 capacity 或已使用 capacity。
因此 states 数量为 Θ(nW)。
Guess 是否选择 item
对 item i,有两个 choices:
- 不选择 i:value 为 K(i+1,c);
- 选择 i(若 wᵢ ≤ c):value 为 vᵢ + K(i+1,c−wᵢ)。
recurrence:
K(i,c) =
max(
K(i+1,c),
vᵢ + K(i+1,c − wᵢ) if wᵢ ≤ c
)
base case 是没有 items 或 c = 0,此时 value 为 0。
Complexity
每个 state 只有两个 choices:
time = Θ(nW)
space = Θ(nW)
用 rolling rows 可以把 value table 的 space 降到 Θ(W)。如果要恢复选择了哪些 items,需要保存 parent choices,或在 table 上反向追踪。
为什么不能简单 greedy
按 value 最大、weight 最小或 value/weight ratio 最大来选,都不能保证 0/1 Knapsack 的最优解。一个看似很划算的 item 可能阻止两个组合起来更有价值的 items。
DP 通过“选或不选”的 exhaustive choices 覆盖所有组合,但利用相同的 suffix + capacity state 避免重复计算。
Pseudopolynomial time
Knapsack 的 Θ(nW) 是一个重要例子。若 W 是输入中的数值,algorithm 看起来是 polynomial;但计算机输入通常以 binary 表示 W,需要 log W bits。
因此:
- input length 中容量字段只占 Θ(log W);
- algorithm time 是 Θ(nW);
- W 可能是 2 的 log W 次方;
- 所以 Θ(nW) 对输入 bit length 可能是 exponential。
这样的复杂度称为 pseudopolynomial time:对数值大小 polynomial,但不一定对输入编码长度 polynomial。
这不是 DP recurrence 错误,而是 state space 把每一个 capacity 值 0,1,...,W 都显式列出来的结果。若问题要求严格 polynomial algorithm,需要进一步利用 approximation、不同的 state 定义,或证明问题不存在这样的算法。
三类 DP 的共同结构
| 问题 | State | Guess | 典型 complexity |
|---|---|---|---|
| Parenthesization | interval i,j | split k | Θ(n³) |
| Edit distance | suffix/prefix pair i,j | delete / insert / replace | Θ( |
| Knapsack | item index i + capacity c | take / skip | Θ(nW) |
它们都遵循:
- 定义有限 subproblems;
- 猜最优 solution 的一个局部 choice;
- 把 choice 后的部分转成更小 state;
- 通过 memoization 或 bottom-up table 复用结果;
- 用 parent pointers 恢复 actual solution。
小结
- strings/sequences 的 states 常见为 suffix、prefix 和 substring;多个 sequences 会产生乘积 state space。
- Parenthesization 是 interval DP,猜 split point,典型 complexity 为 Θ(n³)。
- Edit distance 通过 delete、insert、replace 三种 choices 得到 Θ(|x||y|) algorithm。
- LCS 可以视为 edit-based sequence comparison 的特殊关系。
- 0/1 Knapsack 用 item index 和 capacity 作为 state,得到 Θ(nW) 的 pseudopolynomial algorithm。
- DP 的 time analysis 必须同时考虑 subproblem 数量和每个 state 的 choices;数值参数还要考虑 input encoding length。
参考资料
导航
- 上一篇:Algorithms:Dynamic Programming II - Text Justification and Blackjack
- 系列索引:Algorithms-CLRS-MIT 6.006 系列索引
- 下一篇:Lecture 22《Dynamic Programming IV:Guitar Fingering, Tetris and Super Mario Bros.》
评论 (0)