2.2 Data Compression
⌨️AP Computer Science Principles Unit 2 Review
2.2 Data Compression
⌨️ AP Computer Science Principles
TLDR
Data compression shrinks the number of bits needed to store or send a file. Lossless compression lets you rebuild the original file exactly, while lossy compression throws out some data to save more space but only gives back an approximation. On the AP Computer Science Principles exam, you mainly compare the two and pick the better one for a given situation.
Data compression falls under the Data big idea, so expect scenarios like sending a photo, storing a medical scan, or streaming audio where you decide which type of compression fits.
Choosing Between Lossless and Lossy Compression
- Data compression reduces the number of bits used to store or transmit data.
- Fewer bits does not always mean less information, and how much you save depends on both redundancy in the data and the algorithm you use.
- Lossless compression usually shrinks the file while guaranteeing a complete, exact rebuild of the original.
- Lossy compression can shrink files more than lossless, but you only get back an approximation.
- Choose lossless when exact reconstruction or quality matters most; choose lossy when minimizing size or transfer time matters most.
What Data Compression Is
Digital files can get large fast, and big files are slow to send and expensive to store. Data compression is the process of reducing the size, meaning the number of bits, of stored or transmitted data.
How much you can shrink a file depends on two things:
- the amount of redundancy, or repeated information, in the original data
- the compression algorithm you apply
Many compression methods work by spotting patterns or repeated data and representing them in a shorter way. An important idea to remember: fewer bits does not automatically mean you lost information. A well-designed method can store the same information using less space.
How Compression Reduces Size (Example)
A simple way to see compression in action is run-length encoding, where repeated values get replaced with a count and the value. For example, the string "FFFFFIIIIIIVVVVVVVEEEE" could be stored as something like 5F6I7V4E, which uses far fewer characters while keeping all the original information.
This is an example of a compression approach, not required AP content. It is just a helpful way to picture how redundancy gets removed. The exam focus is on comparing categories of compression, not memorizing specific named algorithms.
Lossless vs. Lossy Data Compression
Lossless Data Compression
Lossless compression reduces file size without throwing away any of the original data. Because nothing is lost, you can always rebuild the original file exactly.
Pick lossless when quality or exact reconstruction matters most. As applications of this idea, lossless methods fit situations like:
- files where a tiny change could alter the meaning of the data
- medical or satellite images, where small differences can matter a lot
- software downloads, since a program has to be recreated exactly to run correctly
Lossy Data Compression
Lossy compression sacrifices some data to achieve more size reduction than lossless can. It often does this by dropping details, such as replacing similar colors in a photo with a single color. When you decompress, you get an approximation of the original, not an exact copy.
Pick lossy when minimizing size or transmission time matters most. Many lossy changes are hard for a typical viewer or listener to notice, which is why lossy methods are common for photos, audio, and video, especially when fast transfer matters.
Quick Comparison
| Feature | Lossless | Lossy |
|---|---|---|
| Original data kept | All of it | Some is removed |
| Reconstruction | Exact copy | Approximation only |
| Typical size reduction | Smaller savings | Usually larger savings |
| Best when | Quality or exact rebuild matters | Small size or fast transfer matters |
Keep in mind you do not always have to choose only one. Many real compression systems combine approaches.
Picking Lossless or Lossy for a Scenario
MCQ:
- Read the scenario and find the priority. Is the question stressing exact quality and reconstruction, or small size and fast transfer?
- If exact reconstruction matters, lean lossless. If minimizing size or transmission time matters, lean lossy.
- Expect questions that test whether you know fewer bits does not mean less information. A smaller file can still hold everything.
- Watch for trade-off language. Lossy usually wins on size, lossless wins on guaranteed exact rebuilding.
Common Trap:
- Do not assume compression always loses quality. Lossless keeps everything.
- Do not assume the most compressed file is always best. The right choice depends on the context in the question.
Common Misconceptions
- Misconception: Compression always loses data.Actually: Only lossy compression loses data. Lossless rebuilds the original exactly.
- Misconception: Fewer bits means less information.Actually: Not true. Removing redundancy can shrink the file while keeping all the information.
- Misconception: Lossy compression ruins quality.Actually: Often the loss is hard to notice, which is why lossy is fine for most everyday photos, audio, and video.
- Misconception: One algorithm is always best.Actually: The best choice depends on whether the situation values exact reconstruction or smaller size and faster transfer.
- Misconception: Compression amount depends only on the algorithm.Actually: It also depends on how much redundancy exists in the original data.
Related AP Computer Science Principles Guides
Practice Questions for 2.2
1 of 10
The flowchart above describes an algorithm used to compress a list of words by processing each word individually.
Which of the following statements best describes the nature of this compression algorithm?
A.
It is a lossy compression algorithm because the original words cannot always be perfectly reconstructed from the output.
B.
It is a lossless compression algorithm because the original data can be reconstructed by reversing the truncation process with a lookup table.
C.
It is a lossless compression algorithm because the algorithm follows a consistent rule that can be applied to any word without exception.
D.
It is a lossy compression algorithm because some words lose characters during the truncation process, making the output shorter than the input.
Vocabulary
The following words are mentioned explicitly in the AP® course framework for this topic.Term | Definition |
|---|---|
bit | Shorthand for binary digit; the smallest unit of data in computing, represented as either 0 or 1. |
data compression algorithms | Methods or procedures used to reduce the number of bits needed to represent data while maintaining or approximating the original information. |
lossless data compression | A compression algorithm that reduces the number of bits stored or transmitted while guaranteeing complete reconstruction of the original data. |
lossy data compression | A compression algorithm that significantly reduces the number of bits stored or transmitted but only allows reconstruction of an approximation of the original data. |
redundancy | Repetition or unnecessary duplication in data representation that can be reduced through compression. |
See Fiveable more often in Google
Mark Fiveable as a preferred source and Google is more likely to show you our guides in Top Stories, with a "preferred" badge in AI Mode and AI Overviews.