Run length encoding and dictionary coding for lossless compression.

  • lossless compression works by removing redundancy โ€˜#

two key ways are

  • run length encoding

  • dictionary coding

๐Ÿ”ต 1. Run Length Encoding (RLE)

๐Ÿ’ก Idea (very simple)

If data repeats, instead of storing it again and again, we store:

๐Ÿ‘‰ the value + how many times it repeats

Example

Original data:

AAAAABBCCCC

RLE:

5A2B4C

๐Ÿ‘‰ Instead of writing A five times, we say โ€œ5Aโ€

โ€


๐Ÿง  Why it works

It is effective when:

  • There are lots of repeated values

It is bad when:

  • Data has little or no repetition

Example:

ABCDEFG

RLE might actually make it longer.

๐Ÿ”ด 2. Dictionary Coding

๐Ÿ’ก Idea (simple)

Instead of repeating long patterns, we:

  1. Store patterns in a dictionary

  2. Replace them with short references (indexes)

Example

Original:

the cat sat on the mat and the cat ran

Dictionary:

1 = the  
2 = cat  
3 = sat  
4 = on  
5 = mat  
6 = and  
7 = ran

Encoded:

1 2 3 4 1 5 6 1 2 7

๐Ÿ‘‰ Repeated words are replaced with short numbers