# 计数–最小草图

计数–最小草图（CM 草图）是一种亚线性空间的数据结构，用于对数据流进行摘要，由[华威大学](https://www.edgepedia.org/zh/university-of-warwick)计算机科学家格雷厄姆·科莫德（Graham Cormode）与罗格斯大学的 S. 穆图克里希南（S. Muthukrishnan）在 2005 年《算法杂志》的论文中提出。它用固定的计数器网格和若干哈希函数，回答针对流中任意条目的近似频率查询——“这个条目出现了多少次？”——而无需存储条目本身。其估计值有时偏高，但从不会偏低，高估的幅度由构造时选定的参数所限定。<sup>[1](https://www.researchwithrutgers.org/en/publications/an-improved-data-stream-summary-the-count-min-sketch-and-its-appl-2/)</sup><sup> • </sup><sup>[2](https://quantmemo.com/concepts/count-min-sketch)</sup>

| 关键事实 | 详情 |
|---|---|
| 起源 | 科莫德与穆图克里希南，《一种改进的数据流摘要：计数–最小草图及其应用》，《算法杂志》55(1)，58–75，2005 年 4 月<sup>[1](https://www.researchwithrutgers.org/en/publications/an-improved-data-stream-summary-the-count-min-sketch-and-its-appl-2/)</sup> |
| 结构 | 一个 \( d \times w \) 的计数器网格，配 \( d \) 个独立哈希函数；更新时每行给一个单元格加值，查询时返回各行中的最小值<sup>[3](https://pipecode.ai/blogs/sketches-at-scale-count-min-tdigest-approximate-quantiles)</sup> |
| 误差保证 | 估计值从不会低于真实计数；它超出真实计数 \( \varepsilon \cdot N \)（其中 \( N \) 为流的总长度）以上的概率至多为 \( \delta \)<sup>[4](https://unseel.com/cs/count-min-sketch)</sup> |
| 标准尺寸 | \( d = \lceil \ln(1/\delta) \rceil \) 行，\( w = \lceil e/\varepsilon \rceil \) 列<sup>[4](https://unseel.com/cs/count-min-sketch)</sup> |
| 开销 | 每次插入和每次查询耗时 \( O(d) \)；无论出现多少不同条目，内存都固定为 \( d \times w \) 个计数器<sup>[5](https://www.abstractalgorithms.dev/count-min-sketch-explained)</sup> |
| 实际配置 | 实现中通常取 \( d \) 在 4 到 8 之间，再根据内存预算设定 \( w \)<sup>[6](https://tamnd.github.io/brain/programming/algorithms/05/05.13/)</sup> |
| 关键性质 | 可按单元格相加跨分片合并；不存储键，因此无法枚举条目<sup>[4](https://unseel.com/cs/count-min-sketch)</sup> |

## 工作原理

草图是一个 \( d \) 行 \( w \) 列的二维计数器数组，配 \( d \) 个独立哈希函数，每行一个。要给某个键增加权重，结构会在每一行对该键做一次哈希，并递增 \( d \) 行中各自选中的计数器。要估计某个键的频率，就读取该键哈希到的 \( d \) 个计数器，返回其中的最小值。<sup>[3](https://pipecode.ai/blogs/sketches-at-scale-count-min-tdigest-approximate-quantiles)</sup>

**为什么取最小值。** 每一行都是对该条目计数的一次完整、独立的估计，而且每一行的结果要么恰好正确，要么偏高，绝不会偏低。哈希碰撞——即其他条目哈希到同一单元格——只会在你的计数上叠加别人的计数；没有任何机制会做减法。因此，\( d \) 个读数中最小的那个，就是碰巧碰撞最少的那一行，也是最接近真相的。取平均则会把每一行的污染都揉进答案，而不是将其丢弃。这也解释了为什么该结构永远不会低估：\( d \) 个数每个都至少等于真实计数，它们的最小值自然也不会低于真实计数。<sup>[2](https://quantmemo.com/concepts/count-min-sketch)</sup><sup> • </sup><sup>[7](https://andifathulms.github.io/sketch-lab/en/countmin/)</sup>

误差保证假定哈希函数是两两独立的：知道某元素在第 \( i \) 行哈希到第 \( c \) 列，并不能告诉你它在第 \( j \) 行会哈希到哪里。<sup>[5](https://www.abstractalgorithms.dev/count-min-sketch-explained)</sup> 实现中通常使用快速的非密码学哈希，如 MurmurHash3，选择它是因为速度和分布质量而非安全性，并在使用前验证其分布特性。<sup>[7](https://andifathulms.github.io/sketch-lab/en/countmin/)</sup>

## 误差行为

科莫德与穆图克里希南的分析以两个用户选定的参数给出保证。取 \( d = \lceil \ln(1/\delta) \rceil \) 行、\( w = \lceil e/\varepsilon \rceil \) 列时，估计值超出真实计数 \( \varepsilon \cdot N \) 以上（其中 \( N \) 为流的总长度）的概率至多为 \( \delta \)。深度换来置信度，宽度换来精度。<sup>[4](https://unseel.com/cs/count-min-sketch)</sup><sup> • </sup><sup>[7](https://andifathulms.github.io/sketch-lab/en/countmin/)</sup>

误差是相对于整个流的加性误差，并不与条目自身的计数成比例。长流中的稀有条目可能被高估到远超其真实出现次数：一个计数为 1 的元素，可能因与高频条目碰撞而被膨胀到 100；而一个计数上百万的元素，看到的相对误差却很小。这使得该草图非常适合高频键，而不太适合稀有键。<sup>[7](https://andifathulms.github.io/sketch-lab/en/countmin/)</sup><sup> • </sup><sup>[5](https://www.abstractalgorithms.dev/count-min-sketch-explained)</sup>

在一次演示运行中，对包含 50,000 个事件、1,980 个不同条目的齐夫分布流，所有高频项的估计值都不低于真实计数，高频项中最大的高估为 7，且没有任何估计值超过 \( \varepsilon \cdot N \) 的界限 50。<sup>[7](https://andifathulms.github.io/sketch-lab/en/countmin/)</sup>

## 高频项

高频项是指出现次数超过流中某个阈值比例的条目，例如热门话题标签、洪泛源 IP 地址，或需求激增的商品。草图本身只能回答“这个指定条目出现了多少次？”；它不会给出高频项的列表，因为它不存储键。<sup>[5](https://www.abstractalgorithms.dev/count-min-sketch-explained)</sup><sup> • </sup><sup>[8](https://akisonlyforu.com/blog/catching-bots-without-remembering-clicks/)</sup>

标准做法是将草图与一个大小为 \( K \) 的最小堆配合使用：每次插入时，向草图查询更新后的估计值；若估计值超过高频项阈值，就把该元素加入堆；定期清除估计值已降至阈值以下的堆条目。这样便能找出高频项，每个事件耗时 \( O(d + \log K) \)，内存占用固定为 \( O(d \times w + K) \)。<sup>[5](https://www.abstractalgorithms.dev/count-min-sketch-explained)</sup> 由于估计值从不低于真实值，真正的高频项总能越过同一阈值；代价是可能出现假高频项。<sup>[7](https://andifathulms.github.io/sketch-lab/en/countmin/)</sup>

与 Misra-Gries 相比——后者确定性地用 \( O(k) \) 个计数器维护高频项集合本身，低估量以 \( n/k \) 为界——计数–最小草图是随机化的，可作为任意键的频率预言机，空间为 \( O((1/\varepsilon) \log(1/\delta)) \) 个单元格。将计数–最小与堆配合，可以识别高频项；但若只需要高频集合，Misra-Gries 提供了更简单且确定性的替代方案。<sup>[9](https://senior-stack.uz/Roadmap/Data/datastructures-and-algorithms/22-randomized-algorithms/08-misra-gries-heavy-hitters/middle/)</sup>

## 变体

**保守更新（CMS-CU）。** 该变体中，仅当递增计数器与当前最小值一致时才执行更新，从而减少污染。2022 年的一项形式化分析在独立同分布请求过程下推导出期望估计误差的新上界，给出了高频项检测的改进精度估计和改进的配置规则。<sup>[10](https://arxiv.org/abs/2203.14549)</sup>

**带符号更新变体。** Count-Min-Mean 草图和 Count Sketch 通过带随机 ±1 乘子的带符号更新、并取各行中位数而非最小值，来支持递减操作。它们以部分精度换取了处理删除的能力。<sup>[4](https://unseel.com/cs/count-min-sketch)</sup>

**其他改造。** Count-Min-Log 草图用近似概率计数器替代精确计数器，以改善文本挖掘任务中低频条目的表现。<sup>[11](https://arxiv.org/abs/1502.04885)</sup> 在高速交换机数据平面中，Elastic 草图等级联设计使用基于计数–最小的大象过滤器，将大流与小流分离。<sup>[12](https://arxiv.org/abs/2111.02759)</sup>

## 用途

该草图适用于任何需要在高吞吐流上获取逐键频率、且能容忍高估的场景：网络数据包流、日志行频率、搜索查询速率。有记录的实际部署和用途包括：Twitter 用于热门话题的高频项检测、Akamai 基于每 IP 请求计数的 DDoS 检测、Cisco 的 NetFlow 分析、数据库查询优化器的基数估计，以及 AT&T 的网络异常检测。Druid 和 Pinot 所使用的 Apache DataSketches 库中包含 CountMin 草图，监控系统则使用 CMS 变体处理高基数时间序列。<sup>[4](https://unseel.com/cs/count-min-sketch)</sup><sup> • </sup><sup>[13](https://datasketches.apache.org/docs/pdf/DataSketches_13Mar2025.pdf)</sup>

由于草图是线性结构，每个分片可以维护本地草图，中心节点按单元格相加即可得到全局视图，这使它天然适合分布式聚合。<sup>[4](https://unseel.com/cs/count-min-sketch)</sup> 它也被用于隐私保护统计：在密码学聚合协议中，计数–最小草图和 Count Sketch 将每个数据源的通信量和计算量从随输入规模线性增长降为对数级增长。<sup>[14](https://arxiv.org/abs/1508.06110)</sup>

**何时不宜使用。** 当需要精确计数时，当必须枚举条目时（因为它不存储键），或当流的基数很小时（此时普通哈希表更简单），就应跳过该草图。<sup>[4](https://unseel.com/cs/count-min-sketch)</sup>

## References

1. [Graham Cormode, S. Muthukrishnan (2005). An improved data stream summary: The count-min sketch and its applications. Journal of Algorithms 55(1), 58–75.](https://www.researchwithrutgers.org/en/publications/an-improved-data-stream-summary-the-count-min-sketch-and-its-appl-2/)
2. [Count-Min Sketch, Explained, Quant Memo](https://quantmemo.com/concepts/count-min-sketch)
3. [Sketches at Scale: Count-Min, t-digest & Approximate Quantiles, PipeCode Blog](https://pipecode.ai/blogs/sketches-at-scale-count-min-tdigest-approximate-quantiles)
4. [Count-Min Sketch — How It Works, Complexity & Code, Unseel](https://unseel.com/cs/count-min-sketch)
5. [Count-Min Sketch Explained: Frequency Estimation at Streaming Scale, Abstract Algorithms](https://www.abstractalgorithms.dev/count-min-sketch-explained)
6. [5.13 Count-Min Sketch, brain (tamnd)](https://tamnd.github.io/brain/programming/algorithms/05/05.13/)
7. [Count-Min sketch, Sketch Lab](https://andifathulms.github.io/sketch-lab/en/countmin/)
8. [Catching the Bots Without Remembering the Clicks, Avinash Kumar](https://akisonlyforu.com/blog/catching-bots-without-remembering-clicks/)
9. [Misra-Gries Heavy Hitters, Senior Stack](https://senior-stack.uz/Roadmap/Data/datastructures-and-algorithms/22-randomized-algorithms/08-misra-gries-heavy-hitters/middle/)
10. [Ben Mazziane, Alouf, Neglia (2022). A Formal Analysis of the Count-Min Sketch with Conservative Updates. arXiv:2203.14549.](https://arxiv.org/abs/2203.14549)
11. [Count-Min-Log sketch: Approximately counting with approximate counters. arXiv:1502.04885.](https://arxiv.org/abs/1502.04885)
12. [Count-Less: A Counting Sketch for the Data Plane of High Speed Switches. arXiv:2111.02759.](https://arxiv.org/abs/2111.02759)
13. [Apache DataSketches: A Production Quality Sketching Library (March 2025 overview)](https://datasketches.apache.org/docs/pdf/DataSketches_13Mar2025.pdf)
14. [Melis, Danezis, De Cristofaro (2015). Efficient Private Statistics with Succinct Sketches. arXiv:1508.06110.](https://arxiv.org/abs/1508.06110)

---
*Initially written Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程

Copyright 2026 Edgepedia

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

Source: https://www.edgepedia.org/zh/count-min-sketch

English original: https://www.edgepedia.org/count-min-sketch
