CS22: Data Compression
Lossy and lossless compression methods including Huffman coding and run length encoding, and when to use each type.
Lossy and lossless compression methods including Huffman coding and run length encoding, and when to use each type.
Uncompressed files can be very large. A single uncompressed 3-minute CD-quality song is about 30 MB. A high-resolution photograph can be 50 MB or more. Compression makes files smaller so they take up less storage and can be transmitted faster.
| Property | Lossy Compression | Lossless Compression |
|---|---|---|
| How it works | Permanently removes data that is less important or noticeable | Reduces file size without losing any data |
| Data recovery | Original data CANNOT be fully restored | Original data CAN be fully restored |
| Compression ratio | Higher (much smaller files) | Lower (moderately smaller files) |
| Quality | Some quality is lost | No quality loss - exact original |
| File examples | JPEG, MP3, MP4 | PNG, FLAC, ZIP |
| Typical use | Streaming, web images, music | Text files, code, medical images, archives |
A 50 MB uncompressed WAV file can be compressed to about 5 MB as an MP3 (lossy) or about 30 MB as a FLAC file (lossless). The MP3 loses audio detail that most listeners won't notice. The FLAC preserves every detail of the original recording.
Data: "ABRACADABRA"
Step 1: Count frequencies:
A = 5, B = 2, R = 2, C = 1, D = 1
Step 2: Build the tree (combine two lowest each time):
Combine C(1) and D(1) โ CD(2)
Combine B(2) and R(2) โ BR(4)
Combine CD(2) and BR(4) โ CDBR(6)
Combine A(5) and CDBR(6) โ root(11)
Step 3: Assign codes (left=0, right=1):
A = 0 (1 bit - most frequent)
C = 100 (3 bits)
D = 101 (3 bits)
B = 110 (3 bits)
R = 111 (3 bits)
Using "ABRACADABRA" with the codes above:
A(5 chars x 1 bit) = 5 bits
B(2 chars x 3 bits) = 6 bits
R(2 chars x 3 bits) = 6 bits
C(1 char x 3 bits) = 3 bits
D(1 char x 3 bits) = 3 bits
Total Huffman = 23 bits
With 5 distinct characters, fixed-length encoding needs 3 bits per character (22=4 is too few, 23=8 is enough).
Fixed-length total = 11 characters x 3 bits = 33 bits
Huffman total = 23 bits
Saving = 33 - 23 = 10 bits (30.3% reduction)
RLE is most effective when data contains long runs of repeated values. Instead of storing each value individually, it stores the count followed by the value.
Uncompressed: AAAAAABBCCCCCCDDDDDD
Compressed with RLE: 6A2B6C6D
Original = 20 characters; Compressed = 8 characters
This is a significant saving because there are many repeated runs.
A row of a bitmap image with 1-bit colour depth:
Uncompressed: 000000001111111100000000
RLE: 0,8 1,8 0,8 (frequency,data pairs)
Original = 24 values; Compressed = 6 values
Data: ABCDEFGHIJKLMNOP
RLE: 1A1B1C1D1E1F1G1H1I1J1K1L1M1N1O1P
Original = 16 characters; Compressed = 32 characters
RLE actually makes the file LARGER when there are no repeated runs. Each character needs a count of 1 plus the character itself.
| Scenario | Best Compression | Why |
|---|---|---|
| Streaming music online | Lossy (MP3) | Small file size needed; slight quality loss acceptable |
| Archiving important documents | Lossless (ZIP) | Must preserve exact data; no data loss acceptable |
| Web page photographs | Lossy (JPEG) | Fast loading important; small visual difference OK |
| Medical imaging (X-rays) | Lossless (PNG) | Every detail matters for diagnosis |
| Simple bitmap with blocks of colour | RLE | Long runs of same colour compress very well |
| Text with varying character frequencies | Huffman coding | Frequent characters get short codes, saving space |
| Source code files | Lossless (ZIP/Huffman) | Code must be exact - no data loss acceptable |
| Video streaming (Netflix, YouTube) | Lossy (MP4/H.264) | Massive size reduction needed; some quality loss OK |
| Mistake | Why It's Wrong | How to Fix It |
|---|---|---|
| Saying "lossy compression is bad because it loses data" | Lossy is ideal for many purposes where small size matters | Consider the use case - streaming needs lossy |
| Building Huffman tree incorrectly | Must always combine the TWO lowest frequency nodes | |
| Forgetting RLE can increase file size | RLE adds a count to every value, even singles | RLE is only effective with long repeated runs |
| Not comparing Huffman to fixed-length | Exam questions often ask for the comparison | Calculate both and show the saving |
| Confusing Huffman with RLE | They work on completely different principles | Huffman = variable-length codes; RLE = frequency/data pairs |
Q1: Explain the difference between lossy and lossless compression.
Q2: The string "MISSISSIPPI" has these character frequencies: M=1, I=4, S=4, P=2. Construct a Huffman tree and assign codes to each character.
Q3: Using the Huffman codes from Q2, calculate the total bit length of "MISSISSIPPI" and compare it to a fixed-length encoding.
Q4: Apply run length encoding to this bitmap row: 111110000011111
Q5: Explain why RLE would not be a good compression method for a photograph.
โ Confusing lossy and lossless compression โ Lossy compression permanently removes data to reduce file size; the original cannot be restored. Lossless compression reduces file size without losing any data; the original can be perfectly reconstructed.
โ Thinking lossy compression makes files useless โ Lossy compression removes data that is less noticeable to humans (e.g. high frequencies in audio, fine detail in images). The result is still usable โ just not identical to the original.
โ Not understanding how RLE works โ thinking it compresses all data โ Run-length encoding (RLE) only compresses data with repeated sequences. Data without repetition (e.g. ABCDEF) would actually increase in size with RLE, not decrease.
โ Believing lossless compression can achieve any compression ratio โ Lossless compression has limits based on the information content of the data. Typical lossless ratios are 2:1 to 3:1; claiming 100:1 for lossless is unrealistic.
A bitmap image contains the following pixel data: RRRRGGGGBBBBYYYY. Explain how run-length encoding would compress this data, and calculate the compression ratio. [4 marks]
Original data: RRRRGGGGBBBBYYYY (16 characters) RLE compression stores each colour and its count: R4G4B4Y4 (8 characters) Each run is encoded as: colour character + count digit. Compression ratio = original size รท compressed size = 16 รท 8 = 2:1 The data has been compressed to half its original size. RLE is effective here because the data contains long runs of identical values. If the data were RGBYRGBY (no repeats), RLE would not compress it effectively.
AO1 (Computational Thinking โ 40%): Demonstrate knowledge and understanding of the principles and concepts of computer science, including data compression: lossy and lossless methods for AQA 8525, OCR J277 & Edexcel 1CP2.
AO2 (Application โ 40%): Apply knowledge and understanding of computer science, including data compression: lossy and lossless methods to analyse problems in computational terms and to design, write and evaluate solutions.
AO3 (Evaluation โ 20%): Evaluate the effectiveness, correctness and efficiency of computational solutions, including data compression: lossy and lossless methods, and make reasoned judgements about trade-offs.
Get the best revision books and guides to boost your grades.