Lesson
The theory — Run-Length Encoding (RLE)
Run-length encoding replaces each maximal run of identical symbols with one (symbol, count) pair. What reaches the readout is not the text but two numbers: N characters and R runs. Every other figure on the page is those two divided by one another.
What each symbol means
N- the number of characters going in. It is the length of what you typed, nothing more.
R- the number of runs — maximal blocks of one repeated symbol. R goes up by one wherever two neighbours differ, so it counts boundaries, not characters.
L̄N/R, the average run length. This is the only quantity that decides the outcome.ratioN/2R, which isL̄/2. Above 1 the output is smaller than the input.
Where the formula comes from
- Count runs rather than characters. In
0010111100001111the neighbours differ in five places, so there are six runs and the row readsR=6. - The encoded size is
2Rand nothing else. Which symbols the runs were made of never enters the arithmetic — six runs cost twelve units whether they are pixels, letters or digits. - Divide:
ratio = N/2R = L̄/2. So the break-even sits atL̄ = 2, and it is exactly reachable — typeAABBand the bars read 4 bytes in, 4 bytes out. - The two directions are not symmetric. Upward there is no limit: a single run of a million characters encodes to one pair. Downward the floor is
0.50×and cannot be beaten, because the worst an input can do is give every character its own run.
How to read what you see
All four presets are exactly sixteen characters long, which makes them worth reading as a set. Image scanline has three runs and encodes to 6 bytes. Classic runs has four and gives 8. Bitmap mask has six and gives 12. Worst case has sixteen and gives 32. Same length in, and out comes anything from 6 bytes to 32 — a factor of five, settled entirely by R. Then type hello world: eleven characters, ten runs, 20 bytes out. Ordinary prose sits near the bottom of that range, which is the whole reason RLE is a component of compressors rather than one on its own.
- Assumes
- That a pair costs exactly 2 units, that a symbol and a count are the same width, and that a count can be any size. Real encoders give the count a fixed width — one byte, so 255 — and must split a longer run into several pairs, which raises the break-even slightly above 2. The equal-width assumption fails hardest on the data RLE is best at: in a 1-bit fax scan the symbol is one bit and the count is not.
- Breaks when
- The
0.50×floor is not a defect waiting to be engineered away. A lossless encoder has to be reversible, so distinct inputs must map to distinct outputs; and there are fewer short strings than long ones to map onto. Any scheme that makes even one input shorter therefore makes some other input longer. That is a counting fact, not a property of run-length encoding. PackBits, in the third insight above, pushes its worst case down to one control byte per 128 — under 1% — and that is as far as anyone can go, because the floor can be lowered without limit but never reached. What RLE does is wear the cost on the outside, in a readout you can watch move.
Problem solved in full
-
Run-length encoding AAAAAABBBCCDDDDD into four pairs 5 steps
Run-length encoding turns AAAAAABBBCCDDDDD into four pairs. Work out the compression, and then the exact condition under which this scheme makes a file bigger.
-
The encoding is the obvious one: replace each run by the character and its length. Sixteen characters collapse to four pairs.
-
Two numbers describe any input to this scheme — its length and its number of runs — and their ratio is the mean run length. Here it is 4.
-
Now count honestly. Each pair costs two tokens, a character and a count, so the output is 2R against an input of N. Nothing else about the data matters.
-
So the scheme wins exactly when 2R < N, which rearranges to a mean run length above 2. Here 8 against 16 — a clean halving — and the verdict line on the panel says the same thing in one symbol.
-
Below that threshold it loses, and at a mean run length of 1 it loses maximally: every character becomes a pair, doubling the file.
Answer
The tool prints N = 16 and R = 4, a mean run of 4 and a 2× saving. The condition is the thing to keep: RLE compresses if and only if runs average more than two, and it is one of the few compression schemes whose break-even point is a single number you can check by eye. Type ABCD into the box and watch it expand to twice the size. That is not a flaw — it is why RLE survives only where runs are guaranteed by construction: fax scan lines, sparse bitmaps, and the flat regions of a JPEG after quantisation, never general text.
-
Learning path
Compression by hand
References (3)
- Insight block 3 — PackBits, and the control byte that bounds its worst case: Adobe Systems, TIFF Revision 6.0, section 9 (PackBits Compression), 1992 — the format where Apple's variant became a standard compression mode.
- The run lengths themselves as the thing to be coded: S. W. Golomb, "Run-length encodings (Corresp.)." IEEE Transactions on Information Theory 12(3), 399–401, 1966.
- And the earlier paper that measured how long runs in real pictures actually are: J. Capon, "A probabilistic model for run-length coding of pictures." IRE Transactions on Information Theory 5(4), 157–163, 1959.