The Complexity of Compressed Membership Problems for Finite Automata

In this paper, a compressed membership problem for finite automata, both deterministic (DFAs) and non-deterministic (NFAs), with compressed transition labels is studied. The compression is represented by straight-line programs (SLPs), i.e. context-free grammars generating exactly one string. A novel technique of dealing with SLPs is employed: the SLPs are recompressed, so that substrings of the input word are encoded in SLPs labelling the transitions of the NFA (DFA) in the same way, as in the SLP representing the input text. To this end, the SLPs are locally decompressed and then recompressed in a uniform way. Furthermore, in order to reflect the recompression in the NFA, we need to modify it only a little, in particular its size stays polynomial in the input size. Using this technique it is shown that the compressed membership for NFA with compressed labels is in NP, thus confirming the conjecture of Plandowski and Rytter (Jewels Are Forever, pp. 262–272, Springer, Berlin, 1999) and extending the partial result of Lohrey and Mathissen (in CSR, LNCS, vol. 6651, pp. 275–288, Springer, Berlin, 2011); as this problem is known to be NP-hard (in Plandowski and Rytter, Jewels Are Forever, pp. 262–272, Springer, Berlin, 1999), we settle its exact computational complexity. Moreover, the same technique applied to the compressed membership for DFA with compressed labels yields that this problem is in P, and this problem is known to be P-hard (in Markey and Schnoebelen, Inf. Process. Lett. 90(1):3–6, 2004; Beaudry et al., SIAM J. Comput. 26(1):138–152, 1997).


Compression and Straight-Line Programms
Due to ever-increasing amount of data, compression methods are widely applied in order to decrease the data's size.Still, the stored data sometimes need to be processed and decompressing it on each occasion is wasteful, Thus there is a large demand for algorithms working directly on the compressed data.Such task is not as desperate, as it may seem: it is a popular outlook, that compression basically extracts the hidden structure of the text and if the compression rate is high, the text must have a lot of internal structure.And it is natural to assume, that such a structure will help devising methods dealing directly with the compressed representation of the data.Indeed, efficient algorithms for fundamental text operations (pattern matching, checking equality, etc.) are known for various practically used compression methods (LZ, LZW, etc.) [2,3,5].
When devising algorithms for compressed data, quite early one needs to focus on the exact compression method, to which the algorithm is applied.The most practical, and challenging, choice is one of the widely used standards, like LZW or LZ.However, a different approach was also proposed: for some applications and for most of theory-oriented considerations it would be useful to model one of the practical compression standard by a more mathematically well-founded method.This idea, among other, lay at the foundations of the notion of Straight-Line Programms (SLP), whose instance can be simply seen as context-free grammars generating exactly one string.
SLPs are the most popular theoretical model of compression.This is on one hand motivated by a simple, 'clean' and appealing definition, on the other hand, they model the LZ compression standard: each LZ compressed text can be converted into an equivalent SLP with only logarithmic overhead (and in polynomial time) while each SLP can be converted to an equivalent SLP with just a constant overhead (an in polynomial time).
The approach of modelling compression by SLP in order to develop efficient algorithms turned out to be fruitful.Algorithmic problems for SLP-compressed input strings were considered and successfully solved [9,10,16].The recent state-of-the-art efficient algorithms for pattern matching in LZ and LZW compressed text essentially use the reformulation of LZW and LZ methods in terms of SLPs [2,3].SLPs found their usage also in programme verification [4,8].Attempts were made in order to make out of the box tools for texts represented by SLPs, e.g., efficient indexing structure for SLPs was recently developed [1].
Surprisingly, while SLPs were introduced mainly as a model for practical applications, they turned out to be useful also in strictly theoretical branches of computer science, for instance, their usage was important in the famous proof of Plandowski, that satisfiability of word equations is in PSPACE [17].

Membership problem
As it should be already clear, that SLPs are used, both in theoretical, and applied research in computer science.Hence, tools for them should be developed.In particular, one should be aware, that whenever working with strings, these strings may be supplied as respective SLPs.Hence, all the usual string problems should be reinvestigated in the compressed setting, as the classical algorithms may not apply directly, be inefficient or worse, some of these problems may become computationally difficult.
From language theory point of view, the crucial questions stated in terms of strings, is the one of compressed string recognition.To be more precise, we consider classic membership problems, i.e. recognition by automata, generation by a grammar etc., in which the input is supplied as an SLP.We refer to such problems as compressed membership problems.These were first studied in the pioneering work of Plandowski and Rytter [18], who considered compressed membership problem for various formalism for defining languages.Already in this work it was observed, that we should precisely specify, what part of the input is compressed?Clearly the input string, but what about the language representation (i.e.regular expression, automaton, grammar, etc.).Should it be also compressed or not?Both variant of the problem are usually considered, with the following naming convention: when only the input string is compressed, we use a name compressed membership, when also the language representation, we prepend fully to the name.
In years to come, the compressed membership problem was investigated for various language classes [5,6,11,12,18].Compressed word problem for groups and monoids [11,14,15], which can be seen as a generalisation of membership problem, was also investigated.
Despite the large attention in the research community, the exact computational complexity of some problems remain open.The most notorious of those is the fully compressed membership problem for NFA, considered already in the work of Plandowski and Rytter [18].Here, the compression of NFA is done by allowing the transitions by strings, and only by single letters, and representing these strings as SLPs.
It is relatively easy to observe that the compressed membership problem for the NFA is in P, however, the status of the fully compressed variant remained open for a long time.Some partial results were obtained by Plandowski and Rytter [18], who observed that it is in PSPACE and is NP-hard for the case of unary alphabet, both of these bounds being relatively simple.Moreover, they showed that this problem is in NP for some particular cases, for instance, in the case of one-letter alphabet.Further work on the problem was done by Lohrey and Mathissen [13], who demonstrated that if the strings defined by SLP have polynomial periods, the problem is in NP, and when all strings are highly aperiodic, it is in P.

Our results and techniques
We establish the computational complexity of fully compressed membership problems for both NFAs and DFAs.Theorem 1. Fully compressed membership problem for NFA is in NP, for DFA it is in P.
Our approach to the problem is essentially different than the approach of Plandowski and Rytter [18] and Lohrey and Mathissen [13].The main ideas utilised in the previous approaches focused on the properties of strings described by SLPs.We take a completely different approach: we analyse and change the way strings are described by the SLPs in instance.That is, we focus on the SLPs, and not on the encoded strings.Roughly speaking, our algorithm aims at having all the strings in the instance compressed 'in the same way'.To achieve this goal, we decompress the SLPs.Since the compressed text can be exponentially long, we do this locally: we introduce explicit strings into the right-hand sides of the productions.Then, we recompress these explicit strings uniformly.Since such pieces of text are compressed in the same way, we can 'forget' about the original substrings of the input and treat the introduced nonterminals as atomic letters.The idea is that such recompression should shorten the text significantly: roughly one 'round' of recompression, in which every pair of letters that was present at the beginning of the 'round' is compressed, should shorten the encoded strings by a constant factor.It remains to implement the recompression (and changes in the NFA) in npolytime, while keeping the size of N polynomial.
We stress, that the idea of local decompression and synchronous recompression of SLP is new and promising: there is hope that it can be applied to other problem related to SLPs.

Straight line programmes
Formally, a Straight line programme (SLP) is context free grammar G over the alphabet Σ, with a language consisting of exactly one string.Usually it is assumed that G is in a Chomsky normal form, i.e. each production is either of the form X → Y Z or X → a.This assumption in particular implies, that strings defined by nonterminals have length at most 2 n ; since our algorithm will replace some substrings by shorter ones, none string defined by SLPs during the run of algorithm will exceed this length.
We denote the string defined by nonterminal A by word(A).This notion extends to word(α) for α ∈ (X ∪ Σ) * in the usual way.

Input
The instance of the fully compressed membership problem for NFA consists of an input string, represented by an SLP, and an NFA N , whose transitions may be labelled by SLPs.
For our purposes it is more convenient to assume, that all SLPs are given as a single context free grammar G with a set of nonterminals X = {X 1 , . . ., X n }, the input string is defined by X n and the NFAs transitions are labelled with nonterminals of G. Furthermore, in our proof, it is essential to drop the usual assumption, that G is in a Chomsky normal form.However, we still impose some conditions on the productions' right-hand sides.Thus, we require that the grammar during the run of algorithm satisfies the following constraints on its form: each nonterminal has exactly one production, which is of the form (1a) The strings u, v and their substrings appear explicitly in a rule, this notion is introduced to distinguish them from the substrings of word(X i ).Notice, that (1) does not exclude the case, when X i → ǫ and allowing such a possibility streamlines the analysis.
Without loss of generality we may assume that the input string starts and ends with designated, unique symbols, denoted as $ and #.These are not essential, however, the first and last letter of word(X n ) need to be treated in a somewhat special manner, furthermore, this applies to their appearances in the NFA as well.Having special symbols for the first and last letter makes the analysis smoother.

Input size, complexity classes
The size |G| of the representation of grammar G is the sum of length of the right-hand sides of G's rules.The size |N | of the representation of NFA N is the sum of number of its states and transitions.The size |Σ| of alphabet Σ is simply the number of elements in Σ.
The input (or, in general, current instance) size is polynomial in N , G, Σ and n, which denotes the number of nonterminals in G.We point out, that one of the crucial properties of our algorithm is that n is not modified during the whole run of the algorithm.
By npolytime (polytime) we denote the class of algorithms running in non-deterministic (deterministic, respectively) polynomial time, and by NP (P, respectively) the corresponding complexity classes of the decision problems.

Known results
We use the following basic result, which states that the fully compressed membership problem, when the input string is over a unary alphabet, is in NP for NFA and in P for DFA.
Lemma 1 (cf.[18,Theorem 5]).The fully compressed membership problem restricted to the input string over an alphabet Σ = {a} is in NP for NFA and in P for DFA.
The first claim can be easily inferred from the result of Plandowski and Rytter [18,Theorem 5], who proved that this problem is in NP, when Σ = {a}, i.e. also transitions in the NFA are labelled by powers of a only.The second claim is trivial.

Path and labels in NFA
Since we deal with automata, proofs will be concerned with (accepting) paths for strings.We shall consider NFAs, for which transitions are labelled with either letters, or non-terminals of G.That is, that transition relation δ satisfies δ ⊆ Q × (Σ ∪ X ) × Q.Consequently, a path P from state p 1 to p k+1 is a sequence α 1 α 2 . . .α k , where α i ∈ Σ ∪ X and δ(q i , α i , q i+1 ).We write, that P induces such a list of labels.The word(P) defined by such a path P is simply word(α 1 . . .α k ).We also say that P is a path for a string word(P).A path is accepting, if it ends in an accepting state.A string w is accepted by N if there is an accepting path from the starting state for w.

Basic classifications and outline of the algorithm
In this section we present the outline of the algorithm for fully compressed membership for NFAs.Its main part consist of recompression, i.e. replacing strings appearing in word(X n ) by shorter ones.In some cases, such replacing is harder, in other easier.It should be intuitively clear, that it depends on the position of letters inside encoded texts: if a is a first or last letter of some word(X i ), then recompressing strings including a looks difficult, as such strings can be split into different nonterminals and recompression requires heavy modification of G, or even rebuilding of the NFA.On the other hand, if a letter a is only 'inside' strings encoded by nonterminals, its compression is done only 'inside' rules of G, which seems easy.Thus, before we state the algorithm, we firstly introduce classification of letters (and strings) into 'easy' and 'difficult' to compress.

Crossing appearances, types of letters, non-extendible appearances
We say that a string w has a crossing appearance in a (string defined by) nonterminal X i with a production X i → uX j vX k , if w appears in word(X i ), but this appearance is not contained in neither u, v, word(X j ) nor word(X k ).Intuitively, this appearance 'crosses' the symbols in of u word(X j )v word(X k ),i.e, at the same time part of w is in the explicit substring (u or v) and part is in the compressed strings (word(X j ) or word(X k )).This notion is similarly defined for nonterminals with productions of the form X i → uX j v, productions of the form X i → u clearly do not have crossing appearances.
A string w has a crossing appearance in the NFA N , if there is a path in N inducing list of labels α 1 α 2 , where α 1 , α 2 ∈ {X 1 , . . ., X n } ∪ Σ with at least one of α, α 2 being a nonterminal, such that w appears in a word(αα 2 ), but this appearance is not contained in the word(α 1 ), nor in word(α 2 ).The intuition is similar as in the case of crossing appearance in a rule: it is possible that a string w is split between two transitions' labels.Still, there is nothing difficult in consecutive letter transitions, thus we treat such a case as a simple one.
We say that a pair of different letters ab is a crossing pair, if ab has a crossing appearance of any kind.Otherwise, such a pair is non-crossing.
We say that a letter a ∈ Σ is right-outer (left-outer), if there is a nonterminal X i , such that a is the leftmost (rightmost, respectively) symbol in word(X i ).A letter is outer, if it is left-outer or right-outer.Otherwise, the letter is inner.Notice, that if a pair ab is crossing, then a is left-outer or b is right-outer.The outer letters and crossing pairs correspond to the intuitive notion of 'hard' to compress.
The following lemma shows, that while G may encode long strings, they have relatively few different short substrings and few outer letters.Lemma 2. There are at most 2n different outer letters and at most |G| + 3n different pairs of letters appearing in word(X n ), . . ., word(X n ).
The set of outer letters, the set of crossing pairs and the set of non-crossing pairs appearing in word(X n ), . . ., word(X n ) can be computed in polytime.
The notions of (non-) crossing pairs do not apply to aa, still, an analog can be defined: for a letter a ∈ Σ we say that a ℓ is a a's non-extendible appearance of length ℓ, if it appears in some string defined by some nonterminal and it is surrounded by letters other than a, formally, if there exist two letters x, y ∈ Σ, where x = a = y and a nonterminal X i , such that xa ℓ y is a substring of word(X i ).Similarly to crossing pairs, it can be shown that there are not too many different non-extendible appearances of a. Lemma 3.For an inner letter a and a grammar G there are at most |G| different lengths of a's non-extendible appearances in word(X 1 ), . . ., word(X n ).The set of these lengths can be calculated in polytime.

Outline of the algorithm
Our algorithm consists of two main operations performed on strings encoded by G appearance compression of a For each a ℓ that has a non-extendible appearance in word(X n ), replace all a ℓ s in word(X 1 ), . . ., word(X n ) by a fresh letter a ℓ .Modify N accordingly.
pair compression of ab For two different letters ab replace each of ab in word(X 1 ), . . ., word(X n ) by a fresh letter c.Modify N accordingly.
We denote the string obtained from w by compression of appearances of a by AC a (w), and the string obtained by compression of a pair ab into c by P C ab→c (w).We adopt the following notational convention throughout rest of the paper: whenever we refer to a letter a ℓ , it means that the last appearance compression was done for a and a ℓ is the letter that replaced a ℓ .
The main idea behind the algorithm is that appearance compression and pair compression shorten the encoded texts significantly.The challenging part of the algorithm is the modification of NFA N 'accordingly' to the changes of SLPs.The general schema is given by Algorithm 1. for non-crossing pair ab in word(X n ), a, / ∈ {$, #} do

Algorithm 1 Outline of the main algorithm
compress ab, modify N accordingly 7: L ← list of outer letters, except $ and # 8: compress appearances of a, modify N accordingly 10: compress a ℓ b, modify N accordingly 12: Decompress X n and solve the problem naively.
There are two important remarks to be made: • there is no explicit non-deterministic operation in the code, however, it appears implicitly in the term 'modify the NFA accordingly' in lines 4 and 9. Roughly, one needs to solve fully compressed membership for string a ℓ for that, and this is known to be NP-hard.
• the compression (both of pairs and appearances) is never applied to $, nor to #.The markers were introduced so that we do not bother with strange behaviour where first or last letter is compressed, and so we do not touch the markers.
It should be more or less obvious, that the compressions performed by Algorithm 1 shortens word(X n ).This is formally stated as follows.Remark 1. Notice, that pair compression P C ab→b is in fact introducing a new nonterminal with a production c → ab, similarly AC a .Hence, Algorithm 1 creates new SLPs, encoding strings from the instance.However, these new nonterminals are never expanded, they are always treated as individual symbols.Thus it is better to think of them as letters.In particular, the analysis of running time of Algorithm 1 relies on the fact, that no new nonterminals are introduced by Algorithm 1.

Details
In this section we describe in detail how to implement the appearance compression and pair compression.In particular, we are going to formulate the connections between NFA and SLPs preserved during Algorithm 1 and demonstrate how to modify the NFA accordingly.

Invariants
The invariants below describe the connection between the grammar kept by Algorithm 1 and the input one.SLP 1 The set of used nonterminals is a subset of X = {X 1 , . . ., X n } and the productions are of the form described in (1).
SLP 2 For every production X i → α i , the original instance contained a production X i → α ′ i , where the nonterminals appearing in α appear (in the same order) in α ′ .SLP 3 The nonterminal X n has a production are not used in other productions.
The following invariants represent the constraints on the NFA.
Aut 1 every transition of N is labelled by a single letter of Σ (letter transition) or by a nonterminal (nonterminal transition), each nonterminal labels at most one transition.No transition is labelled with X n .
Aut 2 there is a unique starting state that has a unique outgoing transition, by letter $, and no incoming transitions; there is no other transition by $.Similarly, there is a unique accepting state that has a unique incoming transition, by letter #, it does not have any outgoing transitions; there is no other transition by # in N .
Algorithm 1 will preserve (SLP 1)-(Aut 2), and we shall always assume, that the input of the subroutines satisfies (SLP 1)-(Aut 2).We assume that the input instance satisfies (SLP 1)-(Aut 2), moreover, that that the input grammar is in the Chomsky normal form.It is routine to transform (in polytime) the input instances not satisfying these conditions into equivalents instance that satisfy them.

Compression of non-crossing pairs and inner letters
The compression of non-crossing pairs and appearance compression for inner letters is intuitively easy: whenever these appear in strings encoded by G or on paths in N , they cannot be split between nonterminals or between transitions.Thus, it should be enough to replace their explicit appearances in the grammar and in the NFA.This is formalised and shown in this subsection

Compression of non-crossing pairs
We first demonstrate, how to perform the pair compression for non-crossing pairs.Consider a non-crossing pair ab.Since it is non-crossing, it can only appear in the the explicit strings in the rules of G. Hence, compressing ab into a fresh letter c consists simply of replacing each explicit ab by c in right-hand side of every production.Still, ab can appear on a path in N .But since ab is non-crossing, this can be either wholly inside a nonterminal transition (and so compression was already taken care of), or on two consecutive letter transitions.This is also easy to handle: whenever there is a path from p to q by a string ab, we introduce a new letter transition by c from p to q.This description is formalised in Algorithm 2.
To distinguish between the input and output G and N , we utilise the following convention: 'unprimed' names refer to the input (like G, X i , N ), while 'primed' symbols refer to the output (like G, X ′ i , N ′ ).This convention is used for lemmata concerning algorithms through the paper.
Algorithm 2 Pair compression for a non-crossing pair ab 1: for each production replace each explicit ab in α by c if there is a path for ab from p to q then 5: put a transition δ N (p, c, q) We show, that Algorithm 2 indeed realises the pair compression for a non-crossing pair.

Inner letter appearance compression
We can apply the same approach, as the one used for non-crossing pairs compression, to the inner letters appearance compression.However, in this case, the modification of N uses nondeterminism.
Since a is an inner letter, it cannot appear as the last or first letter of any nonterminal, and so every non-extendible appearance of a in word(X 1 ), . . ., word(X n ) is an explicit substring in the right-hand sides of G's rules; so we simply replace explicit a ℓ by a fresh letter a ℓ in each right-hand side of G's rules.Before considering the NFA, notice that as a is an inner letter, a ℓ cannot have a crossing appearance in N , and no nonterminal defines a ℓ .Hence, when a ℓ is a substring of a string defined by a path in N , then a ℓ appears wholly inside a nonterminal transition, and this was already taken care of when considering G, or a ℓ labels a path using letter transitions only.So it is enough to check, whether there is a path for a ℓ from p to q using only letter transitions.
The operations in line 6 of Algorithm 3 can be performed in npolytime, other operations can be performed in polytime.
Each of the new letters a ℓ is inner.N recognises word(X n ) if and only if N ′ recognises word(X ′ n ) for some non-deterministic choices.If N is DFA, so is N ′ .

Compression of outer letters and crossing pairs
Now, we turn our attention to the compression of outer letters and crossing pairs.The outline is as follows: we fix an outer letter a and modify the instance, so that a becomes inner.Then, Algorithm 3 is applied to a. Next, we want to compress each pair of the form a ℓ b.Such a pair can be crossing, as b can be a left-outer letter.Thus, we modify the instance again, so that none of a ℓ b is a crossing pair so that it can be compressed using Algorithm 2.

Transforming an inner letter to an outer letter
The reason, why a is an outer letter, is that it is the first or the last symbol in some word(X i ).
To make it an inner letter, it is enough to remove each nonterminal's a-prefix and a-suffix.To be more precise: fix i and let word(X i ) = a ℓi ua ri , where u does not start nor end with a. Then our goal is to modify G so that word(X ′ i ) = u.(If word(X i ) is a power of a, we simply give u ′ = ǫ and r i = 0.) This can be done in a bottom-up fashion, starting from X 1 : it is enough to calculate and memorise the lengths of the a-prefixes and a-suffixes for consecutive nonterminals, see the loop in lines 1-4 of Algorithm 4. Then we need to modify the NFA accordingly: it is enough to replace the transition labelled with X i by path consisting of three transitions, labelled with a ℓi , X ′ i and a ri .Algorithm 4 Changing an inner letter a to an outer letter.
1: for i = 1 . .n do ⊲ Removing a-prefix and suffix 2: let the production for X i be X i → α i 3: replace any nonterminal X j in α i by a ℓj X j a rj , remove those defining ǫ 4: calculate the explicit a-prefix a ℓi and a-suffix a ri of α i and remove them if there is a nonterminal transition δ N (p, X i , q) in N then ⊲ modification of NFA 6: create new states p 1 , q 1 in N , remove transition δ N (p, X i , q) 7: set transitions: δ N (p, a ℓi , p 1 ), δ N (p 1 , X i , q 1 ), δ N (q 1 , a ri , q) The removed a-prefixes and a-suffixes can be exponentially long, and so we store them in the rules in a succinct way, i.e. a ℓ is represented as (a, ℓ); the size of representation of ℓ is O(log ℓ), that is, linear in n.We say, that such a grammar is in an a-succinct form.The situation is similar for the NFA, as it might have transitions labelled with a ℓ , which are stored in succinct way as well.We say that N satisfies a-relaxed (Aut 1), if its transitions are labelled by nonterminals, a single letter or by a ℓ , where ℓ ≤ 2 n .
Let word(X i ) = a ℓi u i a ri , where u i does not begin, nor end with a. Then (after running Algorithm 4), word(X ′ i ) = u i .In particular, after running Algorithm 4 the letter a is inner.
Since a is no longer an outer letter, we may compress its non-extendible appearances using Algorithm 3. Some small twitches are needed to accommodate the a-succinct form of G and the fact that N is a-relaxed, though basically we just perform appearances compression for inner a, as described by Algorithm 3: the non-trivial part of Algorithm 3 was the application of Lemma 1, which works for such large powers of a in npolytime.Other actions of Algorithm 3 generalise in a simple way.

Crossing pair compression
By Lemma 8 all letters a ℓ are inner.However, a pair of the form a ℓ b can still be crossing and this can happen only when b is a left-outer letter, so we would like to make such b not a left-outer letter.To do so, we 'pop' one letter from the beginning of each nonterminal (that is, all left outer letters), i.e. we modify the grammar so that word(X i ) = first(X i ) word(X ′ i ), where first(X i ) denotes the first letter of word(X i ).Clearly, after such operations there are some (perhaps other) left-outer letters in G.Still, we show, that none a ℓ b is crossing.
Popping letters is performed similarly to the removal of the a-prefix, i.e. in a bottom-up fashion, starting from X 1 : in a rule X i → α it is enough to replace each nonterminal X j in α by first(X j )X ′ j , then store the first letter from α in first(X i ) and remove it from α.It is easy to modify the NFA N accordingly: when there is a transition δ N (p, X i , q), we change it into a chain of two transitions: δ N ′ (p, first(X i ), p 1 ) and δ N ′ (p 1 , X ′ i , q).This operation is not performed on X n , as the letter $ is not going to be compressed anyway.There is a little detail to take care of: if | word(X i )| = 1 then popping a letter from X i creates X ′ i , which defines ǫ.Then X ′ i should be removed from the right-hand sides of the rules and in the NFA N we simply replace the transition by X i by a transition by word(X i ).This description is formalised in Algorithm 5.

Algorithm 5 Popping letters
replace each X j in α by first(X j )X j , remove those defining ǫ 4: set first(X i ) ← the first letter of word(X i ) 5: remove the first letter from α if there is a transition δ N (p, X i , q) in N then ⊲ NFA modification 8: remove transition δ N (p, X i , q) 11: set transition δ N (p, first(X i ), q) Lemma 9. Algorithm 5 runs in time polytime and preserves (SLP 1)-(Aut 2).Let word(X i ) = bu, where b ∈ Σ, then word(X ′ i ) = u for i < n and word(X ′ n ) = word(X n ).After running Algorithm 5, pairs of the form a ℓ b appearing in word(X n ) are non-crossing.
N ′ accepts word(X ′ n ) if and only if N accepts word(X n ).If N is deterministic, so is N ′ .Now, it is enough to apply the pair compression for non-crossing pairs to each pair of the form a ℓ b.For convenience, we write the whole procedure for pair compression for crossing pairs in Algorithm 6.

Running time
Since the running time of each algorithm is npolytime, it is enough to show that the size of Σ, G and N are always polynomial in n (recall, that n is unchanged throughout Algorithm 1).

Lemma 11.
During Algorithm 1, the sizes of Σ, G, N are polynomial in n.
Using Lemmas 5-11 it is now possible to conclude that Algorithm 1 correctly solves the fully compressed membership problem for NFA, in nondeterministic polynomial (in n) time.The only source of non-determinism is the one in Lemma 1, and so for DFA the corresponding problem can be solved deterministically.

Appendix
A Additional material for Section 1 (see page 3) proof of Theorem 1.The proof follows by showing that Algorithm 1 properly verifies, whether word(X n ) is accepted by N and that Algorithm 1 runs in npolytime.
Let us first show correctness of Algorithm 1.All subroutines of Algorithm 1 (non-deterministically) modify the instance, changing G, N and X n into G ′ , N ′ and X ′ n (notice, that the output depends on the non-deterministic choices).Let N (i) , G (i) , X (i) n for i = 1, . . ., k be the consecutive obtained instances, with i = 1 representing the input instance.Then N (i) accepts word(X n ) (i) if and only if for some non-deterministic choices the resulting N (i+1) accepts word(X n ) (i+1) .This is shown in Lemmata 5, 6, 7, 8, 9, 10 (if some of the procedures are deterministic, then the output does not depend on any choices).So, if N (1) does not accept word(X (1) 1 ) also N (k) does not accept word(X (k) 1 ).On the other hand, if N (1) accepts word(X (1) n ), then there exits a sequence of instances (representing proper non-deterministic guesses), such that for each i N (i) accepts word(X ) can be decompressed and acceptance by N (k) can be checked naively in polytime.Now, we should show that the running time is in fact (non-deterministic) polynomial.Lemmata 5, 6, 7, 8, 9, 10 claim, that each of the subroutine runs in time npolytime in the size of the current instance.However, by Lemma 11, the size of this instance is polynomial in n.So, it is left to show that each of the subroutine is run only polynomially many times.Notice, that each invocation of Algorithms 2, 3, 6 introduces a new letter to Σ, and we know by Lemma 11 that the final size of Σ is poly(n).Moreover, each invocation of Algorithm 4 if followed by Algorithm 3 (in the extended version), similarly, each Algorithm 5 can be associated with Algorithm 6 invoked after it.And so also Algorithms 4, 5 are invoked poly(n) times.So the whole running time is npolytime.
It is left to show, that if the input is a DFA, Algorithm 1 can be determinised.Firstly, notice that by Lemmata 5, 6, 7, 8, 9, 10, if the instance consisted of a DFA, each instance kept by Algorithm 1 is also a DFA.Observe, that the only non-deterministic choices in Algorithm 1 are performed when calling a subroutine for a fully compressed membership problem for a string over an alphabet consisting of a single letter (see Lemma 1).However, the same lemma states, that when the input consists of a deterministic automaton, the problem is in P. Thus, there is no non-determinism in Algorithm 1.

B Additional material for Section 2
It is more convenient to represent path's list of labels as where each u ij ∈ Σ * is a string representing the consecutive letter labels and X ij represents a nonterminal label.Notice, that u ij may be empty.We write, that P induces such a list of labels.The word(P) defined by such a path P is (see page 4) Proof of Lemma 1. Notice, that if the input string w is over an alphabet {a}, no accepting path in NFA for w may use transitions that denote strings having letters other than a.Thus, any such transitions can be deleted from N and we end up with an instance, to which the result of Plandowski and Rytter [18, Theorem 5] can be applied directly.
Consider now the deterministic automaton.As shown in the previous paragraph, we can limit ourselves to transitions by powers of a. Since the automaton is deterministic, for each state there is at most one transition labelled with a power of a, and so the path for w cycles after at most n transitions.As the length of the cycle can be calculated in polytime, the whole problem can be easily checked in polytime.

C Additional material for Section 3
(see page 5) Proof of Lemma 2. Since there are n nonterminals, there are at most n left-outer letters and n right-outer letters.Clearly, they can be calculated in polytime.
We estimate the total number of pairs of letters that appear in word(X 1 ), . . ., word(X n ).Consider first such pairs that appear in some explicit string on the right-hand side of some production.Since the total length of explicit strings is |G|, there are at most |G| such pairs.Other pairs are assigned to nonterminals X 1 , . . ., X n : a pair ab is assigned to X i , if it appears in word(X i ) and it does not appear in word(X 1 ), . . ., word(X i−1 ).We show, that at most three different pairs are assigned to each nonterminal.In this way, total number of different crossing-pairs is at most |G| + 3n.Indeed, if ab is assigned to X i with a production X i → uX j vX k , then ab does not appear neither in u, v, as in such case it was already accounted; nor in word(X j ), word(X k ), as in such case it is not assigned to X i .Thus, there are three possibilities: • a is the last letter of u and b is the first letter of word(X j ), • a is the last letter of word(X j ) and b is the first letter of v, • a is the last letter of v and b is the first letter of word(X k ).
The cases, in which u or v is empty or there are less nonterminals on the right-hand side of the production, are similar.
The above description can be turned to a straightforward algorithm computing both the list of all non-crossing and crossing pairs appearing in word(X 1 ), . . ., word(X n ).First, the list of all pairs of letters with such appearances is calculated: clearly, it is enough to read every rule (for X i ) and store the pairs that appear in the explicit strings and the pairs that are assigned to X i .Then, for each pair of letters it should be decided, whether it is crossing.To this end, we check, whether it has a crossing appearance in any nonterminal or in N , which can be done in polytime.Such pairs are crossing, other are non-crossing.

Instead of Lemma 3 we show an extended version
Lemma 12 (stronger variant of Lemma 3).For an inner letter a and a grammar G, which can be given in an a-succinct form, there are at most |G| different lengths of a's non-extendible appearances in word(X 1 ), . . .word(X n ).The set of these lengths can be calculated in polytime.
(see page 5) Proof.The proof is similar to the proof of Lemma 2.
Since a is an inner letter, all of its non-extendible appearances are explicit substrings in productions of G.In particular, each symbol in the productions can uniquely assigned to the nonextendible appearance to which it belongs; this is true regardless of whether the symbol represents letter or block of letters written in a succinct way.Thus, there are at most |G| different nonextendible appearances for a.To calculate the lengths of these appearances, it is enough to read the explicit strings in the rules, adding the appropriate lengths, which can be done in polytime, as these lengths are at most 2 n .
(see page 6) Proof of Lemma 4. Consider any 2 consecutive letters ab, where a = $ and b = #, appearing in the word(X n ) at the beginning of loop starting in line 1.We show, that at least one of these two letters is compressed before the next execution of this loop.In this way, if we partition word(X n ) into blocks of 4 consecutive letters, each block is shortened by at least one letter in each iteration of the loop from line 1.Thus the length of word(X n ) decreases by a factor of 3/4 in each iteration and so this loop is executed at most O(n) times, as in the input instance | word(X n )| ≤ 2 n .Assume for the sake of contradiction, that none of letters a, b is compressed during this iteration of the loop.
If a = b, then this pair of consecutive letters is going to be compressed, either in line 4 or 9, depending on whether a is inner or outer.Contradiction.
So suppose now that a = b.Since the pair ab was not compressed before line 7 in Algorithm 1, it is crossing in this line and thus at least one of the letters a, b is outer.This outer letter is going to be compressed with the next letter in line 11 (or earlier).Contradiction.This ends the case inspection.Since in the input instance it holds that P ≤ 2 n , this guarantees, that the loop in line 1 of Algorithm 1 is run O(n) times.

D Additional material for Section 4
(see page 7) Proof of Lemma 5.The bound on the running time is obvious from the code.
Since Algorithm 2 only modifies the grammar by shortening some strings in the productions (it does not create ǫ-rules), and it does not affect $ and # in the rules, the (SLP 1)-(SLP 3) are preserved.The only modification in N is the introduction of new transition by a single letter (namely, by c) between states that are joined by a path for ab.The only change in N is the introduction of a new letter transitions.Moreover, if there is a new transition δ N ′ (p ′ , c, q ′ ), then p has an outgoing production by a / ∈ {$, #}, and so it was not a starting or accepting state, and q had an incoming transition by b / ∈ {$, #}, and similarly it was not a starting nor accepting state.Thus (Aut 1)-(Aut 2) hold for N ′ as well.Notice, that if N is deterministic, so is N ′ : suppose that there are two different transitions by a letter d from state p in N ′ .If d = c, then these two transition are also present in N , which is not possible, as N is deterministic.If d = c, then in N there are two different paths from p for a string ab, which is also a contradiction.
We now show that N ′ recognises word(X ′ n ) if and only if N recognises word(X n ).To this end we demonstrate, how Algorithm 2 affects word(X i ): Claim 1.After performing Algorithm 2, it holds that word(X ′ i ) = P C ab→c (word(X i )). ( Proof.Notice, that as a = b, P C ab→c is well defined for each string. The claim follows by a simple induction on the nonterminal's number: Indeed, this is true when the production for X i has no nonterminal on the right-hand side (recall the assumption that a = b), as in this case the pair compression on right hand side of the production for with the last equality following by the induction assumption.Notice, that since ab is a non-crossing pair, all occurrences of ab in word(X i ) are contained in u, v, word(X j ) or word(X k ), as otherwise ab would be a crossing pair, which contradicts the assumption.Thus, which shows that P C ab→c (word(X i )) = word(X ′ i ), ending the proof of the claim.The second claim similarly establishes, how the pair compression of a non-crossing pair affects the NFA.To be more precise, what happens to a string defined by a path in the NFA after applying pair compression to the underlying NFA.
Claim 2. Consider a non-crossing pair ab and a path P in NFA N , which defines a list of labels: where each u ij ∈ Σ * is a string representing the consecutive letter labels and X ij represents a transition by a nonterminal transition.Then Proof.Similarly as in Claim 1, notice, that as ab is a non-crossing pair, the appearance of ab in the string defined by P cannot be split between a nonterminal and a string (other nonterminal).Thus, replacement of pairs ab takes place either wholly inside string u or inside word(X i ).The former is done explicitly by P C ab→c , while (2) establishes the form of the latter.This ends claim's proof.Now, after proving Claims 2-1, it is easy to show the main thesis of the lemma, i.e. that word(X ′ n ) is accepted by N ′ if and only if word(X n ) is accepted by N .⇐ Suppose first that word(X n ) is accepted by N .Consider the accepting path P for word(X n ), represent it as a list of labels: We will construct an accepting path P ′ in N ′ inducing a list of labels Using ( 3) and recalling that word(P) = word(X n ) will be enough to conclude that word(P ′ ) = P C ab→c (word(X n )).Notice, that by Algorithm 2 • if there is a transition δ N (p, d, q) for a letter d ∈ Σ in N , then there is the same transition δ N ′ (p, d, q) in N ′ .
• if there is a path from p to q for a string ab in N then there is a transition δ N ′ (p, c, q) in N ′ .
Thus, by a trivial induction on the length of the string u, if δ N (p, u, q) then also δ N ′ (p, P C ab→c (u), q).The situation is similar for nonterminals: if there is a transition δ N (p, X i , q) in N , then there is an analogous transition δ N ′ (p, X ′ i , q) in N ′ .Thus, a path P ′ with the same starting and ending state as P and the list of labels as in ( 5) is inductively defined.Since the starting (accepting) state in N and N ′ coincide, this shows that word(P ′ ) is accepted by N ′ .
⇒ Suppose now that a string P C ab→c (word(X ′ n )) is recognised by N ′ .Let the path of the accepting computation in N ′ be P ′ , with a list of labels Similarly to the previous case, we will inductively define an accepting path P in N with a list of labels where u ij is obtained from u ′ ij by replacing each c by ab.Notice, that by Algorithm 2, • if there is a letter transition δ N ′ (p, c, q) in N ′ , there is a path from p to q for a string ab in N .
• if the a letter transition δ N ′ (p, d, q) for a letter d = c, there there is the same transition δ N (p, d, q) in N .
Thus, by a simple induction, we conclude that if there is path in N ′ from p to q for a string u ′ ij , then there is a path in N from p to q for a string u ij .Now observe, that if there is a transition δ N ′ (p, X i , q) in N ′ , then there is an analogous transition δ N (p, X i , q) in N .This completes the construction of P. Since the starting and accepting states in N and N ′ coincide, the constructed path P is also accepting, furthermore, P's list of labels is as in (6).
It is left to show, that word(P) = word(X n ).Since P C ab→c is a one-to-one function, it is enough to show that P C ab→c (word(P)) = P C ab→c (word(X n )).Notice, that the latter equals word(X ′ n ), by (2).Using (3) we can conclude that which concludes the proof.
(see page 8) Proof of Lemma 6, stronger version.We shall proof Lemma 6 in a little stronger version: we allow the grammar to be in an a-succinct form and NFA N is assumed to satisfy the a-relaxed-(Aut 1), see appropriate definition between Algorithm 4 and Lemma 7.This weaker assumption will allow proving Lemma 8 here.
Notice, that by Lemma 12, there are only |G| + 3n possible lengths of a's non-extendible appearance and that they can be calculated in polytime; hence line 1 takes polytime.All the loops in Algorithm 3 have only polynomially many iterations.All operations listed in Algorithm 3 are elementary and can be clearly performed in polytime, except replacing each a ℓ by a ℓ in a rule in line 4 and for the verification in line 6, for the former operation, it is enough to read the rules of the grammar: recall, that a ℓ is represented as a pair (a, ℓ).Since ℓ ≤ 2 n , addition of the lengths can be performed in polytime.The verification is more involved, we outline how to perform it: For given two states p, q and a string a ℓ we want to verify, whether there is a path from p to q for a string a ℓ (notice, that since a is inner, none of word(X i ) is a power of a).This can be rephrased as a fully compressed membership problem for NFA over a unary alphabet: it is enough to • restrict NFA N to transitions by powers of a, • make p the unique starting state, • make q the unique accepting state.
Notice, that we can restrict ourselves by transitions by powers of a: since a is an inner letter, no word(X i ) begins or ends with a. Hence, when a path in the NFA defines a ℓ , then this path uses only transitions by powers of a.
Observe, that all considered powers a ℓ appear in strings defined by G, and so ℓ ≤ 2 n .,So each such a ℓ can be represented by an SLP of polynomial size.In particular, Lemma 1 is applicable here and so the verification is in npolytime.
Concerning the preservation of invariants: we first show, that the G ′ is not in the a-succinct form, nor N ′ is a-relaxed.When Algorithm 3 finishes its work, all non-extendible appearances of a are replaced, in particular, there are no succinct representations of a powers inside the grammar.This should apply to transitions labelled with powers of a in N , and so the last line of Algorithm 3 should be modified, so that all transitions by powers of a are removed.Now we can return to showing the preservation of the invariants: since the only change to the productions consists of replacing non-extendible occurrences of a by a single letter, (SLP 1)-(SLP 3) are preserved.Also, the only modifications to the NFA is the addition of new letter transitions.Thus, (Aut 1) holds.To see that also (Aut 2) holds, notice, that if p receives new incoming (outgoing) transition in N ′ , this transition is of the form a ℓ and p had an incoming (outgoing, respectively) transition by a ℓ in N .In particular, the starting and accepting state remain unaffected and no transition by $ and # are introduced.Thus, also (Aut 2) holds for N ′ .
Notice, that if N is deterministic, so is N ′ : suppose that there are two different transitions by d in N ′ .If d is not one of the new letters, i.e. it is a nonterminal or an old letter, then the same transitions were present in N , contradiction.If there are two transitions by a new letter a ℓ from the same state p, then in N there are two different paths for a ℓ from state p, contradiction.Claim 3.After performing Algorithm 3, it holds that word( Claim 4. Consider an inner letter a and a path P in N , which has a list of labels: where each u ij ∈ Σ * is a string representing the consecutive letter labels and X ij represents a nonterminal transition, similarly as in Claim 2. Then The proofs are analogous as in Lemma 5 and are thus omitted.Notice, that the properties stated in Claims 3-4 do not depend on the non-deterministic choices of Algorithm 3. It is left to show the main claims of the lemma: N recognises word(X n ) if and only if NFA N ′ recognises word(X ′ n ) for some non-deterministic choices.⇐ Suppose first that the N ′ accepts word(X ′ n ), using the path P ′ .Clearly word(P ′ ) = word(X ′ n ).Let the list of labels on P ′ be Let u ′ ij be obtained from u ij be replacing each a ℓm with a ℓm .We shall construct a path P in N , which has the same starting and ending as P ′ and induces a list of labels Notice, that • if there is a transition δ N ′ (p, X ′ i , q) in N ′ then there is a transition δ N (p, X i , q) in N • if there is a transition δ N ′ (p, b, q) for b = a in N ′ , then there is a transition δ N (p, b, q) in N • if there is a transition δ N (p, a ℓm , q) in N ′ for some a ℓm that has a non-extendible in one of word(X 1 ), . . ., X n , then there is a path from p to q for a string a ℓm in N .
Therefore, by an easy induction, P is a valid path in N , moreover, since P ′ is accepting, so is P. It is left to demonstrate, that P defines word(X n ): since AC a is a one-to-one function, it is enough to show that AC a (word(P)) = AC a (word(X n )).By (7) it holds that AC a (word(X n )) = word(X ′ n ).The value of AC a (word(P)) is already known from (8), and so it is enough to show that But this is simply the fact, that path P ′ defines word(X ′ n ), which hold by the assumption.⇒ Suppose now that N accepts word(X n ).Consider the case in which Algorithm 3 always made a correct non-deterministic choice, i.e. that each time it correctly verified in line 6.
Let the accepting path P in N has a list of labels where, similarly as in Claim 4, each u ij is a string representing the consecutive letter labels (u ij may be empty) and X ij represents a transition by a nonterminal transition.By the definition, Now consider the a's appearance compression applied to both side of this equality.By (7) and ( 8) We will construct an accepting path P ′ with a list of labels Notice, that word(P ′ ) = word(X ′ n ), and so construction of such path P ′ will conclude the proof.We iteratively transform P into P ′ .Notice, that • if there is a path in N from p to q for string a ℓ that has non-extendible appearance in some word(X i ), then there is a transition δ N ′ (p, a ℓ , q) in N ′ (by the assumption that Algorithm 3 guessed correctly) And by an easy induction P ′ is valid path in N ′ and has the same starting and ending state as P .Since P is accepting in N and the starting and accepting states in N and N ′ coincide, P ′ is an accepting path in N ′ .(see page 9) Proof of Lemma 7. We first explain, how to calculate the a-prefix (a-suffix); since G is in a-succinct form, this might be non-obvious.It is enough to scan the explicit strings stored in the productions' right-hand sides, summing the lengths of the consecutive a's appearances.This clearly works in polytime also for G stored in an a-succinct form, as the powers of a may have length at most 2 n , and so the length of their representation is linear in n. (the correctness of this approach is shown later).
Algorithm 4 runs in polytime, as the first loop has n iterations and the second |N | iterations and each line can be performed in polytime.
Concerning the preservation of the invariants: in each rule of the grammar at most 4 nonextendible appearances of a are introduced.They may be long, however, in compressed form we treat them as singular symbols.In this way rules of G are stored in an a-succinct form, which was explicitly allowed.Then the a-prefix and suffix are removed and nonterminals defining ǫ are removed from the right-hand sides of the productions.This does not affect the (SLP 1)-(SLP 3) (recall, that a is not $, neither #).Since the NFA is also changed, we inspect the invariants regarding N : introducing new states p 1 , q 1 and replacing transition δ N (p, X i , q) by three transitions δ N ′ (p, a ℓi , p 1 ), δ N ′ (p 1 , X, q 1 ), δ N ′ (q 1 , a ri , q) preserves the (Aut 1)-(Aut 2), with the exception that it a-relaxes (Aut 1).
Notice, that if N is deterministic, so is N ′ : as already mentioned the only change done to N is the replacement of transition by X i by a path of three transitions, such the the two states in the middle have exactly one incoming and outgoing transition, which clearly preserves determinism of the automaton.
To show the correctness, we prove by induction on i the two main claims of the lemma: • Algorithm 4 correctly calculates the length of the a-prefix and a-suffix of word(X i ), i.e. ℓ i and r i , • word(X i ) = a ℓi word(X ′ i )a ri .As simple corollary, observe that these two conditions imply that a is not the last, nor the first letter of word(X ′ i ).For i = 1 notice, that the whole production for X 1 is stored explicitly, and so Algorithm 4 correctly calculates the a-prefix and suffix of word(X 1 ) and after their removal, word(X 1 ) = a ℓ1 word(X ′ 1 )a r1 .
For the induction step, let X i → uX j vX k .Then by the induction assumption: There are cases to consider, depending on whether word(X ′ j ) = ǫ or not and whether word(X ′ k ) = ǫ or not.We will describe the one with word(X ′ j ) = ǫ and word(X ′ k ) = ǫ, other cases are treated in a similar way.Then the rule is rewritten as ua ℓj word(X ′ j )a rj va ℓ k a r k .Since by the inductive assumption, a is not the first, nor the last letter of word(X ′ j ), the a prefix (a-suffix) of word(X ′ j ) is the a-prefix (a-suffix, respectively) of ua ℓj (of a rj va ℓ k a r k , respectively).Thus it is correctly calculated by Algorithm 4. As the last action, Algorithm 4 removes the a-prefix and a-suffix, which shows that a ℓi word(X ′ i )a ri = word(X i ).It is left to show that N accepts word(X n ) if and only if N ′ accepts word(X ′ n ).To this end, notice, that the only modification to N is the replacement of the transition of the form δ N (p, X i , q) by a path labelled with a ri , X ′ i , a ℓi .Furthermore, the two vertices inside this path have only one incoming and one outgoing transition.The path labelled with a ri , X ′ i , a ℓi defines the string a ri word(X ′ i )a ℓi , which was already shown to be word(X i ).It is left to observe, that none of the newly introduced states in the middle of the path is accepting, nor starting.Hence the starting (accepting) states of N and N ′ coincide, each string is accepted by N if and only if it is accepted by N ′ .
(see page 9) Proof of Lemma 8. Notice, that the proof of Lemma 6 was shown in a stronger version, which coincides with the statement of Lemma 8.
(see page 10) Proof of Lemma 9. Majority of the proof is similar to the proof of Lemma 7. Nevertheless, it is written for completeness.
The loop is executed polynomially many times, and also each line of the code can be performed in polytime, and so in total Algorithm 5 runs in polytime.
Concerning the preservation of invariants: since the only operation performed on G is replacing nonterminal X i by aX ′ i and then deleting the first letter of the nonterminal, and nonterminals generating ǫ are explicitly removed from the rules, the resulting grammar is in the form (1). Notice, that the invariants (SLP 1)-(SLP 2) are clearly preserved by the listed operations.Since the first and last symbol of word(X n ) is not modified, (SLP 3) holds as well.
Let us move to the NFA invariants: the only change applied to NFA is the replacement of the transitions δ N (p, X i , q) to by a path δ N ′ (p, first(X i ), p 1 ), δ N ′ (p 1 , X i , q), or by δ N ′ (p, first(X i ), q), which does not affect (Aut 1)-(Aut 2).For the same reason, if N is deterministic, so is N ′ : We first show by induction the second claim of the lemma, i.e. that if word(X i ) = bu for some b ∈ Σ, u ∈ Σ * , then word(X ′ i ) = u.For the induction basis consider i = 1: let the rule for X 1 be X 1 → bu for some b ∈ Σ and u ∈ Σ * .Then x 1 = b and u ′ = u and the production for Clearly the claim holds in this case.
For the inductive step, consider i > 1 and let X i 's production be X i → uX j vX k .Then, word(X i ) = u word(X j )v word(X k ) and by by induction assumption: Since u first(X j ) is non-empty, first(X i ) can be computed in line 4 of Algorithm 5. We conclude that word( The analysis for other forms of the rule, i.e.X i → uX j v or X i → u, is similar. We show now that N ′ accepts exactly the same strings as N .The only change done in the NFA is the replacement of transitions of the form δ N (p, X i , q) by a path inducing list of labels with first(X i ) and X ′ i or by a transition δ N (p, first(X i ), q), let us consider the former case, the latter is similar.Notice, that word(X i ) = first(X i ) word(X ′ i ) and so the new path denotes the same string, as the replaced transition.Furthermore, the newly introduced state in the middle of this path has only one ingoing and outgoing transition.The starting and accepting states were not modified, and so the both automata recognise the same strings.Since word(X n ) = word(X ′ n ), this ends the proof.
It is left to show, that none of the pairs a ℓ b is crossing.Assume for the sake of contradiction, that some of such pairs a ℓ b is.The analysis splits between cases, why the pair a ℓ b is crossing.
a ℓ b has a crossing appearance in the NFA N ′ In this case there is a state p and a pair of incoming and outgoing transitions, such that a ℓ is the last letter of the string encoded on the incoming transition and b is the first letter on the outgoing string encoded transition.Moreover, at least one of this transitions is labelled with a nonterminal, we distinguish these two subcases: transition into p is labelled with X ′ i In such a case a ℓ is the last letter of word(X ′ i ), and since word(X i ) = first(X i ) word(X ′ i ), also the last letter of word(X i ).Thus a ℓ is a right-outer letter in G, contradiction with Lemma 8. transition into p is labelled with a letter and transition from p is labelled with X ′ i In this case a ℓ is the label of the transition incoming to p. Observe, that due to the algorithm, if X ′ i labels the transmission from p, there is a unique transition to p, labelled with first(X i ).This means that a ℓ is the first letter of word(X i ), which is not possible by Lemma 8.
a ℓ b has a crossing appearance in nonterminal X ′ i Let the production for X ′ i be X ′ i → uX ′ j vX ′ k , the cases of other productions are similar.There are three possibilities for a crossing appearance of a ℓ b in word(X ′ i ): a ℓ is the last letter in u and b is the first letter in word(X ′ j ) Algorithm 5 replaced X j with first(X j )X ′ j in the rule for X ′ i .Thus, since a ℓ is the letter preceding X ′ j in the rule for X ′ i , it holds that a ℓ was the first letter in word(X j ), which is a contradiction with Lemma 8. a ℓ is the last letter in word(X ′ i ) and b is the first letter in v Since the last letter of word(X ′ i ) and word(X i ) are the same, as word(X i ) = first(X i ) word(X ′ i ), a ℓ is the last letter of word(X i ), which is a contradiction with Lemma 8. a ℓ is the last letter in v and b is the first letter in word(X ′ k ) the proof is the same as in the first case.
(see page 10) Proof of Lemma 10.Notice first, that there are at most |G| different letters a i : they were introduced by Algorithm 3 as representations of non-extendible appearances of a, and by Lemma 12 there are at most |G| different such appearances.Other operations of Algorithm 6 constitute of call to Algorithm 5 and Algorithm 2, and thus the running time and preservation of the invariants follows properties of these two algorithms, stated in Lemma 8 and Lemma 9 (see page 10) Proof of Lemma 11.We first bound the size of Σ.We show, that at the beginning of each iteration of the main loop in Algorithm 1 each right-hand side of the production has at most 48n explicit letters, and that inside each iteration of the main loop of Algorith 1 there are at most 12n new letters added (we exclude the letter replacing compressed strings).Notice, that during the run of Algorithm 1 grammar G may be in succinct form, and accordingly we treat a ℓ as one symbol.
Clearly, these bounds are true when Algorithm 1 starts working.Let us fix a rule and consider, how many new letters may be introduced in this rule.There are only two cases, in which new letters (not coming from compression) are introduced to a rule: changing an outer letter to an inner one (line 3 of Algorithm 4) There are at most 4 new powers of a (all possibly in succinct form) that may be introduced in each invocation of Algorithm 4. While these powers of a are written in a succinct representation, they will be all replaced by single letters in the later appearance compression for a.
In total, Algorithm 4 is invoked for each outer letter, and there are at most 2n such letters, by Lemma 2. So there are at most 8n new symbols introduced in this way for one iteration of the main loop of Algorithm 1.
popping letters (line 3 of Algorithm 5) Each invocation of of Algorithm 5 introduces at most 2 new symbols.As Algorithm 5 is also used for each outer letter, in this way at most 4n new symbols were introduced per each iteration of the main loop of Algorithm 1.
Hence, there are at most 12n new letters added to the right-hand side of a production in each iteration of the main loop in Algorithm 1.Still, the main task performed by Algorithm 1 is the compression: an argument similar as in the proof Lemma 4 can be used to show that the size of the explicit strings in the rules decreases by a factor of 3/4 in each iteration of loop from line 1 in Algorithm 1.Of course, the newly introduced letters may be unaffected by this compression.
It is left to verify, that 48n is indeed the upper bound on the size of the right-hand size of a rule, let it be X i → α i : Which proves the bound on |G| at the end of each iteration of the main loop.Notice, that as there at most 12n new letters added to this rule, inside the phase the size of G is at most 60n 2 .We now turn our attention to the size of Σ. Again, consider the execution of Algorithm 1 and one iteration of the main loop.We show, that there are poly(n) letters introduced in one such iteration.New letters are introduced when compression of pairs or appearance compression is applied.There are the following possibilities compression of an inner letter (line 1 of Algorithm 2) Each compression of an inner letter decreases the total length of explicit strings used in G by at least 1.Since the size of each right-hand side is at most 48n at the beginning of the iteration and there are at most 12n new letters introduced to a rule in each iteration, there can be at most 60n

1 : 2 :something changed do 3 : 4 :
while | word(X n ) > n| do while for a: inner letter do compress appearances of a, modify N accordingly 5:

Lemma 4 .
There are O(n) executions of the loop in line 1 of Algorithm 1.

Algorithm 6 3 :Lemma 10 .
Pair compression for crossing pairs a ℓ b 1: pop the first letter from each nonterminal (run Algorithm 5) 2: for each a ℓ do for each b such that a ℓ b appears in word(X n ) do 4:run Algorithm 2 for a pair a ℓ b Algorithm 6 runs in polytime and preserves (SLP 1)-(Aut 2).It implements pair compression for ab, in the sense that word(X

compression of a non-crossing pair (line 4 of Algorithm 3)
2such compressions.The same argument as in the previous case applies.

appearance compression for of an outer letter (Algorithm 4 followed by Algorithm 3)
There are at most 2n outer letters, by Lemma 2, and each of them has at most |G| different lengths of non-extendible appearances, by Lemma 12. Compressing all of them introduces at most 2n|G| new letters to Σ.

compression of a crossing pair (call to Algorithm 2 made by Algorithm 6)
The crossing pairs compression is run for each of the 2n outer letters.When such an outer letter is fixed, there are at most |G| + 3n different pairs of letters appearing in word(X n ), by Lemma 2, this may introduce up to |G|+3n new letters.Hence, in total this might introduce up 2n | G|+6n 2 new letters to Σ. Thus, the size of |Σ| is poly(n).It is left to bound the size of |N |: as there are at most n non-terminal transitions, the the size of transition function ofN is at most O(|Q N | 2 • (|Σ| + n)).So it enough to bound the number of states of N by poly(n).Recall, that for simplicity we assumed that all transitions in the input DFA are labelled with different SLPs, in particular, this polynomial bound holds for the input instance.There are only two situations, in which new states are introduced:changing a from outer letter to inner one (Algorithm 4 in line 6) Algorithm 4 introduces two states per nonterminal transition, and there are at most n such transitions, by (Aut 1).So it is enough to estimate, how many times Algorithm 4 is invoked.Algorithm 4 is run for each outer letter, and there are at most 2n outer letters.Thus, in one iteration of the main loop of Algorithm 1 it adds at most O(n 2 ) states in total in this case.poppingletters(Algorithm 5 in line 10) A similar argument applies, with the only difference, that Algorithm 5 introduces one new state per nonterminal transition.It is left to recall, that by Lemma 4 the main loop of Algorithm 1 is run O(n) times.This ends the proof of the lemma.