AQA 8525 GCSE Computer Science · 3.3 Fundamentals of Data Representation

Stretch tasks Data Compression, Huffman and RLE

Task 1

Quiz Challenge

A quiz game against four computer players, then short and long written answers.

  1. Quiz Blitz12 questions against the clock
  2. Quick Fire4 short answers
  3. Big Think2 long answers

About 20 minutes

Task 2

This or That

Pixel the cat asks 8 questions. Each has two answers: pick the right one.

  • Two answers to every question
  • A fish treat for every right answer
  • Streaks win Pixel shades, then a party hat

About 4 minutes

Task 3

Memory Match

Flip the cards two at a time to match each term card to its meaning card.

  • 12 cards: each term has a meaning
  • One peek at every card
  • Combo points for pairs found in a row

6 pairs, about 4 minutes

Task 4

Sort It

Sort each statement into the right box, like the tick-box tables in the exam.

  • 11 cards to sort into 3 boxes
  • Drag a card, tap a box or press its number
  • Combo points for cards sorted in a row

11 cards, about 4 minutes

The games need JavaScript. Every question is below, with the answers at the end.

Read the lesson: Data Compression, Huffman and RLE

Data Compression, Huffman and RLE: the questions

Quiz Challenge, round 1: Quiz Blitz

  1. Which of these is lossless?

    • JPEG
    • MP3
    • MP4
    • PNG
  2. Run length encode W W W W W B B W W W W W W B B B.

  3. Why can run length encoding make a file bigger?

    • It deletes data
    • It adds metadata
    • It needs a key
    • Data with no runs gives a pair for every single value
  4. In Huffman coding, which characters get the shortest codes?

    • Capital letters
    • The most frequent
    • The least frequent
    • The first in the alphabet
  5. When building a Huffman tree, which nodes are joined first?

    • Any two
    • The two with the lowest frequencies
    • The two with the highest frequencies
    • The first two letters
  6. How many bits does BANANA take in 7-bit ASCII?

  7. With A = 0, N = 10 and B = 11, how many bits does BANANA need?

  8. A Huffman code is read from the leaf up to the root.

    • True
    • False
  9. Which TWO are reasons to compress files?

    Tick 2.

    • They become more secure
    • Their quality improves
    • They gain metadata
    • They take less storage
    • They take less time to send
  10. Which compression suits a program's source code?

    • Lossy, to save the most space
    • Lossy, but only for comments
    • None: code cannot be compressed
    • Lossless, so nothing changes
  11. Lossless compression removes redundancy in how data is stored, not the data itself.

    • True
    • False
  12. What does MP3 compression remove?

    • The quietest track
    • The metadata only
    • Frequencies most people cannot hear
    • Every second note

Quiz Challenge, round 2: Quick Fire

  1. Explain how run length encoding works, using an example. [2 marks]

  2. Explain why a stream of Huffman codes can be decoded without any separators. [2 marks]

  3. Compare lossy and lossless compression. [2 marks]

  4. WWWWWBBWWWWWWBBB takes 16 bytes. It is stored as 5W 2B 6W 3B, where each value and each count takes 1 byte. Calculate the saving. [2 marks]

Quiz Challenge, round 3: Big Think

  1. Build a Huffman tree for BANANA and use it to work out how many bits are saved compared with 7-bit ASCII. [4 marks]

  2. A company stores photographs, music and spreadsheets. Recommend lossy or lossless compression for each, and justify your choices. [6 marks]

This or That: tick the right answer

  1. Which suits run length encoding best?

    • A detailed photograph
    • A logo with flat blocks of colour
  2. Does lossless compression remove data?

    • No: it stores it more efficiently
    • Yes: the least useful data
  3. In a Huffman tree, what is a left branch usually labelled?

    • 1
    • 0
  4. Which is lossy?

    • ZIP
    • MP3
  5. Why compress video before streaming it?

    • It improves the picture
    • There is less data to send
  6. Which is decoded by following bits down from a root?

    • A run length pair
    • A Huffman code
  7. Run length encoding ABABABAB gives a result that is...

    • Bigger
    • Smaller
  8. What do you get by removing characters from a program to shrink it?

    • A faster program
    • A broken program

Memory Match: match each term to its meaning

TermMeaning
A Lossy1 Reconstructed exactly
B Lossless2 Data sent in a given time
C RLE3 WWWWW
D Huffman coding4 Original cannot be recovered
E 5W5 A value and a count for each run
F Bandwidth6 Short codes for frequent characters

Sort It: tick one box in each row

Lossy compression, run length encoding or Huffman coding?

StatementLossyRun lengthHuffman
JPEG discards fine colour detail
BANANA in 9 bits instead of 42
Codes are read from the root of a tree
Can make data with no runs bigger
MP3 removes sounds most people cannot hear
Replaces a run of values with a count
5W 2B 6W 3B
Frequent characters get the shortest codes
The original cannot be recovered
Join the two lowest frequencies first
Suits graphics with blocks of flat colour
Answers

Round 1: Quiz Blitz

  1. PNG PNG and ZIP are lossless. JPEG and MP3 are lossy.
  2. 5W 2B 6W 3B Five W, two B, six W, three B.
  3. Data with no runs gives a pair for every single value ABABABAB becomes eight pairs: sixteen values instead of eight.
  4. The most frequent Short codes for common characters cut the total number of bits.
  5. The two with the lowest frequencies Joining the lowest pushes rare characters deepest, giving them the longest codes.
  6. 42 6 characters x 7 bits = 42 bits.
  7. 9 B 2 bits, three As 3 bits, two Ns 4 bits: 9 bits.
  8. False Read it from the root down to the leaf.
  9. They take less storage; They take less time to send Less storage, faster transmission and less bandwidth.
  10. Lossless, so nothing changes Removing characters from a program gives a broken program.
  11. True The original can be reconstructed exactly.
  12. Frequencies most people cannot hear MP3 is lossy: it discards what listeners are least likely to notice.

Round 2: Quick Fire

    • It replaces a sequence of repeated values with a count and the value
    • For example, WWWWW becomes 5W
    Model answer

    Run length encoding replaces each sequence of repeated values with a count and the value: WWWWWBB becomes 5W 2B.

    • No character's code is the start of another's
    • So following the bits from the root always reaches exactly one leaf
    Model answer

    No character's code is the start of another character's code, so following the bits from the root of the tree always reaches exactly one leaf, then decoding starts again at the root.

    • Lossy permanently removes data, giving a large reduction
    • Lossless reconstructs the original exactly, with a more modest reduction
    Model answer

    Lossy compression permanently removes data, giving a large reduction, and the original cannot be recovered. Lossless compression reconstructs the original exactly, but the reduction is more modest.

    • 4 pairs of 2 bytes = 8 bytes
    • A saving of 8 bytes, or 50 per cent
    Model answer

    The compressed version is 4 pairs of 2 bytes, which is 8 bytes. The saving is 16 - 8 = 8 bytes, which is 50 per cent.

Round 3: Big Think

    • Join the two lowest frequencies, B and N, first
    • Codes such as A = 0, N = 10, B = 11
    • The Huffman total is 9 bits
    • ASCII needs 42 bits, so 33 bits are saved
    Model answer

    The frequencies are A 3, N 2 and B 1. Join the two lowest, B and N, into a node worth 3, then join that with A to make the root. Labelling left branches 0 and right branches 1 gives A = 0, N = 10 and B = 11. BANANA then needs 2 + 3 + 4 = 9 bits, compared with 6 x 7 = 42 bits in ASCII, a saving of 33 bits.

    • Photographs: lossy, such as JPEG, because the eye will not notice
    • Music: lossy, such as MP3, which removes sounds people cannot hear
    • Spreadsheets must be reconstructed exactly, such as in a ZIP
    • Losing data from a spreadsheet would corrupt it
    • Lossy gives much smaller files
    • Smaller files are faster to send and use less bandwidth
    Model answer

    Photographs should use lossy compression such as JPEG, because it discards fine colour detail the eye barely notices, giving much smaller files. Music should also be lossy, such as MP3, which removes frequencies most people cannot hear; smaller files are faster to send and use less bandwidth. Spreadsheets must use lossless compression, such as a ZIP archive, because every value must be reconstructed exactly: removing data would corrupt the numbers.

This or That

  1. A logo with flat blocks of colour RLE needs long runs of the same value. Photographs have almost none.
  2. No: it stores it more efficiently The original can be rebuilt exactly.
  3. 0 Left branches 0 and right branches 1, applied consistently.
  4. MP3 ZIP must give back every file exactly.
  5. There is less data to send Smaller files use less bandwidth and arrive faster.
  6. A Huffman code Each bit chooses a branch until a leaf is reached.
  7. Bigger Every run has length one, so the pairs double the size.
  8. A broken program That is why code must only ever be compressed losslessly.

Memory Match

  • A 4 Lossy: Original cannot be recovered
  • B 1 Lossless: Reconstructed exactly
  • C 5 RLE: A value and a count for each run
  • D 6 Huffman coding: Short codes for frequent characters
  • E 3 5W: WWWWW
  • F 2 Bandwidth: Data sent in a given time

Sort It

  1. Lossy JPEG discards fine colour detail That is lossy compression.
  2. Huffman BANANA in 9 bits instead of 42 Huffman coding's saving over 7-bit ASCII.
  3. Huffman Codes are read from the root of a tree Huffman codes come from the tree.
  4. Run length Can make data with no runs bigger Every run of one becomes a pair.
  5. Lossy MP3 removes sounds most people cannot hear MP3 is lossy.
  6. Run length Replaces a run of values with a count That is run length encoding.
  7. Run length 5W 2B 6W 3B Run length pairs: a count and a value.
  8. Huffman Frequent characters get the shortest codes That is Huffman coding.
  9. Lossy The original cannot be recovered Lossy compression is irreversible.
  10. Huffman Join the two lowest frequencies first That is how a Huffman tree is built.
  11. Run length Suits graphics with blocks of flat colour Long runs compress well.