The idea: a few buckets instead of the whole window

A stream of bits never stops. We want to know, at any time, how many 1s are in the last N bits (the window). Keeping the N bits answers it exactly, but N can be a billion, and there may be a million such streams. The DGIM algorithm (Datar, Gionis, Indyk, Motwani) keeps only about log N buckets and answers within 50% of the true count.

The last 24 bits of the stream at t = 40, with the window t = 25 to 40 shaded. Under the bits are five buckets of sizes 4, 4, 2, 1 and 1. The oldest bucket of size 4 ends at 27 and has only 2 of its 1s in the window. The estimate is 4/2 + 4 + 2 + 1 + 1 = 10 and the exact count is 10
DGIM keeps five (end, size) pairs, not 16 bits. Half of the oldest bucket plus all the others gives 10, the exact count here.

The page uses a window of N = 16 bits. Press Add 1 or Add 0 to push a bit, and Query to compute the estimate. The stream panel shows the bits (the page keeps them only to draw them and to show the exact count). The bucket list is everything DGIM itself stores.

Buckets

Every bit has a timestamp: the first bit is 1, the next 2, and so on. A bucket is a pair (end, size):

  • end is the timestamp of the bucket's newest bit, and that bit is always a 1.
  • size is the number of 1s in the bucket, and it is always a power of 2 (1, 2, 4, 8, …).
  • Every 1 in the window is in exactly one bucket; buckets never overlap.
  • There are one or two buckets of each size, and sizes never get smaller as you go back in time.

The end only needs to be stored mod N (4 bits for N = 16), and the size as j = log2 size (3 bits). The bucket list on the canvas prints both forms.

What happens when a bit arrives

  1. The clock moves to t. If the oldest bucket's end is now ≤ t − N, its newest 1 has left the window, so the whole bucket is dropped. This happens for a 0 too (Demo 3: the bucket of size 4 that ends at 6 leaves at t = 22).
  2. If the bit is 0, nothing else changes.
  3. If the bit is 1, a new bucket (end t, size 1) is made. If there are now three buckets of size 1, the two oldest merge into one bucket of size 2, whose end is the newer of the two ends. That can leave three buckets of size 2, which merge into a 4, and so on (Demo 1, Demo 2).
Four rows going down in time. Before bit 7 the buckets are 2, 2, 1, 1. Bit 7 adds a bucket of size 1, so there are three 1s and the two oldest, ending at 5 and 6, merge into a 2. Now there are three 2s and the two oldest, ending at 2 and 4, merge into a 4. The result is 4, 2, 1
The seventh 1 makes a cascade: 1 + 1 → 2, then 2 + 2 → 4. At most two buckets of each size remain.

A merge looks at most at a few buckets per size, so one bit costs O(log N) work.

The estimate and the 50% bound

To count the 1s in the window, add the sizes of all buckets except the oldest, plus half the oldest. Every bucket but the oldest is fully inside the window. The oldest one ends inside the window, but we do not know how much of it is still in, so we guess half.

Why the error is at most 50%: say the oldest bucket has size 2j. The guess is off by at most 2j−1. The true count is at least 1 (the end of the oldest bucket) plus one bucket of each smaller size, 2j−1 + … + 2 + 1 = 2j − 1, so it is at least 2j. An error of 2j−1 is at most half of that.

DemotBuckets (size@end)EstimateExactError
1: bits 101142@3, 1@423−33.3%
2: seven 1s74@4, 2@6, 1@757−28.6%
3: a bucket leaves224@14, 2@17, 1@18, 1@2168−25%
4: the 40-bit stream404@27, 4@32, 2@35, 1@38, 1@3910100%
5: old bucket mostly outside278@13, 4@17, 2@20, 2@24, 1@251310+30%
6: 64 ones648@56, 4@60, 2@62, 1@63, 1@641216−25%

The error can go both ways. When the oldest bucket is fully inside the window, half of it is missed (Demo 6). When most of it is outside, half of it is too much: in Demo 5 only 1 of the 8 ones in the oldest bucket is in the window, and the estimate is 13 for 10.

Memory: O(log² N) bits

Sizes go up to at most N, so there are about log2 N sizes and at most 2(log2 N + 1) buckets. Each bucket needs log2 N bits for its end and about log2 log2 N bits for its size. That is O(log² N) bits in all, against N bits for the exact answer.

NExact (bits)DGIM, worst case
16 (this page)1610 buckets × 7 bits = 70
1,0241,02422 × 14 = 308
220 ≈ 1 million1,048,57642 × 25 = 1,050
230 ≈ 1 billion1,073,741,82462 × 35 = 2,170

For N = 16, DGIM uses more bits than the window itself. It pays off only for big windows, which is where it is used. In Demo 6, 64 ones in a row still leave only 5 buckets.

DGIM answers one question about a window of recent bits. A Count-Min Sketch estimates how often each item appeared, HyperLogLog counts distinct items, and Reservoir Sampling keeps a fair sample of the whole stream. For how stream processors define windows over event time, see Stream Windows and Watermarks. Mining frequent itemsets, another topic of the same course, is on FP-Growth.

See also FP-Growth, which mines frequent itemsets from a stored database that it reads exactly twice, unlike a stream that can be read only once.

What the page leaves out

Queries for the last k < N bits (use only the buckets that end after t − k); trading memory for accuracy by allowing r − 1 or r buckets of each size, which gives an error of at most about 1/(r − 1); counting sums of integers instead of bits, by running one DGIM per bit position; exponentially decaying windows, the other answer in MMDS chapter 4; and real timestamps that wrap mod N.

On the mind map

This page on the Indexing mind map · 42 pages in 8 branches · open it to see the whole map and test yourself in recall mode.