Skip to content
CoreSource checked

LZ77

Compresses repeated substrings by referring back to earlier bytes in a sliding window.

Quick Facts

Compact metadata for implementation and discovery.

Category
Compression
Difficulty
Intermediate
Family
Dictionary Compression
Tier
Core
Status
Verified
Review
Source Checked
Sources
1
Formulas
1
Relationships
8
Code example
Not ready
Visualization
Ready

Overview

LZ77 scans input while maintaining a window of recently seen data. When the next bytes match earlier bytes, the encoder emits a distance and length instead of repeating the literal bytes.

Core Concepts

sliding windowdictionary referencedictionary compressionsymbol representationlossless decodingstream encoding

Quick facts

Complexity

Runtime and memory behavior at a glance.

Best

O(n)

Average

O(nw)

Worst

O(nw)

Space

O(w)

When to Use

  • Use when repeated sequences tend to appear near each other.
  • Use as a dictionary stage before entropy coding.

When Not to Use

  • Avoid expecting strong compression on random or encrypted data.
  • Avoid when match searching is too expensive for the latency budget.

What It Does

  • Maintains a sliding history window
  • Finds the longest useful previous match
  • Emits literals or length-distance references

Use Cases

  • Lossless byte compression
  • Preprocessing before entropy coding
  • Data with local repeated substrings

Inputs

  • Byte or symbol stream
  • Window size

Outputs

  • Literals and back-references

Key Formulas (1)

Back-reference token

token = (offset, length, nextsymbol)
token = (offset, length, next_symbol)

Represents a repeated phrase by pointing backward into the sliding window.

Pseudocode

  1. 1.Initialize an empty sliding window
  2. 2.At the current input position, find the best match in the window
  3. 3.If the match is long enough, emit distance and length
  4. 4.Otherwise emit the next literal byte
  5. 5.Advance input and update the window

Canonical Example

Input
'ABCABCABC'
Output
literal 'ABC' plus a reference to repeat the previous three bytes twice
Explanation
The later 'ABC' runs are encoded as backreferences into the sliding window.

Implementation Notes

  • Define maximum distance, maximum length, and minimum match length.
  • Use round-trip tests where references overlap the bytes being decoded.
  • Keep match finding separate from token encoding.

Common Pitfalls

  • Distance and length must refer only to bytes already decoded.
  • Window-size limits affect both compression ratio and decoder memory.
  • Greedy longest-match choices are not always globally optimal.

Interactive Mechanism Walkthrough

Sequence Window & Automaton Alignment

Show the lookback window and a repeated phrase turning into a reference.

Input:ABABABCABAB
Window:ABAB
Step 1 of 5: Initialize an empty sliding window
sliding-window-compression

Notes

LZ77 requires decoder state to mirror the encoder's sliding window exactly.

Sources (1)

What each reference supports for this entry.

Introduction to Data Compression

Book

Data compression textbook reference for the coding or dictionary method used by LZ77.

Explore Next

Good next jumps from the relationship graph, ranked for useful discovery.

Used byDeflate

Deflate uses LZ77-style backreferences before Huffman coding.

Alternative toLZW

LZ77 uses sliding-window references, while LZW grows a dictionary of phrases.

Related Algorithms

Tags

CompressionSliding WindowDictionary CompressionClassic