← Back to course

Data Compression

You’ll be able to

Why compress data

Data compression reduces the number of bits needed to store or send information. Smaller data means less storage space, faster transmission over a network, and lower cost. The catch is captured by a central question: can we get the original data back exactly? The answer splits compression into two families, and choosing between them is a trade-off between how small the data gets and how much fidelity is preserved.

Lossless: nothing lost

Lossless compression shrinks data in a way that allows the exact original to be perfectly reconstructed. It works by removing redundancy — for example, replacing a long run of repeated values with a short code that says "repeat this 200 times." Because no information is discarded, lossless is required whenever every bit matters: program files, text documents, spreadsheets, and formats like PNG and ZIP. The downside is that it generally cannot compress as much as the alternative.

Lossy: smaller, but permanent

Lossy compression achieves much smaller sizes by permanently discarding some information — details judged least noticeable to human senses. JPEG images, MP3 audio, and streaming video use it. The original can never be fully recovered, but for photos and music a well-chosen loss is nearly imperceptible while cutting size dramatically. The trade-off is direct: lossy gives smaller files but lower fidelity; lossless preserves everything but stays larger. Never use lossy where exactness matters.

Compression ratio
compression ratio = original size / compressed size
A ratio of 4:1 means the compressed data is one-quarter the original size. Equivalently, space saved = (original − compressed) / original.
Worked example

A 2,000 KB image is compressed to 500 KB. Find the compression ratio and the percentage of space saved.

  1. 1.Compression ratio = original / compressed = 2000 / 500 = 4, i.e. a 4:1 ratio.
  2. 2.Space saved = original − compressed = 2000 − 500 = 1500 KB.
  3. 3.Percentage saved = 1500 / 2000 = 0.75 = 75%.
Answer: The compression ratio is 4:1 (the file is now one-quarter its original size), and 75% of the space has been saved.
Checkpoint

A programmer needs to compress an executable software file so it downloads faster, and the file must run correctly afterward. Which type of compression must be used, and why?

Watch out

The deciding question is always: must the exact original be recoverable? If yes (text, code, data), use lossless. If some loss is acceptable (photos, music, video), lossy trades fidelity for much smaller size.

Checkpoint

A 3,000 KB file is compressed to 750 KB. What is the compression ratio?

On the exam

Remember the trade-off in one line: lossless = exact but larger; lossy = smaller but permanent loss. For a compression ratio, divide original by compressed — a bigger ratio means a smaller file.

Answer the 2 checkpoints as you read.

Sign in to save your progress