Unit 1: Digital information · Lossless data compression

Article

Compressing text without losing anything

7 min read

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:

  1. Walk along the string and split it into runs: KKKKKK, TTT, W.
  2. Write the length of each run, then its character: 6K, 3T, 1W.
  3. Join them: 6K3T1W.

Run-length encoding in three moves

  1. Original

    KKKKKKTTTW (10 characters)

    split into runs
  2. Runs

    KKKKKK TTT W

    count each run
  3. Pairs

    6K 3T 1W

    join
  4. Compressed

    6K3T1W (6 characters)

To decompress, run the same picture backwards. Read each count and character, and write the character that many times.

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:

OriginalRunsRLECharacters beforeCharacters after
AAAAAAAA18A82
AAAABBCC34A2B2C86
ABCDEFGH81A1B1C1D1E1F1G1H816

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 hat

The phrase the cat sat on the appears twice. Let # be its code.

  1. 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 and and hat, adds 9 + 4 = 13. The message is 49 characters long.
  2. Replace both copies with the code. The message becomes # mat and # hat, which is 15 characters.
  3. Write the dictionary: #=the cat sat on the. This is 20 characters.
  4. Add them: 15 + 20 = 35 characters.

The compressed file has two parts

  1. Original

    49 characters

    swap the phrase for #
  2. Message with codes

    15 characters: # mat and # hat

    add the dictionary
  3. Compressed file

    15 + 20 = 35 characters

The dictionary travels inside the compressed file, so its 20 characters count towards the size.
PartCharacters
Original message49
Message with codes15
Dictionary20
Compressed total35

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. ABB is 1A2B, not A2B.
  • Counting a two-digit count as one character. 12A is 3 characters.
  • Merging runs that are not next to each other. In AABAA there are three runs: 2A1B2A, not 4A1B.
  • 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.

AP® is a trademark registered by the College Board, which is not affiliated with, and does not endorse, this site. All lessons and questions here are written by JrCodex.