Article
Compressing text without losing anything
Look at this message: AAAAAAAAAAAAAAAAAAAA. You do not need to read every letter. You can see "20 letters A" at a glance. That is the whole idea of compressing text: describe the repeats instead of writing them out.
Both methods in this article are lossless. You can always rebuild the exact original text from the compressed text. In this article, one character is one unit of size, so we can count characters to compare sizes.
Method 1: run-length encoding
A run is a group of the same character that appear one after another. In AAABB there are two runs: three A's and then two B's.
Run-length encoding, or RLE, writes each run as a count followed by the character. So AAABB becomes 3A2B.
Here are the steps, with the string KKKKKKTTTW:
- Walk along the string and split it into runs:
KKKKKK,TTT,W. - Write the length of each run, then its character:
6K,3T,1W. - Join them:
6K3T1W.
Run-length encoding in three moves
Original
KKKKKKTTTW(10 characters)split into runsRuns
KKKKKKTTTWcount each runPairs
6K3T1WjoinCompressed
6K3T1W(6 characters)
The original had 10 characters and the result has 6. To decompress, read each pair and write the character that many times. Nothing is lost. In this article the text has only letters, so a digit is always part of a count.
Check your understanding
Use run-length encoding (the count first, then the character) on the string AAABBBBBC. What is the result?
When RLE fails
RLE only helps when there are long runs. Look at what happens to a string where nothing repeats:
| Original | Runs | RLE | Characters before | Characters after |
|---|---|---|---|---|
AAAAAAAA | 1 | 8A | 8 | 2 |
AAAABBCC | 3 | 4A2B2C | 8 | 6 |
ABCDEFGH | 8 | 1A1B1C1D1E1F1G1H | 8 | 16 |
Every run of length 1 still needs a count, so it costs 2 characters instead of 1. If a text has many short runs, RLE makes it bigger. Also, a count of 10 or more takes two characters, so 10A is 3 characters.
Check your understanding
Run-length encoding writes each run as a count and a character. Which string becomes longer when it is encoded this way?
Method 2: replacing repeated patterns
Real text repeats whole words and phrases, not just single letters. Another lossless method finds a pattern that appears many times, gives it a short code, and replaces every copy with the code. The pairs of code and pattern are kept in a dictionary.
Rules:
- The code must be something that does not appear in the text, so you can tell it apart.
- The dictionary is part of the compressed file. You need it to decompress, so you must count it in the size.
Worked example
The message is:
the cat sat on the mat and the cat sat on the hatThe phrase the cat sat on the appears twice. Let # be its code.
- Count the original. The phrase has 18 characters (3 + 1 + 3 + 1 + 3 + 1 + 2 + 1 + 3, with spaces). Two copies are 36. The rest,
mat andandhat, adds 9 + 4 = 13. The message is 49 characters long. - Replace both copies with the code. The message becomes
# mat and # hat, which is 15 characters. - Write the dictionary:
#=the cat sat on the. This is 20 characters. - Add them: 15 + 20 = 35 characters.
The compressed file has two parts
Original
49 characters
swap the phrase for#Message with codes
15 characters:
# mat and # hatadd the dictionaryCompressed file
15 + 20 = 35 characters
| Part | Characters |
|---|---|
| Original message | 49 |
| Message with codes | 15 |
| Dictionary | 20 |
| Compressed total | 35 |
The ratio is 49 ÷ 35 = 1.4, so about 1.4 to 1. To decompress, look up each # in the dictionary and put the phrase back. The result is exactly the original.
The saving is small here because the message is short and the dictionary is a big part of it. In a long text with many repeats, the dictionary is tiny next to the savings. Real tools such as ZIP use cleverer versions of this idea.
Common mistakes
- Forgetting the count for runs of length 1.
ABBis1A2B, notA2B. - Counting a two-digit count as one character.
12Ais 3 characters. - Merging runs that are not next to each other. In
AABAAthere are three runs:2A1B2A, not4A1B. - Leaving the dictionary out of the size. The dictionary has to be stored too.
- Using a code that appears in the text. Then you cannot tell code from text.
On the exam
You may be asked to explain what a lossless method keeps (everything) and what it relies on (repeated patterns). You do not need to learn named algorithms. Be ready to compute a compressed size by hand, and to say why some data barely shrinks.
Key words
Run: a group of the same character one after another.
Run-length encoding (RLE): writing each run as a count and a character.
Dictionary: a table that pairs short codes with the patterns they replace.
Lossless: a method where the exact original can be rebuilt.