Chapter 27 · Online Algorithms
按 CLRS 第四版第 27 章重建在线决策、竞争分析、等待电梯、搜索列表与在线缓存,并绑定模型契约、势能证明与 adversarial witness。
学习目标
- 能定义在线算法的 information boundary、合法动作、cost model、初始状态、offline comparator 与 adversary 合同,并写出竞争比不等式。
- 能用 threshold、move-to-front 与 k-distinct phase argument 分析等待电梯、维护搜索列表和在线缓存的最坏情况成本。
- 能回答:为什么把 future suffix、不同 cache capacity 或不同 adversary power 偷渡进比较,足以让一个看似正确的 online proof 失效?
先问:为什么“再等一秒”不能偷看未来?
先预测:你决定放弃等电梯并走楼梯时,电梯可能下一秒到达;也可能还要等很久。在线算法的难点不是不知道如何执行一次动作,而是动作发生时只能看到已经到达的 request prefix,不能读取 future suffix。一个可审计的结论必须把这个信息边界写进模型,而不是在证明中悄悄使用未来。
本章把四个场景放进同一套语言:online algorithms / 在线算法、waiting for an elevator / 等待电梯、maintaining a search list / 维护搜索列表、online caching / 在线缓存。先定义 online 与知道全序列的 offline optimum 如何比较,再分别使用 rent-or-buy threshold、list potential 和 k-distinct phases 建立上界与下界。
CLRS 4e · Chapter 27 · Online Algorithms
未来未知时,什么叫“做得足够好”?
切换 threshold、search-list potential、cache phases 与 information leak,沿时间线观察 online policy 如何面对 unknown future。
第 1 / 6 步 · 声明 request prefix、cost model 与 offline OPT
先固定 information arrival 与 cost model,再让每个 action 回指 threshold、potential 或 phase proof。
权威目录定位
本章对齐 CLRS 第四版第 27 章,官方概念包括:
- online algorithms / 在线算法;
- waiting for an elevator / 等待电梯;
- maintaining a search list / 维护搜索列表;
- online caching / 在线缓存。
正文是独立教学重构:先固定信息到达和成本合同,再用等待电梯演示阈值决策,用维护搜索列表演示势能记账,最后用缓存阶段与 个 page 构造上下界。组件中的四个 mode 也分别对应这些概念,便于把模型假设和证据逐项核对。
常见误区与故障注入
online algorithms / 在线算法
↡只能依据已经到达的 request prefix 作出动作、不能读取 future suffix 的算法模型把输入看作逐步揭示的 sequence σ。第 t 步到达 request 后,算法可以读取当前 prefix、自己的 state 和题目允许的 metadata,然后选择 legal action;它不能把尚未到达的 suffix 当作 feature。一个完整的模型至少要声明:request 何时 revealed、动作有哪些、每种动作如何计 cost、initial state 是什么、offline comparator 拥有什么 resource,以及 adversary 如何产生后续 request。
设 online algorithm 为 ,知道完整序列 σ 的 offline optimum 为 OPT。在 minimization problem 中,若存在与 sequence length 无关的常数 α 和 β,使每条合法 sequence 都满足
则称 A 为 α-competitive。OPT 是 performance benchmark,不要求可高效计算;β 可以吸收 initial state 或最后一个不完整 phase 的边界成本,但不能随输入规模增长。若随机化,还要说明比较的是 expected cost,以及 adversary 是在 random bits 之前固定 sequence,还是能根据观察到的动作适应性地产生后续输入。
↡用持续等待成本与一次切换成本的平衡点决定何时停止等待的确定性规则把未知 future 转换成可检查的 stopping rule;等待超过阈值后继续租用的代价,不能再被“也许下一刻就结束”掩盖。
↡online cost 相对于同一 sequence 上 offline optimum cost 的最坏倍数保证只在 comparator、cost model 和 adversary 合同固定时有意义。它不等同于 average latency,也不保证每个实例都接近 α 倍;它保证的是所有合法输入上的上界。
↡根据当前不可预知输入选择后续请求、用于构造最坏序列的对手模型规定 adversary 如何产生输入;它必须和 random bits 的揭示时序、comparator resource 与 initial state 一起写进定理。
waiting for an elevator / 等待电梯
Rent-or-buy 模型
把场景压缩成两个量:走楼梯固定成本为 ;电梯还需等待的时间为 ℓ,而 ℓ 在决策时未知。电梯到达后的共同乘坐时间可归一化掉,因此知道 ℓ 的 offline optimum 成本为
Always-stairs 在 ℓ 很小时支付 ,而 OPT 几乎立即乘梯;always-wait 在 ℓ 很大时持续支付等待成本,而 OPT 早已走楼梯。两个极端策略都不能给出与 无关的好界。
Threshold hedge
确定性策略先等待 个时间单位;若电梯在此之前到达就乘梯,否则立刻走楼梯:
WAIT-OR-WALK(stair_cost b)
1 wait until the elevator arrives or elapsed time reaches b
2 if the elevator arrives, take it
3 otherwise take the stairs immediately若 ℓ≤b,online 与 OPT 都等待 ℓ;若 ℓ>b,online 先等待 再支付 走楼梯,而 OPT 直接支付 。因此
重点不是电梯,而是 rent-or-buy 的阈值设计:当持续成本累积到一次切换成本时停止等待。云实例按小时租还是预留、idle machine 维持高功耗还是 sleep,都可以先问相同的 break-even threshold。
为什么阈值必须与 同量级?
设策略等待 后走楼梯。若 远小于 ,adversary 可让电梯在放弃后不久到达,online 支付 ,而 OPT 只需等待略多于 ;若 远大于 ,adversary 可让电梯迟迟不来,online 支付 ,而 OPT 支付 。平衡这两类损失会把 放在 附近;固定 threshold 后再挑最不利 arrival time,才是有效的 deterministic lower-bound witness。
maintaining a search list / 维护搜索列表
Access rank 与 move-to-front
保存 个 items 的 list。访问 item x 需要从 head 扫描到当前位置,cost 为 one-based rank;访问完成后是否可以免费移动元素,必须在模型中声明。如果未来 frequency distribution 已知,静态最优顺序会把高频 item 放在前面;在线状态下既不知道 frequency 是否稳定,也不知道下一次 request。
Move-to-front(MTF)在访问后立即把 x 移到 head:
MOVE-TO-FRONT(L, x)
1 scan L from the head until x is found
2 pay the current one-based rank of x
3 unlink x from its current position
4 insert x at the head of LMTF 让 burst 和 working set 中刚访问的 item 下一次成本为 1,但 alternating pattern 也会扰动它。因此不能只说“它响应 locality”,还要与知道完整 request sequence、可以提前重排 list 的 offline comparator 比较。
↡用 online list 与 offline comparator list 的相对次序差异记账、把重排状态转成未来收益的函数是分析 state difference 的工具,不是对未来访问的预测。它通常从 inversion count 构造:一对 items 在两个 list 中相对次序不同,就贡献一个 inversion。
Inversion potential 证明骨架
令 online MTF list 为 L,offline comparator list 为 L*,定义
访问 x 时,假设 x 在 MTF 中的 rank 为 ,在 OPT 中的 rank 为 。MTF 把 x 移到 front,会消除一批位于 x 前且在 OPT 中位于 x 后的 inversions,也可能新增另一批;共同位于 x 前面的 items 数量不超过 。因此,online 的实际 access cost 加上 potential change,可以由 OPT 本次 access 与它允许支付的 transpositions 的常数倍控制。
把每次 amortized inequality 在整个 sequence 上求和,potential terms telescoping,只剩初始与最终 potential 的边界项。在 free-move-after-access 模型下,MTF 相对允许同类重组的 comparator 有 constant competitive bound;在 access rank 计费、OPT 的额外 adjacent transposition 也计费的常见模型中,紧分析给出 2 倍界。若把 MTF 自身的每次 adjacent swap 也计费,cost model 改变,常数必须重新证明。
online caching / 在线缓存
Hits、faults 与 eviction
缓存容量为 ,每次请求一个 page。hit cost 归一化为 0;miss 或 page fault cost 为 1,cache 满时必须选择一个 page evict。offline furthest-in-future policy 知道未来,online replacement policy 只能看到 prefix。
LRU 逐出最久未被请求的 resident page,FIFO 按进入 cache 的时间逐出:
function accessLRU(cache: string[], page: string, capacity: number) {
const hit = cache.indexOf(page);
if (hit >= 0) cache.splice(hit, 1);
if (hit < 0 && cache.length === capacity) cache.shift();
cache.push(page); // left is least recent, right is most recent
return hit < 0;
}LRU 与 FIFO 对同一 trace 可能产生不同 faults,但都能用 phase argument 给出 coarse worst-case guarantee。LIFO 或 least-frequently-used 在合适 adversarial sequence 上可能长期保留坏 pages,不能只凭策略名称推出 competitive ratio。
↡把请求按 distinct page 数切分、用于聚合 cache fault 成本并比较 online 与 OPT 的阶段是证明里的输入分区,而不是 LRU 内部的状态。capacity、initial cache、prefetch 和 dirty-page policy 仍必须单独写清。
-distinct phase argument
把 request sequence 划成 maximal -phases:每个 phase 最多包含 个 distinct pages;下一次出现第 个 distinct page 时开始新 phase。phase 由 input 决定,不依赖 LRU 内部 state。
LRU 在一个 phase 中对每个 distinct page 至多 fault 一次,因为 phase 内再次访问会使它比上个 phase 残留但未访问的 page 更 recent。因此每个完整 phase 至多产生 faults。另一方面,相邻 phase 会引入上一 phase distinct set 之外的新 page;同容量为 的 offline algorithm 不可能在所有 phase transitions 上都零 fault。把相邻 phase 的 lower bound 适当 charge,可得到
同类 conservative 或 marking argument 也能给 FIFO 一个 -competitive upper bound。这个 bound 只说明最坏情况下两者处于同一 asymptotic class,不说明真实 locality workload 上两者平均表现相同。
Deterministic lower bound 与 resource augmentation
考虑 个 pages。对任何 deterministic online cache,adversary 总请求当前不在 cache 中的 page,使 online 每次 fault;offline 知道未来,可逐出未来最晚使用的 page,因此 fault frequency 低得多。于是同 capacity comparator 下,任何 deterministic online paging algorithm 都不能保证优于 量级的 competitive ratio。这个 lower bound 与 LRU 的 upper bound 相遇,显示瓶颈来自 information gap,而不只是 LRU 实现不够聪明。
若 online 有 个 slots 而 OPT 只有 个 slots,比较的问题已经不同;phase argument 会出现与 相关的 resource-augmentation 界。额外 metadata cache、prefetch buffer 或 victim cache 也属于 resource augmentation,工程 benchmark 不能把它们藏在“同一个 cache policy”里。随机化可以在 oblivious-adversary 模型下改善 bound,但必须另行定义 expected cost、random source 和 adversary power。
AUDIT-ONLINE-CACHE(trace, k)
1 replay exactly the same trace for policy and OPT baseline
2 record initial cache, capacity, prefetch, and write policy
3 separate hits, compulsory faults, replacement faults, and dirty costs
4 partition the trace into k-distinct phases
5 report both worst-case certificate and measured workload behavior从证明到工程决策
Competitive analysis 回答“没有 future oracle 时最坏会损失多少”,trace simulation 回答“这批 workload 上实际发生什么”。两者应同时存在:证明防止策略在罕见输入上失控,measurement 暴露 constant factors、cache hierarchy、prefetch 和 distribution shift。若报告只给 ratio,不给 trace 与 state contract,读者无法复现实验;若只给平均 miss count,也无法判断 adversarial witness 是否会击穿策略。
小结
- online algorithms / 在线算法先固定 information boundary、legal actions、cost model、offline comparator 与 adversary timing。
- waiting for an elevator / 等待电梯把继续等待和一次走楼梯成本平衡为 threshold,得到确定性 2-competitive guarantee。
- maintaining a search list / 维护搜索列表用 move-to-front 响应 recency,并以 online/OPT list 间的 inversion potential 记账。
- online caching / 在线缓存用 -distinct phases 给 LRU/FIFO 的 worst-case upper bound,再以 pages 构造 deterministic lower bound。
- 竞争比不是平均延迟;改变 transposition cost、cache capacity 或 adversary power 后,原结论不能直接复用。
练习
-
Threshold 证明题:走楼梯成本为 ,电梯等待时间为未知的 。证明“等待至多 ,否则走楼梯”的策略满足 ,并分别给出 adversary 在阈值前到达和阈值后到达时的两类成本。
-
Demo 修改题:把实验切换为
cache或list模式,任选其一补上一个可审计的证据。要求写出至少三项代码或状态改变,并说明它们如何对应 phase fault 或 inversion potential。 -
模型审计题:一个报告声称“LRU 比 OPT 只差两倍”,但 online cache 有 4 个 slots、OPT 有 3 个 slots,且 OPT 可以读取完整未来。列出至少三个必须修正或补充的合同,并说明修正后应如何命名结论。
Glossary
名词解释
本章出现的专业名词,用大白话再讲一遍。
- 在线算法
- 只能依据已经到达的 request prefix 作出动作、不能读取 future suffix 的算法模型。
- 阈值策略
- 用持续等待成本与一次切换成本的平衡点决定何时停止等待的确定性规则。
- 竞争比
- online cost 相对于同一 sequence 上 offline optimum cost 的最坏倍数保证。
- 势能
- 用 online list 与 offline comparator list 的相对次序差异记账、把重排状态转成未来收益的函数。
- 缓存阶段
- 把请求按 distinct page 数切分、用于聚合 cache fault 成本并比较 online 与 OPT 的阶段。
- 对手模型
- 根据当前不可预知输入选择后续请求、用于构造最坏序列的对手模型。