Dynamic Programming II:Text Justification、Blackjack 与 Parent Pointers
Algorithms:Dynamic Programming II:Text Justification and Blackjack
本文按 MIT 6.006 Fall 2011 Lecture 20《Dynamic Programming II:Text Justification, Blackjack》的顺序整理。重点是 dynamic programming 的五步设计法、text justification、perfect-information Blackjack,以及如何用 parent pointers 从最优值恢复实际 solution。
TL;DR
- 五步法:define subproblems、guess a part of the solution、relate subproblem solutions、recurse + memoize 或 bottom-up、solve the original problem。
- Text justification:对每个 suffix words[i:] 求最小 badness;猜第一行结束位置 j,得到 Θ(n²) algorithm。
- Badness:一行过宽时为 ∞,否则可用剩余空白的三次方衡量视觉不均匀程度。
- Perfect-information Blackjack:已知整副牌顺序时,把“已经使用到第 i 张牌”作为 state,猜 hit 次数,目标是最大化未来收益。
- Parent pointers:不要只保存 min/max value,还保存产生它的 argmin/argmax choice,最后沿 pointer chain 恢复完整 solution。
- DAG view:每个 subproblem 是 DAG 中的 vertex,dependency 是 directed edge;DP 就是在这个隐式 DAG 上做最优路径或最优值计算。
目录
| 章节 | 说明 |
|---|---|
| Lecture 20:Dynamic Programming II | 本讲的五步设计法 |
| Five easy steps | 从问题到 DP algorithm |
| Text justification | 最小化排版 badness |
| Text justification 的 recurrence | suffix state、guess 和 Θ(n²) |
| Perfect-information Blackjack | 已知牌序下的最优策略 |
| Blackjack 的 recurrence 与 complexity | hit choices 和 Θ(n³) |
| Parent pointers | 从最优值恢复实际 solution |
| 小结 | 本讲要点 |
Lecture 20:Dynamic Programming II
上一讲从 Fibonacci 和 shortest paths 介绍了 memoization、subproblems、guessing 与 bottom-up。本讲把同一套方法应用于两个更具体的问题:
- 如何把一段文字分成视觉上更好的 lines;
- 在完全知道 deck order 的 Blackjack 游戏中,如何选择 hit 或 stand 以最大化收益。
这两个例子共同强调一个实用事实:
DP 不只计算最优 cost;如果要输出实际 solution,就必须保存产生最优值的 choice。
Five easy steps
MIT 6.006 将 DP 设计整理成五步:
1. Define subproblems
把原问题拆成数量合理的 subproblems,并明确每个 state 表示什么。先数清楚 subproblems 的数量。
2. Guess a part of the solution
对每个 subproblem 枚举一个 solution choice,例如第一行结束在哪里、下一次 hit 几张牌、最后一条 edge 是什么。
3. Relate subproblem solutions
写出 recurrence:当前 subproblem 的最优值如何由更小的 subproblems 组合出来。计算每个 subproblem 需要尝试多少 choices。
4. Recurse + memoize,或 build bottom-up table
top-down 版本用 recursion 加 memoization。bottom-up 版本则检查 dependencies 的 DAG,并按 topological order 填表。
常见 time estimate 是:
time = number of subproblems × time per subproblem
因为每个 subproblem 第一次求解后会被 cache,后续 recursive calls 可以视作常数级查表。
5. Solve the original problem
原问题可能正好是某个 subproblem,例如 Text Justification 的 DP[0];也可能需要把多个 subproblem 的结果组合起来,并计算额外的 reconstruction cost。
Text justification
给定一串 words 和固定的 page width,需要把 words 分成多行,使整体排版 badness 最小。
一个简单的 greedy 方法是:第一行尽可能多放 words,直到再放一个就超宽,然后对剩余 words 重复。但这种方法可能在后面产生很差的行:
- 一行几乎填满,视觉上较好;
- 下一行可能只剩一个很短的 word,或者不得不留下很大的空白;
- 局部最满不代表全局总 badness 最小。
Line badness
定义 words[i:j] 放在同一行时的 badness:
- 如果 words[i:j] 的总长度超过 page width,则 badness(i,j) = ∞;
- 否则可以令 badness(i,j) 等于剩余空白的三次方。
三次方会强烈惩罚空白很多的 line,从而鼓励较均匀的排版。具体 penalty function 可以变化,但 DP 结构不变。
目标是:
最小化所有 lines 的 badness 之和
Text justification 的 recurrence
Subproblem
令 DP[i] 表示从第 i 个 word 开始的 suffix words[i:] 的最小总 badness:
DP[i] = words[i:] 的最小排版 badness
共有 Θ(n) 个 subproblems,其中 n 是 words 数量。
空 suffix 的 base case 是:
DP[n] = 0
因为没有 words 时不需要任何 line,也没有 badness。
Guess
对 DP[i],猜第一行结束位置 j。第一行包含 words[i:j],剩余部分从 j 开始,因此:
DP[i]
= min over j in {i+1, ..., n}
{ badness(i,j) + DP[j] }
如果 words[i:j] 超出 page width,该 choice 的 badness 为 ∞,自然不会成为最优选择。
Evaluation order
DP[i] 依赖 DP[j],其中 j > i。因此要从后往前计算:
for i = n, n − 1, ..., 0:
compute DP[i]
每个 i 最多尝试 O(n) 个 j,subproblems 数量为 Θ(n),所以:
time = Θ(n²)
space = Θ(n)
如果 badness(i,j) 每次都从头计算,可能额外引入 word-length summation 成本。可以用 prefix sums 在 O(1) 时间得到任意 words[i:j] 的总长度,从而维持 Θ(n²) 总复杂度。
Text justification 的 pseudocode
TEXT-JUSTIFY(words, width):
n ← length(words)
DP[n] ← 0
for i = n − 1 down to 0:
DP[i] ← ∞
for j = i + 1 to n:
cost ← BADNESS(words[i:j], width) + DP[j]
if cost < DP[i]:
DP[i] ← cost
return DP[0]
这段代码只返回最小 badness。要恢复每一行的实际 words 范围,需要额外保存使 cost 最小的 j。
Perfect-information Blackjack
第二个例子是一个 simplified Blackjack:
- 给定整副 deck 的顺序 c₀, c₁, ..., cₙ₋₁;
- player 与一个 stand-on-17 dealer 对局;
- player 可以选择 hit 或 stand;
- 每局 bet 固定为 1;
- 目标是最大化总 winnings;
- 因为未来 cards 已知,player 拥有 perfect information。
真实 Blackjack 中通常不知道未来牌,因此这个模型重点不是赌场策略,而是 DP state 和 guessing 的结构。
一个反直觉点是:在某些设定下,player 可能愿意接受当前一局的 loss,以便让后续 hand 获得更有利的 cards。这说明局部赢牌不一定等于全局最优策略。
Blackjack 的 recurrence 与 complexity
Subproblem
令 BJ[i] 表示已经使用到第 i 张 card 后,从剩余 deck 开始的最大期望或确定性收益:
BJ[i] = cards[i:] 上的最优 play value
共有 Θ(n) 个 subproblems。
当剩余 cards 少于一局所需的最小数量时,可以设置 base case:
BJ[i] = 0
Guess
对 BJ[i],猜 player hit 的次数,也就是当前 hand 消耗多少 cards。每个合法的 hit count 都对应一种可能 play。
对每个 guess:
- 取出 player 的 cards;
- 检查是否 bust;
- 按 stand-on-17 规则取 dealer cards;
- 计算这一局的 outcome:+1、0 或 −1;
- 加上剩余 deck 对应的 BJ[next].
形式上:
BJ[i] = max over valid hit counts h
{ outcome(i,h) + BJ[next(i,h)] }
Complexity
subproblems 数量是 Θ(n)。每个 state 可能尝试 O(n) 个 hit counts;如果计算 player 和 dealer totals 也需要 O(n),那么每个 subproblem 的工作可能达到 Θ(n²),总体可达:
time = Θ(n³)
space = Θ(n)
若用 prefix sums 或其他累计结构快速计算 card totals,可以降低每个 choice 的常数甚至降低一部分内层开销;但本讲重点是 state、guess 和 recurrence,而不是实现层面的所有优化。
Parent pointers
只保存 DP value 只能回答“最优值是多少”,不能回答“具体 solution 是什么”。
保存 argmin 或 argmax
Text Justification 中,对每个 i 保存使 recurrence 最小的 line ending j:
DP[i] = (best_cost, parent[i])
parent[i] = argmin j
计算完 DP[0] 后,从 i = 0 开始沿 parent pointers 跳转:
i ← 0
while i ≠ n:
输出 words[i:parent[i]]
i ← parent[i]
这样就能恢复每一行的边界。
为什么 reconstruction 是线性的
每次沿 pointer 都会把 i 向右推进,因此每个 word 最多被访问一次。输出实际 lines 的额外时间通常是 Θ(n),不改变主 algorithm 的 Θ(n²) time。
通用模式
对于 min problem 保存 argmin,对于 max problem 保存 argmax:
| DP 类型 | 保存的 value | 保存的 pointer |
|---|---|---|
| min | 最小 cost | 产生最小 cost 的 choice |
| max | 最大 reward | 产生最大 reward 的 choice |
| decision | true/false | 使状态成立的 choice |
Parent pointers 可以在 memoization 和 bottom-up 两种实现中自动加入;它们是从“最优值 algorithm”转为“实际 solution algorithm”的标准步骤。
DAG view
每个 subproblem 可以看成一个 DAG vertex。若 state A 的 recurrence 依赖 state B,就有 dependency edge A → B。
Text Justification 的 state graph 是:
- vertices:word positions i;
- edge:从 i 选择 j 作为第一行结束位置;
- edge weight:badness(i,j);
- 从 0 到 n 的最短 path:整个 text 的最小总 badness。
因为每条 dependency 都从较小的 word position 走向较大的 position,graph 不会有 cycle。DP table 的 backward loop 正是在这个 DAG 上使用一个合法 evaluation order。
Blackjack 也有相同结构:
- vertices:已经使用到第 i 张 card 的 states;
- edges:不同的 hit choices;
- edge value:当前 hand 的 outcome;
- 最优 strategy:从初始 state 出发的 maximum-weight path。
这就是“DP ≈ shortest paths in some DAG”的具体含义。
小结
- DP 的五步法是 define subproblems、guess、relate、memoize/bottom-up、solve original problem。
- Text Justification 把 suffix words[i:] 作为 state,猜第一行结束位置,得到 Θ(n²) algorithm。
- Perfect-information Blackjack 把已使用的 card position 作为 state,猜 hit 次数,求未来收益的 maximum。
- DP value 与实际 solution 是两件事;要恢复 solution,需要保存 parent pointers。
- Text Justification 的 parent pointer 记录每个 suffix 的最佳下一行结束位置,沿 pointer chain 即可恢复完整排版。
- DP state 和 dependency 可以表示成 DAG;table order 就是 dependency graph 的 topological order。
参考资料
导航
- 上一篇:Algorithms:Dynamic Programming I - Fibonacci and Shortest Paths
- 系列索引:Algorithms-CLRS-MIT 6.006 系列索引
- 下一篇:Lecture 21《Dynamic Programming III:Parenthesization, Edit Distance and Knapsack》
评论 (0)