Could a black-and-white school logo take up less space without losing a pixel?
Your school has a black-and-white logo with a big white background. A bitmap records the colour of every pixel. If lots of pixels in a row are white, could you store that row in less space and still get every pixel back?
A file takes up space because it stores bits. Compression reduces the number of bits needed to represent the data. With lossless compression, you can recover the original exactly. A smaller file needs less storage and can transfer faster over the same connection.
Scan this row from left to right. You see three zeros, then two ones, then two more zeros. Each group of identical values next to one another is a run. The ones break up the zeros, so you have three separate runs.
Look at the row zero, zero, one, zero, zero. Follow it from left to right and start a new group each time the value changes. How many runs are there? Pick an answer.
Split the row into three zeros, two ones and two zeros. Run length encoding stores each run as a pair: frequency first, then data value. Read the pairs back in order: write three zeros, two ones and two zeros. Join them and you've got the original row.
Try the row one, one, one, zero, zero, one. Count the values in each run, then write each pair with the count first and the value second. Which list of pairs matches the row? Pick one.
Both rows have six pixels. Six zeros form one long run, so you need just one pair. In the alternating row, the value changes at every step, so you need six pairs. Storing all those counts takes space, and the encoded row can be larger than the original.
If an exam question asks for pairs for each row, encode row one and stop at its end. Start a fresh list for row two. Even though row one ends in two zeros and row two starts with two zeros, merging them would give a pair that spans both rows.
Start at the root. Follow zero to A. Follow one then zero to B, or one then one to C. The digits along each path give you the code. Here, the most frequent letter gets the shortest code. You save a bit each time you use A's one-bit code instead of two bits.
Write the run length encoding for each row as frequency, data pairs. In row one, the ones separate the two runs of zeros, so write three pairs. Start fresh for row two. Its first zero gets its own pair, even though row one ends in zeros.