arXiv:2610.32298cs.DSOct 2026

Manuscript Offprint

arXiv:2610.32298

Exact Algorithms and Complexity of the Smallest Grammar Problem

Xiangle Chen

Abstract

The Smallest Grammar Problem (SGP) asks for a smallest straight-line grammar generating a given string, where the size of a grammar is the total length of its right-hand sides. The main difficulty is that rules may be nested: which repeated fragments receive rules affects the value of all other fragments, so locally optimal choices need not combine into a globally optimal grammar.

Our algorithmic results start from a simple structural fact: in an optimal grammar, one can choose an occurrence of the expansion of each auxiliary nonterminal such that the chosen intervals are pairwise disjoint. Thus, although rules are nested, the entire dictionary can be read off from a single partition of the input. This disjoint-representative property decouples the choice of the dictionary, which we enumerate via partitions, from the form of the individual rules, which we compute by dynamic programming. It underlies our algorithmic results and the polynomial formulation; the hardness results rely on a separate encoding-and-projection technique.

First, combining disjoint representatives with a bound on how often dictionary strings of length two are referenced, we obtain an exact algorithm running in O∗(ρn)O^{*}(\rho^n) time and O(n)O(n) words of space, where ρ=1.8722572196…<2\rho = 1.8722572196\ldots < 2. This improves the O∗(3n)O^{*}(3^n) bound of Casel et al. for general grammars.

Second, we study the role of the alphabet size. Over a small alphabet there are few short strings, so they can be enumerated by content rather than by position. With σ≥2\sigma \geq 2 the alphabet size and R=log⁡σnR = \log_\sigma n, we give a 2O((n/R)log⁡(R+1))2^{O((n/R)\log(R+1))}-time algorithm, which is subexponential for every fixed alphabet. On the other hand, we prove that SGP remains NP-complete over a binary alphabet, answering a question of Casel et al., and we construct hard instance families of tunable alphabet size on which, under ETH, no 2o(n/R)nO(1)2^{o(n/R)}n^{O(1)}-time algorithm exists. Both bounds are governed by n/Rn/R and differ only by a factor of O(log⁡(R+1))O(\log(R+1)) in the exponent.

Third, disjoint representatives also serve as certificates. For every c>1c > 1, they yield a constant-degree integer polynomial formulation of the decision problem with O(cn)O(c^n) variables and evaluation time. By the barrier theorem of Belova, Golovnev, Kulikov, Mihajlin, and Sharipov, any proof that SGP is λn\lambda^n-SETH-hard under their deterministic fine-grained reductions, for a constant λ>1\lambda > 1, would also prove a new lower bound for Boolean series-parallel circuits or for arithmetic circuits.

Keywords. Smallest grammar problem; grammar-based compression; exact algorithms; alphabet encoding; ETH.

1 Introduction

When a string contains many repeated fragments, a straight-line grammar can define each fragment once and refer to it thereafter through a nonterminal. Since rules may refer to other rules, repetitions nested several levels deep can be represented as well. Such grammars are the basic representation in grammar-based compression and also support indexing directly on the compressed text [4]. A smallest grammar for a string T marks the limit of this form of compression and serves as the benchmark for approximation algorithms. Finding it is the Smallest Grammar Problem (SGP); throughout, the size of a grammar is the total number of symbols on the right-hand sides of its rules.

It is precisely the nesting of rules that makes exact algorithms difficult. Two useful fragments may contain one another, their occurrences in the input may overlap, and once a fragment is selected, one must still decide which shorter fragments define it. A direct enumeration of rules must therefore handle two issues at once: which dictionary to use, and how the rules depend on one another. Charikar et al. [1] studied approximation algorithms and the complexity of SGP. Casel et al. [2] showed that the problem remains NP-complete even over fixed alphabets of 24 or 17 symbols, and gave an O∗(3n)O^{*}(3^{n})-time exact algorithm for general grammars, where n=∣T∣n = |T|.

Our starting point is the following observation: although the rules of an optimal grammar may be nested, its dictionary can always be specified by pairwise disjoint intervals of the input. More precisely, for the expansion of each auxiliary nonterminal one can choose an occurrence such that the chosen representative intervals are pairwise disjoint. Taking these intervals as blocks and every remaining position as a block of its own, we can read off the whole dictionary from a partition of the input. Once the dictionary is fixed, a shortest parsing of each rule can be computed by dynamic programming. Hence, enumerating all 2n−12^{n-1} partitions and computing shortest parsings for each is guaranteed to find an optimal grammar.

This framework admits further savings. For dictionary strings of length two, we additionally exploit how often they are referenced in an optimal grammar, which reduces the enumeration enough to bring the base of the exponent below two. Disjoint representatives also suit the small-alphabet setting: short strings are enumerated by content and long strings by the positions of their disjoint occurrences, and balancing the two yields a time bound that depends on the alphabet size.

How much room for improvement do these algorithms leave? To address this question, we turn to hardness over small alphabets. Here an additional difficulty arises: once the source symbols are encoded, a target grammar may reuse fragments of codewords, so the optimum before encoding does not carry over directly. We overcome this obstacle by quantitatively comparing grammar sizes before and after encoding. This yields NP-completeness of binary SGP as well as ETH lower bounds that depend on the alphabet size. The role of the alphabet is thus visible from both sides: in the amount of enumeration performed by the algorithm, and in the encoding length of hard instances.