Computer Science / Huffman Coding & Compression
FREE TO EXPLORE
LABORATORY 16 · COMPUTER SCIENCE

Huffman Coding & Compression lab report

A worked huffman coding & compression lab report with benchmark data tables, asymptotic graphs, calculations, uncertainty and conclusions.

WORKED EXAMPLE · BENCHMARK OBSERVATIONS

A complete example laboratory report.

Simulated demonstration data.

This report illustrates formal algorithmic benchmarking, operation counting arithmetic, and regression analysis. Replace its observations with your own benchmark telemetry when preparing a practical submission.

Student: __________________Student ID: __________________Course: __________________Date: __________________Partner: __________________Instructor: __________________

1. Title and aim

Empirical Analysis of Optimal Prefix Coding: Huffman Coding, Shannon Entropy, Min-Heap Priority Queues, Prefix Property Invariants, and Lossless Data Compression Asymptotics. To investigate the theoretical foundations and microarchitectural mechanics of optimal lossless prefix coding, evaluate David Huffman's greedy tree construction via Min-Heap priority queues against Shannon entropy limits H(X), verify instantaneous decodability and the Kraft-McMillan inequality, and empirically demonstrate the linear compression scaling law B(n) = L_bar · n + B_0 = 2.5000 · n + 64.0000 bits across input stream sizes n ∈ [500, 50000] symbols.

2. Abstract

Seven benchmark trials were generated across raw symbol stream lengths n = 500 to n = 50000 symbols (uncompressed 8-bit ASCII size from 4000 bits to 400000 bits; compressed bitstream B from 1314 to 125064 bits). An ordinary least-squares regression of compressed bitstream size against input symbol count yielded an empirical average code length of 2.5000 bits/symbol and a canonical header intercept of 64.0000 bits, demonstrating exact agreement with David Huffman's optimal prefix coding model B(n) = 2.5·n + 64 (difference = 0.0000%). The empirical telemetry confirms that Huffman coding compresses redundant 8-bit ASCII streams by 68.75%, approaching the theoretical Shannon source entropy limit H(X) = 2.4464 bits/symbol while guaranteeing instantaneous, ambiguity-free prefix decoding.

3. Apparatus and fixed settings

4. Theory and graph choice

Data compression is a fundamental pillar of modern computing, telecommunications, and digital infrastructure. In 1948, Claude Shannon published 'A Mathematical Theory of Communication', establishing the theoretical limits of lossless data compression through the concept of Information Entropy: H(X) = -∑ p_i log₂(p_i) bits per symbol. Shannon proved that no lossless encoding scheme can represent a memoryless information source with an average code length shorter than its entropy. Four years later, in 1952, MIT graduate student David A. Huffman solved the open problem of constructing an optimal prefix code in his landmark paper 'A Method for the Construction of Minimum-Redundancy Codes'. Huffman's algorithm utilizes a greedy bottom-up strategy powered by a Min-Heap priority queue: starting with |Σ| individual character leaf nodes weighted by frequency, the algorithm repeatedly extracts the two lowest-frequency nodes, merges them into a composite parent node whose weight is their sum, and reinserts the parent back into the heap until a single rooted binary tree remains in O(|Σ| log |Σ|) time. Assigning binary 0 and 1 digits to the branches yields variable-length codewords where frequently occurring characters receive short bit sequences and infrequent characters receive longer sequences. Crucially, the resulting code satisfies the Prefix Property (no codeword is a prefix of any other), guaranteeing instantaneous, ambiguity-free bitstream decoding without lookahead or backtracking. While basic ASCII encodes every character using a uniform 8 bits (imposing 8n bits per stream), Huffman coding condenses structured streams down to its average length L_bar bits/symbol plus a minimal codebook header overhead B_0 bits. When combined with Lempel-Ziv sliding-window substitution (LZ77), Huffman coding forms the core entropy engine of modern compression formats including DEFLATE (RFC 1951), Gzip, ZIP, PNG, and JPEG. In this laboratory investigation, students benchmark bitstream compression across text lengths n ∈ [500, 50000], verify the exact linear scaling law B(n) = 2.5000 · n + 64.0000 bits via ordinary least-squares regression, contrast Huffman coding with Run-Length and Arithmetic coding, and analyze SIMD bit-packing bottlenecks in modern CPU architectures.

B(n) = \bar{L} · n + B_0 = 2.5000 · n + 64.0000 bits

The mathematical regression fits the linear model y = mx + b, where x = n represents the input stream length in symbols, y = B represents total compressed bitstream size in bits, m represents the average prefix code length L_bar, and b represents the canonical codebook header table overhead B_0 (theoretically B_0 = 64.0000 bits). The gradient has physical units bits/symbol.

5. Method

  1. Initialize the test bench and generate a stationary text stream of designated length n symbols drawn from the calibrated 8-symbol vocabulary with average code length L_bar = 2.5000 bits/symbol.
  2. Profile symbol frequencies f(c) using the Frequency Profiler, instantiate a leaf node for each distinct symbol, and insert all |Σ| = 8 nodes into the Min-Heap priority queue.
  3. Execute the Huffman greedy tree construction loop: repeatedly pop the two minimum-weight nodes from the heap, create an internal parent node with weight equal to their sum, link the children as 0 (left) and 1 (right), and push the parent back into the heap until a single root node remains.
  4. Traverse the completed Huffman binary tree from root to leaves via depth-first search to generate the binary codeword table. Verify the Prefix Property: confirm that no generated codeword is a prefix of another.
  5. Feed the raw text stream into the Bitstream Bit-Packer, concatenate the variable-length codewords into a continuous binary stream, prepend the B_0 = 64-bit canonical header table, and record the exact total compressed bitstream length B(n).
  6. Repeat the benchmarking protocol across input lengths n ∈ [500, 1000, 2000, 5000, 10000, 20000, 50000] symbols. Perform ordinary least-squares linear regression of B(n) against n to extract the empirical average code length L_bar and header intercept B_0.

6. Observations and calculation table

All benchmark observations use strictly controlled memory buffers. Values are recorded deterministically from the algorithmic comparator register. Residuals are computed relative to the ordinary least-squares regression line.

Table 1. Seven simulated algorithmic benchmarks; x and y represent computational state metrics.
No.Raw input text stream length n (symbols) (symbols)Compressed bitstream size B(n) (bits)Raw input text stream length n (symbols)Compressed bitstream length B(n) = 2.5000 · n + 64.0000 (bits)Fitted yResidual
15001314500.00001314.00001314.00000.0000
2100025641000.00002564.00002564.00000.0000
3200050642000.00005064.00005064.00000.0000
45000125645000.000012564.000012564.00000.0000
5100002506410000.000025064.000025064.00000.0000
6200005006420000.000050064.000050064.00000.0000
75000012506450000.0000125064.0000125064.00000.0000

Download the sample observations (CSV) ↓

7. Worked calculation for one observation

Raw Input Stream Length n = 500 symbols; Alphabet |Σ| = 8 symbols; Fixed-Width ASCII Size = 8 × 500 = 4000 bits. Compressed Bitstream Size: B(n) = L_bar · n + B_0 = 2.5000 × 500 + 64 = 1314 bits (Compression Savings: 67.15%). Verification: The Huffman encoder emits an average of L_bar = 2.5000 bits per input symbol across the 500 stream symbols (1250 payload bits). Adding B_0 = 64 bits for the 8-entry canonical codebook header yields strictly 2.5·n + 64 = 1314 bits.

8. Graph, gradient and final calculation

-17248.50-5440.0022970.259905.0063189.0025250.00103407.7540595.00143626.5055940.00Raw input text stream length n (symbols)Compressed bitstream length B(n) = 2.5000 · n + 64.0000 (bits)n = 500n = 1000n = 2000n = 5000n = 10000n = 20000n = 50000

Circular markers represent empirical benchmark observations. The solid line represents the ordinary least-squares asymptotic linear fit.

x̄ = 12642.85714; ȳ = 31671.14286
Σ(x − x̄)² = 1.9114e+09
Σ(x − x̄)(y − ȳ) = 4.7784e+09
m = Σ(x − x̄)(y − ȳ) / Σ(x − x̄)² = 2.50000000
b = ȳ − mx̄ = 64.00000000
L_bar = m = 2.5000 bits/symbol; Intercept B_0 = 64.0000 bits (theoretical L_bar = 2.5000, B_0 = 64.0000)

9. Uncertainty and comparison

Percentage difference = |estimate − theoretical value| / |theoretical value| × 100 = 0.0000%.

For n = 7 benchmark observations, Σresidual² = 0.00000; s² = Σresidual²/(n − 2). Gradient standard error = √[s²/Σ(x − x̄)²] = 2.2873e-11. Intercept standard error = √[s²(1/n + x̄²/Σ(x − x̄)²)] = 4.7590e-07.

Propagating the fit parameters yields a fit standard error of 2.2873e-11 bits/symbol, with an ideal coefficient of determination R² = 1.000000.

In discrete information theory and algorithm analysis, the compressed bitstream size B(n) = L_bar · n + B_0 = 2.5000·n + 64.0000 bits for stationary memoryless sources is an exact deterministic mathematical invariant. Unlike continuous physical instrumentation, there is no analog sensor noise, thermal fluctuation, or calibration drift in measuring discrete binary bit counts emitted by software encoders. However, in physical semiconductor microprocessors, wall-clock compression and decompression throughput are modulated by microarchitectural and memory hierarchy phenomena: (1) Bit-accumulator register spills and byte alignment: CPUs manipulate 32-bit and 64-bit integer words natively, requiring bitwise shift (`<<`), OR (`|=`), and conditional flush instructions to assemble variable-length codewords into memory buffers, introducing instruction-level serialization; (2) Branch predictor mispredictions in decoding loops: bit-by-bit tree traversal evaluates conditional branch instructions on unpredictable 0/1 bits, stalling superscalar execution pipelines by 15–20 clock cycles per misprediction; (3) Cache locality of lookup decoding tables (LUTs): table-driven decoders replace tree walking with direct array lookups (e.g. 10-bit LUTs), which achieve near-100% L1 data cache hits (~1 ns latency) and boost decompression throughput by 4× to 8×; and (4) Vector SIMD parallel bit manipulation: modern x86-64 and ARM processors leverage AVX2/AVX-512 vector bit-gather (`PEXT`, `PDEP`, `VPERMB`) instructions to encode and decode multiple symbol streams concurrently. Ordinary least-squares linear regression of observed compressed bit counts against stream length n across inputs from 500 to 50,000 symbols yields an empirical average code length L_bar = 2.5000 bits/symbol and header intercept B_0 = 64.0000 bits with zero residual variance (R² = 1.00000), rigorously verifying the linear entropy scaling bound with mathematical certitude.

10. Result and discussion

L_bar = 2.5000 bits/symbol
Theoretical reference value: 2.5000 bits/symbol

The empirical benchmark observations provide rigorous quantitative verification of David Huffman's minimum-redundancy prefix coding algorithm and Shannon's Source Coding Theorem. For an information source over an 8-symbol vocabulary with probability distribution P = {0.35, 0.20, 0.15, 0.10, 0.08, 0.06, 0.04, 0.02}, the theoretical Shannon source entropy is H(X) = -∑ p_i log₂(p_i) = 2.4464 bits/symbol. The Min-Heap greedy tree construction iteratively merges the two lowest-probability subtrees, synthesizing an optimal full binary trie with codeword lengths l = [2, 2, 3, 3, 4, 4, 5, 5] bits. The resulting average code length L_bar = ∑ p_i · l_i = 2.5000 bits/symbol strictly satisfies Shannon's fundamental entropy bound: H(X) ≤ L_bar < H(X) + 1, with minimal redundancy R = L_bar - H(X) = 0.0536 bits/symbol due to integer bit constraints. The regression slope m = 2.5000 confirms that each additional input symbol consumes exactly 2.5000 bits on average, while the intercept b = 64.0000 bits exactly reflects the 64-bit canonical codebook header table. Across all trials, the regression exhibits zero residual variance (R² = 1.00000). Comparative analysis across compression paradigms illuminates fundamental algorithmic trade-offs: Fixed-width 8-bit ASCII requires 8n bits per stream, yielding zero compression. Run-Length Encoding (RLE) compresses repeating consecutive runs but expands non-repeating diverse text. Huffman coding operates optimally on stationary symbol statistics, guaranteeing instantaneous O(B) decoding via the Prefix Property without lookahead. Lempel-Ziv dictionary algorithms (LZ77/LZ78) capture multi-character string repetitions by emitting (distance, length) offset tokens. In industry-standard formats such as DEFLATE (used in Gzip, ZIP, and PNG), LZ77 and Huffman coding are cascaded: LZ77 eliminates string redundancies, and Huffman coding compresses the resulting literal and distance token alphabets. Finally, Arithmetic Coding eliminates the integer bit constraint of Huffman coding by mapping messages to fractional intervals, achieving compression closer to H(X) at the cost of higher CPU arithmetic overhead.

11. Precautions and limitations

The simulated execution environment models idealized scalar CPU bitstream packing where each symbol emits its exact theoretical Huffman codeword length L_bar = 2.5000 bits. In physical microprocessor hardware, observable compression latency and memory bus throughput are modulated by CPU L1/L2 cache locality of translation tables, branch mispredictions during bitstream bit-testing loops, bit-accumulator flush boundaries (32-bit vs 64-bit words), and vectorization capabilities of hardware SIMD units. In the Huffman lossless compression benchmark across raw text stream lengths n in [500, 50000] symbols generated under a standardized 8-symbol stationary probability distribution (entropy H(X) = 2.45 bits/symbol, theoretical average Huffman code length L_bar = 2.5000 bits/symbol), each incoming symbol is translated into its variable-length prefix code. Across n independent identically distributed symbols, total emitted payload bits scale deterministically as 2.5000 · n bits. Adding B_0 = 64.0000 bits for the canonical codebook header (storing symbol bit lengths for 8 symbols at 8 bits per length) yields the exact linear relation B(n) = 2.5000 · n + 64.0000 bits. Ordinary least-squares regression across all benchmark trials yields an ideal coefficient of determination R² = 1.000000 with zero residual variance.

12. Conclusion

This laboratory investigation quantitatively verified the greedy tree construction, Kraft-McMillan invariants, and asymptotic compression scaling of Huffman prefix coding. Through systematic benchmarking across stream sizes from n = 500 to 50000 symbols, we verified the empirical average code length L_bar = 2.5000 bits/symbol and canonical header intercept B_0 = 64.0000 bits with an ideal R² = 1.00000. The investigation established the microarchitectural efficiency and theoretical optimality of Min-Heap tree synthesis, canonical codebook serializations, and table-driven bitstream decoders, proving why Huffman coding remains the universal entropy coding engine underpinning modern digital telecommunications, multimedia formats, and global internet data transmission.