Abstract 1 Introduction 2 Related Work 3 Preliminaries 4 Our Key Data Structure 5 Data Structure for Range Query 6 Quadratic Time Algorithm for 𝜶𝟏𝜷𝜶𝟐𝜷𝜶𝟑 7 Quadratic Time Algorithm for General Case References Appendix A Proof of Proposition 4 Appendix B Proof of Lemma 14 Appendix C Proof of Lemma 15 Appendix D 𝑶(#𝒖|𝑸𝓐|𝟐)-time Complexity Analysis of NextPhase

Matching Regular-Typed Pattern Languages: Quadratic-Time Algorithms

Yuya Uezato ORCID CyberAgent, Inc., Tokyo, Japan
National Institute of Informatics, Tokyo, Japan
Abstract

Pattern languages (PAT) are a class of languages generated by expressions called patterns that may contain variables. In a pattern, each variable can be instantiated with an arbitrary string. Typed pattern languages extend PAT by associating a type (constraint) with each variable that restricts the domain of allowed substitutions. In this paper, we study regular-typed PAT (PATwRT), where all types are represented either by a regular expression or by an ε-NFA. We consider the PATwRT matching problem for patterns with a single repeated variable of the form P=α1βα2ββαK. We present simple algorithms whose running time is linear in K and quadratic in the input length N, with polynomial dependence on the sizes of the type representations. Our results extend previous quadratic-time work in two directions: (1) the quadratic-time algorithm for untyped PAT of Fernau et al. (STACS 2015), and (2) the quadratic-time algorithm for the restricted PATwRT K=3, i.e., α1βα2βα3 of Nogami and Terauchi (MFCS 2025).

Keywords and phrases:
Pattern languages, Regular expressions, String algorithms
Copyright and License:
[Uncaptioned image] © Yuya Uezato; licensed under Creative Commons License CC-BY 4.0
2012 ACM Subject Classification:
Theory of computation Formal languages and automata theory
; Theory of computation Pattern matching
Acknowledgements:
The author is deeply grateful to Soh Kumabe for fruitful discussions during the early stages of this research and for providing valuable comments. The author also thanks the anonymous reviewers for their insightful feedback, which significantly improved the presentation of this paper. In particular, one of the reviewers kindly suggested using an O(nω)-time algorithm for computing the reflexive transitive closure of a Boolean matrix, which improved the running time of our algorithm for dense ϵ-NFAs.
Funding:
This work was supported by JST, CREST Grant Number JPMJCR21M3.
Editors:
Philip Bille and Nicola Prezza

1 Introduction

In this paper, we study the following decision problem for the formalism of regular-typed pattern languages (PATwRT).

Matching Problem for Regular-Typed Pattern Languages

 

Input 1: An input string W.

Input 2: A pattern P and a type constraint 𝒞.

Task: Deciding whether W𝒞(P) where 𝒞(P) denotes the language generated by P under 𝒞.

To explain PATwRT, we first recall pattern languages (PAT), since PATwRT is an extension of them. Pattern languages were introduced by Angluin in the context of automata learning theory [4, 5]. In PAT, variables can be used to describe repeated substrings. For example, consider the following pattern:

P=αβ#βαwhere αβ are variables and # is a letter.

Over an alphabet Σ, the language (P) generated by P consists of all strings obtained by substituting strings wα,wβΣ for α and β, respectively; that is,

(P)={wαwβ#wβwα:wα,wβΣ}.

For example, let Σ={#,0,1} and consider an assignment φ such that φ(α)=01 and φ(β)=111. Then, the instantiated string φ(P)=01 111# 111 01 belongs to (P). For Σ={#,0,1}, the language (P) is neither regular nor context-free (cf. [31, Theorem 8]). In general, the language class PAT is contained in NL, the class of languages recognized by nondeterministic log-space Turing machines [6, 23]. On the other hand, PAT does not include all regular languages. For example, the regular language ((ab))={ε,ab,abab,ababab,} cannot be generated by any pattern.

To restrict the domains used when assigning strings to variables, typed pattern languages (PATwTy) were introduced by Wright [36] and studied by Koshiba [25] in the context of learning theory. In PATwTy, in addition to pattern expressions, we consider a type constraint 𝒞 that assigns a type 𝒞(ν) to each variable ν. For example, in the pattern P above, if we set 𝒞(β)={wΣ:the numbers of 0s and 1s in w are equal}, then 𝒞(P) contains 11 0101# 0101 11, but not 11 010# 010 11.

If all types are regular languages, we obtain regular-typed pattern languages (PATwRT). Let us consider the following example pattern Q and type constraint 𝒞:

Q=α#βαγ,𝒞(α)=(Σ{#})+,𝒞(β)=𝒞(γ)=(Σ{#}),

where # is a separator letter. The generated language 𝒞(Q) under 𝒞 is the following:

L={w#w1ww2:w(Σ{#})+,w1,w2(Σ{#})}.

Using this language L, we can perform pattern matching as follows. To test whether a target string wtarget(Σ{#})+ occurs in a text T(Σ{#})+, it suffices to check whether wtarget#TL. As this example illustrates, and since PATwRT is strictly more expressive than both REG and PAT, developing faster algorithms for the PATwRT matching problem is of both practical and theoretical interest. Moreover, PAT and PATwRT can be viewed as important fragments of regular expressions with backreferences, REWB [33, 24]. Efficient matching algorithms for PATwRT provide efficient procedures for a non-trivial fragment of REWB and play an important role in practical text processing.

Our Contribution

To state our main results clearly, let us formalize our matching problem and its parameters. For an integer K3, we consider the matching problem for the pattern

PK=α1βα2βα3ββαK,

where α1,,αK,β are distinct variables, and only β occurs more than once in PK. Let W be an input string of length N:=|W|. For the type constraints 𝒞 associated with the variables, let m:=max{|𝒞(α1)|,,|𝒞(αK)|,|𝒞(β)|} be the maximum size of the representations, where |𝒞(ν)| denotes the expression size |E| if given by a regular expression E, or the number of states |Q𝒜| if given by an ε-NFA 𝒜.

Theorem 1 (Complexity with Regular-Expression Representation).

The matching problem W𝒞(PK) with regular-expression types can be solved in O((K+1)N2m2) time.

Theorem 2 (Complexity with ε-NFA Representation).

The matching problem W𝒞(PK) with ε-NFA types can be solved in O((K+1)N2mω) time.111We recall that the constant ω is the exponent of fast Boolean matrix multiplication: given two n×n Boolean matrices M1 and M2, we can compute their matrix product M1M2 in O(nω) time. The current value of ω is <2.371339 [3].

In particular, if K is fixed, then Theorems 1 and 2 yield O(N2m2)-time and O(N2mω)-time algorithms, respectively.

As we will see in Section 2, our result extends previous work in two directions. We generalize the quadratic-time result of Fernau et al. [12, 13] for untyped pattern languages to the regular-typed setting. We also extend the result of Nogami and Terauchi [29] from the restricted form α1βα2βα3 to the general form considered here.

Organization.

In Section 2, we compare our results with previous work by Fernau et al. [12, 13] and by Nogami and Terauchi [29]. We also review existing results on pattern languages. In Section 3, we introduce the necessary preliminaries. In Section 4, we define and compute our key building block 𝐒𝐮𝐛𝐬occ. In Section 5, we introduce and compute our data structure . In Section 6, we combine 𝐒𝐮𝐛𝐬occ and to obtain a quadratic-time algorithm for the basic case α1βα2βα3. In Section 7, we extend this algorithm to the general form α1βα2βα3ββαK.

2 Related Work

We first review the result of Fernau et al. [12, 13] for (untyped) pattern languages.

Theorem ([13, Theorem 4.2]; [12, Lemma 2]).

The matching problem for Patrvar1 is solvable in O(N2+N|P|) time, where |P| is the length of the input untyped pattern P.

In their notation, Patrvar1 denotes the class of patterns with at most one repeated variable. Their result addresses the untyped case of this restriction. The present paper studies the regular-typed matching problem for patterns of the form α1βα2ββαK and shows that, for fixed K, it is solvable in quadratic time in N. In this sense, our result can be viewed as a regular-typed extension of the quadratic-time result of Fernau et al. [13, 12].

We next review the result of Nogami and Terauchi [29].

Theorem ([29, Theorem 5 (Recap.)]).

The matching problem for REWB of the form e0(e)1e1\1e2, where e0,e,e1,e2 are regular expressions, can be solved in O(N2M2) time, where M:=|e0|+|e|+|e1|+|e2|.

They consider regular expressions with backreferences (REWB) e0(e)1e1\1e2, rather than (regular-typed) pattern languages. However, this expression is equivalent to the following simple instance of PATwRT:

the pattern: α1βα2βα3where 𝒞(α1)=e0𝒞(β)=e𝒞(α2)=e1𝒞(α3)=e2.

It should be noted that they only consider the very restricted form α1βα2βα3 and do not consider the general case α1βα2βα3ββαK. Since O(M2)=O(m2) holds for the parameter M in their statement, our result extends theirs to the general form. Furthermore, our approach is technically simpler and relies only on elementary tools such as suffix tries.

For REWB, Schmid shows that every fixed REWB expression can be simulated by a two-way multihead finite automaton (2WMFA) [33, Lemma 18]. Since the language class of 2WMFA coincides with NL [19], the language class of REWB is contained in NL. Schmid also gives an automata-theoretic characterization of REWB by introducing memory automata (MFA) [34]. Freydenberger and Schmid consider a deterministic subclass of REWB (DRX) and give an efficient linear-time matching algorithm [17].

It is natural to employ regular languages as types. Similar systems have been studied from several directions [10, 30], [6, Definition 4.7]. It should be noted that Câmpeanu and Yu’s formalization [10] permits nesting of pattern variables, and thus is different from standard typed pattern languages (cf. [10, Example 1]).

The general matching problem – deciding, for an input pattern P and a word w, if w?(P) – is NP-complete even without types [4, 11, 22, 27]. This NP-hardness does not contradict the above quadratic-time algorithm by Fernau et al. [13, 12], since it concerns the general matching problem, whereas their (and our) algorithm treats the number of repeating variables as a fixed parameter. Fernau and Schmid [14] refine the NP-completeness by analyzing bounded-parameter restrictions (such as the number of variables and the alphabet size |Σ|) and determining when they remain NP-complete or not. Fernau et al. [15] show that many natural parameterizations are 𝐖[𝟏]-hard. Under ETH (Exponential Time Hypothesis, cf. [20]), they also rule out algorithms with subexponential dependence on the number of variables even when |Σ| is bounded. For a comprehensive survey on matching patterns with variables, refer to [26].

As mentioned in Introduction, the language class of patterns PAT is incomparable with the language class REG (the class of regular languages) and CFL (the class of context-free languages) [4, Theorem 3.10]. Besides, the language class PAT is contained in NL [6, 23]. Jain et al. studied conditions under which a given pattern belongs to REG (and CFL) [21]. By their results, it can be shown that a pattern P1=αββα forms a non-context-free language [21, Lemma 11] (if |Σ|2). On the other hand, a similar pattern P2=αββγ generates a regular language: indeed, (P2)=Σ. Reidenbach and Schmid studied the setting |Σ|=2 or |Σ|=3 [31]. They showed that the language of a (fixed) pattern varies significantly by changing the alphabet size |Σ|. For a pattern Q=α 0ββ 1γ, (1) if Σ={0,1}, then (Q) is regular; however, (2) if Σ={0,1,2}, then (Q) is not regular [31, Proposition 9].

3 Preliminaries

Strings and Languages

Let Σ={σ1,σ2,,σn} be a finite alphabet. We write |Σ| for its size. In this paper, we regard Σ as fixed. Hence, |Σ| is treated as a constant in time complexity.

A word (string) over Σ is a finite sequence of symbols from Σ. The special symbol ε denotes the empty word. For a word w, we write |w| for its length; in particular, |ε|=0. We write w1w2 (or simply w1w2) for the concatenation of words w1 and w2. We extend concatenation to sets of words (languages) as follows: AB:={w1w2:w1A,w2B} for two languages A and B. We write Σi for the set of all words of length i over Σ and write Σ for the set of all words over Σ: Σ:=i0Σi with Σ0={ε}. We write w[i] for the i-th letter of w. We use 1-based indexing; thus, w[1] is the first letter. We write w[i..j] to denote the substring w[i]w[i+1]w[j] and w[i..j) to denote w[i]w[i+1]w[j1]. In particular, w[i..j]=ε with j<i and w[i..j)=ε with ji. To denote a prefix, we write w[..i) instead of w[1..i). To denote a suffix, we write w[i..) instead of w[i..|w|].

Boolean Matrices over the Boolean Semiring

Let 𝔹=({0,1},,) be the Boolean semiring, where 0 and 1 represent false and true respectively, addition is the Boolean-OR , and multiplication is the Boolean-AND .

Let M1 and M2 be n×n Boolean matrices. To denote the Boolean matrix multiplication, we write M1M2. As mentioned in Introduction, we can compute M1M2 in O(nω) time. In particular, it is known that ω<2.371339 [3].

We write M1M2 for the elementwise Boolean OR of M1 and M2. It is clear that we can compute M1M2 in O(n2) time.

Regular Expressions

Regular expressions over Σ are inductively defined by the following grammar:

EσεE+EEEE

where σΣ is a symbol. We write Regex(Σ) to denote the set of regular expressions over Σ.

The language ()Σ is defined inductively in the usual way:

(σ)={σ},(ε)={ε},()=,(E1+E2)=(E1)(E2),(E1E2)=(E1)(E2),(E)=((E)).

For ERegex(Σ), we write |E| to denote the size (i.e., the number of symbols) of E.

𝜺-NFAs (Nondeterministic Finite Automata with 𝜺-transitions)

An ε-NFA 𝒜 is a tuple 𝒜=(Q,Σ,Δ,,q0,F) where Q is a finite set of states, Σ is an input alphabet, ΔQ×Σ×Q is a set of transition rules, Q×Q is the set of ε transitions, q0Q is the initial state, and FQ is a set of final (accepting) states. We write |𝒜| to denote the number of its states |Q𝒜|.

For each transition (p,σ,q)Δ, we write p𝜎q. For (p,q), we write p𝜀q. A rule p𝜎q means that we can move from p to q by consuming the letter σ. We write p𝜀q if q is reachable from p by a (possibly empty) sequence of ε-transitions: p𝜀𝜀q. We also write p𝜎q if we can move from p to q while consuming exactly one letter σ: i.e., p,p′′Q.p𝜀p𝜎p′′𝜀q. We naturally extend this notation to arbitrary words wΣ as p𝑤q. Then, the language of 𝒜, denoted (𝒜), is the set of words accepted by 𝒜, formally defined as:

(𝒜):={wΣ:qF.q0𝑤q}.

By well-known Thompson’s construction, every regular expression E can be translated in linear time into a sparse ε-NFA 𝒜E.

Proposition 3 (Thompson’s construction [35];[32, Sec. 5]).

Every regular expression E can be translated into a sparse ε-NFA 𝒜E=(Q,Σ,Δ,,q0,F) in O(|E|) time such that (E)=(𝒜E). Moreover, |Q|,|Δ|,||O(|E|).

Pattern Languages

Let Σ be a finite alphabet, 𝒱={α,β,γ,} be a set of variables, disjoint from Σ. A pattern P is a string of Σ𝒱, P(Σ𝒱). For example, P=0 1αβαdef is a pattern where α,β𝒱 are variables and 0,1,d,e,fΣ are symbols. To instantiate the variables in a given pattern, we use assignments φ:𝒱Σ. We naturally extend φ to the monoid homomorphism φ^:(Σ𝒱)Σ by letting φ^(σ)=σ for every σΣ. For example, when φ(α)=1111 and φ(β)=010, then the above pattern P is instantiated as follows:

φ^(P)=0111110101111def.

The language generated by P is

(P):={φ^(P):φ:𝒱Σ is an assignment }.

Note that we consider the erasing semantics of pattern languages, where variables can be replaced by the empty word ε.

Typed and Regular-Typed Pattern Languages

Typed pattern languages are an extension of pattern languages with constraints on variables. In plain pattern languages as defined above, we cannot restrict the domains of words assigned to variables. For example, let us consider generating the language {w#w:wΣ,w does not contain #} where #Σ. However, the plain pattern Q=α#α generates (Q)={w#w:wΣ}, and in particular ###(Q).

For practical applications, however, it is often useful to restrict the word domains for variables. To this end, in typed pattern languages, we introduce constraints (types) on the variables. Although the original papers introducing typed pattern languages allow a very general class of types [36, 25], in this paper, we use regular languages as types.

We write 𝒞(ν) for the type assigned to ν, represented either by a regular expression over Σ or by an ε-NFA: i.e., 𝒞(ν) is a regular expression E or an ε-NFA 𝒜. Thus, |𝒞(ν)| means the size |E| of the assigned expression E or the number of states |𝒜|(=|Q𝒜|) of 𝒜.

An assignment φ:𝒱Σ is proper with respect to 𝒞 if φ(ν)(𝒞(ν)) for all variables ν occurring in P. The language generated by a regular-typed pattern P under 𝒞 is

𝒞(P):={φ^(P):φ is proper with respect to 𝒞}.

For example, let Σ={#,} and 𝒱={α}, and again consider Q=α#α. Using the type 𝒞(α)=(Σ{#}), we obtain 𝒞(Q)={w#w:w(Σ{#})}, and ###𝒞(Q).

4 Our Key Data Structure

Let W be the string used throughout this section, and let N:=|W| denote its length. We assume that the alphabet Σ is fixed in advance and is not part of the input.

In this section, we introduce our key data structure, 𝐒𝐮𝐛𝐬occ, which stores, for every (distinct) non-empty substring of W, all of its occurrences in sorted order. Our construction is very simple and in fact uses only suffix tries (rather than suffix trees) to build 𝐒𝐮𝐛𝐬occ in O(N2) time.

4.1 Sorted Occurrences and Size Bound

Let 𝐒𝐮𝐛𝐬(W) (or simply, 𝐒𝐮𝐛𝐬) be the set of nonempty substrings of W. In this paper, a substring is a contiguous factor of W. Namely, for S=abcde, bcd is a substring of S whereas ace is not.

Notation for Substrings

For any substring u of W (i.e., u𝐒𝐮𝐛𝐬(W)), we introduce the following notation:

  • OccW(u) denotes the set of all the occurrences of u in W. That is, the set of starting positions i with 1iN|u|+1 such that W[i..i+|u|)=u.

  • SoccW(u)=[i1,i2,,in] denotes the sorted list of OccW(u) in ascending order, so i1<i2<<in. In other words, SoccW() is the sorted version of OccW().

If there is no ambiguity, we simply write Occ(u) and Socc(u) instead of OccW(u) and SoccW(u), respectively.

Example.
IfW=index1 2 3 4 5 6 7 8 910letteraabaababba, then Socc(ab)=[2,5,7].

The following straightforward proposition bounds the total size of all occurrence lists, which corresponds to the total number of all substring occurrences in W.

Proposition 4.
u𝐒𝐮𝐛𝐬|Occ(u)|=u𝐒𝐮𝐛𝐬|Socc(u)|=Θ(N2).

We can show this bound by a simple calculation. The proof can be found in Appendix A.

4.2 Building Our Data Structure in 𝑶(𝑵𝟐) Time

While 𝐒𝐮𝐛𝐬(W) is just the set of nonempty substrings of W, our actual data structure is

𝐒𝐮𝐛𝐬occ(W):={u,Socc(u):u𝐒𝐮𝐛𝐬(W)}.

We now show how to compute this data structure in O(N2) using the suffix trie of W. Before delving into the formal algorithm, we illustrate our approach.

Illustration of Our Approach.

Our strategy is to augment the standard suffix trie of W into a decorated version, where each node is labeled with the sorted occurrence list of the substring it represents. The following example illustrates this target structure. The left tree is the (plain) suffix trie of W$=a1b2a3a4b5a6$ constructed by our procedure, where $Σ is the standard extra terminal symbol used in suffix tries. The right tree is its decorated version, where each non-root node n is labeled with SoccW$(Γ(n)). Here Γ(n) denotes the substring represented by node n.

Suffix Trie

We briefly recall the standard definition of suffix tries [9, 8]. A suffix trie of W is a rooted tree of maximum out-degree |Σ|+1 representing all suffixes of the string W$:=W$, where $Σ is a unique terminal symbol. The suffix trie can be constructed by the following procedure:

  1. 1.

    Initialize 𝒯 to be the empty tree whose root is unlabeled.

  2. 2.

    For every suffix s=a1a2ak$ of W$, traverse 𝒯 from the root using the letters a1,a2,,ak,$ in this order. When traversing, if there is no outgoing edge labeled by the current letter from the current node, add such an edge to a fresh child node.

  3. 3.

    Assign an integer 𝖨𝖽𝗑(n) to each leaf node n, denoting the starting index of the suffix represented by n in W$.

We write 𝒯W to denote this suffix trie built for W$. We can construct 𝒯W in O(N2) time.

Proposition 5 (Known Fact).

The suffix trie 𝒯W can be constructed in O(N2) time.

For every node n of 𝒯W, we write Γ(n) for the string spelled on the path from the root to n. Namely, if rootσ1n1σ2σkn, then Γ(n)=σ1σ2σk. In particular, for every leaf node n labeled with 𝖨𝖽𝗑(n), the string Γ(n) is the suffix of W$ starting at 𝖨𝖽𝗑(n).

Decorated Suffix Trie

We define the decorated suffix trie 𝒯~W as the suffix trie 𝒯W augmented with an additional label Υ𝑠𝑜𝑐𝑐(n) for every non-root node n, satisfying Υ𝑠𝑜𝑐𝑐(n)=SoccW$(Γ(n)).

Lemma 6.

We can transform 𝒯W into the corresponding 𝒯~W in O(N2) time.

Proof.

We traverse 𝒯W by a DFS in postorder.

When visiting a leaf node n of 𝒯W, we simply set Υ𝑠𝑜𝑐𝑐(n):=[𝖨𝖽𝗑(n)].

Next, consider an internal node n. Suppose that n has k children {(σ1,c1),(σ2,c2),,(σk,ck)} where σiΣ{$} and the pair (σi,ci) means that there exists an edge nσici in 𝒯W. Then, Γ(ci)=Γ(n)σi holds.

In this postorder DFS, all lists Υ𝑠𝑜𝑐𝑐(ci) for ci𝑐ℎ𝑖𝑙𝑑𝑟𝑒𝑛(n)={c1,,ck} have already been computed when n is visited. Hence, we build Υ𝑠𝑜𝑐𝑐(n) as follows:

Υ𝑠𝑜𝑐𝑐(n):=merge(Υ𝑠𝑜𝑐𝑐(c1),Υ𝑠𝑜𝑐𝑐(c2),,Υ𝑠𝑜𝑐𝑐(ck)).

Here Υ𝑠𝑜𝑐𝑐(n) indeed equals SoccW$(Γ(n)), because every occurrence of Γ(n) in W$ is followed by exactly one character, which determines a unique child ci𝑐ℎ𝑖𝑙𝑑𝑟𝑒𝑛(n). In particular, the lists Υ𝑠𝑜𝑐𝑐(c1),,Υ𝑠𝑜𝑐𝑐(ck) are pairwise disjoint. Since each Υ𝑠𝑜𝑐𝑐(ci) is a sorted list, we can efficiently merge them using the well-known k-way merge (or k-pointer) technique. Because k|Σ|+1=O(1), the time needed for this merging is O(i=1k|Υ𝑠𝑜𝑐𝑐(ci)|).

Summing over all internal nodes, the total merge cost (i.e., the running time) is Θ(N2) as follows:

n:internal nodec𝑐ℎ𝑖𝑙𝑑𝑟𝑒𝑛(n)|Υ𝑠𝑜𝑐𝑐(c)|=n:nodes,nroot|Υ𝑠𝑜𝑐𝑐(n)|=u𝐒𝐮𝐛𝐬(W$)|SoccW$(u)|=Θ(N2).

For the last equation, we apply Proposition 4 to W$. Since |W$|=N+1, this yields Θ(N2).

To build our goal 𝐒𝐮𝐛𝐬occ(W), we only need to visit every internal node except the root node of 𝒯~W because the sentinel $ appears only at the leaves, meaning that the internal nodes (excluding the root) exactly correspond to all nonempty substrings of W. This leads to the main result of this section.

Lemma 7.

We can build 𝐒𝐮𝐛𝐬occ(W) in O(N2) time.

5 Data Structure for Range Query

Let W be the string used throughout this section and let N:=|W| denote its length.

Let 𝒜=(Q,Σ,Δ,,q0,F) be an ε-NFA. Recall that Δ is the set of labeled transitions p𝜎q and is the set of ε-transitions p𝜀q.

In this section, we show an efficient way to compute a Boolean table [i][j][p][q] that satisfies the following: for 1ijN and p,qQ,

[i][j][p][q]=1q is reachable from p by consuming the substring W[i..j].
Preprocessing.
  • For , we build its adjacency list Adj in O(||) time and adjacency matrix in O(|Q|2) time.

  • For Δ, we build its adjacency list AdjΔ(σ) in O(|Δ|) time and adjacency matrix Δ(σ) in O(|Q|2) time for each σΣ.

5.1 Computing 𝓡 for Sparse NFAs

Let SQ be a set of states. We represent S as a Boolean array indexed by states, so that membership queries qS can be answered in O(1) time.

We first consider the following two functions:

ε-closure(S):={qQ:pS.p𝜀q},𝗆𝗈𝗏𝖾(S,σ):={qQ:pS.p𝜎q}.

(1) We can compute ε-closure(S) in O(|Q|+||) time as follows. We consider the directed graph G:=(Q,) consisting only of ε-transitions. Then ε-closure(S) is exactly the set of vertices reachable from the source set S in G. Hence, using the adjacency list Adj of G, we can compute ε-closure(S) by a standard multi-source graph search: initialize the queue with all states in S, mark them as visited, and traverse only ε-edges. By the standard linear-time analysis of BFS on adjacency-list graphs [7, Chapter 20], this takes O(|Q|+||) time. (2) We can compute 𝗆𝗈𝗏𝖾(S,σ) in O(|Δ|) time by scanning the labeled transition set Δ once and collecting all targets q such that (σ,q)AdjΔ(p) with pS.

We now consider the following function that computes the set {qQ:pS.p𝜎q}:

𝖼𝗈𝗇𝗌𝗎𝗆𝖾(S,σ):=ε-closure(𝗆𝗈𝗏𝖾(ε-closure(S),σ)).

It can be computed in O(|Q|+||+|Δ|) time. This construction and time bound are standard in the context of efficient ε-NFA simulation; see, e.g., [2, Section 3.7].

Now we can compute the table in O(N2|Q|(|Q|+||+|Δ|)) time as follows. We assume all entries of are initialized to 0.

Proposition 8.

We can compute for 𝒜 in O(N2|Q|(|Q|+||+|Δ|)) time.

5.2 Computing 𝓡 for Dense NFAs

When the ε-NFA is dense (i.e., |Δ|+||=Θ(|Q|2)), the above procedure for requires O(N2|Q|3) time. Here, for dense ε-NFAs, we give an alternative O(N2|Q|ω)-time algorithm.

Computing 𝓔~ in 𝑶(|𝑸|𝝎) time.

We first compute the ε-reachability relation ~Q×Q as a |Q|×|Q| Boolean matrix in O(|Q|ω) time. Note that computing ~ by running the above 𝜺-closure({q}) for every qQ would take O(|Q|2+|Q|||) time, which becomes cubic when ||=Θ(|Q|2).

Observe that ~ is the reflexive transitive closure of . Namely, ~= holds. Now, we use the following classical result for efficiently computing . On the Boolean semiring 𝔹, for a given n×n Boolean matrix M, we can compute the reflexive transitive closure M of M in O(nω) time [28, 18, 16, 1]. This leads to the following property.

Proposition 9 (Known Result [28, 18, 16];[1, § 5.9]).

We can compute ~ in O(|Q|ω) time.

Computing 𝓡.

Using ~, we can compute in O(N2|Q|ω) time as follows.

For each σΣ, we pre-compute Δ~σ:=~Δ(σ)~ in O(|Q|ω) time. By definition, Δ~σ[p][q]=1 iff p𝜎q. We then run the following O(N2|Q|ω)-time DP algorithm. For notational convenience, we identify [i][j] with the |Q|×|Q| Boolean matrix whose (p,q)-entry is [i][j][p][q].

This procedure leads to the following time complexity.

Proposition 10.

We can compute for 𝒜 in O(N2|Q|ω) time.

6 Quadratic Time Algorithm for 𝜶𝟏𝜷𝜶𝟐𝜷𝜶𝟑

In this section, we consider the matching problem for the pattern α1βα2βα3. Namely, given an input string W, we decide whether there exists a decomposition

W=w1wβw2wβw3such that wi(𝒞(αi)) for i{1,2,3} and wβ(𝒞(β))

In Section 7, we extend the algorithm presented here to the more general patterns α1βα2βα3ββαK.

Notation

We first set up the notation used in this section. Let W be an input string and N:=|W|.

We work uniformly with ε-NFA representations: for i{1,2,3}, let 𝒜i denote the ε-NFA for 𝒞(αi), and let denote the ε-NFA for 𝒞(β). If a type is given by a regular expression, we first replace it by the equivalent ε-NFA obtained by Thompson’s construction (Proposition 3).

Let m=max{|𝒞(α1)|,|𝒞(α2)|,|𝒞(α3)|,|𝒞(β)|} and mQ:=max(|QA1|,|QA2|,|QA3|,|QB|). In the regular expression case, Thompson’s construction yields mQ=O(m). In the ε-NFA case, by definition, mQ=m.

For any ε-NFA 𝒜=(Q,Σ,Δ,,q0,F), once the table 𝒜 has been computed using Proposition 8 or 10, we define a Boolean table 𝒜𝒞𝒞𝒜[i][j] for 1ijN by:

𝒜𝒞𝒞𝒜[i][j]=1𝒜 accepts the substring W[i..j].

Given 𝒜, the table 𝒜𝒞𝒞𝒜 can be built in O(N2|Q|) time by checking, for each 1ijN, whether there exists qF with 𝒜[i][j][q0][q]=1.

We also use half-open interval notation for these tables. For ij, we write 𝒜[ij) for 𝒜[i][j1]; in particular, 𝒜[ii) is the ε-reachability matrix. Similarly, we write 𝒜𝒞𝒞𝒜[ij) for 𝒜𝒞𝒞𝒜[i][j1] when i<j. We set 𝒜𝒞𝒞𝒜[i..i)=1 iff ε(𝒜).

Finally, for substring u of W, we write Soccu for Socc(u) and #u for |Soccu|. For 1x#u, we write x for Soccu[x].

Preprocessing Cost

Building 𝐒𝐮𝐛𝐬occ(W) takes O(N2) time by Lemma 7.

Once the four tables 𝒜1,𝒜2,𝒜3, have been computed, building the corresponding four 𝒜𝒞𝒞 tables takes the following time in total:

O(N2(|Q𝒜1|+|Q𝒜2|+|Q𝒜3|+|Q|))O(N2mQ).

If the input types 𝒞(ν) are given by regular expressions, then by Thompson’s construction (Proposition 3), each type is translated into a sparse ε-NFA with O(|𝒞(ν)|) states and transitions. Hence, by Proposition 8, computing the four -tables takes the following time:

O(N2(|Q𝒜1|2+|Q𝒜2|2+|Q𝒜3|2+|Q|2))O(N2mQ2).

If the input types are given by ε-NFAs, then by Proposition 10, computing the four -tables takes the following time:

O(N2(|Q𝒜1|ω+|Q𝒜2|ω+|Q𝒜3|ω+|Q|ω))O(N2mQω).

Therefore, using mQ=O(m) in the regular-expression case and mQ=m in the ε-NFA case, we obtain the following time bound.

Proposition 11.

The total preprocessing time is O(N2m2) when the types are given by regular expressions, and O(N2mω) when they are given by ε-NFAs.

6.1 Our Procedure

Concentrating on 𝒘𝜷𝜺.

We first handle the easy case wβ=ε. If ε(), let 𝒜 be the ε-NFA obtained by concatenating 𝒜1,𝒜2,𝒜3, where |Ξ𝒜|=O(|Ξ𝒜1|+|Ξ𝒜2|+|Ξ𝒜3|) for Ξ{Q,Δ,}. Then deciding whether W(𝒜) is just the standard membership problem for ε-NFAs, which can be solved in O(N(|Q𝒜|+|Δ𝒜|+|𝒜|)) time by standard ε-NFA simulation [2, Section 3.7]. If this test succeeds, we return MATCH.

Therefore, in the remainder of this section, we focus on the non-empty case.

Our Strategy.

Our overall strategy is as follows:

  1. 1.

    For each pair u,Soccu𝐒𝐮𝐛𝐬occ(W), we treat u as a candidate for wβ.

  2. 2.

    Check whether u() in O(1) time by picking arbitrary iSoccu and testing 𝒜𝒞𝒞[i..i+|u|).

  3. 3.

    If u(), we decide whether there exists a valid decomposition W=w1uw2uw3 in O(#u|Q𝒜2|2) time. This deciding procedure will be given below (Section 6.2).

The total running time over all candidates u is

u,Soccu𝐒𝐮𝐛𝐬occ(W)O(#u|Q𝒜2|2)=O(|Q𝒜2|2u,Soccu𝐒𝐮𝐛𝐬occ(W)#u)=by Prop.4O(N2|Q𝒜2|2).

6.2 Procedure for Deciding whether 𝑾=𝒘𝟏𝒖𝒘𝟐𝒖𝒘𝟑

For a fixed substring u of W, we give a decision procedure for deciding whether there exists such a decomposition that runs in O(#u|Q𝒜2|2) time.

Before giving our procedure, we introduce an auxiliary procedure NextPhase as follows.

NextPhase(Cur,𝒜)

 

Input: Cur: #u-length Boolean vector and 𝒜: ε-NFA.

Output: Next: #u-length Boolean vector that satisfies the following condition called (): for every x[1..#u],

Next[x]=1z[1..#u].z+|u|xCur[z]=1W[z+|u|..x)(𝒜).

We give a complete implementation of this procedure in Section 6.3. In particular, this procedure runs in O(#u|Q𝒜|2) time.

Using NextPhase, we build our procedure FindDecomposition(u) as follows:

 Remark.

For any ε-NFA 𝒜, if i=N+1, 𝒜𝒞𝒞𝒜[i..)=1 iff ε(𝒜).

By Lines 35, we have 𝐜𝐡𝐞𝐜𝐤𝒜1[x]=1W[..x)(𝒜1). We then have the following from the property () of NextPhase:

𝐜𝐡𝐞𝐜𝐤𝒜2[x]=1z[1..#u].z+|u|xW[..z)(𝒜1)W[z+|u|..x)(𝒜2).

This equivalence and Lines 710 immediately lead to the correctness of our procedure.

Lemma 12.

FindDecomposition(u) returns 1 iff there exists a valid decomposition W=w1uw2uw3 such that w1(𝒜1), w2(𝒜2), and w3(𝒜3).

Time Complexity.

The above procedure runs in O(#u|Q𝒜2|2) for each fixed substring u. Hence, the total time is O(N2|Q𝒜2|2) as (✩). Proposition 11 leads to the following results.

Lemma 13.

The matching problem W𝒞(α1βα2βα3) can be solved in O(N2m2) time when the types are given by regular expressions, and in O(N2mω) time when they are given by ε-NFAs.

6.3 Implementation and Properties of NextPhase

A naive implementation of NextPhase checks condition () for every pair (z,x) with 1z<x#u. This takes O(#u2) time, which is slow for our target bound.

To avoid this quadratic dependence on #u, our approach is based on a monotone two-pointer scan. Let 𝐈𝐧𝐢𝐭𝒜 be the |Q𝒜|-length Boolean vector representing the ε-closure of the initial state of 𝒜.

For a fixed target occurrence x, after Line 6 the vector 𝐬𝐭𝐚𝐭𝐞𝐬 already contains the contribution of all already-scanned indices z<y with Cur[z]=1, advanced from the previous endpoint (x1) to the current endpoint x. The while-loop then scans newly eligible indices y satisfying y+|u|x. Whenever Cur[y]=1, we add the runs of 𝒜 starting at y+|u| and ending at x. Hence, after the while-loop terminates, 𝐬𝐭𝐚𝐭𝐞𝐬 represents exactly the states reachable from the initial state of 𝒜 after reading W[z+|u|..x) for all already-processed indices z<y with Cur[z]=1. We formalize this argument by the following invariant.

Invariant 𝐈𝐧𝐯(𝐬𝐭𝐚𝐭𝐞𝐬,x,y)

 

For all states qQ𝒜, the following conditions are equivalent:

  • 𝐬𝐭𝐚𝐭𝐞𝐬[q]=1

  • there is an index z such that

    • z<y and Cur[z]=1 and z+|u|x and

    • q is reachable from the initial state of 𝒜 after reading W[z+|u|..x).

With the invariant 𝐈𝐧𝐯(𝐬𝐭𝐚𝐭𝐞𝐬,x,y) formally defined, we now state the key preservation property of this invariant. This property serves as the cornerstone for proving the overall correctness of our algorithm.

Lemma 14.

During the execution of the procedure NextPhase(Cur,𝒜), the invariant 𝐈𝐧𝐯(𝐬𝐭𝐚𝐭𝐞𝐬,x,y) holds at Line 7 and Line 12.

The key point is that Line 6 advances all already represented runs from (x1) to x, while the while-loop incorporates each newly eligible witness exactly once. The full proof is deferred to Appendix B.

We now use this preservation property to derive the correctness of NextPhase.

Lemma 15.

NextPhase(Cur,𝒜) returns a vector Next that satisfies the following condition: for every x[1..#u],

Next[x]=1z[1..#u].z+|u|xCur[z]=1W[z+|u|..x)(𝒜).

Using Lemma 14, we observe that, when the while-loop terminates, no unprocessed index can still satisfy z+|u|x. Hence the invariant captures exactly all valid witnesses. The full proof is deferred to Appendix C.

We next analyze the running time of NextPhase.

Lemma 16.

The time complexity of the procedure NextPhase(Cur,𝒜) is bounded by O(#u|Q𝒜|2), where |Q𝒜| is the number of states of 𝒜.

The key observation is that the pointer y moves only forward, so the body of the while-loop is executed at most #u times in total. The full amortized analysis is deferred to Appendix D.

7 Quadratic Time Algorithm for General Case

We now extend the construction of Section 6 to the general form α1βα2ββαK. As in Section 6, we first handle the easy case wβ=ε separately: if εL(), we test whether W is accepted by the ε-NFA obtained by concatenating 𝒜1𝒜2𝒜K. In the remainder of this section, we therefore focus on the case wβε.

Procedure for a Fixed Candidate 𝒖

As a representative example, consider the pattern α1βα2βα3βα4. For a fixed nonempty candidate substring u, we extend our FindDecomposition as follows.

By the same argument as in Lemma 12, FindDecomposition(u) returns 1 iff there exists a valid decomposition W=w1uw2uw3uw4 such that wi(𝒜i) for i{1,2,3,4}.

The same construction extends immediately to the general pattern PK=α1βα2ββαK: for a fixed candidate u,

  1. 1.

    First, define 𝐜𝐡𝐞𝐜𝐤𝒜1[x]=1 iff W[..x)(𝒜1); and then

  2. 2.

    Next, iteratively set 𝐜𝐡𝐞𝐜𝐤𝒜i:=NextPhase(𝐜𝐡𝐞𝐜𝐤𝒜i1,𝒜i) for i=2,,K1.

  3. 3.

    Finally, accept iff there exists x such that 𝐜𝐡𝐞𝐜𝐤𝒜K1[x]=1 and 𝒜𝒞𝒞𝒜K[x+|u|..)=1.

Time Complexity

For a fixed candidate u, the procedure performs K2 calls to NextPhase. Hence, by Lemma 16, its running time is O((K2)#um2) where m=max{|𝒞(α1)|,,|𝒞(αK)|,|𝒞(β)|}. Summing over all candidates u and using u𝐒𝐮𝐛𝐬(W)#u=O(N2), we obtain O((K2)N2m2). Recall that, for a type 𝒞(ν), if 𝒞(ν) is a regular expression E, then |𝒞(ν)| is its expression size |E|; if 𝒞(ν) is an ε-NFA 𝒜, then |𝒞(ν)| is its number of states |Q𝒜|.

In the regular-expression case, together with the preprocessing cost for the K+1 ε-NFAs 𝒜1,,𝒜K and , this yields O((K+1)N2m2). In the ε-NFA case, together with the preprocessing cost, this yields O((K+1)N2mω).

Now we have the following main theorems, which are restated from Introduction.

See 1 See 2

References

  • [1] Alfred V. Aho, John E. Hopcroft, and Jeffrey D. Ullman. The Design and Analysis of Computer Algorithms. Addison-Wesley, USA, 1974. URL: https://dl.acm.org/doi/10.5555/578775.
  • [2] Alfred V. Aho, Monica S. Lam, Ravi Sethi, and Jeffrey D. Ullman. Compilers: Principles, Techniques, and Tools (2nd Edition). Addison Wesley, 2006.
  • [3] Josh Alman, Ran Duan, Virginia Vassilevska Williams, Yinzhan Xu, Zixuan Xu, and Renfei Zhou. More asymmetry yields faster matrix multiplication. In SODA 2025, pages 2005–2039. SIAM, 2025. doi:10.1137/1.9781611978322.63.
  • [4] Dana Angluin. Finding patterns common to a set of strings. J. Comput. Syst. Sci., 21(1):46–62, 1980. doi:10.1016/0022-0000(80)90041-0.
  • [5] Dana Angluin. Inductive inference of formal languages from positive data. Inf. Cont., 45(2):117–135, 1980. doi:10.1016/S0019-9958(80)90285-5.
  • [6] Henning Bordihn, Jürgen Dassow, and Markus Holzer. Extending regular expressions with homomorphic replacement. RAIRO Theor. Inf. Appl., 44(2):229–255, 2010. doi:10.1051/ita/2010013.
  • [7] Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein. Introduction to Algorithms, 4th edition. MIT Press, 2022. URL: https://mitpress.mit.edu/9780262046305/introduction-to-algorithms/.
  • [8] Maxime Crochemore, Christophe Hancart, and Thierry Lecroq. Algorithms on Strings. Cambridge University Press, 2007. doi:10.1017/CBO9780511546853.
  • [9] Maxime Crochemore and Wojciech Rytter. Jewels of Stringology. World Scientific, 2002. doi:10.1142/4838.
  • [10] Cezar Câmpeanu and Sheng Yu. Pattern expressions and pattern automata. Inf. Process. Lett., 92(6):267–274, 2004. doi:10.1016/j.ipl.2004.09.007.
  • [11] Andrzej Ehrenfeucht and Grzegorz Rozenberg. Finding a homomorphism between two words is NP-complete. Inf. Process. Lett., 9(2):86–88, 1979. doi:10.1016/0020-0190(79)90135-2.
  • [12] Henning Fernau, Florin Manea, Robert Mercaş, and Markus L. Schmid. Pattern matching with variables: Fast algorithms and new hardness results. In STACS 2015, volume 30 of LIPIcs, pages 302–315. Schloss Dagstuhl, 2015. doi:10.4230/LIPIcs.STACS.2015.302.
  • [13] Henning Fernau, Florin Manea, Robert Mercaş, and Markus L. Schmid. Pattern matching with variables: Efficient algorithms and complexity results. ACM Trans. Comput. Theory, 12(1):6:1–6:37, 2020. doi:10.1145/3369935.
  • [14] Henning Fernau and Markus L. Schmid. Pattern matching with variables: A multivariate complexity analysis. Inf. Comput., 242:287–305, 2015. doi:10.1016/j.ic.2015.03.006.
  • [15] Henning Fernau, Markus L. Schmid, and Yngve Villanger. On the parameterised complexity of string morphism problems. Theory Comput. Syst., 59(1):24–51, 2016. doi:10.1007/s00224-015-9635-3.
  • [16] Michael J. Fischer and Albert R. Meyer. Boolean matrix multiplication and transitive closure. In SWAT 1971, pages 129–131. IEEE Computer Society, 1971. doi:10.1109/SWAT.1971.4.
  • [17] Dominik D. Freydenberger and Markus L. Schmid. Deterministic regular expressions with back-references. J. Comput. Syst. Sci., 105:1–39, 2019. doi:10.1016/j.jcss.2019.04.001.
  • [18] M. E. Furman. Application of a method of rapid multiplication of matrices to the problem of finding the transitive closure of a graph. Doklady Akademii Nauk SSSR, 194(3):524, 1970. URL: https://www.mathnet.ru/eng/dan35686.
  • [19] Juris Hartmanis. On non-determinancy in simple computing devices. Acta Informatica, 1(4):336–344, 1972. doi:10.1007/BF00289513.
  • [20] Russell Impagliazzo, Ramamohan Paturi, and Francis Zane. Which problems have strongly exponential complexity? J. Comput. Syst. Sci., 63(4):512–530, 2001. doi:10.1006/jcss.2001.1774.
  • [21] Sanjay Jain, Yuh Shin Ong, and Frank Stephan. Regular patterns, regular languages and context-free languages. Inf. Process. Lett., 110(24):1114–1119, 2010. doi:10.1016/j.ipl.2010.09.010.
  • [22] Tao Jiang, Efim Kinber, Arto Salomaa, Kai Salomaa, and Sheng Yu. Pattern languages with and without erasing. Int. J. Comp. Math., 50(3–4):147–163, 1994. doi:10.1080/00207169408804252.
  • [23] Neil D. Jones and Sven Skyum. Recognition of deterministic ETOL languages in logarithmic space. Inf. Control., 35(3):177–181, 1977. doi:10.1016/S0019-9958(77)90058-4.
  • [24] Vladimir Komendantsky. Matching problem for regular expressions with variables. In TFP 2012, pages 149–166. Springer, 2013. doi:10.1007/978-3-642-40447-4_10.
  • [25] Takeshi Koshiba. Typed pattern languages and their learnability. In EuroCOLT 1995, volume 904 of LNCS, pages 367–379. Springer, 1995. doi:10.1007/3-540-59119-2_192.
  • [26] Florin Manea and Markus L. Schmid. Matching patterns with variables. In Robert Mercaş and Daniel Reidenbach, editors, Combinatorics on Words, pages 1–27. Springer, 2019. doi:10.1007/978-3-030-28796-2_1.
  • [27] Alexandru Mateescu and Arto Salomaa. Aspects of classical language theory. In Grzegorz Rozenberg and Arto Salomaa, editors, Handbook of Formal Languages, Volume 1: Word, Language, Grammar, pages 175–251. Springer, 1997. doi:10.1007/978-3-642-59136-5_4.
  • [28] Ian Munro. Efficient determination of the transitive closure of a directed graph. Inf. Process. Lett., 1(2):56–58, 1971. doi:10.1016/0020-0190(71)90006-8.
  • [29] Taisei Nogami and Tachio Terauchi. Efficient matching of some fundamental regular expressions with backreferences. In MFCS 2025, volume 345 of LIPIcs, pages 81:1–81:19. Schloss Dagstuhl, 2025. doi:10.4230/LIPIcs.MFCS.2025.81.
  • [30] Dirk Nowotka and Max Wiedenhöft. The equivalence problem of E-pattern languages with length constraints is undecidable. In CPM 2025, volume 331 of LIPIcs, pages 4:1–4:23. Schloss Dagstuhl, 2025. doi:10.4230/LIPIcs.CPM.2025.4.
  • [31] Daniel Reidenbach and Markus L. Schmid. Regular and context-free pattern languages over small alphabets. Theor. Comput. Sci., 518:80–95, 2014. doi:10.1016/j.tcs.2013.07.035.
  • [32] Jacques Sakarovitch. Elements of Automata Theory. Cambridge University Press, 2009. doi:10.1017/CBO9781139195218.
  • [33] Markus L. Schmid. Inside the class of regex languages. Int. J. Found. Comput. Sci., 24(7):1117–1134, 2013. doi:10.1142/S0129054113400340.
  • [34] Markus L. Schmid. Characterising REGEX languages by regular languages equipped with factor-referencing. Inf. Comput., 249:1–17, 2016. doi:10.1016/j.ic.2016.02.003.
  • [35] Ken Thompson. Regular expression search algorithm. Commun. ACM, 11(6):419–422, 1968. doi:10.1145/363347.363387.
  • [36] Keith Wright. Inductive identification of pattern languages with restricted substitutions. In COLT 1990, pages 111–121. Morgan Kaufmann, 1990. URL: http://dl.acm.org/citation.cfm?id=92595.

Appendix A Proof of Proposition 4

Proposition 4. [Restated, see original statement.]
u𝐒𝐮𝐛𝐬|Occ(u)|=u𝐒𝐮𝐛𝐬|Socc(u)|=Θ(N2).
Proof.

Let h be an integer with 1hN, and write 𝐒𝐮𝐛𝐬h for the set of h-length substrings: 𝐒𝐮𝐛𝐬h:={u𝐒𝐮𝐛𝐬:|u|=h}.

For all positions i with 1iNh+1, there is a unique h-length substring u=W[i..i+h). Thus, the multiset of all occurrences of all h-length substrings is in one-to-one correspondence with the set of all possible starting positions {1,,Nh+1}. Therefore, u𝐒𝐮𝐛𝐬h|Occ(u)|=Nh+1 holds.

Summing over all lengths h=1,2,,N, we obtain

u𝐒𝐮𝐛𝐬|Occ(u)|=h=1N(Nh+1)=N(N+1)2=Θ(N2).

Since |Socc(u)|=|Occ(u)| by our definition, the above equation completes the proof.

Appendix B Proof of Lemma 14

Lemma 14. [Restated, see original statement.]

During the execution of the procedure NextPhase(Cur,𝒜), the invariant 𝐈𝐧𝐯(𝐬𝐭𝐚𝐭𝐞𝐬,x,y) holds at Line 7 and Line 12.

(Line 7 and Line 12 refer to the numbered comment lines in the pseudocode.)

Proof.

Fix an outer-loop index x[2..#u]. We show that 𝐈𝐧𝐯(𝐬𝐭𝐚𝐭𝐞𝐬,x,y) holds at the following two evaluation points:

  1. (1)

    Line 7: immediately before each test of the while-condition in the x-th iteration; and

  2. (2)

    Line 12: immediately after the increment of y inside the while-loop.

For the base case, consider the first outer-loop iteration x=2. At the first evaluation point (Line 7), Line 6 has already been executed. Since 𝐬𝐭𝐚𝐭𝐞𝐬 was initialized to the all-zero vector, multiplying it by 𝒜[(x1)..x) still yields the zero vector. Besides, y=1. As there is no index z<1, both sides of the equivalence are false for every state q.

Next, assume the invariant holds after termination of the while-loop in the (x1)-st outer iteration. Then Line 6 multiplies 𝐬𝐭𝐚𝐭𝐞𝐬 by 𝒜[(x1)..x), which advances every currently represented run from endpoint (x1) to endpoint x. Moreover, since the previous while-loop terminated only when no further eligible index remained, every index z<y already satisfies z+|u|(x1). Therefore, at Line 7, all runs already represented in 𝐬𝐭𝐚𝐭𝐞𝐬 are correctly advanced to the new endpoint x, and the corresponding witnesses z<y remain valid. Hence the invariant also holds at the first evaluation point (Line 7) in the x-th iteration.

Now assume that the invariant holds before some test of the while-condition, and suppose the condition is true. If Cur[y]=0, then Line 10 is skipped, 𝐬𝐭𝐚𝐭𝐞𝐬 is unchanged, and incrementing y does not add any new witness; hence the invariant continues to hold. If Cur[y]=1, then Line 10 adds exactly the states reachable from the initial state of 𝒜 after reading W[y+|u|..x), i.e., it adds precisely the missing witness z=y. After the increment of y, the invariant is restored in the form with z<y.

Thus the invariant holds at all claimed program points.

Appendix C Proof of Lemma 15

Lemma 15. [Restated, see original statement.]

NextPhase(Cur,𝒜) returns a vector Next that satisfies the following condition: for every x[1..#u],

Next[x]=1z[1..#u].z+|u|xCur[z]=1W[z+|u|..x)(𝒜).
Proof.

First, consider the base case x=1. For x=1, in the right-hand side of (), any candidate witness z satisfies z1, hence zx. Since |u|1, we have z+|u|>x. Therefore the right-hand side of () is false. Since Next[1]=0 holds, () also holds.

Next, fix an outer-loop index x[2..#u]. By Lemma 14, the invariant 𝐈𝐧𝐯(𝐬𝐭𝐚𝐭𝐞𝐬,x,y) holds at every evaluation point of the while-loop: namely, immediately before each test of the while-condition (Line 7), and, whenever the loop body is executed, immediately after the increment of y (Line 12). Therefore, when the while-loop terminates, the invariant still holds: if the loop body has been executed at least once, this is given by the last occurrence of Line 12; otherwise, it is given by the first occurrence of Line 7.

Observe the termination condition of the while-loop. It terminates when either y>#u or y+|u|>x. Because the sequence of occurrence indices zz is strictly increasing, this termination condition guarantees that no index zy can satisfy z+|u|x. In other words, any index z[1..#u] satisfying z+|u|x must strictly satisfy z<y.

Therefore, we can safely drop the restriction z<y from the invariant. It follows that for every state qQ𝒜, we have states[q]=1 if and only if there exists an index z[1..#u] such that Cur[z]=1, z+|u|x, and q is reachable from the initial state of 𝒜 after reading W[z+|u|..x).

At Line 14, the procedure sets Next[x]=1 if and only if 𝐬𝐭𝐚𝐭𝐞𝐬F𝒜. This intersection is non-empty exactly when there exists an accepting state qF𝒜 such that states[q]=1. Combining this with the above equivalence, we obtain:

Next[x]=1qF𝒜.𝐬𝐭𝐚𝐭𝐞𝐬[q]=1z[1..#u].z+|u|xCur[z]=1qF𝒜.q is reachable from the initial state of 𝒜 after reading W[z+|u|..x)z[1..#u].z+|u|xCur[z]=1W[z+|u|..x)(𝒜).

Together with the base case x=1, this establishes () for all x[1..#u].

Appendix D 𝑶(#𝒖|𝑸𝓐|𝟐)-time Complexity Analysis of NextPhase

We show the following about NextPhase(Cur,𝒜).

Lemma 16. [Restated, see original statement.]

The time complexity of the procedure NextPhase(Cur,𝒜) is bounded by O(#u|Q𝒜|2), where |Q𝒜| is the number of states of 𝒜.

Proof.

Recall that every precomputed 𝒜[a..b) can be accessed in O(1) time.

We break down the cost of each operation as follows.

1. Initialization Phase.

Lines 24 perform the necessary initializations. Initializing the index y takes O(1) time. The Boolean array states, which acts as a state configuration vector, is initialized to all zeros in O(|Q𝒜|) time. Similarly, the Boolean array Next of length #u is initialized to all zeros in O(#u) time. Thus, the total initialization cost is O(#u+|Q𝒜|).

2. Main Loop and Vector-Matrix Multiplications.

The (outer) for-loop iterates #u1 times. In each iteration, we perform the following key operations:

  • State Extension (Line 6): We compute the multiplication of a Boolean row vector states of size |Q𝒜| and a Boolean matrix 𝒜 of size |Q𝒜|×|Q𝒜|. Using standard matrix multiplication over the Boolean semiring, this operation takes O(|Q𝒜|2) time per iteration. Over the entire for-loop, this contributes O(#u|Q𝒜|2) to the total running time.

  • Acceptance Check (Lines 1314): Checking the intersection 𝐬𝐭𝐚𝐭𝐞𝐬F𝒜 corresponds to a bitwise AND operation between two Boolean arrays of size |Q𝒜|, followed by a check if any bit is set. This takes O(|Q𝒜|) time per iteration, contributing O(#u|Q𝒜|) overall. Assigning 1 to Next[x] takes O(1) time.

3. Amortized Analysis of the While Loop.

The while-loop incrementally adds the contributions of newly eligible y with Cur[y]=1 to 𝐬𝐭𝐚𝐭𝐞𝐬. Although it is nested within the for loop, the index variable y is initialized to 1 and is only incremented at Line 11. Because y is never decreased and can reach at most #u, the body of the while loop is executed at most #u times in total across all iterations of the outer for loop. The total number of evaluations of the while-condition is also O(#u), which does not affect the final bound.

Inside the while loop, the array lookup Cur[y] takes O(1) time. The dominant operation is Line 10, where we again multiply the Boolean vector 𝐈𝐧𝐢𝐭𝒜 by the precomputed matrix 𝒜[y+|u|..x) and compute the element-wise Boolean OR () with the current states array. The vector-matrix multiplication takes O(|Q𝒜|2) time, and the Boolean OR takes O(|Q𝒜|) time. Since this inner body runs at most #u times globally, the aggregated time spent inside the while loop over the entire procedure is strictly bounded by O(#u|Q𝒜|2).

Summing the costs of initialization, the outer loop operations, and the globally bounded inner loop operations, the overall time complexity is:

O(#u+|Q𝒜|Lines 24+#u|Q𝒜|2total Line 6+#u|Q𝒜|2total Line 10+#u|Q𝒜|total Line 13)=O(#u|Q𝒜|2).