Chapter 16 of 22 Part IV: Information Processing

Making Big Things Small

Each project below is tailored to a specific algorithm and scientific question from Chapter 16, with concrete inputs, verifiable numerical outputs, boundary cases, and links to the related lab and pioneer.

Project 1 of 3 · v1-c16-p01 Foundational · ~20 min

Run-Length Encoding (RLE) Compressor & Expansion Test

Lab activity: 01 | Run-length encoding · Related code: block-01.js, block-04.js, block-07.js · Pioneer: View profile

Real-World Problem: Simple line drawings and fax scans contain long runs of identical pixels that waste storage if stored raw.
Scientific Question: When does Run-Length Encoding shrink a file, and when does it actually make the file larger?

Objective: Implement lossless RLE encoding/decoding and compare compression ratios on repetitive vs. random strings.

Hypothesis to Test: RLE shrinks long repeated runs dramatically but expands alternating strings like "ABABABAB" up to 2×.

Step-by-Step Implementation

  1. Write rleEncode(str) outputting [count, symbol] pairs and rleDecode(pairs) reconstructing exact text.
  2. Verify round-trip identity rleDecode(rleEncode(s)) === s on 5 test strings.
  3. Measure compression ratio on repetitive, English prose, and alternating inputs.
  4. Connect expansion on random inputs to the pigeonhole principle.
Concrete Input: Input 1: "AAAAAAAAAABBBBBCCCC" (19 chars); Input 2: "ABCDEFGHIJ" (10 chars).
Expected Output: Input 1 -> "10A5B4C" (7 chars, 63% saved); Input 2 -> "1A1B1C1D1E1F1G1H1I1J" (20 chars, +100% expansion).

Test Cases: "" -> []; "AAAA" -> [[4,"A"]]; "ABCD" -> 4 pairs (expansion).

Boundary Condition: Digit characters inside raw text require tuple/binary framing so counts are not confused with data digits.

Common Mistake to Avoid: Encoding raw text containing numbers as flat "35" (is that three 5s or thirty-five?).

Interpreting Results: No lossless compressor works equally well on every input; algorithm choice depends on data structure.

Show Hint, Extension Challenge & Reflection

Hint: Review the worked example and equation in Chapter 16 before testing the boundary case: Digit characters inside raw text require tuple/binary framing so counts are not confused with data digits.

Extension Challenge: Add an escape-run optimization that only RLE-encodes runs of length >= 3.

Reflection Question: Why does zipping an already-compressed ZIP or JPEG file usually make it slightly bigger?

Project 2 of 3 · v1-c16-p02 Intermediate · ~35 min

Huffman Prefix Tree Builder & Entropy Comparison

Lab activity: 02 | Huffman coding · Related code: block-02.js, block-05.js · Pioneer: View profile

Real-World Problem: Fixed 8-bit ASCII wastes bits by giving rare letters like Z the same length as common letters like E.
Scientific Question: How close does Huffman's greedy binary tree get to Claude Shannon's theoretical entropy limit H?

Objective: Build a Huffman tree from symbol frequencies, verify the prefix-free property, encode/decode bitstreams, and compare average bits/symbol against Shannon entropy.

Hypothesis to Test: Huffman's average code length L always satisfies H <= L < H + 1 bits per symbol.

Step-by-Step Implementation

  1. Count character frequencies and create leaf nodes.
  2. Repeatedly merge the two lowest-frequency nodes into a parent node until one root remains.
  3. Traverse left=0, right=1 to assign prefix-free bitstrings and verify no code is a prefix of another.
  4. Compare average bit length against H = -Σ p_i log2(p_i).
Concrete Input: Text: "BANANA_BANDANA" (skewed frequencies: A=6, N=4, B=2, D=1, _=1).
Expected Output: A gets 1-bit code (0); average length ≈ 2.00 bits/symbol vs. Shannon entropy H ≈ 1.96 bits/symbol (vs. 8 bits ASCII).

Test Cases: Single unique symbol ("AAAA") -> handle specially with 1-bit code "0"; Skewed text -> exact lossless round-trip.

Boundary Condition: An input with only 1 unique character produces a 1-node tree and must still assign a 1-bit code.

Common Mistake to Avoid: Forgetting that the decoder also needs the frequency table or canonical code lengths stored in the header.

Interpreting Results: Huffman coding is mathematically optimal among symbol-by-symbol prefix-free codes.

Show Hint, Extension Challenge & Reflection

Hint: Review the worked example and equation in Chapter 16 before testing the boundary case: An input with only 1 unique character produces a 1-node tree and must still assign a 1-bit code.

Extension Challenge: Compare uniform distribution ("ABCDEFGH") vs. skewed distribution and confirm uniform yields 3.00 bits/symbol.

Reflection Question: How do LZ77 (Abraham Lempel & Jacob Ziv) and Huffman work together inside DEFLATE (ZIP and PNG)?

Project 3 of 3 · v1-c16-p03 Intermediate · ~25 min

Lossy Scalar Quantization & Distortion Trade-Off

Lab activity: 03 | Quantization · Related code: block-03.js, block-04.js, block-05.js, block-06.js, block-07.js · Pioneer: View profile

Real-World Problem: Raw photographs and audio recordings are huge, so media formats discard subtle detail humans rarely notice.
Scientific Question: How does reducing bit depth (quantization levels) trade file size against mean-squared reconstruction error?

Objective: Quantize a 0–255 brightness ramp into 16, 4, and 2 levels and compute exact Mean Squared Error (MSE).

Hypothesis to Test: Halving the number of bits per sample cuts raw size by 50% while roughly quadrupling quantization step size and MSE.

Step-by-Step Implementation

  1. Map 8-bit values (0–255) into k-bit buckets and reconstruct bucket midpoints.
  2. Compute Mean Squared Error MSE = mean((orig − recon)^2).
  3. Verify that quantization is irreversible (original exact values cannot be recovered).
  4. Compare rate-distortion points across 8, 6, 4, 2, and 1 bit per sample.
Concrete Input: 8-bit ramp [0, 1, ..., 255] quantized to 4 bits (16 levels) vs. 2 bits (4 levels).
Expected Output: 4-bit: 50% smaller, small visible banding; 2-bit: 75% smaller, severe posterization and high MSE.

Test Cases: 8-bit -> MSE = 0; 1-bit -> only 2 brightness levels (max distortion).

Boundary Condition: Clamp reconstructed values strictly inside [0, 255].

Common Mistake to Avoid: Thinking lossy compression can be undone by re-saving a JPEG at 100% quality.

Interpreting Results: Rate-distortion theory quantifies the exact trade-off between bitrate and perceptual fidelity.

Show Hint, Extension Challenge & Reflection

Hint: Review the worked example and equation in Chapter 16 before testing the boundary case: Clamp reconstructed values strictly inside [0, 255].

Extension Challenge: Add simple error-diffusion dithering on 1-bit quantization and observe improved perceived smoothness.

Reflection Question: When is lossy compression appropriate (streaming video) vs. unacceptable (medical scans, source code)?