Data compression (lossy vs. lossless)

Computer Science · Foundations

Question 1 of 10

0

    What is the purpose of the "dictionary" in a dictionary-based compressor such as LZ77 or LZW?

    Stuck? Read the explanation

    Unlocks at a streak of 5 correct answers in a row.

    Print a free worksheet (PDF)

    Embed this practice on your site or class page

    Paste this HTML into your page. Students practise right there; it is free and needs no account.

    Data compression (lossy vs. lossless) explained

    Data compression reduces the number of bits needed to store or send information. Lossless compression keeps every bit of the original, so the exact original can be rebuilt. Lossy compression discards detail judged to matter less, such as sounds a listener is unlikely to notice.

    Lossless methods suit text, program code and spreadsheets, where one changed character matters. Lossless formats include PNG for images, ZIP for general files and FLAC for audio. Lossy formats such as JPEG for photographs and MP3 for music are much smaller, but discarded detail cannot be recovered.

    Run-length encoding (RLE) is a simple lossless method. It replaces each run of repeated values with a count and the value. It works well on long runs, but it can enlarge data with few repeats. Huffman coding is another lossless method. It gives frequent symbols short codes and rare symbols longer codes, so the average code length falls.

    The compression ratio compares original and compressed sizes. A ratio of 4:1 means the compressed file is one quarter of the original size.

    Worked example

    Compress the string AAAAABBBCCDDDD with RLE, using count then value.

    1. Count the runs: A appears 5 times, B 3 times, C 2 times and D 4 times.
    2. Write each count followed by its letter: 5A3B2C4D.
    3. The original is 14 characters and the output is 8, so the ratio is 148=1.75\frac{14}{8} = 1.75, or 1.75:1.

    Decoding 5A3B2C4D gives back the original exactly, so the method is lossless.

    What you will practise

    104 multiple-choice questions (Easy, Medium, Hard). Each answer is followed by an explanation.

    Example questions

    • Which type of compression lets the original file be restored exactly, bit for bit?
    • Which of these is a lossy image format commonly used for photographs?
    • Which of these is a lossless audio format?
    • What is the main purpose of data compression?
    • Using count-then-value notation, what is the run-length encoding of AAAA?
    • Using count-then-value notation, what is the run-length encoding of XXXYYY?
    • A photograph is saved as a JPEG at a low quality setting. What kind of compression has been applied?
    • A file is compressed with a ratio of 4:1. Which statement is correct?

    …and 96 more.