OCR H446 · A Level Computer Science · ~12 min read
Notes
—
Video
—
Slides
—
Worksheet
—
Quiz
Why Compress Data?
Compression reduces the size of a file or data stream by encoding information more efficiently. It is used to:
Reduce storage requirements (smaller files take up less disk space).
Reduce transmission time (smaller files transfer faster over networks).
Reduce bandwidth usage (less data sent = lower network costs).
Enable streaming (e.g. video/audio streamed in real time must be small enough to transmit as it plays).
There are two fundamentally different approaches: lossless and lossy compression.
Lossless Compression
In lossless compression, the original data is perfectly reconstructed after decompression — no data is lost. The compressed file, when decompressed, is identical (bit-for-bit) to the original.
Used when every bit of data matters: text files, executable programs, source code, spreadsheets, database files.
Cannot achieve as high a compression ratio as lossy compression for media files.
Examples of lossless formats: ZIP, PNG, GIF, FLAC (audio), PDF (usually).
Run-Length Encoding (RLE)
Run-Length Encoding is a simple lossless algorithm that replaces consecutive repeated values with a count and the repeated value.
Example: The string AAABBBBCCDDDDDD becomes 3A4B2C6D — where each number is the run length (count) of the following symbol.
RLE is efficient when data contains many consecutive repeated values (long runs).
Most effective on: simple bitmap images (e.g. black-and-white fax images, simple icons), PCX image format.
Inefficient on: photographs, random data, already-compressed files — adding the count can make the file larger for short runs.
Binary RLE for images: each pixel row is encoded as runs of the same colour — e.g. 50 white 30 black 20 white instead of storing 100 individual pixel values.
Dictionary Coding (e.g. LZ77, LZW)
Dictionary coding replaces repeated sequences of symbols with a reference to a dictionary entry (an index number pointing to a previously seen pattern). The most common variants are LZ77 and LZW, which underpin ZIP and GIF compression.
LZW (Lempel–Ziv–Welch): builds a dictionary as it reads the data. Repeated sequences are replaced with their dictionary index.
Example: in a file with many repetitions of "the", "and", "computer", each occurrence after the first is replaced by a short numeric code.
The dictionary is rebuilt from the compressed data during decompression — so it doesn't need to be stored separately.
Huffman Coding
Huffman coding assigns shorter bit codes to more frequent symbols and longer codes to less frequent symbols, reducing the total number of bits needed.
A Huffman tree is built: the most frequent characters are near the root (short path = short code); least frequent are at the leaves (long path = longer code).
Used in JPEG, MP3, ZIP, and many other formats.
In standard ASCII, every character takes 8 bits. In Huffman coding, a frequent letter like 'E' might take only 3 bits while a rare letter like 'Z' takes 10 bits — overall fewer bits if the frequency distribution is skewed.
This is a variable-length encoding scheme.
Lossy Compression
In lossy compression, some data is permanently discarded — typically data that humans are unlikely to notice is missing. The original file cannot be perfectly reconstructed from the compressed version.
Achieves much higher compression ratios than lossless compression.
Used for media (images, audio, video) where perfect accuracy is less important than file size.
Repeated lossy compression degrades quality further each time.
Converts each block from RGB colour to a frequency representation using Discrete Cosine Transform (DCT).
High-frequency detail (fine edges and textures) is discarded more aggressively than low-frequency information (broad areas of similar colour) — the human eye is less sensitive to high-frequency variation.
A quality slider controls how much detail is discarded — lower quality = smaller file = more visible artefacts.
Lossy Audio Compression: MP3
Uses perceptual audio coding — discards sounds the human ear is unlikely to perceive.
Louder sounds mask quieter sounds at similar frequencies (auditory masking) — the quieter sounds are discarded.
Very high and very low frequencies beyond normal hearing range are also discarded.
The result is a file roughly 10× smaller than uncompressed WAV/AIFF audio with minimal perceived quality loss.
Comparing Lossless vs Lossy
Feature
Lossless
Lossy
Data after decompression
Identical to original (bit-perfect)
Approximation — some data permanently lost
Compression ratio
Lower (2:1 to 4:1 typical)
Higher (10:1 to 100:1 for video)
Use cases
Text, programs, databases, archives
Images (JPEG), audio (MP3), video (MP4)
Repeated compression
No quality degradation
Each cycle further degrades quality
Examples
ZIP, PNG, FLAC, GIF
JPEG, MP3, MP4, AAC
Compression Ratio
The compression ratio measures how much a file has been reduced:
Compression ratio = Original size ÷ Compressed size
Example: a 10 MB file compressed to 2 MB has a compression ratio of 10 ÷ 2 = 5:1.
Exam tip: Know both lossless and lossy compression with examples and use cases. Be able to apply RLE to a given sequence and calculate the result. State clearly WHY each is appropriate for different file types.
Exam tip: Know that RLE is most effective when there are many consecutive repeated values (long runs). If the data has short, varied runs, RLE can make the file larger. This is a common exam question.
⚠ Common Mistakes
Saying lossy compression removes 'unimportant' data — be more precise: for images, it removes HIGH-FREQUENCY information (fine detail) the human eye is less sensitive to; for audio, it removes sounds that are MASKED by louder sounds or outside the audible range.
Confusing PNG and JPEG — PNG is LOSSLESS (exact reproduction); JPEG is LOSSY. A scanned document should be PNG; a photo can be JPEG.
Saying GIF uses lossy compression — GIF uses LZW which is lossless, but GIF is limited to 256 colours, which IS a form of quality reduction when converting from higher-colour images. GIF itself is lossless within its 256-colour constraint.
✓ Notes completed!
▶
Video coming soon
Click to advance · Arrow keys also work
Click slide or press arrow keys to navigate
✍
Worksheet — 1.3.1a Compression
8 questions · 20 marks · instantly marked
Q1Define 'lossless compression' and give TWO examples of file formats that use it.[2 marks]
✓ Mark scheme
Lossless compression reduces file size while allowing the original data to be perfectly reconstructed (bit-for-bit identical) when decompressed — no data is lost [1]; examples (any 2): ZIP, PNG, GIF, FLAC, BMP (uncompressed is not compression — so accept ZIP/PNG/GIF/FLAC) [1].
Q2Apply Run-Length Encoding (RLE) to the following string and show the result: WWWWWBBWWWWWWWW. State how many characters are used before and after RLE.[3 marks]
✓ Mark scheme
RLE result: 5W2B8W (or equivalent: 5W, 2B, 8W) — 5 white, 2 black, 8 white [1]; original: 15 characters (W W W W W B B W W W W W W W W); after RLE: 6 tokens (three count-value pairs: 5W, 2B, 8W), representing 6 characters or 3 pairs [1]; compression achieved — original 15 characters reduced to 6 [1].
Q3Describe the key principle of Huffman coding and explain why it achieves compression.[3 marks]
✓ Mark scheme
Huffman coding assigns shorter bit codes to more frequently occurring symbols and longer bit codes to less frequent symbols [1]; a Huffman tree is built from symbol frequencies — the most frequent symbols are near the root (short path/short code) and least frequent symbols are at the leaves (longer path/longer code) [1]; compression is achieved because the total number of bits to represent the data is reduced when frequent symbols are encoded with fewer bits — saving more than the extra bits used for rare symbols costs [1].
Q4Explain why lossy compression is acceptable for music files (e.g. MP3) but NOT acceptable for a text document or program executable.[3 marks]
✓ Mark scheme
For music: the human ear cannot perceive all frequencies and cannot detect sounds masked by louder sounds — MP3 discards this imperceptible information; the perceived quality loss is minimal while file sizes are dramatically reduced (roughly 10× smaller than WAV) [1]; for text/programs: every single bit must be preserved exactly — changing one character in a document alters the meaning; changing one bit in an executable program can corrupt it and prevent it from running or cause incorrect behaviour [1]; therefore lossless compression is required for text and programs because they cannot tolerate any data loss, while lossy is suitable for media where the human perception threshold means lost data is not noticed [1].
Q5A file is 24 MB before compression and 3 MB after. Calculate the compression ratio. State whether this is more likely to be lossless or lossy compression, with a justification.[3 marks]
✓ Mark scheme
Compression ratio = 24 ÷ 3 = 8:1 [1]; this is more likely lossy compression [1]; because an 8:1 compression ratio is higher than is typically achievable with lossless compression (which typically achieves 2:1 to 4:1 for most data); such a high ratio requires discarding some data permanently — this level of compression is achievable with lossy techniques like JPEG (images) or MP3 (audio) [1].
Q6Explain how RLE can actually make a file LARGER rather than smaller. Give an example of data for which this would occur.[2 marks]
✓ Mark scheme
RLE can make a file larger when there are very few repeated consecutive values — each unique character becomes 1 + count stored as two values instead of one [1]; example: ABCDEFGHIJ — each character appears once, so RLE gives 1A1B1C1D1E1F1G1H1I1J = 20 characters instead of 10; or a photo where adjacent pixels are rarely the same colour [1].
Q7State TWO reasons why compression is important for streaming video over the internet.[2 marks]
✓ Mark scheme
Any 2 from: the data must be transmitted fast enough to play smoothly in real time — uncompressed video would require far more bandwidth than typical internet connections provide [1]; bandwidth costs money — compressing video reduces the amount of data transferred, reducing costs for both the streaming provider and user [1]; compressed video loads faster, with shorter buffering times [1]; storage of video libraries would be impractical without compression — a single hour of uncompressed HD video would be hundreds of gigabytes [1].
Q8Explain how dictionary coding (such as LZW) achieves compression. Include in your answer what a 'dictionary' is in this context and how repeated sequences are handled.[2 marks]
✓ Mark scheme
A dictionary in LZW is a lookup table mapping sequences of characters to short numeric codes, built dynamically as the data is read [1]; when a sequence is seen again, it is replaced by its numeric dictionary code — because the code is shorter than the sequence it represents, the data takes fewer bits to store; the dictionary is reconstructed during decompression so it does not need to be transmitted separately [1].
Topic Quiz
1 of 15
You scored
out of 15
🎯
Mini Test — 1.3.1a Compression
10 questions · 10 marks · 10 minutes
5 MCQ + 5 short answer
⏱10:00
10 marks
Section A — Multiple Choice
Q1Which of these is a lossless compression format?
Q2What does Run-Length Encoding replace in data?
Q3In Huffman coding, which characters receive the shortest bit codes?
Q4A file is 50 MB before compression and 10 MB after. What is the compression ratio?
Q5MP3 achieves compression by discarding:
Section B — Short Answer
Q6Apply RLE to: RRRRRGGGGG. What is the result?
Mark scheme5R5G — original is 10 characters; RLE result is 4 characters (5R5G). Compression achieved. [1 mark]
Q7State why lossy compression is unsuitable for compressing program executables.
Mark schemeChanging even a single bit in a program executable could corrupt it, causing it to crash, behave incorrectly, or not run at all. Every bit must be preserved exactly, so only lossless compression is acceptable. [1 mark]
Q8Give one reason why compression is important for streaming video over the internet.
Mark schemeUncompressed video requires far more bandwidth than typical internet connections can provide to stream smoothly in real time; compression reduces the data rate so video can be transmitted fast enough to play without buffering. [1 mark]
Q9Give one disadvantage of lossy compression compared to lossless compression.
Mark schemeThe original data cannot be perfectly reconstructed — data is permanently lost. Repeated lossy compression of the same file degrades quality further each time. Not suitable when exact data preservation is required. [1 mark]
Q10State when RLE is most effective and when it is least effective.
Mark schemeMost effective: when data contains many consecutive repeated values (long runs), e.g. simple black-and-white bitmaps, fax images, icons. Least effective: when data has short, varied runs with few repetitions, e.g. photographs or already-compressed data — RLE can make the file larger in these cases. [1 mark]