ArticleslgStudy

computer science

Misra–Gries heavy hitters algorithm

Misra–Gries heavy hitters algorithm is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Misra–Gries heavy hitters algorithm rather than just read about it. In short: Misra and Gries defined the heavy-hitters problem (though they did not introduce the term heavy-hitters) and described the first algorithm for it in the paper Finding repeated elements. Their algorithm extends the Boyer-Moore majority finding algorithm in a significant way.

Key takeaways

  • Misra–Gries heavy hitters algorithm belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Misra–Gries heavy hitters algorithm to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Misra–Gries heavy hitters algorithm from memory before moving on to harder problems.

Reference excerpt

Misra and Gries defined the heavy-hitters problem (though they did not introduce the term heavy-hitters) and described the first algorithm for it in the paper Finding repeated elements. Their algorithm extends the Boyer-Moore majority finding algorithm in a significant way. One version of the heavy-hitters problem is as follows: Given is a bag b of n elements and an integer k ≥ 2. Find the values that occur more than n ÷ k times in b. The Misra-Gries algorithm solves the problem by making two passes over the values in b, while storing at most k values from b and their number of occurrences during the course of the algorithm. Misra-Gries is one of the earliest streaming algorithms, and it is described below in those terms in section #Summaries.

Misra–Gries algorithm A bag is like a set in which the same value may occur multiple times. Assume that a bag is available as an array b[0:n – 1] of n elements. In the abstract description of the algorithm, we treat b and its segments also as bags. Henceforth, a heavy hitter of bag b is a value that occurs more than n ÷ k times in it, for some integer k, k≥2. A k-reduced bag for bag b is derived from b by repeating the following operation until no longer possible: Delete k distinct elements from b. From its definition, a k-reduced bag contains fewer than k different values. The following theorem is easy to prove: Theorem 1. Each heavy-hitter of b is an element of a k-reduced bag for b. The first pass of the heavy-hitters computation constructs a k-reduced bag t. The second pass declares an element of t to be a heavy-hitter if it occurs more than n ÷ k times in b. According to Theorem 1, this procedure determines all and only the heavy-hitters. The second pass is easy to program, so we describe only the first pass. In order to construct t, scan the values in b in arbitrary order, for specificity the following algorithm scans them in the order of increasing indices. Invariant P of the algorithm is that t is a k-reduced bag for the scanned values and d is the number of distinct values in t. Initially, no value has been scanned, t is the empty bag, and d is zero.

Whenever element b[i] is scanned, in order to preserve the invariant: (1) if b[i] is not in t, add it to t and increase d by 1, (2) if b[i] is in t, add it to t but don't modify d, and (3) if d becomes equal to k, reduce t by deleting k distinct values from it and update d appropriately.

algorithm Misra–Gries is t, d := { }, 0 for i from 0 to n-1 do if b[i] ∉ {\displaystyle \notin } t then t, d:= t ∪ {b[i]}, d+1 else t, d:= t ∪ {b[i]}, d endif if d = k then Delete k distinct values from t; update d endif endfor

A possible implementation of t is as a set of pairs of the form (vi, ci) where each vi is a distinct value in t and ci is the number of occurrences of vi in t. Then d is the size of this set. The step "Delete k distinct values from t" amounts to reducing each ci by 1 and then removing any pair (vi, ci) from the set if ci becomes 0. Using an AVL tree implementation of t, the algorithm has a running time of O(n log k). In order to assess the space requirement, assume that the elements of b can have m possible values, so the storage of a value vi needs O(log m) bits. Since each counter ci may have a value as high as n, its storage needs O(log n) bits. Therefore, for O(k) value-counter pairs, the space requirement is O(k (log n + log m)).

Summaries In the field of streaming algorithms, the output of the Misra-Gries algorithm in the first pass may be called a summary, and such summaries are used to solve the frequent elements problem in the data stream model. A streaming algorithm makes a small, bounded number of passes over a list of data items called a stream. It processes the elements using at most logarithmic amount of extra space in the size of the list to produce an answer. The term Misra–Gries summary appears to have been coined by Graham Cormode.

References

Worked examples

Example 1 — a first encounter with Misra–Gries heavy hitters algorithm

Start with the simplest possible case. Write down what Misra–Gries heavy hitters algorithm claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Misra–Gries heavy hitters algorithm before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Misra–Gries heavy hitters algorithm ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Misra–Gries heavy hitters algorithm

In research
Misra–Gries heavy hitters algorithm appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Misra–Gries heavy hitters algorithm in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Misra–Gries heavy hitters algorithm is common in secondary-school and first-year university syllabi. It links to neighbouring topics Streaming algorithms, so understanding it makes those chapters shorter.
In everyday life
Look for Misra–Gries heavy hitters algorithm outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Misra–Gries heavy hitters algorithm” →

Affiliate

Preply — study more efficiently by working with a personal tutor. 50% off.

How to study Misra–Gries heavy hitters algorithm in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Misra–Gries heavy hitters algorithm means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Misra–Gries heavy hitters algorithm out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Misra–Gries heavy hitters algorithm in simple terms?

Misra and Gries defined the heavy-hitters problem (though they did not introduce the term heavy-hitters) and described the first algorithm for it in the paper Finding repeated elements. Their algorithm extends the Boyer-Moore majority finding algorithm in a significant way.

Why does Misra–Gries heavy hitters algorithm matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Misra–Gries heavy hitters algorithm?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Misra–Gries heavy hitters algorithm.

Tags

  • Streaming algorithms

Keep exploring