Theoremresearch
NP-hardness of optimal tokenization
Also called tokenisation is NP-complete, optimal BPE hardness.
Deciding whether K new pieces or K merges can shrink a corpus to a given number of tokens is NP-complete, so the optimization is NP-hard and greedy BPE is a heuristic.
Statement
Let be a finite alphabet and let be a data set: a finite multiset of strings over . Let be the total number of characters in . A piece is a string over of length at least 2.
Direct tokenization. A vocabulary is for a set of pieces, so . Each string of is tokenized by a shortest sequence of members of whose concatenation is , and the cost of is the sum of these lengths over , with multiplicity. The decision problem asks: given , and , is there a vocabulary with pieces and cost at most ?
Bottom-up tokenization. A merge is a pair of tokens. Applying it to a token sequence replaces, from left to right, every occurrence of followed by with the single token . Each string of starts as its sequence of characters, and the merges of a sequence are applied in order. The cost of is the total number of tokens left in . The decision problem asks: given , and , is there a merge sequence of length with cost at most ?
Theorem 1 (Whittington, Bachmann and Pimentel 2025, Theorem 1). The direct tokenization decision problem is NP-complete.
Theorem 2 (Whittington, Bachmann and Pimentel 2025, Theorem 2). The bottom-up tokenization decision problem is NP-complete.
Corollary. The optimization versions (find a vocabulary, or a merge sequence, of minimum cost) are NP-hard: an algorithm for either one answers the decision problem by comparing the minimum cost with .
The hypotheses matter. In both proofs the alphabet grows with the input: its size is (direct) or (bottom-up) for a max-2-SAT instance with variables. The theorems say nothing about a fixed alphabet such as the 256 bytes; Kastreva et al. treat that case (see Tightness).
In plain words
A tokenizer gets a budget: it may add a fixed number of new pieces to its alphabet, or a fixed number of merge steps. The aim is to write a text with as few tokens as possible. The theorem says that this aim is as hard as the hardest search problems that can be checked quickly. If someone hands you a vocabulary, you can count its tokens fast. But to find the best vocabulary, no fast method is known, and one would give fast methods for thousands of other hard problems.
So byte-pair encoding, which always merges the most frequent pair next, is a quick rule of thumb. It can miss the best answer even on a tiny text, as the example below shows.
Proof
The proof has two parts for each theorem: the problem is in NP, and max-2-SAT reduces to it. Max-2-SAT asks, for a set of clauses with two literals each over variables and a number , whether some assignment satisfies at least clauses. Garey, Johnson and Stockmeyer (1976) proved it NP-complete.
Membership in NP (Lemmas 1 and 5 of the source). A piece that is not a substring of a string in is never used, and has fewer than distinct substrings. So a certificate only needs to list the useful pieces of a vocabulary, and if is at least the number of substrings the answer is found by taking them all. Likewise, a merge that applies nowhere changes nothing, and each useful merge removes at least one token, so at most merges matter. A certificate then has size polynomial in . For a vocabulary, the cost of one string of length is a shortest path in its lattice. Positions are fenceposts , and is the edge , present when . The program , over the edges into runs in the TROPICAL semiring (min, +). It takes substring tests per string, each time with a hash set, so the check is polynomial. For a merge sequence, apply the merges in order; each pass is linear in the current length.
Hardness of direct tokenization (Reduction 1 and Lemmas 2 to 4 of the source). Given a max-2-SAT instance, use a separator symbol and, for each variable , two symbols and . A literal becomes and a literal becomes . The data set has three parts:
- : the strings and for each , each times;
- : the string for each , times;
- : the string once for each clause .
Set , , and . If an assignment satisfies at least clauses, take the piece when is true and when it is false. Each pair in then costs tokens, each string in costs 3, and a clause string costs 3 if one of its literals is true and 5 if not. The total is at most . For the converse, the large weights and force an optimal vocabulary to spend its pieces on exactly one of and for each : any other choice loses more on or than all of can gain. Such a vocabulary is an assignment, and a cost of at most means that at least clause strings cost 3, so at least clauses are satisfied. The source gives this step in full in its appendix (the proof of Lemma 3). The construction has polynomial size, which proves Theorem 1.
Hardness of bottom-up tokenization (Lemmas 6 to 8 of the source). The reduction follows the same plan with a second separator symbol, five families of strings and merges, so that the merges chosen for each variable encode its value. Membership and this reduction prove Theorem 2.
A check of Reduction 1 (measurement by script). For and the four clauses , , and , at most clauses can be satisfied. Then , , and the data set has 2,026 characters. An exhaustive search over all vocabularies of two pieces finds the minimum cost 1,340, which is exactly for and more than for . The best vocabulary is , the assignment with both variables true.
Consequences
- Greedy BPE is not optimal (theorem, by example). Take = "abc" twice, "cbcb" three times and "bcb" twice, 24 characters, and . BPE first counts the pairs, without overlaps: cb occurs 8 times, bc 7 times and ab 2 times, so it merges c + b (16 tokens left), then cb + cb (13 tokens: a b c, cbcb, b cb). The merges b + c, then bc + b give a bc, c bcb and bcb, so 12 tokens. An exhaustive search confirms that 12 is the best for bottom-up tokenization with 2 merges. The best direct vocabulary of 2 pieces, {abc, bcb}, gives 10 tokens, because its pieces need not be built from earlier merges. No step of greedy BPE has a tie here. The source gives no such example; this one was found and checked by a script.
- The builders are heuristics. Unless P = NP, no polynomial-time algorithm finds an optimal vocabulary or merge sequence in general. BPE, WordPiece and the unigram language model give good tokenizers in practice, but none of them is optimal for these objectives.
- Approximation. The theorems give no approximation ratio. Kozma and Voderholzer (2024) measure the compression utility (how much a pair encoding shortens the input) and show that maximizing it is APX-complete, so a polynomial-time approximation scheme is unlikely. They also show that the worst-case approximation ratio of BPE for this utility is between 1/3 and about 0.4324.
- The objective is one of many. The theorems concern the number of tokens. A tokenizer is also judged by the quality of the model trained on it, which these results do not cover.
Tightness
- One merge is easy (theorem). For , the cost of a merge is minus the number of left-to-right replacements it makes, so the best single merge is the pair with the highest count without overlaps. That is the first step of greedy BPE when it counts pairs in this way.
- A fixed budget is polynomial (theorem). For a constant , the bottom-up problem has at most merge sequences to try, since each step has at most adjacent token pairs, and each try is checked in polynomial time. The direct problem has at most vocabularies built from substrings. So the hardness needs to be part of the input.
- Bounded alphabets. The reductions above use an alphabet that grows with . Kastreva, Whittington, Komm and Pimentel (2025) prove that both problems stay NP-complete over a binary alphabet, and are APX-hard there, and that direct tokenization is NP-complete even over a unary alphabet.
- Other objectives. The source notes that similar results for other objective functions remain open.
History
Byte-pair encoding began as a compression method (Gage 1994), and Sennrich, Haddow and Birch (2016) used its greedy merges to build subword vocabularies for translation. In 2024, two groups gave hardness proofs at about the same time: Whittington, Bachmann and Pimentel (arXiv 2412.15210, published at ACL 2025) proved both the direct and the bottom-up decision problems NP-complete by reductions from max-2-SAT, and Kozma and Voderholzer (arXiv 2411.08671) independently proved hardness for the bottom-up case and studied how well BPE approximates the optimum. Kastreva et al. (2025) removed the need for a growing alphabet.
References
- Philip Whittington, Gregor Bachmann and Tiago Pimentel. Tokenisation is NP-complete. Proceedings of ACL 2025 (long papers), pages 28133 to 28153. doi:10.18653/v1/2025.acl-long.1365. arXiv 2412.15210.
- László Kozma and Johannes Voderholzer. Theoretical analysis of byte-pair encoding. arXiv 2411.08671, 2024.
- Violeta Kastreva, Philip Whittington, Dennis Komm and Tiago Pimentel. Tokenisation over bounded alphabets is hard. arXiv 2511.15709, 2025.
- Michael R. Garey, David S. Johnson and Larry Stockmeyer. Some simplified NP-complete graph problems. Theoretical Computer Science 1 (1976), 237 to 267. doi:10.1016/0304-3975(76)90059-1.
- Rico Sennrich, Barry Haddow and Alexandra Birch. Neural machine translation of rare words with subword units. ACL 2016. doi:10.18653/v1/P16-1162.
- Philip Whittington, Gregor Bachmann and Tiago Pimentel, Tokenisation is NP-complete, ACL 2025 (long papers), pages 28133 to 28153, arXiv 2412.15210, Definition 1 (max-2-SAT), Definitions 2 to 5 (the direct and bottom-up problems), Theorem 1 with Lemmas 1 to 4 and Reduction 1 (Section 5.2), Theorem 2 with Lemmas 5 to 8 (Section 6.2), footnote 2 (Kozma and Voderholzer)
- László Kozma and Johannes Voderholzer, Theoretical analysis of byte-pair encoding, arXiv 2411.08671, 2024 (preprint), the abstract (APX-completeness of the pair encoding problem and the bounds on the approximation factor of BPE)
- Violeta Kastreva, Philip Whittington, Dennis Komm and Tiago Pimentel, Tokenisation over bounded alphabets is hard, arXiv 2511.15709, 2025 (preprint), the abstract (binary and unary alphabets)
- Michael R. Garey, David S. Johnson and Larry Stockmeyer, Some simplified NP-complete graph problems, Theoretical Computer Science 1 (1976), 237 to 267, the NP-completeness of max-2-SAT
- Rico Sennrich, Barry Haddow and Alexandra Birch, Neural machine translation of rare words with subword units, ACL 2016, the section on byte pair encoding (the greedy merge learning algorithm)
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W0 stub |
|
Tag 81MQ79 Edit this page History Report an error Markdown