Chapter 9 · Simulation

String Compression Algorithms

← Chapter 9

Press Prepare to begin.

Compressed Output

Ratio: —
No result yet.

Runs

Runs appear here.

    String Compression Algorithms

    Run-length encoding (RLE) and Lempel-Ziv-Welch (LZW) are shown step by step.

    How to use

    • Type an input text (up to 24 characters) and choose an algorithm. Changing the algorithm loads a suitable example.
    • Prepare generates all the steps; Next Step advances one step, Auto Play plays the remaining steps, and Reset clears the run.
    • Yellow cell: the character being processed. Green cells: the current run (RLE).
    • The lines at the bottom keep the last few steps; the ratio appears when the run finishes.

    Run-length encoding (RLE)

    RLE replaces each run of identical symbols with a count-symbol pair, for example AAAAABB → 5A2B. It works well when the input has long runs; otherwise it can make the output longer.

    Lempel-Ziv-Welch (LZW)

    LZW starts with a dictionary of the single characters in the input. While scanning, it extends the current string w with the next character c. If wc is in the dictionary, w = wc; otherwise it writes the code of w to the output, adds wc as a new code, and restarts with w = c. The dictionary grows as new strings appear.

    In the dictionary panel, the green entry is the current w and the yellow entry is the code added in this step.

    Compression ratio

    Ratio = compressed units / original units. For RLE the units are the characters of the output pieces; for LZW they are the codes written.

    Complexity

    AlgorithmTimeExtra memory
    RLEO(n)O(1) besides the output
    LZWExpected O(n) (hash dictionary)O(n) dictionary