Abstract 1 Introduction 2 Problem Statement and First Results 3 Out-of-Order Monoid Evaluation 4 Constant-Space Out-of-Order Semigroup Evaluation 5 Other Complexity Regimes 6 Conclusion and Future Work References

Out-Of-Order Membership in Regular Languages

Antoine Amarilli ORCID Univ. Lille, INRIA, CNRS, Centrale Lille, UMR 9189 CRIStAL, F-59000 Lille, France    Sebastien Labbe ORCID Univ. Lille, CNRS, INRIA, Centrale Lille, UMR 9189 CRIStAL, F-59000 Lille, France    Charles Paperman ORCID Univ. Lille, CNRS, INRIA, Centrale Lille, UMR 9189 CRIStAL, F-59000 Lille, France
Abstract

We introduce the task of out-of-order membership to a formal language L, where the letters of a word w are revealed one by one in an arbitrary order. The length |w| is known in advance, but the content of w is streamed as pairs (i,w[i]), received exactly once for each position i, in arbitrary order. We study efficient algorithms for this task when L is regular, seeking tight complexity bounds as a function of |w| for a fixed target language. Most of our results apply to an algebraically defined variant dubbed out-of-order evaluation: this problem is defined for a fixed finite monoid or semigroup S, and our goal is to compute the ordered product of the streamed elements of w.

We show that, for any fixed regular language or finite semigroup, both problems can be solved in constant time per streamed symbol and in linear space. However, the precise space complexity strongly depends on the algebraic structure of the target language or evaluation semigroup. Our main contributions are therefore to show (deterministic) space complexity characterizations, which we do for out-of-order evaluation of monoids and semigroups.

For monoids, we establish a trichotomy: the space complexity is either Θ(1), Θ(logn), or Θ(n), where n=|w|. More specifically, the problem admits a constant-space solution for commutative monoids, while all non-commutative monoids require Ω(logn) space. We further identify a class of monoids admitting an O(logn)-space algorithm, and show that all remaining monoids require Ω(n) space.

For general semigroups, the situation is more intricate. We characterize a class of semigroups admitting constant-space algorithms for out-of-order evaluation, and show that semigroups outside this class require at least Ω(logn) space. At the same time, we exhibit semigroups for which specialized techniques yield intermediate bounds such as an O(n)-space algorithm, suggesting that the landscape may be richer and less well-behaved than for the monoid setting.

Keywords and phrases:
Automata, Complexity, Algebra
Category:
Track B: Automata, Logic, Semantics, and Theory of Programming
Funding:
Charles Paperman: ANR-24-CE25-2874.
Copyright and License:
[Uncaptioned image] © Antoine Amarilli, Sebastien Labbe, and Charles Paperman; licensed under Creative Commons License CC-BY 4.0
2012 ACM Subject Classification:
Theory of computation Regular languages
Related Version:
Full Version: https://arxiv.org/abs/2602.13100 [6]
Editors:
Sayan Bhattacharya, Danupon Nanongkai, Michael Benedikt, and Gabriele Puppis

1 Introduction

The class of regular languages can be seen as a robust formalism to define the properties of words that can be tested efficiently, i.e., using constant time per symbol and constant memory overall. These bounds can be achieved by feeding the input word to an automaton: they hold when the word is stored in an array and read from left-to-right, and also when the word is streamed character by character.

However, it is not always realistic to assume that words can be processed from left-to-right: in some contexts the word is revealed character by character in an out-of-order fashion. One example is distributed processing frameworks (e.g., MapReduce), in the spirit of out-of-order data processing [3]: if we want to determine some property of a sequence of data items processed concurrently by multiple workers, then the value of each item will typically arrive as each worker finishes, in an unpredictable order (see, e.g., [7]). Another example is networking: large data typically travels using packet switching, and the packets typically arrive in an out-of-order fashion. There, a line of work has investigated how to efficiently detect patterns in such a stream of out-of-order packets, without reassembly [27], with algorithms adapted from suffix trees [10] or automata [29, 28].

Such practical scenarios lead us to consider the task of out-of-order membership to a regular language, which we think is also a natural theoretical question. In this problem, we fix a regular language L, we consider a word w whose length |w| is known in advance, and we are streamed the contents of w in an out-of-order fashion, i.e., as pairs (i,w[i]) giving us the letter at position i of w. Each position is streamed exactly once, and there is no guarantee on the order (i.e., it is adversarial). The algorithm must correctly determine, at the end of the stream, whether wL or wL, while minimizing the space usage and the time spent processing each character. In particular, in contrast with left-to-right streaming, we will see that it may no longer be possible to decide membership with a constant state complexity, i.e., the memory usage may depend on the length of the word. To our knowledge this natural question of the complexity of out-of-order membership has not yet been studied, though it relates to many lines of work on membership variants, presented next.

Related work.

One first related topic is the query complexity of regular languages L, i.e., the asymptotic number of (interactively chosen) characters that need to be inspected in an input word w to decide whether wL. This problem requires Ω(n) on words of length n for almost all regular languages L (and in O(1) for a narrow class); but it admits a more interesting classification in the quantum setting [1]. This question has been further extended to property testing [4] where we only need to discriminate between words in L and words that are far from L according to a certain distance. In this context, a trichotomy on the query complexity of property testing has been achieved [8]. However, all of these problems differ from out-of-order membership: they assume that we can freely choose positions to inspect, whereas in out-of-order membership the streaming order is arbitrary. They also focus on the number of queries needed, whereas with out-of-order membership we receive all positions and focus on the time and space complexity.

Another related line of work is sliding window membership, where we want to test membership to a fixed language L for a sliding window over a stream. Works on this problem have investigated the complexity of various languages L, first in terms of time per character [13], and then in terms of memory usage, with again a recent trichotomy on space complexity for regular languages in several settings [12]. Other works extend sliding windows by considering possible arrival of out-of-order elements, in particular for aggregation [26, 23, 9]. However, one important general difference of the sliding window setting is that we must maintain membership to L for the various states of the sliding window across the stream, with letters that leave the sliding window when they expire. By contrast, in our context, we only test membership of a single word, and revealed positions never expire; but the arrival order is arbitrary and not sequential. Hence, for instance, the language aΣ admits constant space out-of-order membership (by storing the first character) but requires linear space in the sliding window setting [12], and conversely one can show that out-of-order membership to the language ΣaaΣ requires linear space (see Proposition 2.3) but it can be tested in logarithmic space in the sliding window setting [12].

Another related setting is dynamic membership, where we must test whether a word w belongs to a regular language L and maintain this information under updates to w (not just the push-right and pop-left updates of sliding windows). In particular, a conditional trichotomy has been shown under substitution updates [5], i.e., where we receive updates (a,i) that change the i-th letter of the word to the letter a. Out-of-order membership is analogous to a special case of this setting where each position is set exactly once and never changed afterwards. However, [5] always assumes to have linear memory to store the current state of w – their trichotomy is on time complexity and not space complexity. In fact, unlike their setting, we will show that all regular languages admit an out-of-order membership algorithm in O(1) time per character.

Last, the setting closest to ours is the communication complexity of regular language membership, studied in particular by Tesson and Thérien [25] and then by Ada [2]. In this setting, we wish again to determine whether a word w belongs to a regular language L, but w is split across multiple players and we wish to minimize how much information they need to exchange to determine whether wL. Many communication complexity models are quite different from out-of-order membership, e.g., the multiparty setting of [20], along with randomized settings [25] and nondeterministic settings [2]. The most relevant model is the two-player setting of [25]: the word is split between Alice (for odd positions) and Bob (for even positions), and they can exchange information in arbitrarily many rounds to determine membership. Communication lower bounds in this model imply space complexity lower bounds in our setting: if we are first streamed the even positions, then the information that we remember gives a one-way protocol from Alice to Bob. However, upper bounds in their setting do not necessarily translate to ours, because our algorithms do not obey a fixed partition. As we show in the special case of languages featuring a neutral letter (i.e., the case of monoids), the complexity of out-of-order membership happens to coincide with the communication complexity for another model studied in [25], namely, the simultaneous setting in which Alice and Bob must each send information to a referee. This is surprising because, as far as we understand, there is no reduction from their model to ours or vice-versa. Nevertheless, as the tractability boundary is the same, our algebraic proofs in this specific setting relate to theirs, as we will explain. In any case, all results in [25] crucially assume the presence of a neutral letter (i.e., some of the letters of Alice and/or Bob may be ϵ): the issue is further discussed in [24, Section 5.2], and it has significant impact in our setting as well, as we will see when presenting our contributions below.

Contributions.

In this paper, we introduce the out-of-order membership problem for regular languages, and give first results on its complexity, including a complete space trichotomy classification for the case of monoids and a characterization of constant space for semigroups. We present these contributions in more detail below along with the structure of the paper.

We introduce formally the problem and our model in Section 2, and give some first complexity results. Specifically, in the unit-cost RAM model with logarithmic word size, we show that every fixed regular language admits an out-of-order membership algorithm with constant time per streamed character and linear memory usage (Theorem 2.1). For this reason we only focus on space complexity in the remainder of the paper. We close the section by adapting the classical technique of fooling sets for unconditional lower bounds, which we use throughout the paper; we exemplify it to show a linear space lower bound for ΣaaΣ (Proposition 2.3). By contrast, we show that better bounds are possible for some languages, namely, commutative languages admit constant space out-of-order membership (Lemma 2.4).

As classifying regular languages turns out to be challenging, we focus in the rest of the paper on algebraically defined variants of the problem. In Section 3, we introduce the simplest such problem: the out-of-order evaluation problem for a monoid M, where we are streamed a word w of monoid elements in an out-of-order fashion and must compute the product of w in M, while minimizing the space usage. The point of out-of-order evaluation is that it can provide algorithms for out-of-order membership to languages (via their syntactic monoid), and it intuitively amounts to assuming that languages have been closed under the addition of a neutral letter, simplifying the classification. Further, we show that the complexity regimes of out-of-order monoid evaluation form varieties of monoids, so the problem is amenable to algebraic tools. We accordingly study out-of-order monoid evaluation and prove (Theorem 3.2) that monoids admit three space complexity regimes, with matching upper and lower bounds: constant space for commutative monoids, logarithmic space for monoids in a class dubbed 𝐅𝐋𝐂𝐨𝐦, and linear space for all other monoids. The variety 𝐅𝐋𝐂𝐨𝐦 intuitively extends commutative monoids with the ability to remember a constant number of leftmost and rightmost occurrences of each element. It turns out that the join variety 𝐅𝐋𝐂𝐨𝐦 is the same as the variety 𝐖 introduced in [25] (with a less informative characterization). Thus, the complexity of deterministic simultaneous communication complexity according to [25], in the specific case of monoids, happens to coincide with the space complexity of our out-of-order evaluation problem – even though neither problem seems to capture the other. Our classification is summarized in the first column of Table 1, with examples (as syntactic monoids of languages) in Table 2.

We then turn in Section 4 to the more general problem of out-of-order evaluation for semigroups, i.e., monoids but without the requirement of having a neutral element. The problem turns out to be much more challenging in the semigroup setting, and thus we only focus on the constant space regime, which we are able to characterize (Theorem 4.4). Namely, we show a constant space algorithm for semigroups in 𝐋𝐢𝐂𝐨𝐦, i.e., intuitively extending commutative semigroups with the ability to test constant-length prefixes and suffixes of the word (see Table 2 for an example). We show a logarithmic lower bound for all other semigroups.

In Section 5, we show that classifying the higher complexity regimes appears much more intricate. For convenience we present results on the out-of-order membership problem, but the difficulties that we highlight already appear for out-of-order semigroup evaluation (for space complexities beyond the constant regime). We first give ad hoc logarithmic upper bounds for aba (Proposition 5.1) and for ababa (Proposition 5.2) using completely different techniques. We then show (Proposition 5.3) an unexpected sublinear O(n) space upper bound for ababab for which we have no better complexity upper bound and no superlogarithmic lower bound. See Table 1 for the overall complexity bounds achieved for out-of-order evaluation, and Table 2 for examples.

We conclude and propose further research directions in Section 6. For lack of space, most detailed proofs are deferred to the appendix.

Table 1: Out-of-order evaluation space complexity. The results of the first column are in Theorem 3.2, the O(1) result for semigroups is in Theorem 4.4.
Space Monoids Semigroups
O(1) 𝐂𝐨𝐦 𝐋𝐢𝐂𝐨𝐦
O(logn) 𝐅𝐋𝐂𝐨𝐦 ?
O(n) All monoids All semigroups
Table 2: Out-of-order evaluation/membership problem space complexity examples. The notations M() and S() respectively denote the syntactic monoid and syntactic semigroup of the indicated languages.
Space Monoids Semigroups Languages
Θ(1) M((aa)) Ex. 3.3 S(aΣb) Prp. 4.3 (ab) Ex. 2.5
Θ(logn) M(ab) Ex. 3.9 S(abc) S(abba) Ex. 4.8-4.10 aba Prp. 5.1
O(n) ababab Prp. 5.2
Θ(n) M(abba) Ex. 3.12 S((ab)) Prp. 4.11 ΣaaΣ Prp. 2.3

2 Problem Statement and First Results

In this section, we formally define the out-of-order membership problem, and give a constant-time and linear-space upper bound for this problem on arbitrary fixed regular languages (Theorem 2.1). Then we formally define fooling sets and give a linear-space lower bound for the language ΣaaΣ (Proposition 2.3). We close by an easy constant-space upper bound for commutative languages (Lemma 2.4) which will be reused throughout the paper.

Problem statement.

Let Σ be an alphabet, let n>0 be a word length, let w=w[1]w[n] be a word over Σ of length n, and let πn be an n-permutation, i.e., a permutation of {1,,n}. The out-of-order stream of the word w defined by πn is the finite sequence R of length n whose successive elements are (w[π(i)],π(i),n) for i=1,,n. Intuitively, we receive the letters of w together with their position in the order π(1),,π(n). Note that the length n of the word is provided as the third component of each element: this is because we assume throughout the work that the length n is always known and we do not want to account for it when measuring space complexity.

We only consider regular languages in this paper, and we omit their definition. A letter aΣ is neutral for a language LΣ if for any s,tΣ we have satL if and only if stL. For a fixed regular language L over Σ, the out-of-order membership problem for L is the following task: letting w be a word of length n over Σ, letting πn be an n-permutation, and letting R be the out-of-order stream on w defined by πn, we receive the elements of R one after the other and must decide at the end whether wL or not.

Note that the standard membership test to a fixed language L for a word w streamed from left-to-right corresponds to the case where πn is the identity permutation. For left-to-right membership testing, we can simply use an automaton for L and achieve constant space and constant time per character; but for out-of-order membership the challenge is that we must support arbitrary permutations. Further note that we can always solve the problem by storing the letters in an array T as they arrive, i.e., whenever we receive (a,i,n) we set T[i]a and we test at the end whether the word stored in T belongs to L. This gives a naive algorithm which uses linear space and runs in constant worst-case complexity per streamed character except that it spends linear time after the last character. Let us now present a more efficient algorithm that avoids this drawback.

Time complexity upper bound.

We first study efficient algorithms for out-of-order membership to fixed regular languages in terms of time complexity per streamed character. For this result on time complexity, and only for this result, we need to be precise about the computational model that we assume; subsequent results will only focus on space complexity and will apply no matter which computations are allowed. We work in the standard unit-cost RAM model with logarithmic word size [11, 14]. Memory consists of cells of size w=Θ(logn) bits, and reading, writing, or performing standard operations on these cells takes constant time. Our set of allowed operations includes addition, subtraction, increment, comparisons, integer division with remainder, shifts, bitwise Boolean operations, and computing the most significant bit. We will also use the standard lazy array evaluation technique to assume when allocating arrays that they can be initialized in constant time [14]. The algorithm receives the successive triples (a,i,n) of the out-of-order stream, with the values being stored in three Θ(logn)-bit cells. Here, a uses constant bits (as the alphabet is fixed), while i and n are integers represented on Θ(logn) bits.

In the computational model defined above, we can show that all regular languages admit an algorithm with O(1) complexity per letter and a linear space usage of O(n) bits (i.e., amounting to O(n/logn) memory cells).

Theorem 2.1.

For any fixed regular language L, there is an algorithm for out-of-order membership to L with O(1) time per streamed character and a space complexity of O(n) bits.

Proof sketch.

Let M be the syntactic monoid of L (see Section 3 for the formal definitions). We divide the input word into blocks of length logn, so that each block can be represented in a constant number of RAM cells. The algorithm has two layers. The macro layer treats complete blocks as atomic symbols: for every maximal contiguous interval of complete blocks, it stores the interval length at both endpoints and the product in M of the block values at the left endpoint. The micro layer maintains analogous information inside each block, using one bit vector to record which positions of the block have already been streamed and a packed vector of monoid elements to store the values in M of maximal contiguous streamed intervals (which correspond to maximal contiguous intervals of 1’s in the bit vector), each value being stored at the left endpoint of its interval.

When a letter is streamed, we first update its block in the micro layer. To do this, we must compute the maximal contiguous interval of 1’s to which the streamed letter belongs. This is accomplished by creating a singleton interval of 1’s in the bit vector, and then trying to merge it with the interval of 1’s immediately to its right and the interval immediately to its left. If there is an interval to the right, then merging with this interval is immediate because the monoid value of the interval is stored at its left endpoint (i.e., immediately to the right of the new position). For the merge with the left interval, we must find the left endpoint of that interval, which we do in constant time by shifting the bit vector of the block and applying the most-significant-bit operation. Hence the micro update takes constant time, noting that multiplication in M is constant-time since L is fixed.

Now, if this update completes the block, we insert it into the macro structure. To do this, we first create the block as a singleton block interval in the macro layer, whose monoid value is the value of the newly computed block. Then we compute the maximal interval of complete blocks to which the block belongs, again by attempting to merge its singleton interval with the neighboring interval on the right and the neighboring interval on the left if they exist. These macro merges take constant time because the interval lengths and monoid values are stored at interval endpoints.

At the end of the stream, all blocks form a single macro interval. Its stored monoid value is μ(w), from which we can deduce whether wL.

The overall space bound is O(n) bits: the macro arrays have O(n/logn) entries of one RAM cell each, while the packed micro bit vectors and monoid vectors use O(logn) bits per block. We use lazy array evaluation [14] to ensure that our data structures are initially zero without the need for running a preprocessing phase.

In light of Theorem 2.1, in the remainder of the paper we focus on space complexity. We only study deterministic space complexity. We always account for space complexity in terms of the number of bits used (not memory cells), and we only consider the space that must be memorized between any two elements of the out-of-order stream; we do not consider the computation time and interim memory needed when processing an element.

Fooling sets for lower bounds.

We now introduce fooling sets as a general technique to prove unconditional lower bounds, and showcase fooling sets to show a linear space lower bound on the specific language ΣaaΣ. Our fooling sets are an out-of-order analogue of standard techniques (see, e.g., [18, Definition 2] for a similar notion) used for the state complexity of regular languages when reading words from left to right, e.g., to show that any deterministic automaton for the language Ln=ΣaΣn must have at least 2n+1 states (see [17, Example 2.13] which proceeds along similar lines).

Let _ be a fresh placeholder symbol, and write X¯X{_} for a set extended by _. A partial word over an alphabet Σ is a word w over the alphabet Σ¯. The domain of w is the subset dom(w) of the positions where w does not carry a placeholder, i.e., dom(w){i{1,,|w|}w[i]Σ}. Two partial words w1,w2 are homogeneous if |w1|=|w2| and dom(w1)=dom(w2), and a set S of partial words is homogeneous if all its partial words are pairwise homogeneous. Now, two partial words w1,w2 are compatible if they have the same length and if dom(w1)dom(w2)=, i.e., for all 1i|w1| at least one of w1 and w2 has a placeholder at position i. Their composition w1w2 is then the partial word defined by (w1w2)[i]w1[i] if idom(w1), and (w1w2)[i]w2[i] otherwise. We further call two compatible words w1 and w2 complementary if dom(w1)dom(w2)={1,,n}, equivalently, w1w2 is a word of Σ without placeholders. Given a language LΣ, a word length n, and two homogeneous partial words w1,w2Σ¯n, we call complementary witness of w1,w2 for L a partial word vΣ¯ complementary to both w1 and w2, such that precisely one of w1v and w2v belongs to L.

For L a language over Σ and n,m, an (m,n)-fooling set for L is a homogeneous set W={w0,,wm1} of m partial words wiΣ¯n such that any two distinct partial words wi,wjW have a complementary witness for L.

Lemma 2.2 (Fooling sets lower bound).

Let L be a fixed language over alphabet Σ, and let f: be a non-decreasing function. If for every integer N there exists n>N such that there is an (f(n),n)-fooling set for L, then the out-of-order membership problem to L has Ω(logf(n)) space complexity.

The proof of Lemma 2.2 is standard and deferred to the full version [6] : we simply argue that the algorithm must have enough memory to distinguish each partial word among those of the fooling set. Note that, in the result statement above and everywhere in the paper, we write g=Ω(f) for two functions f,g:>0>0 to mean that lim supng(n)/f(n)>0. This is the Ω notation of Hardy and Littlewood [16] and not the one by Knuth [19] (which is defined as meaning f=O(g)). For instance, letting g be the function defined by g(n)n for odd n and g(n)1 otherwise, we have g=Ω(n). This is important, e.g., to show lower bounds on out-of-order membership for languages that are hard for words of odd length and trivial for words of even length.

Let us showcase Lemma 2.2 and prove that some languages require linear space for out-of-order membership:

Proposition 2.3.

The out-of-order membership problem for the language ΣaaΣ on alphabet Σ={a,b} requires linear space.

Proof.

For each length n, we define a (2n,3n)-fooling set Wn for ΣaaΣ formed of the 2n partial words defined by the regular expression (b(a+b)_)n. Note that they are all homogeneous and have length 3n. Let wi,wj be two distinct partial words of Wn. There must be a position 0p<n such that wi[3p+2]wj[3p+2]. Note that this means one is a and the other is b; without loss of generality up to exchanging wi and wj we assume that wi[3p+2]=a and wj[3p+2]=b. Now, we let vij(__b)p1(__a)(__b)np. We see that vij is complementary to both wi and wj. Further, by construction wivij belongs to ΣaaΣ because there are two contiguous a’s at positions 3p+2 and 3p+3. By contrast, wjvij does not belong to ΣaaΣ. This establishes that Wn is indeed a (2n,3n)-fooling set for ΣaaΣ and concludes the proof.

We will use fooling sets to show all the lower bounds in this paper; in particular these bounds are all unconditional. Note that this also implies that all our lower bounds already hold for streaming orders that are chosen in advance, i.e., when we show a lower bound on a language for words of length n then we show the existence of an n-permutation such that the space lower bound applies even on algorithms that work only for that specific permutation. We also note that lower bounds shown with this method can be understood as a lower bound on fixed-partition communication complexity for a one-way protocol, where Alice has access to the letters on the domain of the fooling set and Bob has access to the rest of the word.

Constant space upper bound for commutative languages.

We complement the previous result by noticing that the linear space complexity lower bound does not apply to all languages: we can sometimes solve out-of-order membership with better space complexity. We will show the easy fact that, for commutative regular languages, out-of-order membership can be solved with constant space. Here, a language is commutative if, for every word w, letting n|w|, for every n-permutation π, we have wL if and only if w[π(1)]w[π(n)]L. Then:

Lemma 2.4.

For any fixed commutative regular language L, there is an algorithm for out-of-order membership to L with O(1) space complexity.

Proof.

Fix a deterministic automaton A that accepts L. Now, whenever we receive some tuple (a,i,n), we feed a to A. This takes constant time, and the memory of the algorithm stores the automaton state, which has constant size because L is fixed. At the end of the stream, we accept if A accepts. It is immediate that A was fed a permutation of the word w: as L is commutative, the algorithm is correct, which concludes.

The condition of being commutative is sufficient but is not necessary to have constant-space complexity, as illustrated below:

Example 2.5.

There is an algorithm with O(1) space complexity for out-of-order membership to the language (ab). Indeed, we simply need to check that all streamed a’s are at an odd position, that all streamed b’s are at an even position, and that the word length is even.

In summary, we have shown that out-of-order membership to fixed regular languages can always be solved in constant time per streamed character and linear memory, and this memory bound is optimal for some languages (e.g., ΣaaΣ) but suboptimal for others (e.g., commutative languages or (ab) for which the bound is O(1)). Thus, our goal is to classify regular languages according to the space complexity in this sense. Towards this, we will present algebraic variants of the problem on which classifications are easier to show.

3 Out-of-Order Monoid Evaluation

In this section we focus on monoids, i.e., finite sets equipped with an associative composition law and a neutral element. We consider the out-of-order evaluation problem for monoids, which can be seen as a special case of out-of-order membership for regular languages. In this section, we first define this problem, and then show that tractable monoids for this problem are closed under the monoid variety operators. We then show a classification (Theorem 3.2) of the possible space complexity regimes.

Out-of-order evaluation.

All monoids in this paper are implicitly finite. Let M be a fixed monoid, let n>0 be the word length, and let w=w[1]w[n] be a word over M of length n; its product is the value w[1]w[n] of M obtained according to the composition law of M. We define out-of-order streams like in the previous section. The out-of-order evaluation problem for M is the following task: letting w be a word of length n, letting πn be an n-permutation, and letting R be the out-of-order stream on w defined by πn, we receive the elements of R one after the other and must compute at the end the product of w in M.

We note that the out-of-order evaluation problem for a monoid M can be expressed in terms of out-of-order membership problems. Indeed, for any xM, the language Lx over M defined as the words of M having product x is always a regular language. Likewise, the out-of-order membership problem for a language L reduces to the out-of-order evaluation problem for the syntactic monoid of L, i.e., the finite monoid obtained by quotienting Σ by the syntactic congruence equivalence relation L defined by L. Intuitively, L equates words whose behavior relative to L is the same, and formally we have uLv for two words u,vΣ whenever for any s,tΣ we have sutL if and only if svtL. It is well-known that for a regular language L the syntactic congruence L has indeed a finite number of equivalence classes, and in fact this property precisely characterizes the regular languages; see, e.g., [22] for more details.

Thus, algorithms for out-of-order evaluation for the syntactic monoid M of a language L imply an algorithm for out-of-order membership to L. However, there are cases where out-of-order membership for L is easier than the same problem for M, e.g., because L may not feature a neutral letter but M always has a neutral element. For this reason, classifying the complexity of out-of-order evaluation for monoids is a coarser task than classifying the complexity of out-of-order membership for regular languages, and so it serves as a useful first step.

To start our study of out-of-order evaluation for monoids, let us observe that the complexity classes of monoids for out-of-order evaluation are closed under the monoid variety operators.

Closure properties.

The direct product of two monoids M1 and M2 is the monoid (M1×M2,) with the binary operator defined as (x1,x2)(x1,x2)=(x1x1,x2x2) and identity (e1,e2) for e1 and e2 the respective identities of M1 and M2. A sub-monoid of a monoid M with neutral element e is a subset MM of M which is closed under the composition law of M and which is also a monoid with neutral element e. A congruence over a monoid M is an equivalence relation over M which satisfies the following requirement: whenever st and st for s,t,s,tM then we have sstt. A quotient monoid M/ is the quotient of a monoid M by a congruence over M, i.e., it consists of the equivalence classes of M under , its composition law is lifted to these classes, and its neutral element is the equivalence class of the neutral element of M. Note that, thanks to the definition of a congruence, when applying the composition law the results do not depend on the choice of representatives.

A class of monoids is a pseudo-variety, only called variety throughout the paper, if it is closed under direct products, taking sub-monoids, and taking monoid quotients. Let 𝐕 and 𝐖 be two varieties of monoids. Their join, denoted 𝐕𝐖, is the smallest variety containing both the monoids of 𝐕 and those of 𝐖.

We show that, for the problems that we study, each complexity class forms a variety:

Proposition 3.1.

For any function f:, the set of monoids admitting an O(f(n)) space algorithm for out-of-order evaluation forms a monoid variety, i.e., it is closed under direct product, sub-monoid, and monoid quotient.

Proof sketch.

Any data structure for the out-of-order evaluation problem for a monoid M can in particular solve it for any submonoid, and it can also solve it for any quotient (simply by picking arbitrary representatives and returning the equivalence class of the result). Further, for any two monoids M1 and M2, we can solve out-of-order evaluation for M1×M2 by simultaneously using one data structure for M1 and one for M2.

Space complexity of out-of-order monoid evaluation.

Our goal in this section is to classify all monoids M according to the space needed by an algorithm to solve out-of-order evaluation for M. Our results will imply the existence of precisely three complexity regimes (constant, logarithmic, and linear), and characterize the corresponding monoids.

Let us first introduce the varieties that constitute the different regimes. The first one is 𝐂𝐨𝐦, the variety of commutative monoids. We can characterize it as those monoids M satisfying the equation xy=yx for all x,yM, i.e., we write 𝐂𝐨𝐦=xy=yx. In fact, by Reiterman’s theorem [21], we know that varieties of monoids always admit such equational descriptions (see [22] for more details), so we will define our other monoid varieties using equations in this fashion. Monoids from the variety 𝐂𝐨𝐦 admit a constant-space algorithm for the out-of-order evaluation problem, obtained by immediately lifting Lemma 2.4 to monoids. We will show that this in fact characterizes all constant-space monoids, with all other monoids having a logarithmic lower bound.

For the logarithmic space regime, we will characterize it as the join of 𝐂𝐨𝐦 together with another variety, called 𝐅𝐋 (for “first-last”). To give an equation for 𝐅𝐋, we need to define the idempotent power of a monoid M as the smallest non-negative integer ω such that xω=x2ω for all xM. Then we can define 𝐅𝐋=(xy)ωst(xz)ω=(xy)ωsxt(xz)ω. One example of a monoid in 𝐅𝐋 is the syntactic monoid of the language ΣaΣbΣ over the alphabet {a,b,e}, intuitively because membership can be decided by remembering the position of the leftmost a and of the rightmost b. We will show that monoids in 𝐅𝐋 admit a logarithmic-space algorithm for out-of-order evaluation, which will imply the same for 𝐅𝐋𝐂𝐨𝐦 thanks to Proposition 3.1. It turns out that 𝐅𝐋𝐂𝐨𝐦 precisely coincides with the class 𝐖 in [25] that forms the tractability boundary of a similar but seemingly incomparable problem, namely, the monoids with logarithmic simultaneous deterministic communication complexity.

We will last characterize the linear space regime by showing, using an equational characterization of 𝐅𝐋𝐂𝐨𝐦, that monoids outside of this class require linear space.

All told, in this section, we show the following characterization, summarized in the first column of Table 1 (with all upper bounds matched by tight lower bounds) and examples (given as syntactic monoids of languages) in the first row of Table 2:

Theorem 3.2.

Let M be any fixed monoid. Then the space complexity of out-of-order monoid evaluation for M is as follows:

  • If M𝐂𝐨𝐦, then it is in O(1)

  • If M(𝐅𝐋𝐂𝐨𝐦)𝐂𝐨𝐦, then it is in Θ(logn)

  • Otherwise, it is in Θ(n).

We prove this result in the rest of the section. Recall that the first point follows by the immediate analogue of Lemma 2.4 for monoids, for instance:

Example 3.3.

The syntactic monoid of the language (aa), which is the group /2, is commutative and therefore admits a constant-space upper bound.

We give first the logarithmic lower bound for non-commutative monoids (for the lower bound of the second point), then the logarithmic upper bound (for the upper bound of the second point), and then the linear lower bound (for the lower bound of the third point). The upper bound of the third point is immediate by the monoid analogue of Theorem 2.1.

Logarithmic lower bound.

For our two lower bounds in this section, we immediately lift the definitions of fooling sets to the out-of-order monoid evaluation problem in the expected way: the words are defined over the alphabet M¯ featuring the monoid elements and the placeholder, and the only new stipulation is that any two words wiwj of the fooling set have a complementary witness vij that ensures that wivij and wjvij evaluate to different monoid elements (instead of saying that precisely one belongs to the target language).

We then show the first lower bound, establishing that non-commutative monoids need at least logarithmic space complexity:

Lemma 3.4.

For any fixed non-commutative monoid M𝐂𝐨𝐦, its out-of-order evaluation problem has Ω(logn) space complexity.

Proof sketch.

Let x,yM be two elements such that xyyx, and let e be the neutral element. We use Lemma 2.2 (lifted to the setting of monoids) and define for each n>0 the fooling set Wn formed of the words wi(e_)i1(x_)(e_)ni for each 1in. The complementary witnesses for 1i<jn are defined as: vij(_e)i1(_y)(_e)ni, ensuring that wivij evaluates to xy and wjvij to yx.

Logarithmic upper bound.

We now show the logarithmic upper bound for languages in 𝐅𝐋𝐂𝐨𝐦. Thanks to the closure under variety operations (Proposition 3.1), using again the constant upper bound for commutative monoids (Lemma 2.4), it suffices to show an upper bound for 𝐅𝐋.

Remember our equational definition of 𝐅𝐋 using the equation 𝐅𝐋=(xy)ωst(xz)ω=(xy)ωsxt(xz)ω. We will now show that, whenever a monoid M satisfies this equation, then computing the product of elements of M can be achieved simply by looking at a constant number of leftmost and rightmost occurrences of each element. Let us introduce the requisite definitions on words over M:

Definition 3.5 (First-last subword).

Let M be a monoid and let mM. Let u be a word over M of size n. The m-occurrences of u is the subset Om(u)[n] of the positions i[n] such that u[i]=m. We then define the first k positions Fmk(u) as the subset of Om(u) formed of the k smallest positions: note that if there are no more than k occurrences of m in u then Fmk(u)=Om(u). We define the last positions Lmk(u) analogously with the k greatest positions. Now, the k-first-last subword of u is the scattered subword FLk(u) formed by only retaining the first and last k occurrences for each m, i.e. retaining the monoid elements at the positions mMFmk(u)Lmk(u).

We note that the first-last subword defined above also occurs in [25], where it is called REDk. Their work then extends this definition to correspond to their variety 𝐖, which will correspond to 𝐅𝐋𝐂𝐨𝐦 as we explain later. By contrast, we will use first-last subwords directly for the simpler variety 𝐅𝐋 for our upper bound, and the extension to 𝐅𝐋𝐂𝐨𝐦 is for free thanks to Proposition 3.1 and Lemma 2.4. We claim the following property on monoids in 𝐅𝐋:

Claim 3.6.

Let M be a monoid in 𝐅𝐋, and let k|M|. Then for any word uM, letting vFLk(u), the words u and v have the same product in M.

To show this, let us point out how to use the 𝐅𝐋 equation on words of elements of M. Using standard pumping arguments, we can show:

Claim 3.7.

Let M be a monoid in 𝐅𝐋, let k|M|, let mM, and let u=svt be a word of M such that s and t contain exactly k occurrences of m. Let v be the scattered subword of v obtained by removing all occurrences of m, and let usvt. Then u and u have the same product in M.

This claim allows us to show ˜3.6:

Proof sketch of ˜3.6.

We do an induction on the number of elements of M that occur more than 2k times in u. If there are none, then v=u are equal and the claim is immediate. Otherwise, we pick an element m occurring more than 2k times, we isolate its first and last k occurrences, we use ˜3.7 to argue that all other occurrences can be eliminated without changing the product in M, and we conclude using the induction hypothesis on the resulting word which has one less element occurring more than 2k times.

We can now state our upper bound on out-of-order evaluation for monoids in 𝐅𝐋:

Lemma 3.8.

For any fixed monoid M𝐅𝐋, the out-of-order evaluation problem for M has O(logn) space complexity.

Proof sketch.

The algorithm remembers the k minimal and the k maximal positions of each element of M during the stream. This allows it to construct FLk(u) for u the streamed word, and thanks to ˜3.6 the product of FLk(u) in M is identical to the desired answer, namely, the product of u in M.

Example 3.9.

The syntactic monoid of the language ab, which is the five-element monoid {1,a,b,ab,0}, belongs to 𝐅𝐋 because it satisfies the 𝐅𝐋 equation:

(xy)ωst(xz)ω=(xy)ωsxt(xz)ω.

Indeed, it has two idempotents, 0 and 1. If one of the ω-powers evaluates to 0, the equation holds trivially since both sides evaluate to 0. The only remaining case is (xy)ω=(xz)ω=1, which implies x=1, and then both sides evaluate to st.

Linear lower bound.

The only remaining piece to show Theorem 3.2 is the linear lower bound on monoids not in 𝐅𝐋𝐂𝐨𝐦. Formally:

Lemma 3.10.

For any fixed monoid M𝐅𝐋𝐂𝐨𝐦, the out-of-order evaluation problem for M has Ω(n) space complexity.

To show this result, we need an equation characterizing the variety 𝐅𝐋𝐂𝐨𝐦, which we will then use to obtain witnessing elements to build fooling sets. It turns out that 𝐅𝐋𝐂𝐨𝐦 coincides with the class 𝐖 in [25], so we will reuse part of their proof; however our approach has the merit of proposing a simpler equation for the class, and of giving a presentation of 𝐖 as a join involving a simpler variety 𝐅𝐋 with an equational characterization. We also remind the reader that the results of [25] pertain to communication complexity; as far as we understand, it is a coincidence that tractability boundaries of our problem coincides with one of theirs, because there does not appear to be reductions from one problem to the other.

We accordingly give the following characterization of 𝐅𝐋𝐂𝐨𝐦:

Lemma 3.11.

Let M be a monoid. The following conditions are equivalent:

  1. 1.

    The monoid M belongs to 𝐅𝐋𝐂𝐨𝐦.

  2. 2.

    The monoid M satisfies the equation (xa)ωsxtu(xb)ω=(xa)ωstxu(xb)ω.

  3. 3.

    The monoid M satisfies the equations of the monoid variety 𝐖 in [25], namely:

    • (swt)ωxwωy(uwv)ω=(swt)ωxy(uwv)ω

    • (swt)ωxw(uwv)ω=(swt)ωwx(uwv)ω

Proof sketch.

We show 1231. The 12 direction follows from the equations of 𝐅𝐋 and 𝐂𝐨𝐦. The 23 direction is easy by equation manipulation. For 31 we use the REDt,p congruence defined in [25]. We reuse their proof [25, Theorem 3] which shows that any monoid satisfying the equations of 𝐖 is refined by a REDt,p-congruence: this is the analogue of the result we showed for 𝐅𝐋 in ˜3.6. We can then conclude because REDt,p is the intersection of an 𝐅𝐋 congruence and of a 𝐂𝐨𝐦 congruence.

With this characterization, we can now prove the linear lower bound (Lemma 3.10):

Proof of Lemma 3.10.

Let M be a monoid not in 𝐅𝐋𝐂𝐨𝐦. By Lemma 3.11, there are elements in M witnessing a violation of the equation: (xa)ωsxtu(xb)ω(xa)ωstxu(xb)ω. We use Lemma 2.2 and build a (2n,5n+2)-fooling set for any n>0. For each α{0,1}n we define wαi by either _x_e_ or _e_x_ depending on the bit αi, let wα_wα,1wα,2wα,n_, and let Wn{wαα{0,1}n} be the fooling set.

Now, for any αβ, let ι(α,β) be a position of {1,,n} such that αι(α,β)βι(α,β). The complementary witness is then:

vαβ=((xa)nωι(α,β)+1)(e_e_a)ι(α,β)1(s_t_u)(e_e_b)nι(α,β)((xb)nωn+ι(α,β))

where the first and last groups in parentheses correspond to a single element.

We can then see that one of wαvαβ and wβvαβ is (xa)nω(sxtu)(xb)nω=(xa)ωsxtu(xb)ω and the other is (xa)nω(stxu)(xb)nω=(xa)ωstxu(xb)ω, so by assumption they are indeed different, showing that Wn is a fooling set. This implies that the out-of-order monoid evaluation problem for M has Ω(n) space complexity.

We illustrate the lower bound of Lemma 3.10 with the following example:

Example 3.12.

We show that the syntactic monoid of the language M(abba) is not in the class 𝐅𝐋𝐂𝐨𝐦, and therefore admits an Ω(n) space lower bound. This monoid has seven elements {1,a,b,ab,ba,bb,0}, where 1, a, and 0 are idempotent, and satisfies bbb=0=aba.

It does not satisfy the equation of Lemma 3.11

(xy)ωsxtu(xz)ω=(xy)ωstxu(xz)ω,

where we rename the variables of the equation to avoid confusion with the monoid elements. Taking x=y=z=a, s=1, and t=u=b, the left-hand side evaluates to abba=bb, while the right-hand side evaluates to ababa=0.

Hence, by Lemma 3.10, the out-of-order monoid evaluation problem for M(abba) requires Θ(n) space.

4 Constant-Space Out-of-Order Semigroup Evaluation

In the previous section, we characterized the out-of-order evaluation problem for monoids, i.e., the case of out-of-order membership for languages with a neutral letter. This assumption on languages is a common one, and it is made throughout in [25]. We now study what happens when this assumption is removed. For this, we turn to the out-of-order evaluation problem for finite semigroups instead of monoids. In this section, we first define the problem and comment on the relationship to monoids and languages. We then show our main result for semigroups, which characterizes the constant-space class.

Out-of-order semigroup evaluation.

All semigroups in this paper are implicitly finite. The definition of out-of-order evaluation for semigroups is defined exactly like for monoids, but with a semigroup. By the immediate analogue of Proposition 3.1, the complexity regimes of out-of-order semigroup evaluation form varieties of semigroups. Further, the definitions of fooling sets and the corresponding lower bound (Lemma 2.2) also immediately adapt. The syntactic semigroup of a language L is defined like the syntactic monoid but as a quotient of Σ+, i.e., without including the empty word. (However, it may have a neutral element nevertheless, for instance if L features a neutral letter.)

Like monoid evaluation, the out-of-order semigroup evaluation problem is a coarsening of out-of-order membership, but it is less coarse, in particular it does not require the presence of a neutral letter. We note that our results on monoids already imply lower bounds on some semigroups. For instance, semigroups that are not locally commutative, i.e., which admit a subsemigroup which is a non-commutative monoid, directly inherit the logarithmic space lower bound on monoids (Lemma 3.4) from the previous section:

Lemma 4.1.

Let S be a fixed semigroup. If S is not locally commutative, i.e., if S does not satisfy the equation sωxsωysω=sωysωxsω, then the out-of-order evaluation problem for S has Ω(logn) space complexity.

Proof.

Let S be a semigroup and s,x,yS such that sωxsωysωsωysωxsω. Let x=sωxsω and y=sωysω. We remark that the sub-semigroup generated by {sω,x,y} is a monoid with sω as its neutral element. Further, it is non-commutative because xyyx. Hence, its evaluation problem has Ω(logn) space complexity, and thus the evaluation problem of S also has Ω(logn) space complexity by closure under sub-semigroups (Proposition 3.1).

Example 4.2.

The semigroup S(aba+ca) has elements {a,b,c,ba,ac,bac,0}, with a and 0 being the only idempotents. It does not satisfy the equation sωxsωysω=sωysωxsω when taking s=a, x=b, and y=c: the right side evaluates to 0 while the left side evaluates to bac. The elements a, ba, and ac generate a local submonoid {a,ba,ac,bac,0}, which is not commutative. Hence, by Lemma 4.1, the out-of-order evaluation problem for this semigroup has Θ(logn) space complexity.

Note that we can also devise an O(logn) space algorithm for this semigroup by observing that in any word, at most one element containing b (i.e., b, ba, or bac) and at most one element containing c (i.e., c, ac, or bac) can appear. If there are more, or if there is a 0, then the result is 0. If there are one or less of each of these positions, then all remaining positions are necessarily filled with a’s. The algorithm can therefore store the exact positions of the elements containing b and of the elements containing c (and any 0 element if present), rejecting if more than two elements are memorized, and otherwise computing the product of the memorized elements according to the order of their positions, filling gaps with a’s as needed. Since there are only a constant number of positions to memorize and each position can be represented with O(logn) bits, this gives an O(logn) space upper bound. Thus, the out-of-order evaluation problem for S(aba+ca) has Θ(logn) space complexity.

However, the space complexity of out-of-order evaluation for semigroups does not exactly follow the classification on monoids, and new phenomena can arise. For instance:

Proposition 4.3.

Let L be the language aΣb. The out-of-order evaluation problem for the syntactic semigroup S of L has O(1) space complexity.

Proof sketch.

We just memorize the element at position 1 and the element at position n for n the word length, using constant space; and we return their product.

This example shows that, for out-of-order semigroup evaluation, it may be useful to remember the first and last positions of the overall word of semigroup elements. This is not helpful for monoids, intuitively because the first and last positions can always contain the neutral element. The class of semigroups whose evaluation only depends on the leftmost and rightmost elements is a well-studied variety called 𝐋𝐢 and defined by the equation xωyxω=xω, or equivalently 𝐋𝐢=xωyzω=xωzω.

The main result which we show in this section is the following:

Theorem 4.4.

Let S be any fixed semigroup. Then the space complexity of out-of-order semigroup evaluation for S is as follows:

  • If S𝐋𝐢𝐂𝐨𝐦, then it is O(1)

  • Otherwise, it is in Ω(log(n))

We prove this result in the rest of the section. We first show the first point (the upper bound), before showing the second point (the lower bound). We note that the second point only gives a lower bound, which is not tight relative to our linear space upper bound (Theorem 2.1); we come back to this issue in Section 5.

Upper bound.

We first prove the upper bound for the semigroups in 𝐋𝐢 by relying on its equational description and a simple pumping argument.

Lemma 4.5.

Fix a semigroup S𝐋𝐢, then its out-of-order evaluation problem has O(1) space complexity.

Proof sketch.

Let k=|S|, store the first k+1 and last k+1 letters. At the end return the product in order of all the stored elements. This algorithm is in constant space. The correctness follows by using a pumping technique on the first and last k+1 elements to construct idempotent elements and then using the equation 𝐋𝐢=xωyzω=xωzω to conclude that the product of the stored elements is equal to the entire product.

Together with the upper bound on commutative semigroups that follows from the immediate generalization to semigroups of Lemma 2.4, and thanks to the closure under semigroup operations by the analogue of Proposition 3.1 for semigroups, we deduce that semigroups in 𝐋𝐢𝐂𝐨𝐦 have O(1) space out-of-order semigroup evaluation. This establishes the upper bound of Theorem 4.4.

Lower bound.

We now show that all other semigroups require logarithmic space, which completes the proof of Theorem 4.4 by showing the second point.

For this, we first give an equational characterization of the variety 𝐋𝐢𝐂𝐨𝐦. This relates to the general study of semigroups of the form 𝐕𝐋𝐢, with a general methodology to understand these varieties given in [15]. We will prove the equation sωxytω=sωyxtω, and will in fact give an alternative characterization via three equations. Each of these equations will then be used to give a separate lower bound using fooling set constructions.

Lemma 4.6.

The following conditions are equivalent

  1. 1.

    The semigroup S belongs to 𝐋𝐢𝐂𝐨𝐦.

  2. 2.

    The semigroup S satisfies the equation sωxytω=sωyxtω.

  3. 3.

    The semigroup S satisfies the three equations:

    sωxsωtω =sωxtω (1)
    sωxsωysω =sωxysω (2)
    sωxsωysω =sωysωxsω (3)

Proof sketch.

We can show the equivalence between points 2 and 3 simply by equation manipulation, so the key point is to show the equivalence of points 1 and 2. For the easy 12 direction we see that the equations for 𝐋𝐢 and 𝐂𝐨𝐦 both imply the equation of point 2. For the 21 direction, similarly to Lemma 3.11, taking a semigroup S satisfying the equation, we define one congruence of 𝐋𝐢 and one congruence of 𝐂𝐨𝐦 and we show that their intersection refines the congruence defined by S. For this, we use pumping to construct idempotents in the common prefixes and suffixes (thanks to the 𝐋𝐢 congruence), then using the equation between the idempotents to argue that all letters can be reordered and that we can conclude simply from the 𝐂𝐨𝐦 congruence.

The lemma above gives an equational characterization of 𝐋𝐢𝐂𝐨𝐦 with the equation sωxytω=sωyxtω, intuitively implying that everything commutes between two (possibly different) idempotents. However, to show the lower bound part of Theorem 4.4, we will use the three equations of the third point. Namely, for any semigroup not in 𝐋𝐢𝐂𝐨𝐦, we will see which equation among the three is violated, and use a different proof in each case.

We first notice that the case of a violation of the third equation sωxsωysω=sωysωxsω in fact precisely corresponds to the case of a non-commutative semigroup, and the corresponding lower bound was already proven as Lemma 4.1. Hence, only the first and second equations remain. We give the construction for the first equation:

Lemma 4.7.

For any fixed semigroup Ssωxsωtω=sωxtω, the out-of-order evaluation problem for S has Ω(log(n)) space complexity.

Proof sketch.

We invoke Lemma 2.2 using the following (n2,2n)-fooling set for any n>0. For each pair of indices i,j{2,,n1} define

wi=(sω_)i(tω_)niandvij=(_sω)i1(_x)(_tω)ni

Thus the out-of-order evaluation problem for S has Ω(log(n)) complexity.

Example 4.8.

The semigroup S(abc) has four elements {a,b,c,0}, with a, c, and 0 being idempotent. It does not satisfy the equation sωxsωtω=sωxtω when taking s=a, x=b, t=c: the left-hand side evaluates to abac=0 and the right-hand side evaluates to abc=b. Hence, its out-of-order evaluation problem requires Ω(logn) space by Lemma 4.7.

Note that we can also devise an O(logn) space algorithm for this semigroup by storing the maximal occurrence of a, the minimal occurrence of c, the minimal occurrence of b, and the number of occurrences of 0 and b. The result is 0 if there is a 0 or if there are two or more b. If there is no b the result is a or c or 0 depending on whether a or c or both are present. Last, if there is one b, the result is b unless the rightmost a is right of the b or the leftmost c is left of the b, in which case the result is 0. Thus, the out-of-order evaluation problem for this semigroup is in Θ(logn).

We then give the construction for the second equation:

Lemma 4.9.

For any fixed semigroup Ssωxsωysω=sωxysω, the out-of-order evaluation problem for S has Ω(log(n)) space complexity.

Proof sketch.

We invoke Lemma 2.2 using the following (n2,2n)-fooling set for any n>0. For each pair of indices i,j{2,,n1} define

wi(sω_)i1(x_)(sω_)niandvij=(_sω)j1(_y)(_sω)nj

Thus the out-of-order evaluation problem for S has Ω(log(n)) space complexity.

Example 4.10.

The semigroup S(abba) has elements {a,b,ab,ba,bb,0} and does not satisfy the equation sωxsωysω=sωxysω when taking s=a, x=y=b: the left-hand side evaluates to ababa=0 and the right-hand side to abba=bb. Hence, its out-of-order evaluation problem requires Ω(logn) space by Lemma 4.9.

Note that this is the same underlying language as in Example 3.12, but considering the syntactic semigroup instead of the syntactic monoid. We can also devise an O(logn) space algorithm for this semigroup by storing at most two positions for each of the elements ab, ba, bb, and 0. If any of these elements occurs more than twice, we can immediately return 0. Otherwise, we know that all positions that we have not stored are labeled by a, which gives us enough information to compute the product. Thus, the out-of-order evaluation problem for this semigroup is in Θ(logn).

We can now conclude the proof of Theorem 4.4: by combining Lemma 4.6, Lemma 4.7, Lemma 4.9, and Lemma 4.1, we conclude that any semigroup S𝐋𝐢𝐂𝐨𝐦 must have Ω(log(n)) space complexity.

We have illustrated that there are novel algorithms for the case of semigroups which did not exist in the case of monoids. Yet, the case of out-of-order evaluation for semigroups is still coarser than out-of-order membership problem for regular languages. For instance, we can show the following linear semigroup lower bound, which is in contrast to the O(1) space upper bound for the corresponding language in Example 2.5.

Proposition 4.11.

The out-of-order evaluation problem for S((ab)) has Θ(n) space complexity.

5 Other Complexity Regimes

Our results in the previous section have characterized the semigroups enjoying constant-space out-of-order semigroup evaluation; with a logarithmic lower bound applying to all other semigroups. However, unlike our result for monoids (Theorem 3.2 in Section 3), we have not given a classification of all possible complexity regimes.

Unfortunately, in our present understanding this appears quite challenging, as we will now illustrate. For convenience the results presented in this section are stated for out-of-order membership to languages, but we believe that the phenomena illustrated here already apply to out-of-order evaluation for semigroups (beyond the constant-space regime). Thus, classifying out-of-order semigroup evaluation, or out-of-order membership to regular languages, appears much more challenging than the monoid case.

We first show that some languages admit unexpected logarithmic-space algorithms, and then show an example of a language for which we can devise an O(n)-space algorithm.

Logarithmic-space examples.

The first example that we present is the language L=aba. First note that the syntactic monoid of L is not in 𝐅𝐋𝐂𝐨𝐦: intuitively, there is no k>0 for which we can solve out-of-order membership (or evaluation) by remembering the k first and last occurrences of each letter along with commutative information. More precisely, the first and last a’s are uninformative, and the first and last b’s do not allow us to know whether there could be some a’s surrounded by two b’s. So, the out-of-order monoid evaluation problem for the syntactic monoid of L has linear space complexity by Theorem 3.2. By contrast, we can show that out-of-order membership for L can be done in logarithmic space, with an ad-hoc algorithm:

Proposition 5.1.

The out-of-order membership problem for L=aba has Θ(logn) space complexity.

Proof sketch.

We memorize the minimal position bmin and maximal position bmax where a b occurs, along with the number of b’s. A word belongs to the language precisely when the b’s form a contiguous interval, which amounts to verifying whether the number of b’s is equal to the difference between the minimal and maximal positions of b (plus 1).

Now, for the lower bound, we construct the following (n1,2n)-fooling set for any n. For any 1in1, define wi(a_)i(b_)ni. Now, for any i,j{1,,n1} with i<j define vij(_a)j(_b)nj. This ensures that wivij is in a(ba)+b+, i.e., it is not in L; whereas wjvij is in ab+, i.e., it is in L. By Lemma 2.2, the out-of-order membership problem for aba then has Ω(log(n)) complexity.

Note that this algorithm does not work for the syntactic monoid of aba, because we cannot count the neutral elements between bmin and bmax, rendering the technique useless. The result generalizes to languages such as abcba, by maintaining the counts and minima and maxima of each letter and checking first that the c’s form a contiguous interval, and then that the b’s form a contiguous interval around the c’s (offsetting the count of the b’s by that of the c’s).

One can wonder whether the result also generalizes to languages of the form ababa. This is not immediate, because now we cannot easily identify the a’s in the middle interval, or the b’s in the left interval versus the right interval. However, it turns out that logarithmic space complexity can be shown using a more intricate approach (which also could have been used to show Proposition 5.1):

Proposition 5.2.

The out-of-order membership problem for ababa has O(logn) space complexity.

Proof sketch.

We keep track of the first and last b’s along with the number na of a’s, the sum pa of the positions featuring a’s, and the sum qa of the square of positions featuring a’s. At the end of the algorithm, we can fix na,pa,qa to subtract all positions left of the first b or right of the last b – we know that these positions must contain a’s. We let na,pa,qa be the resulting quantities. Then, we can check that the remaining a’s form a contiguous interval within the b’s by using their number na and their sum pa to find a candidate interval, and then verifying that the sum of squares qa is correct. For this, we use the fact that placing na integers of range pa as a contiguous interval, when possible, is the unique way to minimize the sum of squares qa proved in the full version [6].

For the same reasons as previously stated, this argument does not work for monoids because of the neutral element. It appears that the algorithm could be combined with that of Proposition 5.1 to show a logarithmic space upper bound for more complex languages, e.g., abacaba. However it does not appear easy to generalize it to a higher number of alternations between the same symbol, e.g., ababab.

An 𝑶(𝒏)-space algorithm.

We now conclude the section by showing that, for the language ababab, we can nevertheless achieve sublinear space complexity:

Proposition 5.3.

The out-of-order evaluation problem for ababab has O(n) space complexity.

Proof sketch.

We split the word w of length n into n blocks of size b=n, each either fully memorized (i.e., we have an explicit table of its contents) or summarized by the first letter that is seen in the block. When reading letter x at position i in block B, we update B as follows: if B is fully memorized, we write x in its table; if B is empty, it becomes an x-block; if B is an x-block, do nothing; if B is a y-block with yx, we fully memorize B and write x in its table. Whenever we reach more than 6 fully memorized blocks, we reject early, guaranteeing wL since w contains a scattered subword of the form (ab|ba)6, which is impossible in L. At the end, we reconstruct a word w by completing all blocks: the block contents are xb for non-fully-memorized x-blocks, and the table contents for fully memorized blocks, completing the missing cells by the first letter seen in the block to compensate for the letters streamed before we started fully memorizing the block. We can show that w=w so that testing wL is correct. The space usage is O(n) since we store at most 6 full blocks plus block types.

This algorithm would generalize to more complex languages, e.g., (ab)k and (ab)ka for any constant k. We do not know whether the space complexity achieved by this algorithm is optimal: it would be very interesting to show that such languages admit an Ω(n) lower bound, although at this stage we cannot rule out the existence of an O(logn) algorithm (or indeed other sublinear upper bounds below O(n)).

6 Conclusion and Future Work

We have introduced and studied the out-of-order membership problem for regular languages, and the out-of-order evaluation problem for monoids and semigroups. We have shown that all these problems can be solved with constant time per streamed symbol and linear space, and have aimed to characterize the space complexity depending on the fixed monoid, semigroup, or language. Our results give a complete classification of the space complexity of the problem for monoids, between constant space for commutative monoids, logarithmic space for 𝐅𝐋𝐂𝐨𝐦 monoids, and linear space otherwise. These tractability boundaries happen to coincide with those of simultaneous deterministic communication complexity from [25], though as far as we can see this appears to be coincidental. Our results further classify all constant-space semigroups, with logarithmic lower bounds for all other cases. We have given partial results for the other complexity regimes, but the precise characterizations remain elusive. Refer again to Tables 1 and 2 for a summary of our results and examples.

Our work opens many directions for future work. The obvious question is to classify the remaining cases: in particular finding a superlogarithmic lower bound for some semigroup with a sublinear space complexity, characterizing those semigroups to which an O(n) lower bound applies, or characterizing the O(1) regime for languages. We could study the randomized complexity and the non-deterministic complexity of our problems (see also [2]). It would also be interesting to understand how the complexity changes for problem variants, e.g., when some positions are not streamed and implicitly filled with a default letter, or when we are able to stream the same position multiple times (with the same contents). This latter variant would invalidate some algorithms (e.g., that of Proposition 5.1) so we believe it makes a difference in terms of complexity. We could also study the setting where the word length is not known in advance (though this would make the problem less symmetrical), or settings where the positions are labeled with integers that are not necessarily positive, or consecutive, or indeed with rational numbers.

We have also not discussed the complexity of determining the space complexity regime when we are given a language as input (e.g., as an automaton); or the complexity of our algorithms when the monoid or semigroup or language is not fixed but is also part of the input. Another question, pertaining to the connection to property testing [4, 8], would be to ask how the space complexity is changed if we must only distinguish between words belonging to the target language L and words that are sufficiently far from L; we could also study strengthenings like computing the distance of the word to L.

Another interesting problem variant would be the earliest answer setting, where we would require the algorithm to give its answer about membership to L not at the end of the stream but as soon as it becomes certain, i.e., as soon as we reach a partial word for which all completions belong to L or all completions do not belong to L. For some classes (e.g., commutative languages) it does not seem difficult to enforce this, but we have not investigated whether this requirement would increase the complexity of the problem in general.

One last question is of course about the complexity of out-of-order membership for more general languages than regular languages, e.g., for context-free languages we may not be able to have even an algorithm working in constant time per streamed letter because it would in particular give a linear-time parsing algorithm for arbitrary grammars (already in the special case where the letters are streamed from left to right).

References

  • [1] Scott Aaronson, Daniel Grier, and Luke Schaeffer. A quantum query complexity trichotomy for regular languages. In FOCS, 2019. doi:10.1109/FOCS.2019.00061.
  • [2] Anil Ada. On the non-deterministic communication complexity of regular languages. International Journal of Foundations of Computer Science, 21(04), 2010. doi:10.1142/S0129054110007386.
  • [3] Tyler Akidau, Robert Bradshaw, Craig Chambers, Slava Chernyak, Rafael J Fernández-Moctezuma, Reuven Lax, Sam McVeety, Daniel Mills, Frances Perry, Eric Schmidt, and Sam Whittle. The dataflow model: A practical approach to balancing correctness, latency, and cost in massive-scale, unbounded, out-of-order data processing. PVLDB, 8(12), 2015. doi:10.14778/2824032.2824076.
  • [4] Noga Alon, Michael Krivelevich, Ilan Newman, and Mario Szegedy. Regular languages are testable with a constant number of queries. SIAM Journal on Computing, 30(6), 2001. doi:10.1137/S0097539700366528.
  • [5] Antoine Amarilli, Louis Jachiet, and Charles Paperman. Dynamic membership for regular languages. In ICALP, volume 198, 2021. doi:10.4230/LIPIcs.ICALP.2021.116.
  • [6] Antoine Amarilli, Sebastien Labbe, and Charles Paperman. Out-of-order membership in regular languages, 2026. doi:10.48550/arXiv.2602.13100.
  • [7] Ganesh Ananthanarayanan, Srikanth Kandula, Albert Greenberg, Ion Stoica, Yi Lu, Bikas Saha, and Edward Harris. Reining in the outliers in Map-Reduce clusters using Mantri. In OSDI, 2010. URL: https://dl.acm.org/doi/10.5555/1924943.1924962.
  • [8] Gabriel Bathie, Nathanaël Fijalkow, and Corto Mascle. The trichotomy of regular property testing. In ICALP, 2025. doi:10.4230/LIPIcs.ICALP.2025.141.
  • [9] Savong Bou, Toshiyuki Amagasa, and Hiroyuki Kitagawa. o(1)-time complexity for fixed sliding-window aggregation over out-of-order data streams. TKDE, 36(11), 2024. doi:10.1109/TKDE.2024.3419566.
  • [10] Xinming Chen, Kailin Ge, Zhen Chen, and Jun Li. AC-suffix-tree: Buffer free string matching on out-of-sequence packets. In ANCS. IEEE, 2011. doi:10.1109/ANCS.2011.14.
  • [11] Michael L. Fredman and Dan E. Willard. BLASTING through the information theoretic barrier with FUSION TREES. In STOC, 1990. doi:10.1145/100216.100217.
  • [12] Moses Ganardi, Danny Hucke, Markus Lohrey, Konstantinos Mamouras, and Tatiana Starikovskaya. Regular languages in the sliding window model. TheoretiCS, Volume 4, 2025. doi:10.46298/theoretics.25.8.
  • [13] Moses Ganardi, Louis Jachiet, Markus Lohrey, and Thomas Schwentick. Low-latency sliding window algorithms for formal languages. In FSTTCS, 2022. doi:10.4230/LIPIcs.FSTTCS.2022.38.
  • [14] Étienne Grandjean and Louis Jachiet. Which arithmetic operations can be performed in constant time in the RAM model with addition?, 2023. preprint. arXiv:abs/2206.13851.
  • [15] Nathan Grosshans. A note on the join of varieties of monoids with LI. arXiv preprint, 2021. arXiv:2103.15659.
  • [16] G. H. Hardy and J. E. Littlewood. Some problems of diophantine approximation: Part II. the trigonometrical series associated with the elliptic ϑ functions. Acta Mathematica, 37, 1914. doi:10.1007/BF02401834.
  • [17] John E. Hopcroft, Rajeev Motwani, and Jeffrey D. Ullman. Introduction to automata theory, languages, and computation. Addison Wesley, 3rd edition, 2006.
  • [18] Michal Hospodár, Galina Jirásková, and Peter Mlynárčik. A survey on fooling sets as effective tools for lower bounds on nondeterministic complexity, chapter 3, pages 17–32. Springer, 2018. doi:10.1007/978-3-319-98355-4_2.
  • [19] Donald E. Knuth. Big omicron and big omega and big theta. ACM SIGACT News, 8(2), 1976. doi:10.1145/1008328.1008329.
  • [20] Jean-François Raymond, Pascal Tesson, and Denis Thérien. An algebraic approach to communication complexity. In CCC, 2002.
  • [21] Jan Reiterman. The Birkhoff theorem for finite algebras. Algebra universalis, 14(1), 1982.
  • [22] Howard Straubing and Pascal Weil. Varieties. In Jean-Éric Pin, editor, Handbook of Automata Theory, Volume I, chapter 16. European Mathematical Society, 2021.
  • [23] Kanat Tangwongsan, Martin Hirzel, and Scott Schneider. Out-of-order sliding-window aggregation with efficient bulk evictions and insertions. PVLDB, 16(11), 2023. doi:10.14778/3611479.3611521.
  • [24] Pascal Tesson. An algebraic approach to communication complexity, 1998. MSc thesis.
  • [25] Pascal Tesson and Denis Thérien. Complete classifications for the communication complexity of regular languages. In STACS, 2003. doi:10.1007/3-540-36494-3_7.
  • [26] Jonas Traub, Philipp Marian Grulich, Alejandro Rodriguez Cuellar, Sebastian Breß, Asterios Katsifodimos, Tilmann Rabl, and Volker Markl. Scotty: Efficient window aggregation for out-of-order stream processing. In ICDE, 2018. doi:10.1109/ICDE.2018.00135.
  • [27] George Varghese, J Andrew Fingerhut, and Flavio Bonomi. Detecting evasion attacks at high speeds without reassembly. In SIGCOMM, 2006. doi:10.1145/1159913.1159951.
  • [28] Xiaodong Yu, Wu-chun Feng, Danfeng Yao, and Michela Becchi. O3fa: A scalable finite automata-based pattern-matching engine for out-of-order deep packet inspection. In ANCS, 2016.
  • [29] Meng Zhang and Jiu-bin Ju. Space-economical reassembly for intrusion detection system. In ICICS, 2003.