Algorithm Search⌘K

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 Σ\Sigma be a finite alphabet and let DD be a data set: a finite multiset of strings over Σ\Sigma. Let NN be the total number of characters in DD. A piece is a string over Σ\Sigma of length at least 2.

Direct tokenization. A vocabulary is S=Σ∪PS = \Sigma \cup P for a set PP of KK pieces, so ∣S∣=∣Σ∣+K|S| = |\Sigma| + K. Each string xx of DD is tokenized by a shortest sequence of members of SS whose concatenation is xx, and the cost of SS is the sum of these lengths over DD, with multiplicity. The decision problem asks: given DD, KK and δ\delta, is there a vocabulary with KK pieces and cost at most δ\delta?

Bottom-up tokenization. A merge is a pair ⟨s1,s2⟩\langle s_1, s_2 \rangle of tokens. Applying it to a token sequence replaces, from left to right, every occurrence of s1s_1 followed by s2s_2 with the single token s1s2s_1 s_2. Each string of DD starts as its sequence of characters, and the merges of a sequence m=(m1,…,mK)m = (m_1, \ldots, m_K) are applied in order. The cost of mm is the total number of tokens left in DD. The decision problem asks: given DD, KK and δ\delta, is there a merge sequence of length KK with cost at most δ\delta?

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 δ\delta.

The hypotheses matter. In both proofs the alphabet grows with the input: its size is 2J+12J + 1 (direct) or 2J+22J + 2 (bottom-up) for a max-2-SAT instance with JJ 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 II clauses with two literals each over JJ variables and a number ψ\psi, whether some assignment satisfies at least ψ\psi 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 DD is never used, and DD has fewer than N2N^2 distinct substrings. So a certificate only needs to list the useful pieces of a vocabulary, and if KK 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 NN merges matter. A certificate then has size polynomial in NN. For a vocabulary, the cost of one string xx of length nn is a shortest path in its lattice. Positions are fenceposts 0,…,n0, \ldots, n, and x[i:j]x[i{:}j] is the edge i→ji \to j, present when x[i:j]∈Sx[i{:}j] \in S. The program d[0]=0d[0] = 0, d[j]=min⁡i(d[i]+1)d[j] = \min_i (d[i] + 1) over the edges into jj runs in the TROPICAL semiring (min, +). It takes O(n2)O(n^2) substring tests per string, each O(n)O(n) time with a hash set, so the check is polynomial. For a merge sequence, apply the KK 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 ⊚\circledcirc and, for each variable XjX_j, two symbols xjTx_j^T and xjFx_j^F. A literal XjX_j becomes xjTx_j^T and a literal ¬Xj\neg X_j becomes xjFx_j^F. The data set has three parts:

  • D1D_1: the strings ⊚xjT⊚\circledcirc x_j^T \circledcirc and ⊚xjF⊚\circledcirc x_j^F \circledcirc for each jj, each ff times;
  • D2D_2: the string ⊚xjT⊚xjF⊚\circledcirc x_j^T \circledcirc x_j^F \circledcirc for each jj, f′f' times;
  • D3D_3: the string ⊚L1⊚L2⊚\circledcirc L_1 \circledcirc L_2 \circledcirc once for each clause (L1∨L2)(L_1 \lor L_2).

Set K=JK = J, f′=4I+1f' = 4I + 1, f=4f′J+4I+1f = 4f'J + 4I + 1 and δ=(4f+3f′)J+5I−2ψ\delta = (4f + 3f')J + 5I - 2\psi. If an assignment satisfies at least ψ\psi clauses, take the piece ⊚xjT⊚\circledcirc x_j^T \circledcirc when XjX_j is true and ⊚xjF⊚\circledcirc x_j^F \circledcirc when it is false. Each pair in D1D_1 then costs 1+31 + 3 tokens, each string in D2D_2 costs 3, and a clause string costs 3 if one of its literals is true and 5 if not. The total is at most (4f+3f′)J+3ψ+5(I−ψ)=δ(4f + 3f')J + 3\psi + 5(I - \psi) = \delta. For the converse, the large weights ff and f′f' force an optimal vocabulary to spend its JJ pieces on exactly one of ⊚xjT⊚\circledcirc x_j^T \circledcirc and ⊚xjF⊚\circledcirc x_j^F \circledcirc for each jj: any other choice loses more on D1D_1 or D2D_2 than all of D3D_3 can gain. Such a vocabulary is an assignment, and a cost of at most δ\delta means that at least ψ\psi clause strings cost 3, so at least ψ\psi 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 K=8JK = 8J 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 J=2J = 2 and the four clauses (X1∨X2)(X_1 \lor X_2), (¬X1∨X2)(\neg X_1 \lor X_2), (X1∨¬X2)(X_1 \lor \neg X_2) and (¬X1∨¬X2)(\neg X_1 \lor \neg X_2), at most ψ=3\psi = 3 clauses can be satisfied. Then f′=17f' = 17, f=153f = 153, 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 δ\delta for ψ=3\psi = 3 and more than δ=1,338\delta = 1{,}338 for ψ=4\psi = 4. The best vocabulary is {⊚x1T⊚,⊚x2T⊚}\{\circledcirc x_1^T \circledcirc, \circledcirc x_2^T \circledcirc\}, the assignment with both variables true.

Consequences

  • Greedy BPE is not optimal (theorem, by example). Take DD = "abc" twice, "cbcb" three times and "bcb" twice, 24 characters, and K=2K = 2. 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 K=1K = 1, the cost of a merge ⟨a,b⟩\langle a, b \rangle is NN 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 KK, the bottom-up problem has at most NKN^K merge sequences to try, since each step has at most NN adjacent token pairs, and each try is checked in polynomial time. The direct problem has at most (N2K)\binom{N^2}{K} vocabularies built from substrings. So the hardness needs KK to be part of the input.
  • Bounded alphabets. The reductions above use an alphabet that grows with JJ. 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.
  1. 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)
  2. 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)
  3. 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)
  4. 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
  5. 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.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW0 stub
  • the lead paragraph