{"id":227,"date":"2026-11-13T12:35:12","date_gmt":"2026-11-13T04:35:12","guid":{"rendered":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-misra-gries-tracks-heavy-hitters\/"},"modified":"2026-11-13T21:53:13","modified_gmt":"2026-11-13T13:53:13","slug":"how-misra-gries-tracks-heavy-hitters","status":"publish","type":"post","link":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-misra-gries-tracks-heavy-hitters\/","title":{"rendered":"How Misra-Gries tracks heavy hitters"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Short answer:<\/strong> The <strong>Misra-Gries<\/strong> summary keeps <strong>k \u2212 1<\/strong> counters and finds every value whose frequency in a stream of length n is greater than n\/k. A value that is already counted increments its counter. A value that finds a free counter takes it. When every counter is busy, each counter decrements, and a counter that hits zero becomes free. A second pass counts the surviving candidates exactly. The first pass can keep a value that is not heavy. It cannot drop a value that is. Memory is <strong>O(k)<\/strong>.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the problem actually asks<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">A strict majority is the case k = 2, and the Boyer-Moore vote solves it with one counter. The generalization asks for every value that occupies more than a 1\/k fraction of the stream, without storing the stream. At most k \u2212 1 values can clear that bar, because (k) copies of a 1\/k fraction would already exceed n. That numeric fact is why k \u2212 1 counters are enough to retain every heavy hitter, even though the algorithm does not know the frequencies yet.<\/p>\n\n\n<p class=\"wp-block-paragraph\">Decrementing every counter at once is a batch of deletions. Each such batch deletes k occurrences, one per counter. Across the whole stream the number of deleted occurrences of a true heavy hitter is too small to empty its counter for good. A light value has no such promise and can survive anyway if it happens to be written into a counter near the end.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the algorithm guarantees<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">After one pass, every value with frequency greater than n\/k is among the at most k \u2212 1 monitored values. The counter stored for a value is at most its true frequency and at least its true frequency minus n\/k. A second pass that counts the survivors against the original stream, or against a stored copy, deletes the false survivors whose real frequency is at most n\/k. Time is O(n) for the first pass with a hash map from value to counter, and O(n) for the second pass. Extra memory is O(k).<\/p>\n\n\n<p class=\"wp-block-paragraph\">The algorithm is deterministic. A count-min sketch estimates frequencies with a probabilistic error and can track a different set of questions, including values below the 1\/k line, with a different failure story. Misra-Gries does not estimate the frequency of a value it dropped.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>The working steps<\/strong><\/h4>\n\n<ol class=\"wp-block-list\">\n<li>Choose k. Allocate k \u2212 1 counters, initially free.<\/li>\n<li>For each arriving value, increment its counter if it is monitored. Otherwise assign it to a free counter with count 1 if one exists. Otherwise decrement every counter and free those that reach 0.<\/li>\n<li>At the end of the stream the monitored values include every heavy hitter, plus zero or more light values.<\/li>\n<li>Count each survivor in a second pass. Keep a survivor whose count is greater than n\/k.<\/li>\n<li>The k = 2 case is a single counter and is the Boyer-Moore majority vote.<\/li>\n<\/ol>\n\n\n<p class=\"wp-block-paragraph\">The guarantee is one-sided until the second pass. A count-min sketch is a probabilistic frequency estimate. The Boyer-Moore vote is this algorithm at k = 2. Misra-Gries is the k \u2212 1 counter summary.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Approaches that look simpler and fail<\/strong><\/h4>\n\n<figure class=\"wp-block-table\">\n<table>\n<thead>\n<tr>\n<th>Approach<\/th>\n<th>Why it fails when the first-pass candidates are emitted as the answer<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Skip the second pass<\/td>\n<td>A light value can occupy a counter at the end. The first pass promises only that heavy hitters are present, not that every present value is heavy.<\/td>\n<\/tr>\n<tr>\n<td>Decrement a single counter instead of every counter<\/td>\n<td>The deletion argument counted one removal against each of k slots. Decrementing one slot lets a heavy hitter be paired off against itself and dropped.<\/td>\n<\/tr>\n<tr>\n<td>Use fewer than k \u2212 1 counters<\/td>\n<td>There can be k \u2212 1 values each above n\/k. Fewer counters have no room for all of them.<\/td>\n<\/tr>\n<tr>\n<td>Report the raw counter as the exact frequency<\/td>\n<td>The counter is the true frequency minus the deletions, and the deficit can be as large as n\/k. The second pass is an exact count.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n<p class=\"wp-block-paragraph\">One majority candidate is the Boyer-Moore vote. A probabilistic frequency table is a count-min sketch. Misra-Gries is the deterministic heavy-hitter summary.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>A working example<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">This Python fragment keeps k\u22121 = 2 counters, so every value that occurs more than n\/3 times survives. The second pass drops a light survivor. In this stream the only heavy hitter is <code>a<\/code>.<\/p>\n\n<pre><code class=\"language-python\">stream = list(&quot;aabaabaca&quot;)\nk = 3\ncounts = {}\nfor item in stream:\n    if item in counts:\n        counts[item] += 1\n    elif len(counts) &lt; k - 1:\n        counts[item] = 1\n    else:\n        for key in list(counts):\n            counts[key] -= 1\n            if counts[key] == 0:\n                del counts[key]\nn = len(stream)\nheavy = [item for item in counts if stream.count(item) &gt; n \/ k]\nassert heavy == [&quot;a&quot;]\nprint(*heavy)\n<\/code><\/pre>\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Where it is used<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">Misra-Gries tracks heavy hitters in a stream with a fixed number of counters, which is the memory budget of a network monitor. The Space-Saving algorithm is the practical relative. A count-min sketch estimates every frequency, not only the heavy ones.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>When you can drop this algorithm<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">Drop the summary when every frequency, including rare values, must be estimated. Use a sketch or a full table. Keep Misra-Gries when the stream is long, memory holds k counters, and only values above n\/k matter.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Why this stays published<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">We publish this so a search for Misra-Gries returns the k \u2212 1 counters, the simultaneous decrement, and the verifying second pass. <strong>Misra-Gries keeps k \u2212 1 counters so that every value occurring more than n\/k times survives the first pass, and a second pass confirms the true frequencies.<\/strong><\/p>\n\n\n<p class=\"wp-block-paragraph\">\u672c\u6587\u7531 HoHo \u8207 AI \u5354\u4f5c\u6574\u7406\uff0c\u6700\u5f8c\u66f4\u65b0\u65bc 2026 \u5e74 11 \u6708 13 \u65e5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Short answer: The Misra-Gries summary keeps k \u2212 1 counters and finds every value whose frequency in a stream of length n is greater than n\/k. A value that is already counted increments its counter. A value that finds a free counter takes it. When every counter is busy,&hellip;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30,61],"tags":[],"class_list":["post-227","post","type-post","status-publish","format-standard","hentry","category-algorithms","category-streams"],"_links":{"self":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/227","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/comments?post=227"}],"version-history":[{"count":2,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/227\/revisions"}],"predecessor-version":[{"id":626,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/227\/revisions\/626"}],"wp:attachment":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/media?parent=227"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/categories?post=227"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/tags?post=227"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}