Algorithms · Visualized

Compression: Arithmetic Coding

Compression & Encoding

Shrink data with runs, frequencies, and clever codes.

Arithmetic coding compresses a message by representing it as a single fractional number in [0,1): each symbol narrows the current interval to the symbol's probability-scaled sub-interval. Time: O(n) for a message of length n average, O(n) worst; space: O(1) for encoding state (excluding the probability model).

Big-O: Huffman gives prefix-free optimal codes.

Arithmetic Coding

Compression / Encoding

Arithmetic coding compresses a message by representing it as a single fractional number in [0,1): each symbol narrows the current interval to the symbol's probability-scaled sub-interval. Unlike Huffman coding, symbols can cost a fractional number of bits, so the code approaches the theoretical entropy limit.

How Arithmetic Coding Works

  1. Assign each symbol a probability and a sub-interval of [0,1) from its cumulative probability.
  2. Start with the full interval [0,1).
  3. For each symbol of the message, narrow the current interval to that symbol's sub-interval, scaled into the current range.
  4. After all symbols, any value inside the final narrow interval uniquely decodes back to the message.
  5. Decoding: repeatedly find which symbol's sub-interval contains the encoded value, output it, and narrow the range exactly like the encoder.

Arithmetic Coding Worked Example

Input: {msg: AB, probs: {A: 0.6, B: 0.4}}

Start with [0,1). Symbol 'A' occupies [0,0.6) → interval becomes [0,0.6). Symbol 'B' occupies the upper 40% of the current interval → [0 + 0.6·0.6, 0 + 0.6·1.0) = [0.36, 0.6). Any value in [0.36, 0.6), e.g. 0.5, uniquely represents 'AB'.

Compression: Arithmetic CodingO(n)
Shrink a message into one fractional interval — near-entropy optimal.
symbolprobabilitysub-interval
AA0.6[0.0, 0.6)
BB0.4[0.6, 1.0)
A
B
01
lo0
hi1
Step 1 / 4
Default Current / Compare Swap / Write Settled Active / Path Pivot / Reject Locked (hollow) Target
Arithmetic coding: represent the whole message as a single fractional interval, narrowing [lo, hi) symbol by symbol.
Python · Compression Algorithms in Code
def arithmetic_encode(msg, probs):
    lo, hi = 0.0, 1.0
    cum = {}
    acc = 0.0
    for ch, p in probs.items():
        cum[ch] = (acc, acc + p)
        acc += p
    for ch in msg:
        sl, sh = cum[ch]
        span = hi - lo
        lo, hi = lo + span * sl, lo + span * sh
    return (lo + hi) / 2  # any value in [lo, hi) decodes to msg

Behind the Scenes Execution

Press ▶ Run in the visualizer — the call stack, registers and memory will replay here in sync with the code, line by line.

Arithmetic Coding Time & Space Complexity

BestO(n) — one step per symbol
AverageO(n) for a message of length n
WorstO(n)
SpaceO(1) for encoding state (excluding the probability model)

When to Use Arithmetic Coding

  • Image and video compression (JPEG2000, H.264 CABAC).
  • General-purpose compressors (bzip2, the PAQ family).
  • Any scenario needing near-entropy-optimal compression.

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits

Keep the momentum going

Step through a related structure, pattern or course — the ideas reinforce each other and stick faster.