Tokenisation is NP-Complete

Philip Whittington, Gregor Bachmann, Tiago Pimentel

Introduction

Tokenisation is the critical first step in the pipeline of Large Language Models (LLMs), where raw text is partitioned into subword units. While the efficiency of this compression directly impacts training speed and model performance, current industry standards like Byte Pair Encoding (BPE) and UnigramLM rely on greedy heuristics. This research provides a formal theoretical foundation for this practice by proving that finding the mathematically optimal tokeniser—one that achieves maximum compression for a fixed vocabulary size—is an NP-complete problem.

The Tokenisation Problem

The goal of an optimal tokeniser is to minimize the total number of tokens required to represent a dataset, subject to a constraint on the vocabulary size $K$. This problem can be framed in two primary ways:

  1. Direct Tokenisation: Selecting a vocabulary of $K$ strings such that the dataset's decomposition into these strings is minimal.
  2. Bottom-up Tokenisation: Determining an optimal sequence of $K$ merge operations, as seen in BPE.
A directed acyclic graph representing the possible tokenization paths for a string $c$.

In the direct tokenisation framework, finding the shortest path through a graph of potential segmentations (as shown above) is efficient if the vocabulary is fixed. However, the authors prove that selecting the optimal vocabulary to enable such paths is computationally intractable.

Complexity Analysis

The authors establish NP-completeness through polynomial-time reductions from the Maximum 2-Satisfiability (max-2-SAT) problem. By constructing complex "gadget" strings that encode logical clauses, the researchers demonstrate that solving the tokenisation optimization problem is equivalent to solving a known NP-hard logic puzzle.

Key Theorems

  • Theorem 1 (Direct Tokenisation): Deciding if a vocabulary of size $K$ can compress a dataset to at most $\delta$ symbols is NP-complete.
  • Theorem 2 (Bottom-up Tokenisation): Deciding if a sequence of $K$ merges can compress a dataset to at most $\delta$ symbols is NP-complete.

These results remain robust even when considering practical constraints, such as dataset deduplication or treating the input as a single continuous string.

Implications for NLP

This theoretical proof validates the current reliance on approximate algorithms. Since finding the global optimum is computationally intractable, the use of greedy methods like BPE is not a compromise of effort, but a necessary response to the fundamental complexity of the task. It shifts the research focus from seeking "perfect" tokenisers to refining heuristics and understanding their performance bounds.

Limitations and Future Work

The study primarily focuses on compression as the objective function. While compression is a strong proxy for model efficiency, it does not perfectly capture downstream linguistic performance. Furthermore, the proofs assume a variable alphabet; whether the problem remains NP-complete for fixed, small alphabets (e.g., UTF-8 bytes) remains an open question for future investigation.

Summary updated

BibTeX
@inproceedings{whittington2025tokenisation,
  author = {Philip Whittington and Gregor Bachmann and Tiago Pimentel},
  title = {{Tokenisation is NP-Complete}},
  booktitle = {Annual Meeting of the Association for Computational Linguistics},
  year = {2025},
}
Figure