Data
Compression
Learn how computers reduce file sizes while preserving the information that matters: the core principles behind lossless and lossy compression.
What Is Data Compression?
Data compression is the process of encoding information using fewer bits than the original representation. It allows us to store more data in less space and transmit data faster over networks.
Why Compression Matters
Redundancy: The Key Insight
Compression works because most data contains redundancy: patterns, repeated values, or information that can be predicted. The more redundancy, the more a file can be compressed.
Two Types: Lossless vs. Lossy
Key Distinctions at a Glance
| Lossless | Lossy | |
|---|---|---|
| Data after decompression | Identical to original | Approximation of original |
| Typical compression ratio | 2:1 to 8:1 | 10:1 to 50:1 or more |
| File formats | PNG, ZIP, FLAC, GIF | JPEG, MP3, MP4, AAC |
| Best for | Text, code, medical images | Photos, music, video |
| Reversible? | Yes | No |
A photograph survives two different round trips
Fine feather edges and smooth color gradients reveal what survives compression. Lossless decoding restores the input values exactly; lossy decoding can change them.Compare equal-size decoded images
The same 64-by-64 RGB crop passes through both processes. Lossless decoding restores every channel value; the lossy quantizer changes values while preserving the decoded dimensions. Compressed byte count and decoded pixel count measure different things.Measuring Compression: Compression Ratio
We quantify how effective compression is using the compression ratio:
A related metric is the space savings percentage:
How to Analyze Compression
Lossless Technique: Run-Length Encoding
Run-Length Encoding (RLE) replaces consecutive identical values with a single value and a count. It works best on data with long runs of the same value.
Lossless Technique: Huffman Encoding
Huffman Encoding assigns shorter bit codes to more frequent characters and longer codes to rare ones. Unlike fixed-length encoding (where every character uses the same number of bits), Huffman produces variable-length codes.
Count the run lengths and code bits
Lossy Compression in Practice
Lossy compression exploits human perception limits. We cannot hear every frequency in audio or see every pixel variation in an image, so those details can be discarded.
Track the decoded values
Example 1: Calculating Compression Ratio
A text file is 800 KB uncompressed. After applying ZIP compression, it becomes 200 KB. Find the compression ratio and space savings.
Example 2: Applying RLE
Encode the following 21-character string using Run-Length Encoding:
Example 3: Lossy Quality Tradeoff
A photographer has a 24 MB raw image. They save it at different JPEG quality levels:
Common Mistakes to Avoid
Compression Summary
| Concept | Key Detail | |
|---|---|---|
| Compression Ratio | \(\frac{\text{Original}}{\text{Compressed}}\) | Higher = smaller file |
| Run-Length Encoding | Lossless; counts consecutive identical values | Best for data with long runs |
| Huffman Encoding | Lossless; variable-length codes by frequency | Used inside ZIP, PNG, etc. |
| JPEG / MP3 | Lossy; removes imperceptible detail | Much higher compression ratios |
| Metadata | Data about data (headers, tags) | Not removed by compression |
Question 1 of 5
A 12 MB audio file is compressed to 3 MB using MP3 encoding. What is the compression ratio?
Question 2 of 5
Which of the following is true about lossless compression?
Question 3 of 5
A string AAAAABBBCC is encoded using Run-Length Encoding. What is the encoded result?
5A 3B 2C. The count comes first, then the character.Question 4 of 5
A hospital stores medical scans that doctors use for diagnosis. Which type of compression should be used?
Question 5 of 5
In Huffman encoding, which characters receive the shortest bit codes?