Skip to content
ExtendedSource checked

Re-Pair Compression

Re-Pair Compression addresses grammar-based compression by repeatedly replacing the most frequent adjacent symbol pair.

Quick Facts

Compact metadata for implementation and discovery.

Category
Compression
Difficulty
Intermediate
Family
Grammar Compression
Tier
Extended
Status
Verified
Review
Source Checked
Sources
1
Formulas
0
Relationships
3
Code example
Ready
Visualization
Ready

Overview

Re-Pair Compression is a grammar compression method for grammar-based compression. It works by repeatedly replacing the most frequent adjacent symbol pair.

Core Concepts

grammar-based compressiongrammar compressionautomata statescompression

Quick facts

Complexity

Runtime and memory behavior at a glance.

Best

O(n)

Average

O(n)

Worst

O(n log n)

Space

O(n)

When to Use

  • Use Re-Pair Compression when solving grammar-based compression and you want to benefit from repeatedly replacing the most frequent adjacent symbol pair.
  • Choose it when O(n) average/worst-case time and O(n) memory match your workload profile.
  • Prefer it over Context Tree Weighting or Context Mixing when the problem structure favors grammar-based compression.

When Not to Use

  • Avoid when speed, random access, or worst-case expansion matters more than compression ratio.
  • Avoid when encoder and decoder cannot share the same model assumptions.

What It Does

  • Computes grammar-based compression within Grammar Compression workloads (O(n) time, O(n) space).
  • Works by repeatedly replacing the most frequent adjacent symbol pair.
  • Maintains key invariants across grammar-based compression, grammar compression, automata states.

Use Cases

  • Production and research workloads requiring grammar-based compression via repeatedly replacing the most frequent adjacent symbol pair.
  • Grammar Compression pipelines where O(n) time and O(n) space fit system constraints.
  • Engineering comparisons against Context Tree Weighting and Context Mixing when tuning grammar-based compression.

Inputs

  • Input byte or symbol stream
  • Model, dictionary, or coding parameters

Outputs

  • Compressed representation
  • Updated model or dictionary state

Pseudocode

  1. 1.Preprocess the input alphabet, pattern/grammar rules, or symbol frequency tables for Re-Pair Compression.
  2. 2.Construct the lookup automaton, suffix/shift index, or coding state (grammar-based compression, grammar compression).
  3. 3.Scan the input sequence by repeatedly replacing the most frequent adjacent symbol pair.
  4. 4.Emit matches, derivation nodes, or encoded tokens while advancing the state transition table.
  5. 5.Finalize trailing state and return the completed grammar-based compression output.

Canonical Example

Input
Input byte or symbol stream configured for a representative grammar-based compression test case
Output
Compressed representation satisfying Re-Pair Compression invariants
Explanation
Re-Pair Compression processes the input by repeatedly replacing the most frequent adjacent symbol pair, tracking grammar-based compression and grammar compression to produce the verified grammar-based compression result.

Implementation Notes

  • Keep encoder and decoder model updates byte-for-byte consistent.
  • Add round-trip tests before measuring compression ratio.
  • Document framing, dictionary reset, and end-marker behavior.

Common Pitfalls

  • Training or updating the model differently in encoder and decoder.
  • Ignoring worst-case expansion on small or already-compressed inputs.
  • Forgetting bit-level framing and end-of-stream handling.

Interactive Mechanism Walkthrough

Sequence Window & Automaton Alignment

Show repeatedly replacing the most frequent adjacent symbol pair on a small grammar-based compression example.

Input:ABABABCABAB
Window:ABAB
Step 1 of 5: Preprocess the input alphabet, pattern/grammar rules, or symbol frequency tables for Re-Pair Compression.
compression-mechanism

Notes

Re-Pair Compression operates in O(n) time and O(n) space within the Grammar Compression family. Often compared with Context Tree Weighting and Context Mixing.

Sources (1)

What each reference supports for this entry.

Data Compression: The Complete Reference

Book

Compression reference for Re-Pair Compression dictionary grammar compression and repeated-pair replacement.

Explore Next

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

Re-Pair Compression and Context Tree Weighting are practical alternatives within grammar-based compression, with different assumptions and tradeoffs.

Alternative toContext Mixing

Re-Pair Compression and Context Mixing are practical alternatives within grammar-based compression, with different assumptions and tradeoffs.

Alternative toPAQ Compression

Re-Pair Compression and PAQ Compression are practical alternatives within grammar-based compression, with different assumptions and tradeoffs.

Tags

Compression