# 记账法（计算机科学）

记账法，又称银行家法，是平摊分析中的一种技术：对数据结构上的每次操作收取一个虚构的平摊代价，收取额超出实际代价的部分作为信用存储在数据结构上，用以支付日后代价高昂的操作。它与聚合分析法、势能法并列为平摊分析的三种标准方法。<sup>[1](https://www.cs.tulane.edu/~carola/teaching/cmps6610/fall18/slides/Lecture-amortization.pdf)</sup>

| 关键事实 | 详情 |
|---|---|
| 核心规则 | 当平摊代价 \( \hat{c}_i \) 超过实际代价 \( c_i \) 时，差额作为信用存入数据结构；当 \( \hat{c}_i < c_i \) 时，则消耗已存储的信用。<sup>[2](https://www.cs.princeton.edu/~wayne/kleinberg-tardos/pdf/AmortizedAnalysis-2x2.pdf)</sup> |
| 信用不变式 | 数据结构中的信用总额永不为负，因此对任意 \( n \) 都有 \( \sum_{i=1}^{n} \hat{c}_i \ge \sum_{i=1}^{n} c_i \)。<sup>[2](https://www.cs.princeton.edu/~wayne/kleinberg-tardos/pdf/AmortizedAnalysis-2x2.pdf)</sup> |
| 保证 | 从信用为零的初始结构 \( D_0 \) 出发，任意 \( n \) 次操作序列的总实际代价至多等于各平摊代价之和。<sup>[2](https://www.cs.princeton.edu/~wayne/kleinberg-tardos/pdf/AmortizedAnalysis-2x2.pdf)</sup> |
| 二进制计数器结果 | 每次 INCREMENT 收取 2，则每次递增的平摊代价至多为 2，\( n \) 次递增的总代价为 \( O(n) \)。<sup>[3](https://tulasimohan.github.io/teaching/2026_daa/lectures/lec-20-amortized-analysis.html)</sup> |
| 动态表结果 | 向倍增表中每次插入收取 3，足以覆盖偶发的复制开销，使插入的平摊代价为 \( O(1) \)。<sup>[4](https://ocw.mit.edu/courses/6-046j-introduction-to-algorithms-sma-5503-fall-2005/0a02bc84e917b9aeddaf69c8dc151a6f_lec13.pdf)</sup> |
| 归属 | 平摊分析的银行家法与物理学家法之比喻归功于罗伯特·安德烈·塔扬（Robert Endre Tarjan），始见于他 1985 年的综述《平摊计算复杂性》（Amortized Computational Complexity）。<sup>[5](https://cs.uwaterloo.ca/~plragde/flaneries/FDS/Queues.html)</sup> |
| 已知局限 | 该方法对持久化数据结构不成立，因为信用可能通过旧版本被重复花掉；克里斯·冈崎（Chris Okasaki）的修正方案改用延迟计算块上的借项进行分析。<sup>[6](https://drops.dagstuhl.de/storage/00lipics/lipics-vol374-icalp2026/html/LIPIcs.ICALP.2026.185/LIPIcs.ICALP.2026.185.html)</sup> |

## 方法如何运作

这种分析把时间当作金钱：一单位工作量花费一单位信用，每次操作收取的平摊代价 \( \hat{c}_i \) 可以不同于其实际代价 \( c_i \)。<sup>[4](https://ocw.mit.edu/courses/6-046j-introduction-to-algorithms-sma-5503-fall-2005/0a02bc84e917b9aeddaf69c8dc151a6f_lec13.pdf)</sup> 当某次操作被多收费时，盈余作为信用存入数据结构中的特定对象；当某次操作被少收费时，则用已存储的信用支付。<sup>[2](https://www.cs.princeton.edu/~wayne/kleinberg-tardos/pdf/AmortizedAnalysis-2x2.pdf)</sup> 初始数据结构 \( D_0 \) 的信用为零。<sup>[2](https://www.cs.princeton.edu/~wayne/kleinberg-tardos/pdf/AmortizedAnalysis-2x2.pdf)</sup>

使这种收费安排成立的要求是信用不变式：在任意可能的操作序列中的每一点，数据结构持有的信用总额都必须非负。在此不变式下，银行余额永不降至零以下，因此对任意 \( n \) 次操作的序列，平摊代价之和都是真实代价之和的上界。<sup>[4](https://ocw.mit.edu/courses/6-046j-introduction-to-algorithms-sma-5503-fall-2005/0a02bc84e917b9aeddaf69c8dc151a6f_lec13.pdf)</sup> 若对某个序列不变式不成立，则平摊代价可能无法界定该序列的实际代价。<sup>[7](https://algoparc.ics.hawaii.edu/~nodari/teaching/s15/Notes/Topic-15.html)</sup>

在实践中，分析者遵循一套固定流程：陈述实际代价，为每种操作选定收费额，说明信用存放于何处，陈述并证明信用不变式，证明每次操作都付得起，然后计算平摊复杂度。<sup>[8](https://www.cs.toronto.edu/~tfowler/csc263/Lecture6.pdf)</sup> 收费额通常先猜测再验证；一次操作的平摊代价是一种记账产物，不必与该操作自身的实际代价相似。<sup>[9](https://www.cise.ufl.edu/~sahni/dsaac/enrich/c2/amortize.htm)</sup>

## 与其他平摊方法的关系

平摊分析界定的是一个操作序列的最坏情况总代价，而非单次操作的最坏情况，且不假定输入上的任何概率分布。<sup>[10](https://www.cs.cornell.edu/courses/cs312/2007fa/lectures/lec20.html)</sup> 标准方法有三种。聚合分析法先界定整个序列的总代价再除以操作次数；它简单，但对每次操作赋予相同的平摊代价，因而缺乏另外两种方法的精确性。<sup>[1](https://www.cs.tulane.edu/~carola/teaching/cmps6610/fall18/slides/Lecture-amortization.pdf)</sup> 记账法则为每种操作类型指定具体的平摊代价，然后检验信用余额从不为负。<sup>[11](https://people.engr.tamu.edu/andreas-klappenecker/csce411-s19/csce411-amortized1.pdf)</sup>

势能法是记账法最亲近的亲属：它把同样的盈余不存为个别对象上的信用，而是存为定义在整个数据结构上的势函数 \( \Phi \)，平摊代价为 \( \hat{c}_i = c_i + \Phi(D_i) - \Phi(D_{i-1}) \)。<sup>[12](https://www.cis.temple.edu/~pwang/9615-AA/Lecture/08-Amortized.htm)</sup> 在顺序执行的场景下两者等价；把所有信用集中到单一位置，即可将物理学家法视为银行家法的特例。<sup>[6](https://drops.dagstuhl.de/storage/00lipics/lipics-vol374-icalp2026/html/LIPIcs.ICALP.2026.185/LIPIcs.ICALP.2026.185.html)</sup> 萨尼（Sahni）的教学笔记称记账法直观，因为只需验证总收费覆盖总实际代价，并指出它常能给出紧确的界；而势能法往往最难使用，因为合适的势函数难以找到。<sup>[9](https://www.cise.ufl.edu/~sahni/dsaac/enrich/c2/amortize.htm)</sup>

## 标准示例

**二进制计数器。** 对二进制计数器做递增，会把一串末尾的 1 位翻转为 0、把一个 0 位翻转为 1，因此在 \( k \) 位计数器上，单次递增最多可花费 \( k \) 次位翻转。记账方案对每次 INCREMENT 收取 2 个信用：一个信用支付把某位从 0 翻转为 1，第二个信用存储在该位上；把某位从 1 翻转为 0 则用存储在该位上的信用支付。由于每次递增至多把一个位从 0 翻转为 1，每次递增的平摊代价至多为 2，\( n \) 次递增的总代价为 \( O(n) \)。<sup>[3](https://tulasimohan.github.io/teaching/2026_daa/lectures/lec-20-amortized-analysis.html)</sup> 等价地说，每个 1 位持有一个信用，每个 0 位不持有信用。<sup>[2](https://www.cs.princeton.edu/~wayne/kleinberg-tardos/pdf/AmortizedAnalysis-2x2.pdf)</sup>

**带多重弹出操作的栈。** 对于支持 PUSH、POP 和 MULTIPOP 的栈，每次 PUSH 收取 2：一单位支付压栈，另一单位作为信用分配给被压入的元素。POP 和 MULTIPOP 不收费，因为移除每个元素的代价由该元素存储的信用支付。栈中的信用总额永不为负，因此 \( m \) 次操作的总收费（至多 \( 2m \)）界定了总代价，使每次操作的平摊代价为 \( O(1) \)。<sup>[8](https://www.cs.toronto.edu/~tfowler/csc263/Lecture6.pdf)</sup>

**动态表。** 对于满时容量翻倍的表，记账分析对每次插入收取 3 的平摊代价：\$1 支付即时插入，\$2 存储起来备日后表翻倍之用。表翻倍时，\$1 支付移动一个新近元素，\$1 支付移动一个旧元素，因此银行余额永不降至零以下，\( n \) 次插入的最坏情况代价为 \( O(n) \)。<sup>[4](https://ocw.mit.edu/courses/6-046j-introduction-to-algorithms-sma-5503-fall-2005/0a02bc84e917b9aeddaf69c8dc151a6f_lec13.pdf)</sup>

## 持久化与自动化

信用比喻假定数据结构是顺序使用的。在持久化场景中，任何先前的版本都可以再次被操作，对旧版本的一次操作可能花掉另一版本的不变式所依赖的信用，因此传统的银行家法不适用于持久化。<sup>[6](https://drops.dagstuhl.de/storage/00lipics/lipics-vol374-icalp2026/html/LIPIcs.ICALP.2026.185/LIPIcs.ICALP.2026.185.html)</sup> 克里斯·冈崎在《纯函数式数据结构》（Purely Functional Data Structures）中对此作了处理：用延迟计算块扩展数据结构，并改用借项而非信用进行分析。<sup>[6](https://drops.dagstuhl.de/storage/00lipics/lipics-vol374-icalp2026/html/LIPIcs.ICALP.2026.185/LIPIcs.ICALP.2026.185.html)</sup> 相关的形式化工作把记账法编码进一种带[仿射类型系统](https://www.edgepedia.org/zh/affine-type-system)的中间语言，确保信用不会被花费一次以上。<sup>[13](https://arxiv.org/abs/2006.15036)</sup>

为未来工作预付费用这一基本思想也驱动着自动化工具。自动平摊资源分析（Automatic Amortized Resource Analysis，AARA）把基于势能的平摊分析内化到程序的类型系统中，自动推断多项式资源界；其类型编码了存储在程序变量中的势能，约束求解则通过线性规划完成。<sup>[14](https://www.csd.cmu.edu/sites/default/files/phd-thesis/CMU-CS-25-122.pdf)</sup> 这一系列工作源自罗伯特·安德烈·塔扬所综述的手工平摊分析中的势能法。塔扬是普林斯顿大学的计算机科学家，其 1985 年的论文《平摊计算复杂性》确立了平摊运行时间作为数据结构的标准复杂度度量。<sup>[15](https://epubs.siam.org/doi/10.1137/0606031)</sup>

## References

1. [Lecture slides, Amortized Analysis, CMPS 6610, Tulane University](https://www.cs.tulane.edu/~carola/teaching/cmps6610/fall18/slides/Lecture-amortization.pdf)
2. [Amortized Analysis slides (accounting method, banker's method), Kevin Wayne, Princeton University](https://www.cs.princeton.edu/~wayne/kleinberg-tardos/pdf/AmortizedAnalysis-2x2.pdf)
3. [Lecture 20: Amortized Analysis, Tulasimohan Molli](https://tulasimohan.github.io/teaching/2026_daa/lectures/lec-20-amortized-analysis.html)
4. [Amortized Analysis, MIT OCW 6.046J Lecture 13 (Erik Demaine and Charles Leiserson)](https://ocw.mit.edu/courses/6-046j-introduction-to-algorithms-sma-5503-fall-2005/0a02bc84e917b9aeddaf69c8dc151a6f_lec13.pdf)
5. [Queues, Functional Data Structures notes, Prabhakar Ragde, University of Waterloo](https://cs.uwaterloo.ca/~plragde/flaneries/FDS/Queues.html)
6. [Persistent Amortised Analysis, Operationally, LIPIcs ICALP 2026](https://drops.dagstuhl.de/storage/00lipics/lipics-vol374-icalp2026/html/LIPIcs.ICALP.2026.185/LIPIcs.ICALP.2026.185.html)
7. [ICS 311 #15: Amortized Analysis, University of Hawaii](https://algoparc.ics.hawaii.edu/~nodari/teaching/s15/Notes/Topic-15.html)
8. [CSC 263 Lecture 6, University of Toronto](https://www.cs.toronto.edu/~tfowler/csc263/Lecture6.pdf)
9. [Amortized Complexity enrichment notes, Sartaj Sahni, University of Florida](https://www.cise.ufl.edu/~sahni/dsaac/enrich/c2/amortize.htm)
10. [Lecture 20: Amortized Algorithms, Cornell CS312](https://www.cs.cornell.edu/courses/cs312/2007fa/lectures/lec20.html)
11. [Amortized Analysis lecture notes, Andreas Klappenecker, Texas A&M University](https://people.engr.tamu.edu/andreas-klappenecker/csce411-s19/csce411-amortized1.pdf)
12. [Amortized Analysis lecture notes, CIS 9615, Temple University](https://www.cis.temple.edu/~pwang/9615-AA/Lecture/08-Amortized.htm)
13. [Denotational recurrence extraction for amortized analysis, ACM PACMPL / arXiv](https://arxiv.org/abs/2006.15036)
14. [CMU PhD Thesis CMU-CS-25-122: Automatic Amortized Resource Analysis](https://www.csd.cmu.edu/sites/default/files/phd-thesis/CMU-CS-25-122.pdf)
15. [Robert Endre Tarjan (1985). Amortized Computational Complexity. SIAM Journal on Algebraic and Discrete Methods 6(2): 306–318.](https://epubs.siam.org/doi/10.1137/0606031)

---
*Initially written Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 人工智能与数据 › Algorithms and computational methods › Data structures › Analysis of data structure operations

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/accounting-method-computer-science

English original: https://www.edgepedia.org/accounting-method-computer-science
