{
  "slug": "re-pair",
  "term": "Re-Pair compression",
  "aliases": [
    "RePair",
    "Recursive pairing"
  ],
  "definition": "Re-Pair is an offline grammar compressor that repeatedly replaces a most frequent adjacent symbol pair with a new symbol and records its expansion rule.",
  "note": "Mechanism: After each replacement round, pair frequencies are recomputed on the reduced sequence; new symbols can participate in later pairs. The decoder needs both the remaining sequence and the recursively defined rules. [larsson1999]\n\nOverlap: Replacing aa in aaaa produces two A symbols, not three. The teaching model counts disjoint occurrences for each candidate pair and replaces them left to right; aaa alone supplies only one such occurrence. [larsson1999] [example]\n\nTies: Equal-frequency pairs need a selection policy. Different choices can produce different grammar sizes; Re-Pair is not a unique canonical encoding. The finite example aaabaabaab yields 9 versus 8 right-hand-side symbols under the documented minimum/maximum-ID policies. [furuya2020] [example]\n\nStorage: In the teaching wire format, abababab becomes two rules and two root symbols: 6 grammar symbols, but 24 encoded bytes including the header. This deliberately simple encoding is not the original paper's compact dictionary and entropy coder. [example]",
  "related": [
    "grammar-compression",
    "compression-dictionary",
    "byte-pair-encoding"
  ],
  "status": "research",
  "advantages": [],
  "supersedes": [],
  "supersededBy": [],
  "firstSeen": null,
  "lastSeen": null,
  "updated": "2026-11-17",
  "limitations": [
    {
      "text": "The model rescans small inputs and does not implement the papers' efficient data structures or establish their time and memory bounds.",
      "citationIds": [
        "example",
        "bille2016"
      ]
    }
  ],
  "citations": [
    {
      "id": "larsson1999",
      "title": "Larsson and Moffat: Offline Dictionary-Based Compression, DCC 1999, sections 2-4",
      "url": "https://avadeaux.net/larsson.dogma.net/dcc99.pdf",
      "accessed": "2026-11-17"
    },
    {
      "id": "furuya2020",
      "title": "Furuya et al.: Practical Grammar Compression Based on Maximal Repeats, Algorithms 13(4), 103 (2020), sections 2-5",
      "url": "https://www.mdpi.com/1999-4893/13/4/103",
      "accessed": "2026-11-17"
    },
    {
      "id": "bille2016",
      "title": "Bille, Gortz and Prezza: Space-Efficient Re-Pair Compression, arXiv:1611.01479v1 (2016), sections 1-3",
      "url": "https://arxiv.org/pdf/1611.01479v1",
      "accessed": "2026-11-17"
    },
    {
      "id": "example",
      "title": "Hesela: Re-Pair rule, stream and encoded-byte accounting, executable teaching model",
      "url": "https://hesela.dev/research/repair-grammar/README.md",
      "accessed": "2026-11-17"
    }
  ],
  "provenance": [
    "Source-grounded algorithm explanation and constructed byte examples; no production throughput, fleet metric or benchmark replication.",
    "Research status identifies the algorithm literature, not an assertion that implementations do not exist."
  ]
}
