# 流式算法

流式算法在单遍中计算一个长数据项序列的函数，维护一个规模很小的摘要——理想情况下相对于输入为多对数——并对预先固定的问题返回近似答案，例如不同项的数量、给定项的频率或最重项。近似是必需的，而非可选的：任何对 \( k \neq 1 \) 精确计算频率矩 \( F_{k} \) 的随机算法都需要 \( \Omega(n) \) 内存比特，而对数空间足以近似 \( F_{0} \)、\( F_{1} \) 和 \( F_{2} \)。<sup>[1](https://web.math.princeton.edu/~nalon/PDFS/amsz4.pdf)</sup><sup> • </sup><sup>[2](https://resources.mpi-inf.mpg.de/departments/d1/teaching/ss14/gitcs/notes3.pdf)</sup> 存储的摘要称为草图，是一种具有初始化、更新和查询操作的数据结构，只保留其预定义查询所需的信息。<sup>[3](https://book.moa.cms.waikato.ac.nz/chapter_4.html/)</sup>

| 属性 | 值 |
|---|---|
| 目标 | 以至少 \( 1 - \delta \) 的概率对 \( f(S) \) 进行 \( (1 \pm \varepsilon) \)-近似，使用 \( O(\mathrm{polylog}\, n) \) 空间和快速的逐项更新 <sup>[2](https://resources.mpi-inf.mpg.de/departments/d1/teaching/ss14/gitcs/notes3.pdf)</sup> |
| 精确计算 | 对精确的 \( F_{k} \)，\( k \neq 1 \)，需要 \( \Omega(n) \) 比特 <sup>[1](https://web.math.princeton.edu/~nalon/PDFS/amsz4.pdf)</sup> |
| 频率矩 | \( F_{0} \)、\( F_{1} \)、\( F_{2} \) 可在对数空间中近似；\( k \geq 6 \) 的 \( F_{k} \) 需要 \( n^{\Omega(1)} \) 空间 <sup>[1](https://web.math.princeton.edu/~nalon/PDFS/amsz4.pdf)</sup> |
| Count-Min草图 | 宽度 \( w = e/\varepsilon \)，深度 \( d \)；点查询误差在 \( \varepsilon \| a \|_{1} \) 以内，概率为 \( 1 - \delta \) <sup>[4](https://doi.org/10.1016/j.jalgor.2003.12.001)</sup> |
| HyperLogLog | 标准误差 \( 1.04/\sqrt{m} \)；在1.5千字节内以2%精度处理超过 \( 10^{9} \) 的基数 <sup>[5](https://doi.org/10.46298/dmtcs.3545)</sup> |
| 不同元素计数 | 以2/3成功概率实现 \( (1 \pm \varepsilon) \) 近似的最优 \( O(\varepsilon^{-2} + \log n) \) 比特 <sup>[6](https://dl.acm.org/doi/10.1145/1807085.1807094)</sup> |
| \( F_{2} \) 估计 | 旋转门模型中的 \( O(\varepsilon^{-2} \log(1/\delta)) \) 空间 <sup>[7](https://dsl.cds.iisc.ac.in/~course/DBMS/papers/stream_book.pdf)</sup> |

## 如何工作

输入被建模为对一个维度为 \( n \) 的隐式向量 \( a \) 的更新流：一次更新 \( (i, \Delta) \) 将 \( a_{i} \leftarrow a_{i} + \Delta \)，其中 \( \Delta \in \{-M, \ldots, M\} \)。<sup>[8](https://arxiv.org/abs/1407.2151)</sup> 在收银机模型（仅插入）中，项以任意顺序到达，且只发生插入；在旋转门模型中，先前见过的项可以被删除。<sup>[9](https://dimacs.rutgers.edu/~graham/pubs/papers/sk.pdf)</sup><sup> • </sup><sup>[2](https://resources.mpi-inf.mpg.de/departments/d1/teaching/ss14/gitcs/notes3.pdf)</sup> 时间序列流强制有序，并且要求空间界为亚线性，理想情况下在 \( n \) 和不同项数量上为多对数。<sup>[10](https://dimacs.rutgers.edu/~graham/pubs/papers/cormodewdsa.pdf)</sup>

草图通过随机化和哈希获得其保证。长度约为 \( \log m \) 的Flajolet–Martin位图对每个项进行哈希，并记录第一个1比特的位置；位图中最低位的0表示不同项数量的对数。<sup>[11](https://www.cs.cmu.edu/~gibbons/Phillip%20B.%20Gibbons_files/Distinct-Values-Estimation-over-Data-Streams-PBGibbons.pdf)</sup><sup> • </sup><sup>[10](https://dimacs.rutgers.edu/~graham/pubs/papers/cormodewdsa.pdf)</sup> AMS（拔河）草图用4-路独立哈希 \( h \) 将项映射到 \( \{-1, +1\} \)，构造 \( Z = \sum_{i} f_{i} \cdot Y_{i} \)，并使用 \( Z^{2} \) 作为 \( F_{2} \) 的无偏估计量，因为 \( \mathrm{E}[Z^{2}] = F_{2} \) 且 \( \mathrm{Var}[Z^{2}] \leq 2F_{2}^{2} \)；4-路独立性足够，因为关于 \( Y_{i} \) 的多项式次数为4。<sup>[12](https://www.mit.edu/~vakilian/courses/lec_6_6104.pdf)</sup><sup> • </sup><sup>[7](https://dsl.cds.iisc.ac.in/~course/DBMS/papers/stream_book.pdf)</sup> Count-Min草图维护一个 \( d \times w \) 计数器数组，每次更新每行递增一个计数器；点查询计算各行上的最小值。<sup>[4](https://doi.org/10.1016/j.jalgor.2003.12.001)</sup> [HyperLogLog](https://www.edgepedia.org/zh/hyperloglog)存储 \( m \) 个寄存器，每个寄存器保存哈希值最左侧1比特的最大位置 \( M(j) \)，并基于这些寄存器返回估计值，其中 \( \alpha_{m} \) 校正系统偏差。<sup>[5](https://doi.org/10.46298/dmtcs.3545)</sup>

## 如何实现

实践者首先确定查询和更新模型，然后选择精度参数 \( \varepsilon \) 和 \( \delta \)，它们决定草图维度，因此规模只取决于精度，而不取决于全集大小。<sup>[4](https://doi.org/10.1016/j.jalgor.2003.12.001)</sup> 每个到达项通过哈希并更新 \( O(1) \) 或 \( O(\log(1/\delta)) \) 个计数器来处理；查询可在流中任意时刻回答。对于频繁项，若在时刻 \( t \) 有 \( \mathrm{count}_{t}(e) > \varepsilon \cdot t \)，则元素 \( e \) 是 \( \varepsilon \)-频繁项，因此最多有 \( 1/\varepsilon \) 个元素符合条件；Count-Min在长度为 \( a_{1} \) 的仅插入流中找到它们，使用 \( O((1/\varepsilon) \log(1/\delta)) \) 空间和每项 \( O(\log(1/\delta)) \) 时间。<sup>[13](https://www.cs.cmu.edu/~15451-f22/lectures/lec06-streaming.pdf)</sup><sup> • </sup><sup>[4](https://doi.org/10.1016/j.jalgor.2003.12.001)</sup> 大多数草图是可合并的：由两个流构建的草图可合并为一个能回答关于这两个流交错所得数据流的查询的草图，从而实现分布式计算。<sup>[3](https://book.moa.cms.waikato.ac.nz/chapter_4.html/)</sup>

## 起源

最早的非平凡流式算法可追溯到20世纪70年代末和80年代初，包括用于中位数和最频繁项的高效遍历算法。<sup>[9](https://dimacs.rutgers.edu/~graham/pubs/papers/sk.pdf)</sup> 一个更早的近似计数器仅用 \( O(\log \log m) \) 比特实现计数，用5比特就足以数到十亿。<sup>[1](https://web.math.princeton.edu/~nalon/PDFS/amsz4.pdf)</sup><sup> • </sup><sup>[14](https://www.cs.upc.edu/~gavalda/DataStreamSeminar/files/Lecture1.pdf)</sup> [菲利普·弗拉若莱](https://www.edgepedia.org/zh/philippe-flajolet)（Philippe Flajolet）和G·奈杰尔·马丁（G. Nigel Martin）于1985年发表在《计算机与系统科学杂志》上的概率计数论文开启了对不同元素计数的研究方向。<sup>[15](https://doi.org/10.1016/0022-0000%2885%2990041-8)</sup> 该领域本身由[诺加·阿隆](https://www.edgepedia.org/zh/noga-alon)（Noga Alon）及其同事1996年关于频率矩空间复杂度的工作开创。<sup>[1](https://web.math.princeton.edu/~nalon/PDFS/amsz4.pdf)</sup><sup> • </sup><sup>[16](https://arxiv.org/html/2411.02148v2)</sup> Count-Min草图由格雷厄姆·科莫德（Graham Cormode）和S·穆图克里希南（S. Muthukrishnan）于2004年在《算法杂志》上提出。<sup>[4](https://doi.org/10.1016/j.jalgor.2003.12.001)</sup> 随后，\( F_{0} \) 方向经历了首个通过协调采样的 \( (\varepsilon, \delta) \)-近似方案 <sup>[11](https://www.cs.cmu.edu/~gibbons/Phillip%20B.%20Gibbons_files/Distinct-Values-Estimation-over-Data-Streams-PBGibbons.pdf)</sup>、一种保留 \( O(1/\varepsilon^{2}) \) 个采样项并带有 \( \Theta(\log(1/\delta)) \) 个并行副本的算法 <sup>[2](https://resources.mpi-inf.mpg.de/departments/d1/teaching/ss14/gitcs/notes3.pdf)</sup>、玛丽安·杜兰（Marianne Durand）和菲利普·弗拉若莱于2003年提出的LogLog草图 <sup>[2](https://resources.mpi-inf.mpg.de/departments/d1/teaching/ss14/gitcs/notes3.pdf)</sup>、菲利普·弗拉若莱及其同事于2007年发表在《离散数学与理论计算机科学》上的HyperLogLog <sup>[5](https://doi.org/10.46298/dmtcs.3545)</sup>，以及一个最优的 \( O(\varepsilon^{-2} + \log n) \) 比特不同元素算法。<sup>[6](https://dl.acm.org/doi/10.1145/1807085.1807094)</sup>

## 变体

**HyperLogLog++** 由斯特凡·霍伊勒（Stefan Heule）和马克·农克瑟（Marc Nunkesser）在EDBT 2013上提出，并为谷歌的PowerDrill系统实现，增加了稀疏表示、经验偏差校正和64位哈希；在精度14下，稀疏表示将最多约12000的基数的平均误差降低为原来的1/4。<sup>[17](https://openproceedings.org/2013/conf/edbt/HeuleNH13.pdf)</sup> Count Sketch使用 \( O(1/\varepsilon^{2}) \) 内存并找到 \( F_{2} \)-频繁项，这是普通 \( F_{1} \)-频繁项的超集 <sup>[3](https://book.moa.cms.waikato.ac.nz/chapter_4.html/)</sup>；Count Sketch以 \( O(1/\varepsilon^{2}) \) 空间给出 \( \varepsilon \sqrt{F_{2}} \) 误差，而Count-Min以 \( O(1/\varepsilon) \) 空间给出 \( \varepsilon \cdot N \) 误差，且没有哪一种在所有频率向量上都优于另一种。<sup>[9](https://dimacs.rutgers.edu/~graham/pubs/papers/sk.pdf)</sup> 白盒对抗数据流模型由[米克洛什·奥伊陶伊](https://www.edgepedia.org/zh/miklos-ajtai)（Miklós Ajtai）及其同事于2022年提出，其中对手能看到包括随机性在内的完整内部状态。<sup>[18](https://doi.org/10.48550/arxiv.2204.09136)</sup>

## 应用

草图在生产网络监控中运行：Count-Min草图曾部署在AT&T的Gigascope数据流系统中，每秒处理200万到300万次更新，而不会显著加重资源负担。<sup>[7](https://dsl.cds.iisc.ac.in/~course/DBMS/papers/stream_book.pdf)</sup> HyperLogLog++支撑谷歌PowerDrill中的不同元素计数。<sup>[17](https://openproceedings.org/2013/conf/edbt/HeuleNH13.pdf)</sup> 对于图流，半流式模型允许 \( O(n \, \mathrm{polylog}\, n) \) 空间，因为大多数图问题在亚线性空间下难以处理；对于连通性，\( \Theta(n) \) 空间是必要且充分的，可通过维护生成森林实现。<sup>[19](https://sigmodrecord.org/publications/sigmodRecord/1403/pdfs/04.principles.mcgregor.pdf)</sup> 与采样相比，Count-Min对频繁项所需空间为采样所需空间的平方根量级，为 \( O(1/\varepsilon) \)，而样本量为 \( O(1/\varepsilon^{2}) \)。<sup>[9](https://dimacs.rutgers.edu/~graham/pubs/papers/sk.pdf)</sup>

## 局限与替代方案

**下界**标明了不可能之事。即使加性误差为 \( 0.1 F_{0} \)，近似 \( F_{0} \) 也需要 \( \Omega(\log n) \) 比特；任何具有10%相对误差的确定性算法都需要 \( \Omega(n) \) 比特。<sup>[1](https://web.math.princeton.edu/~nalon/PDFS/amsz4.pdf)</sup><sup> • </sup><sup>[11](https://www.cs.cmu.edu/~gibbons/Phillip%20B.%20Gibbons_files/Distinct-Values-Estimation-over-Data-Streams-PBGibbons.pdf)</sup> 对于 \( p > 2 \)，\( F_{p} \) 估计需要 \( \Omega(n^{1-2/p}) \) 空间，在多对数因子内是紧的。<sup>[16](https://arxiv.org/html/2411.02148v2)</sup><sup> • </sup><sup>[20](https://people.eecs.berkeley.edu/~minilek/publications/papers/fastlp.pdf)</sup>

**对抗流**是主要的失效模式。一般来说，线性草图无法做到对抗鲁棒 <sup>[21](https://link.springer.com/chapter/10.1007/978-3-030-84252-9_4)</sup>；有一个问题，其中每个对抗鲁棒算法至少需要 \( w \) 空间，而不感知算法只需要 \( O(\log^{2} w) \)，这是两个模型的首次一般性分离。<sup>[21](https://link.springer.com/chapter/10.1007/978-3-030-84252-9_4)</sup> 删除使实现鲁棒性困难得多：不同元素的翻转数在仅插入流中为 \( O((1/\alpha) \log m) \)，但在旋转门流中可大到 \( \Omega(m) \)。<sup>[22](https://link.springer.com/article/10.1007/s00453-024-01259-8)</sup> 鲁棒性框架包括草图切换、差分隐私、差分估计器以及两全其美方法 <sup>[23](https://simons.berkeley.edu/sites/default/files/2024-06/SA24-BC%20Slides_Omri%20Ben-Eliezer%20%281%29_0.pdf)</sup>；对于有界翻转数 \( \lambda \) 和扭转数 \( \mu \) 的旋转门流，一个对抗鲁棒的 \( F_{2} \) 算法使用 \( \tilde{O}(\sqrt{\alpha\lambda + \mu}/\alpha^{2}) \) 空间。<sup>[22](https://link.springer.com/article/10.1007/s00453-024-01259-8)</sup> 偏斜分布有帮助而不是有害：对于参数为 \( z \) 的齐夫频率，Count-Min只需要 \( O(\varepsilon^{-\min\{1, 1/z\}}) \) 空间 <sup>[7](https://dsl.cds.iisc.ac.in/~course/DBMS/papers/stream_book.pdf)</sup>；只要累积计数保持非负，即严格旋转门模型，Count-Min估计器就能处理删除，而保守更新变体则不能。<sup>[13](https://www.cs.cmu.edu/~15451-f22/lectures/lec06-streaming.pdf)</sup><sup> • </sup><sup>[24](https://ar5iv.labs.arxiv.org/html/2302.05245)</sup>

**近期结果**填补了若干空白。对于 \( (1 \pm \varepsilon) \) 近似 \( F_{2} \) 估计，\( \Omega(\log(n\varepsilon^{2})/\varepsilon^{2}) \) 比特的紧下界与AMS上界匹配。<sup>[16](https://arxiv.org/html/2411.02148v2)</sup> 一个开放问题是，不同元素或 \( \ell_{2} \) 的鲁棒流式处理需要 \( n \) 空间还是多对数空间，即使对伪确定性算法也未解决。<sup>[23](https://simons.berkeley.edu/sites/default/files/2024-06/SA24-BC%20Slides_Omri%20Ben-Eliezer%20%281%29_0.pdf)</sup>

## References

1. [The space complexity of approximating the frequency moments (Alon, Matias, Szegedy)](https://web.math.princeton.edu/~nalon/PDFS/amsz4.pdf)
2. [MPI-INF lecture notes: Streaming Algorithms (models, F0, Count-Min)](https://resources.mpi-inf.mpg.de/departments/d1/teaching/ss14/gitcs/notes3.pdf)
3. [Machine Learning for Data Streams, Chapter 4 (online edition)](https://book.moa.cms.waikato.ac.nz/chapter_4.html/)
4. [Graham Cormode, S. Muthukrishnan (2004). An improved data stream summary: the count-min sketch and its applications. Journal of Algorithms.](https://doi.org/10.1016/j.jalgor.2003.12.001)
5. [Philippe Flajolet and colleagues (2007). HyperLogLog: the analysis of a near-optimal cardinality estimation algorithm. Discrete Mathematics & Theoretical Computer Science.](https://doi.org/10.46298/dmtcs.3545)
6. [An optimal algorithm for the distinct elements problem (Kane, Nelson, Woodruff, PODS 2010)](https://dl.acm.org/doi/10.1145/1807085.1807094)
7. [Data Streams: Algorithms and Applications (Muthukrishnan survey)](https://dsl.cds.iisc.ac.in/~course/DBMS/papers/stream_book.pdf)
8. [Time lower bounds for nonadaptive turnstile streaming algorithms](https://arxiv.org/abs/1407.2151)
9. [Sketch techniques for massive data (Cormode, survey chapter)](https://dimacs.rutgers.edu/~graham/pubs/papers/sk.pdf)
10. [Fundamentals of Analyzing and Mining Data Streams (Cormode survey/tutorial)](https://dimacs.rutgers.edu/~graham/pubs/papers/cormodewdsa.pdf)
11. [Distinct Values Estimation over Data Streams (Gibbons, chapter/survey)](https://www.cs.cmu.edu/~gibbons/Phillip%20B.%20Gibbons_files/Distinct-Values-Estimation-over-Data-Streams-PBGibbons.pdf)
12. [Lecture 6: F₂ approximation, AMS sketching, Heavy Hitters (MIT course notes, 2025)](https://www.mit.edu/~vakilian/courses/lec_6_6104.pdf)
13. [CMU 15-451 Lecture 6: Streaming algorithms (heavy hitters, CountMin)](https://www.cs.cmu.edu/~15451-f22/lectures/lec06-streaming.pdf)
14. [Lecture 1: The data stream model (Gavaldà, UPC)](https://www.cs.upc.edu/~gavalda/DataStreamSeminar/files/Lecture1.pdf)
15. [Probabilistic counting algorithms for data base applications (Journal of Computer and System Sciences, 1985)](https://doi.org/10.1016/0022-0000%2885%2990041-8)
16. [Optimality of Frequency Moment Estimation (Braverman & Zamir)](https://arxiv.org/html/2411.02148v2)
17. [HyperLogLog in practice: algorithmic engineering of a state of the art cardinality estimation algorithm (Heule, Nunkesser, Hall, EDBT 2013)](https://openproceedings.org/2013/conf/edbt/HeuleNH13.pdf)
18. [Ajtai, Miklos and colleagues (2022). The White-Box Adversarial Data Stream Model. arXiv (Cornell University).](https://doi.org/10.48550/arxiv.2204.09136)
19. [Graph Stream Algorithms: A Survey (McGregor, SIGMOD Record 2014)](https://sigmodrecord.org/publications/sigmodRecord/1403/pdfs/04.principles.mcgregor.pdf)
20. [Fast frequency moment estimation / space-optimal F_p with fast update time (Nelson, Woodruff et al.)](https://people.eecs.berkeley.edu/~minilek/publications/papers/fastlp.pdf)
21. [Separating Adaptive Streaming from Oblivious Streaming Using the Bounded Storage Model (TQC 2021, Springer)](https://link.springer.com/chapter/10.1007/978-3-030-84252-9_4)
22. [A Framework for Adversarial Streaming Via Differential Privacy and Difference Estimators (Algorithmica)](https://link.springer.com/article/10.1007/s00453-024-01259-8)
23. [Adversarially Robust Streaming Algorithms (survey slides, Simons Institute, 2024)](https://simons.berkeley.edu/sites/default/files/2024-06/SA24-BC%20Slides_Omri%20Ben-Eliezer%20%281%29_0.pdf)
24. [ar5iv.labs.arxiv.org](https://ar5iv.labs.arxiv.org/html/2302.05245)

---
*Initially written Sep 29, 2026 · Reviewed: Sep 30, 2026 · Edited: Sep 30, 2026 · Last review: Sep 30, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 人工智能与数据 › Algorithms and computational methods

Copyright 2026 Edgepedia

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

Source: https://www.edgepedia.org/zh/streaming-algorithm

English original: https://www.edgepedia.org/streaming-algorithm
