AQA 8525 GCSE Computer Science · 3.3 Fundamentals of Data Representation
Stretch tasks Data Compression, Huffman and RLE
Quiz Challenge
A quiz game against four computer players, then short and long written answers.
- Quiz Blitz12 questions against the clock
- Quick Fire4 short answers
- Big Think2 long answers
About 20 minutes
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
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
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
Which of these is lossless?
- JPEG
- MP3
- MP4
- PNG
Run length encode W W W W W B B W W W W W W B B B.
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
In Huffman coding, which characters get the shortest codes?
- Capital letters
- The most frequent
- The least frequent
- The first in the alphabet
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
How many bits does BANANA take in 7-bit ASCII?
With A = 0, N = 10 and B = 11, how many bits does BANANA need?
A Huffman code is read from the leaf up to the root.
- True
- False
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
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
Lossless compression removes redundancy in how data is stored, not the data itself.
- True
- False
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
Explain how run length encoding works, using an example. [2 marks]
Explain why a stream of Huffman codes can be decoded without any separators. [2 marks]
Compare lossy and lossless compression. [2 marks]
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
Build a Huffman tree for BANANA and use it to work out how many bits are saved compared with 7-bit ASCII. [4 marks]
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
Which suits run length encoding best?
- A detailed photograph
- A logo with flat blocks of colour
Does lossless compression remove data?
- No: it stores it more efficiently
- Yes: the least useful data
In a Huffman tree, what is a left branch usually labelled?
- 1
- 0
Which is lossy?
- ZIP
- MP3
Why compress video before streaming it?
- It improves the picture
- There is less data to send
Which is decoded by following bits down from a root?
- A run length pair
- A Huffman code
Run length encoding ABABABAB gives a result that is...
- Bigger
- Smaller
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
| Term | Meaning |
|---|---|
| A Lossy | 1 Reconstructed exactly |
| B Lossless | 2 Data sent in a given time |
| C RLE | 3 WWWWW |
| D Huffman coding | 4 Original cannot be recovered |
E 5W | 5 A value and a count for each run |
| F Bandwidth | 6 Short codes for frequent characters |
Sort It: tick one box in each row
Lossy compression, run length encoding or Huffman coding?
| Statement | Lossy | Run length | Huffman |
|---|---|---|---|
| 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
- PNG PNG and ZIP are lossless. JPEG and MP3 are lossy.
- 5W 2B 6W 3B Five W, two B, six W, three B.
- Data with no runs gives a pair for every single value ABABABAB becomes eight pairs: sixteen values instead of eight.
- The most frequent Short codes for common characters cut the total number of bits.
- The two with the lowest frequencies Joining the lowest pushes rare characters deepest, giving them the longest codes.
- 42 6 characters x 7 bits = 42 bits.
- 9 B 2 bits, three As 3 bits, two Ns 4 bits: 9 bits.
- False Read it from the root down to the leaf.
- They take less storage; They take less time to send Less storage, faster transmission and less bandwidth.
- Lossless, so nothing changes Removing characters from a program gives a broken program.
- True The original can be reconstructed exactly.
- 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 answerRun 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 answerNo 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 answerLossy 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 answerThe 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 answerThe 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 answerPhotographs 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
- A logo with flat blocks of colour RLE needs long runs of the same value. Photographs have almost none.
- No: it stores it more efficiently The original can be rebuilt exactly.
- 0 Left branches 0 and right branches 1, applied consistently.
- MP3 ZIP must give back every file exactly.
- There is less data to send Smaller files use less bandwidth and arrive faster.
- A Huffman code Each bit chooses a branch until a leaf is reached.
- Bigger Every run has length one, so the pairs double the size.
- 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
- Lossy JPEG discards fine colour detail That is lossy compression.
- Huffman BANANA in 9 bits instead of 42 Huffman coding's saving over 7-bit ASCII.
- Huffman Codes are read from the root of a tree Huffman codes come from the tree.
- Run length Can make data with no runs bigger Every run of one becomes a pair.
- Lossy MP3 removes sounds most people cannot hear MP3 is lossy.
- Run length Replaces a run of values with a count That is run length encoding.
- Run length 5W 2B 6W 3B Run length pairs: a count and a value.
- Huffman Frequent characters get the shortest codes That is Huffman coding.
- Lossy The original cannot be recovered Lossy compression is irreversible.
- Huffman Join the two lowest frequencies first That is how a Huffman tree is built.
- Run length Suits graphics with blocks of flat colour Long runs compress well.