AP Computer Science Principles · Data
Data compression
1Why compress?
Data compression reduces the number of bits needed to store or transmit data. Smaller files:
- take less storage space;
- transfer faster over networks and use less bandwidth.
The amount of reduction depends on how much redundancy (repetition or predictability) the data contains and on the compression method.
Check yourself
Compression works best on data that:
Show the answer
Contains a lot of repetition
Redundancy can be removed.
2Lossless compression
Lossless compression reduces size without losing any information: the original can be reconstructed exactly.
Run-length encoding (RLE) replaces runs of the same value with the value and a count.
a row of pixels: W W W W W W B B W W W W.
- Encoded: 6W 2B 4W.
- 12 symbols become 3 pairs.
- Decoding gives back exactly the original row.
Other lossless methods replace repeated patterns with shorter codes (e.g. a dictionary of common words).
Use lossless when every bit matters: text documents, program files, financial records, medical images.
Run-length encoding stores each run as a count and a value
Check yourself
Using run-length encoding, AAAABBBCCD becomes:
Show the answer
4A3B2C1D
Count then value.
3Lossy compression
Lossy compression gets much smaller files by permanently removing some information, usually details people are unlikely to notice:
- Images: reducing colours, merging similar pixels.
- Audio: removing sounds outside human hearing or masked by louder sounds.
- Video: storing only changes between frames.
The original cannot be recovered exactly. Use lossy when small size matters more than perfect quality: streaming video, photos on websites, music on phones.
Check yourself
Which situation is most suited to lossy compression?
Show the answer
Streaming a video on a phone
Small loss acceptable.
4Choosing a method
- Need exact reconstruction → lossless.
- Need the smallest possible size, small quality loss acceptable → lossy.
- Lossy usually gives a much greater reduction than lossless.
Compressing data that is already compressed (or random) usually gives little or no further reduction.
Check yourself
A photographer must keep every detail of an image for printing large posters. They should use:
Show the answer
Lossless compression
Every detail matters.
5Going deeper: how lossless and lossy methods work, and choosing between them
Compression reduces the number of bits used to store or send data. Fewer bits mean less storage and faster transmission.
The amount of reduction depends on two things: how much redundancy (repetition, predictability) the original contains, and the method used.
Lossless compression: reduces size with no loss of information. The original can be reconstructed exactly.
- It works by finding and encoding patterns and repetition.
- Used where every bit matters: text, program code, spreadsheets, medical images, archives.
- Typical formats: ZIP, PNG, GIF, FLAC.
Run-length encoding: replace a run of repeated values with a count and the value.
compress AAAAAABBBCCCCCCCCD.
- Runs: six A, three B, eight C, one D.
- Encoded: 6A3B8C1D.
- 18 characters become 8.
Worked example (when it backfires): compress ABCDEF.
- No runs: 1A1B1C1D1E1F.
- 6 characters become 12: larger.
- Run-length encoding helps only when the data contain long runs, as in simple graphics with large areas of one color.
Dictionary methods: frequently repeated strings are stored once in a table and replaced by short codes. In a long text, "the" and "and" each become a single short code. Frequency-based codes give the commonest symbols the shortest bit patterns.
Lossy compression: reduces size by discarding some information permanently. Only an approximation of the original can be rebuilt.
- It removes detail people are least likely to notice: subtle color changes the eye cannot see, sounds masked by louder ones or outside the range of hearing.
- It achieves much greater reduction than lossless methods.
- Used for photographs, music, video and streaming.
- Typical formats: JPEG, MP3, AAC, MP4.
- The more it is compressed, the lower the quality: blocky images, tinny sound. Saving a lossy file again and again degrades it further.
Choosing
- When quality or the ability to reconstruct the original matters most: lossless.
- When minimizing size or transmission time matters most: lossy.
Worked example (choose): which method suits each?
- A novel's text: lossless (changing characters would corrupt the words).
- Holiday photos to share on social media: lossy (small files; slight loss is not noticed).
- A program's source code: lossless.
- A film streamed to a phone on a slow connection: lossy.
- A scan a doctor will use for diagnosis: lossless.
- A master recording a musician will keep editing: lossless; the copy released for streaming: lossy.
Measuring compression
a 12 MB image is compressed to 3 MB.
- Compression ratio = 12 ÷ 3 = 4 to 1.
- Space saved = 9 ÷ 12 = 75%.
Worked example (bits): an image of 100 × 100 pixels uses 24 bits per pixel.
- Uncompressed: 10,000 × 24 = 240,000 bits = 30,000 bytes.
- Reducing to 8 bits per pixel (256 colors) gives 80,000 bits: one-third of the size, with some colors lost. That is a lossy change.
Limits
- Not everything compresses well. Data with no patterns, such as random numbers, hardly shrink at all.
- Data already compressed cannot usefully be compressed again.
- Compressing and decompressing take processing time.
Why it matters: without compression, streaming video and music, video calls and sharing photographs would be impractically slow; and storage would fill many times faster.
Common mistakes
- Saying lossy compression can be reversed.
- Saying lossless compression always makes files smaller.
- Choosing lossy for text or code.
- Saying lossless gives the smallest files; lossy usually compresses much more.
- Confusing compression with encryption.
Check yourself
Which statement about lossy compression is true?
Show the answer
It usually achieves smaller files than lossless compression, but the original cannot be fully restored
Information is discarded permanently in exchange for a greater reduction.
6For top marks
- Define both types using the key idea: can the original be reconstructed exactly?
- Justify a choice with the context: quality needs versus size and speed.
- Show RLE working on a short example, and know it can make data larger if there are few repeats (ABCD → 1A1B1C1D).
- Trade-off language: "reduces file size at the cost of some quality".
"A messaging app should send photos with lossy compression to save mobile data, but send documents losslessly so no words are changed."
Check yourself
Why might RLE make the string ABCDEF larger?
Show the answer
There are no runs, so each character gains a count
1A1B1C1D1E1F.
Now test yourself
This lesson has a 8-question quiz and 5 exam-style questions with mark schemes in Impera. Free, with your progress saved.
Practise in Impera