Sub topic 1
Streams: One Pass, Unknown Length, Limited Memory
A stream is a sequence of items that arrives one at a time. Examples include log lines from a web server, readings from a sensor, rows from a database cursor, or tweets from a public feed. This tutorial samples data from streams, so it starts by pinning down which rules a streaming algorithm must follow. These rules are called the streaming model.
Prerequisites
This chapter assumes you are comfortable with:
- Basic Python, including loops, lists, generators, and iterators
- Elementary probability, including uniform distributions, independence, and conditional probability
- Big-O notation for time and space
- Proof by induction, which later chapters use for correctness
The Three Constraints
A streaming algorithm works under three constraints at once. Each is easy to handle alone. Handling all three together is what makes the problem hard.
Single pass
You see each item once, in order. After you move past an item, you cannot go back to it unless you saved it yourself. In Python, a generator behaves this way. When it is exhausted, a second loop over it produces nothing.
Unknown length
You do not know how many items will arrive. The stream might end after ten items or ten billion, and it might never end. The algorithm therefore cannot use the total count n while making decisions. It has to give a correct answer for whatever prefix it has seen so far.
Bounded memory
You can keep only a small amount of state, usually a fixed size or one that grows slowly, such as O(k) or O(log n). Storing the whole stream is not allowed, because n can be far larger than available RAM.
Seeing the Model in Python
The code below builds a stream whose length is random, so the consumer cannot know it in advance. It computes a running count and mean in O(1) memory, then shows that a second pass finds nothing.
import random
def readings(rng):
# Stops at a random point, so the length is unknown in advance
while rng.random() > 0.001:
yield rng.gauss(20.0, 2.0)
stream = readings(random.Random(42))
count = 0
mean = 0.0
for x in stream:
count += 1
mean += (x - mean) / count # incremental mean, O(1) state
print("items seen:", count)
print("mean:", round(mean, 3))
print("second pass:", sum(1 for _ in stream)) # 0: the generator is exhaustedA running mean fits the model because it can be updated from the current item and a fixed amount of state. Choosing a random sample is harder. Which items you keep depends on how many items will eventually arrive, and that number is unknown.
Common Errors
| Mistake | Why it breaks the model |
|---|---|
| Calling `len(stream)` | Generators and network streams have no length |
| Calling `list(stream)` first | Uses O(n) memory and may never finish |
| Iterating twice | The second pass sees an empty or different stream |
| Fixing `n` in the algorithm | The answer is wrong for every other stream length |
Exercise
You must report the maximum value seen in a stream, and separately the median. Which task fits the streaming model with O(1) memory, and why does the other not?
Show answer
The maximum fits, because you keep one number and replace it whenever a larger item arrives. An exact median does not fit, because which item ends up as the median depends on items that have not arrived yet. Computing it exactly in a single pass requires storing a number of items that grows with n.