Two-Way One-Counter Nets Revisited
Abstract
One Counter Nets (OCNs) are finite-state automata equipped with a counter that cannot become negative, but cannot be explicitly tested for zero. Their close connection to various other models (e.g., PDAs, Vector Addition Systems, and Counter Automata) make them an attractive modeling tool.
The two-way variant of OCNs (2-OCNs) was introduced in the 1980’s and shown to be more expressive than OCNs, so much so that the emptiness problem is undecidable already in the deterministic model (2-DOCNs).
In a first part, we study the emptiness problem of natural restrictions of 2-OCNs, under the light of modern results about Vector Addition System with States (VASS). We show that emptiness is decidable for 2-OCNs over bounded languages (i.e., languages contained in ), and decidable and Ackermann-complete for sweeping 2-OCNs, where the head direction only changes at the end-markers. Both decidability results revolve around reducing the problem to VASS reachability, but they rely on strikingly different approaches. In a second part, we study the expressive power of 2-OCNs, showing an array of connections between bounded languages, sweeping 2-OCNs, and semilinear languages. Most noteworthy among these connections, is that the bounded languages recognized by sweeping 2-OCNs are precisely those that are semilinear. Finally, we establish an intricate pumping lemma for 2-DOCNs and use it to show that there are OCN languages that are not 2-DOCN recognizable, improving on the known result that there are such 2-OCN languages.
Keywords and phrases:
Counter Net, Two way, AutomataFunding:
Shaull Almagor: supported by the ISRAEL SCIENCE FOUNDATION (grant No. 989/22).Copyright and License:
![[Uncaptioned image]](x1.png)
2012 ACM Subject Classification:
Theory of computation Automata extensionsAcknowledgements:
We are grateful to Dmitry Chistikov for shedding light on some claims made in [4] and to an anonymous reviewer for providing some key references.Editors:
Jörg Endrullis and Sylvain SchmitzSeries and Publisher:

1 Introduction
A One-Counter Net (OCN) is a finite state automaton equipped with a counter that cannot decrease below zero, but cannot be explicitly tested for zero. It is a natural restriction of several computational models: One-Counter Automata without zero tests, and Pushdown Automata with a single letter stack alphabet. It can also be thought of as 1-dimensional Vector Addition Systems with accepting States and an alphabet.
OCNs are an attractive model for studying the border of decidability, as several problems for them lie close to the decidability frontier (e.g., both determinization and universality may be decidable or undecidable, depending on the precise definition and context [16, 17, 2, 3, 1]).
OCNs suffer from an unsettling asymmetry: the language is OCN-recognizable (by counting on and on ), whereas the language is not OCN-recognizable. In particular, they are not closed under reversal (nor under intersection). A natural way of making OCNs more robust is therefore to look at their two-way variant.
This approach was taken in [26, 7], where the model of two-way one-counter nets (2-OCNs) is introduced and studies. A 2-OCN is a one counter net that receives its input on a tape, surrounded by end-markers and , and is allowed to move a read-only head back and forth on the tape. As the following examples witness, the introduction of a two-way tape significantly increases the expressive power of the model, as well as its deterministic fragment (2-DOCN). For uniformity, we henceforth refer to one-way OCNs as 1-OCN.
Example 1.
Consider the language , which is easily shown not to be 1-OCN-recognizable. can be recognized by a 2-OCN (in fact 2-DOCN, see Fig. 2) by using a “counter reset”: it starts with a forward scan verifying that the format is while counting on and on . Thus, upon reaching , the counter has value (unless , in which case the run terminates due to the counter becoming negative, and the word is not accepted), and in particular . It then moves backwards to , counting on and on , thus resetting the counter back to . Next, it goes forward again to , then similarly computes from right to left. Intuitively, this approach recognizes and uses the closure of 2-DOCN under intersection [26].
The next example demonstrates the fact [7] that 2-OCN are more expressive than 2-DOCN. In addition, it demonstrates that sometimes it is (seemingly) necessary for the head to “change direction” inside the tape, rather than at the end markers.
Example 2.
Let be the language , i.e., words where the length of the first -segment is matched in some later segment. This language is not 1-OCN recognizable, and one may observe that a similar approach to Example 1 fails, because we would need to guess the index for which , but in order to reset the counter, we must “forget” the guess. Thus, in order to recognize this with a 2-OCN, we must compare to via a single visit to the -th segment.
To achieve this, the 2-OCN counts on the first segment , then scans forward and guesses when the correct segment is reached. It then counts on . Thus, upon reaching , the counter holds (and so guarantees that if it survives). Then, a backward scan is performed from counting on in , until is reached, and then continues left to and finally counts on the ’s in . Thus, the counter ends with value . Again, if the run terminates, so the whole run survives if , which combined with gives (see Fig. 2).
A 2-OCN that only changes its head direction at the end marks is called sweeping. Example 2 prompts studying the expressive power of sweeping 2-OCNs, which we take on in this work.
Related Work.
Two-way automata have received much attention since their introduction for finite automata in [30]. 2-PDAs were studied in [24, 14, 4, 27], focusing mainly on closure properties and languages recognizable by 2-PDAs. As noted in [4], languages not expressible by 2-PDA can be derived from the complexity results obtained in [14] (specifically, 2-PDA languages are in linear space and polynomial time, and the time/space hierarchy theorems offer languages outside of these classes). No automata-based, combinatorial techniques are known to the authors to show that some languages is not expressible with a 2-PDA, and most questions about 2-PDA, including whether the deterministic variant can recognize all context-free languages, are open [13].
In the world of counter machines, [12] shows that DFAs with a “blind tape” (a form of a nonnegative counter that cannot be 0-tested) are less expressive than 2-DPDAs. A significant body of work by Ibarra et al. studies reversal bounded counters [19, 21, 18, 10, 20], i.e., counters that cannot increase and decrease unboundedly many times. The heavy reliance on reversal-boundedness makes these works orthogonal to ours. Atig and Ganty [5] study a VASS-CFG hybrid, and use also make use of reductions to VASS with 0-tests, but with significant technical differences.
Closer to our setting are two-way one-counter automata (2-OCAs), that allow explicit zero tests. In [29] certain languages computing squaring or exponentiation are shown not to be 2-DOCA recognizable (the deterministic variant), whereas [11] exhibit nonsemilinear 2-OCA languages. Ibarra and Su [21] note that 1-sweep 2-DOCA have an undecidable emptiness problem; in contrast, we show that this problem is decidable for sweeping 2-OCN. They also note that, over bounded languages, emptiness of 2-DOCA is decidable provided that the counter reaches zero only a constant number of times; in contrast, we show that this emptiness over bounded languages is decidable for any 2-OCN.
A nearly identical model to 2-OCN has been introduced in [26] and further studied in [7]. In their model, the counter updates are , which essentially amounts to encoding counters in unary. In [26], the focus is on 2-DOCN recognizable languages. It is shown that this class is closed under intersection, but not under complementation nor union. Further, over bounded languages, 2-DOCN can be made sweeping111The proof in [26] is actually missing significant details, but those can be reconstructed., and the Parikh images of their languages are semilinear. An example is also given to show that 2-DOCN are less expressive than 2-OCN.
The algorithmic contribution in [7] is that the emptiness problem for 2-DOCN is undecidable. Additionally, it is shown that if a 2-OCN accepts a word of length , then it accepts it with a run of polynomial length in . The latter result no longer holds for our model, due to the binary encoding of the counters.
Contribution and Paper Organization.
In this work we provide a modern view of 2-OCNs and some of its interesting and decidable fragments, namely bounded-language 2-OCNs and sweeping 2-OCNs. In Section 3.1 we give a counter-machine based proof of the undecidability of 2-DOCN emptiness. In Section 3.2, we consider 2-OCNs whose languages are bounded, i.e., of the form . We show that under this restriction, emptiness becomes decidable. In Section 3.3, we consider sweeping 2-OCNs, i.e., 2-OCNs whose head-movement direction only changes at the end-markers. We show that here too emptiness becomes decidable and we give precise complexity bounds, using recent results regarding VASS reachability. In Section 4, we investigate the relationship between semilinear languages and 2-OCN languages. We show that for bounded languages, the set of lengths of words in the language of a 2-OCN is semilinear, and derive explicit languages that are not 2-OCN-recognizable. We also show that a bounded language is semilinear if and only if it is recognizable by a sweeping 2-OCN. In Section 5, we show an elaborate pumping lemma for 2-DOCNs. Apart from its independent usefulness for understanding 2-DOCNs, this enables us to demonstrate a 1-OCN whose language is not 2-DOCN recognizable, establishing a new separation result. We conclude in Section 6 with some open problems.
We encourage the reader to initially skip the proofs and garner a bird’s eye view of our results. Thereafter, we note that Sections 3.2, 17, 14, and 15 have the most interesting proofs. Detailed proofs appear in the appendix or in the full version.
2 Preliminaries
Two-Way OCN and Automata.
For an alphabet set , we denote where are two symbols not appearing in , called left end-marker and right end-marker, respectively. A Two-Way One-Counter Net (2-OCN) is a tuple where is a finite set of states, is a finite alphabet, is a finite set of transitions, are the initial and accepting states, respectively. Intuitively, a transition dictates that from state , when reading letter , we add to the counter value, move the position of the head on the tape by , and reach state . We call the effect and the head shift of the transition. Unless explicitly stated otherwise, we assume that the effect is encoded in binary. We require that if then and if then . That is, upon reaching the end-markers, the head cannot proceed beyond them.
We say that is deterministic (denoted 2-DOCN) if for every there is at most one and such that . We say that is one way (denoted 1-OCN) if every transition moves the head to the right (i.e., the head shift is 1).
A configuration of is representing the current state , counter value , and head position . Note that we do not consider configurations with negative counter values, thus enforcing the semantics of Counter Nets. Consider a word , we augment it to by setting and . A run of on is a finite sequence of configurations such that for all , and for every we have . That is, each configuration follows from the previous one by the transition relation. A run is initial if and . A run is accepting if . We tacitly assume that runs do not continue after the accepting state. The word is accepted by if has an initial and accepting run on it. We define . Note that, just like OCNs, 2-OCNs are monotone, i.e., every word accepted from initial configuration is also accepted from for every . We sometimes consider sequences of pseudo-configurations where the counter value becomes negative. In such cases, we say that the run is cut short by a counter violation, and we mean that the sequence of configurations is a run up to a prefix in which the counter becomes negative.
Finally, we sometimes discuss classical Boolean automata (NFA, DFA, 2-NFA and 2-DFA). For our purposes, they can be thought of as 2-OCN where all the counter updates are , and are therefore omitted.
VASS and VASS with bounded 0-tests.
A Vector Addition System with States (VASS) is a tuple where is a finite set of states and is the transition relation. A configuration of is where and (note that this is a vector of nonnegative integers). We say that configuration is reachable from configuration if there is a finite sequence of configurations such that and for every we have . Intuitively, a VASS runs by repeatedly adding vectors to the configuration, as long as all the counters (i.e., the entries of the vector) remain nonnegative. The reachability problem for VASS asks whether is reachable from and is known to be decidable [9].
While VASS do not allow explicit 0-tests, it is possible to simulate a fixed number of 0-tests within the context of reachability [8, 9]. That is, we can extend the transition relation so that , and a transition can be taken only when the counters that have tests are 0. As long as there is a constant such that there are no more than 0-tests along every run of the VASS, then reachability remains decidable. The simplest approach to achieve this is to increase the dimension by , keeping copies of every counter, and whenever a counter needs to be tested for 0, a copy of it is “frozen” (i.e., never changes again) and the reachability target has on all frozen entries. Note that we can mark which entries are frozen in the states of the VASS, since there are at most of them. We remark that more elaborate approaches can keep the number of added counters small [8, 9]. We call such machines VASS with bounded 0-tests.
3 The Emptiness and Membership Problems in 2-OCNs and Variants
3.1 Emptiness of 2-DOCNs is Undecidable
It is shown in [7] that already for 2-DOCNs, the emptiness problem, namely the problem of deciding whether for a given 2-DOCN , is undecidable. This is shown by a reduction from Hilbert’s 10th problem. More precisely, it is shown that 2-DOCN can simulate multiplication, in a sense.
An arguably cleaner way of obtaining this result while staying in the world of counter machines, is via reduction from the halting problem for Two-Counter Machines, as follows:
Theorem 3 ([7]).
The emptiness problem for 2-DOCN is undecidable.
Proof.
We show the undecidability of 2-DOCN emptiness by reduction from the (complement of the) halting problem for two-counter machines, which is known to be undecidable [25].
Given a two-counter machine , we construct a 2-DOCA which reads input words of the form , representing the values of the counters and along the run of , with each step of the machine separated with . Intuitively, tracks the location of in its state, uses the counter values to determine the location after a jump, and at each steps checks that the counter values between steps are consistent, similarly to Example 1. Then, accepts if at any point the computation reaches the location halt. If a counter violation is encountered, moves to a rejecting sink.
More precisely, starts by checking (using a single right-to-left pass with a DFA) that the word adheres to the format . It then resets the head to , records the location in its state, moves the head to the first and begins the simulation. simulates each step starting from the preceding the segment representing the current counter values, and along the simulation scans only the segment of the word of the form starting at the current head location. Note that crucially, the format of the segment is fixed, so that the head can end the simulation in the middle , thus correctly viewing the counter values of the next step.
If the command at the current location is goto , then checks that and , similarly to Example 1. Similarly, if the current command is inc(x) or dec(y), then checks that and , or that and , respectively, by a similar comparison prefixed by a counter change.
If the current command is if x=0 goto else goto , then checks that and , and then checks whether (by checking if there are any ’s after the first in the segment), and if so moves to and otherwise to .
Finally, if ever reaches the command halt, it accepts, and if any of the comparisons fails, the counter goes below 0 and the word is rejected. If is reached without reaching halt, then moves to a rejecting sink.
We then have that if and only if there exists a word representing the counter values in a halting run of , if and only if halts.
3.2 Emptiness of 2-OCNs over Bounded Languages is Decidable
Consider an alphabet . A language is bounded if . In this section, we show that the emptiness problem of 2-OCNs over bounded languages is decidable.
Remark 4.
Generally, a bounded language is a subset of where each (in particular, e.g., is also bounded). Our results readily extend to these languages, but they add a cumbersome notational layer, which we opt to avoid.
Theorem 5.
The following problem is decidable: given a 2-OCN is ?
We prove Theorem 5 in the remainder of this section. The proof is by reduction to the VASS nonreachability problem, known to be Ackermann-complete [9].
Consider a 2-OCN . We henceforth assume that membership in is already tested within , i.e., that is a bounded language. We further assume that , namely that every letter occurs at least once in every word in (dubbed properly-bounded). Indeed, for the purpose of emptiness we can split to a union over all subsets of properly-bounded languages over .
We obtain from a VASS with bounded 0-tests such that iff reaches for certain states of .
Construction of – Intuitive Overview
We present the intuition behind . The formal construction can be found in Appendix A. simulates the behavior of in several components using several counters. We start by describing the counters of and their intuitive roles.
-
c – tracking the counter of
-
c_freeze_1 – a copy of c, to be frozen at the beginning of a positive loop.
-
c_freeze_2 – a copy of c, to be frozen at the end of a positive loop.
-
and – pairs of counters marking the position of the head from the Left and from the Right for each segment .
The operation of is as follows. The initialization component guesses a word by guessing the numbers and storing them in the R[i] counters of . This is done with self loops that allow to arbitrarily increase each of the relevant counters.
Once initialization is complete, moves to the simulation component. There, aims to simulate runs of on the guessed word. Simulating the counter of is straightforward, using the counter c to match it. In addition, if at any point the accepting state of is reached, then moves to the finalizing component (described below). In order to simulate the head movement, we refer to each as a segment (treating and also as segments), and we split the simulation to two types of moves:
-
A move within a segment simulates reading and the head staying within the segment. To do this, we set the two counters for the segment such that L[i] keeps track of the head location within the segment and (using the initialization counter). To simulate a move to the right we increase L[i] by 1 and decrease R[i] by 1, and vice-versa for a move left. Note that since is constant after initialization, the VASS semantics prevents the simulation from going over the segment borders.
-
A move between segments occurs when guesses that a border of the segment is reached, and an adjacent segment should be moved to. If the move is to the right, then R[i] should be , and we therefore perform a -test on it, and move to segment . Note that from then on, the head movement counters are L[i+1] and R[i+1] (until is reached again). This segment index is stored in the states of .
Since we are only allowed a fixed number of -tests, we store in the states how many moves from segment to were performed. If this number exceeds , where is the number of states of , then the run cannot proceed (it reaches a sink). We justify this in the proof.
An accepting run of might require the counter to be “charged” by repeating a loop, possibly involving moves between segments. Since we cannot simulate this with a bounded number of -tests, we instead detect loops, as follows. While moving between segments, may guess that the current move leads to a repetition of the state and head position in the run of . Thus, may nondeterministically record in its state the current state of (i.e., the state from which the move occurs), and also freezes a copy of the current counter of in c_freeze_1. Then, upon another move between the same two segments, may check whether the current state is again . If so, records the counter at the second move as well in c_freeze_2, at which point the simulation moves to the 2-NFA Phase.
The 2-NFA Phase assumes (and later verifies) that during its run, encountered a positive loop, i.e., a path from configuration to with . In this case, can charge the counter to an arbitrarily large value. Then, the word is accepted if it is accepted in the 2-NFA obtained from by ignoring the counter, starting from . In this phase we repeat the simulation phase above, ignoring the counter of , but still limiting to moves between each two segments, with the reasoning that more than moves imply a cycle, and therefore if there is an accepting run, there is also a shorter one.
Finally, upon encountering the accepting state of , we move to the finalizing component. There, it remains to verify that if the 2-NFA phase was reached, then indeed a positive loop was encountered. To do so, the two frozen counter values of the loop () are decreased simultaneously, and at some point a nondeterministic transition decreases only the (presumably higher) counter c_freeze_2 and moves to the state . Then, all the counters are allowed to be decreased, so that we can reach . This ensures that only if c_freeze_2 was indeed higher than c_freeze_1, witnessing that a positive loop was encountered.
3.3 Emptiness of Sweeping 2-OCNs is Decidable
In this section we consider the sweeping fragment of 2-OCNs. A run of a 2-OCN is said to be sweeping if the head shift of the transitions changes only at the end-markers (i.e., the run “sweeps” forward from to , and then sweeps backward to ). A 2-OCN is sweeping if all its runs are sweeping. We assume w.l.o.g. that a sweeping 2-OCN only accepts when reading (otherwise we add states to complete a backward sweep and then accept). We show that for sweeping 2-OCN, the emptiness problem is Ackermann-complete. The proof is split to the upper bound (Theorem 6) and lower bound (Theorem 7).
Theorem 6.
The emptiness problem for sweeping 2-OCNs is decidable in Ackermannian complexity.
Proof.
We obtain the Ackermann upper bound of Theorem 6 by reducing nonemptiness of Sweeping 2-OCN to VASS-reachability. Similarly to Theorem 5, our proof relies on the observation that once a positive loop is encountered, we can replace the 2-OCN with a Boolean automaton. The challenge here, however, is that the language is not bounded, and therefore we cannot keep track of the head in a fixed number of segments. Instead, we use the sweeping property to simulate all the sweeps of the 2-OCN within a single pass, in a radically different approach than in the previous section.
Specifically, Consider a sweeping 2-OCN . We obtain from a VASS with bounded 0-tests such that iff reaches for certain states of . Moreover, this can be done in single-exponential time, and the size of is single-exponential in that of .
The formal construction appears in Appendix B. We present the intuition here.
Construction of – Intuitive Overview.
As an example, assume that accepts a word using two forward sweeps and two backward sweeps, as depicted in Fig 3.
We can simulate the entire run of on using a VASS by, intuitively, tracking all the forward sweeps in parallel with all the backward sweeps, where for the backward sweeps we simulate the reverse 2-OCN of , denoted . The latter is a 2-OCN obtained from reversing the transitions, as well as negating the counter effects and the head shifts in all transitions. In order to make sure the sweeps concatenate correctly, we guess the states and the counter values. Specifically, we proceed as follows. We start by guessing the state in which each backward sweep ends. In this case, a successful guess is (note that all guesses must start and end in the initial and accepting state). We then guess the counter values with which each backward sweep ends. We store two copies of each guess, one used to simulate the backward run, and the other to simulate the next forward run (except for the last sweep). In our example, the successful guess is (where is the initial counter for the first forward sweep).
At each step, we now nondeterministically guess letters and corresponding transitions from for the forward sweep, and from for the backward sweep. After guessing the correct word and keeping track of the counters in all components, we would then reach the counters . Indeed, the forward run from reaches counter , whereas the backward run from starts from counter , and hence would yield along the reversal of the run. In addition, we track the states of and of along each run.
The last component of verifies that the concatenation is correct, i.e., that each pair of adjacent counters are equal, and that the reached states match. The latter is encoded in the states, and the former is tested by decreasing both counters together, and checking reachability of .
In order to generalize the example above, it is crucial that the number of sweeps be uniformly bounded. To this end, we observe that if there are more than forward-backward sweeps along a run, then a state is visited twice at , i.e., forms a loop. If this loop decreases the counter, then there is a shorter run with higher counters, so we need not simulate this former run. If this loop increases the counter, then by repeating it we can obtain an arbitrarily high counter. We can then discard the counter and look for a short accepting run, based on the 2-NFA obtained from , similarly to Section 3.2. However, unlike Section 3.2, we cannot simply simulate the 2-NFA, since we are committed to a single sweep on the guessed word. In order to overcome this, we initially guess which state will repeat in a positive loop, and then simulate a DFA that is equivalent [31, 30, 32] to the 2-NFA above, starting from state . At the end of the run, we verify using the reachability query that the loop is indeed positive. Combining these ideas yields a VASS with the conditions above.
Theorem 7.
The emptiness problem for sweeping 2-OCN is Ackermann-hard.
Proof.
We show a reduction from VASS reachability, known to be Ackermann-hard [23, 9], to nonemptiness of sweeping 2-OCN. Specifically, we assume that the reachability query is from to , as this preserves the hardness.
Consider a VASS of dimension and configurations to . We construct a sweeping 2-OCN where the alphabet is , i.e., the transitions of . works as follows: given a word , first sweeps forward and checks that the states given by the transitions follow a run from to in (ignoring the counter values). If this fails, rejects.
Next, checks that the counter updates are valid and take the configuration from to . This is done in two phases. In the first phase, makes forward sweeps, where sweep simulates the -th counter of in the transitions given by , and each backward sweep resets the counter to by negating the effect of the transition. Thus, if the sweeps complete successfully, we are assured that prescribes a run that reaches from to for some . Then, makes a forward sweep while keeping the counter at and starts the second phase, where we perform the same simulation but backwards: we check that the run prescribed by (the reverse of ) leads from to for some (note that here we negate the counter operations).
The main observation required to complete the proof is that a for a run of from to we have that (with negated counters) is a valid run from iff . This follows immediately from the VASS semantics.
We conclude that iff is reachable from in .
3.4 The Membership Problem for 2-OCN is Polytime
Recall that by [7], if a 2-OCN has counter updates in , we can compute in polynomial time (in the description of ) a polynomial such that if a word is accepted, then it is accepted with a run of length at most . Unfortunately, this does not carry through to our setting, where counters are encoded in binary, as the following example shows:
Nevertheless, we show that 2-OCN retain efficient membership even with binary counters:
Theorem 8.
The membership problem for 2-OCN (given a 2-OCN and a word , is ?) is decidable in polynomial time.
Proof.
Consider a 2-OCN and a word . We implicitly assume that already includes the end-markers. Similarly to the reasoning in Sections 3.2 and 3.3, we observe that if an accepting run of on visits the same state/position pair twice, then if the counter effect between the visits is nonpositive, a shorter accepting run exists, and if the counter effect is positive, then this cycle can be pumped arbitrarily, so that acceptance is dependent only on the 2-NFA obtained by ignoring the counter effects starting from . Since the number of state/position pairs is , it follows that within steps we visit a cycle. Similarly, in the 2-NFA if an accepting run exists, then there is also one of length at most .
Deciding whether there exists an accepting run in the 2-NFA from is straightforward: we keep track of the reachable set of configurations at each step of the run, up to steps, and if is reached then the run is accepting.
It remains to detect acceptance in the 2-OCN, or to detect a positive cycle. To do so, we proceed as follows. We simulate for steps, and for each step we store a function that stores for each configuration the maximal counter with which can be reached after steps. It is easy to compute from in polynomial time. Once this simulation is complete, if does not appear, then we want to look for a positive cycle. To this end, for each and configuration , we again construct functions similarly to the above, with initial configuration . If at any point we encounter with a counter higher than , then we have a positive cycle. Otherwise, the word is not accepted.
It is easy to verify that all the computations can be carried out in polynomial time.
4 On the Semilinearity of 2-OCN Languages
Preliminaries.
Emptiness decidability has been historically intimately tied to semilinearity. A set is semilinear if it is expressible in first-order logic with addition (there are many equivalent definitions and we will not need a specific one). The Parikh image of a language over an alphabet is the set of vectors such that there is a word in with precisely letters (in any order), for all . In other words, the Parikh image counts the number of times each letter appears in a word , resulting in a set of vectors in . A class of languages is semilinear if for any language in that class, the Parikh image of is semilinear. In that case, if there is an algorithmic way to obtain a representation of that semilinear set from a representation of , then one can check whether , since satisfiability is decidable for first-order logic with addition [6].
General 2-OCN.
Since the emptiness problem is undecidable for 2-DOCN, it is an unsurprising fact that there are nonsemilinear 2-DOCN languages (e.g., ). A tantalizing question surging from the study in Section 3.2 is: Are all 2-OCN bounded languages semilinear? For 2-DOCN, it is proved in [26] that this is indeed the case. For 2-OCN, it is likely that a proof of this statement would provide an alternative proof to Theorem 5. We conjecture that, indeed, 2-OCN are in good company with a wealth of computational models whose bounded languages are precisely those with a semilinear Parikh image. We come short of showing this, but prove, using the construction of Section 3.2 and a result of [15], that:
Theorem 9.
For any 2-OCN with and for any , the set is effectively semilinear.
Proof.
In [15, Corollary 4.3] the following is shown.222In [15] the result is phrased for Petri Nets, but the equivalence between Petri Nets and VASS gives a straightforward translation. In [22], a slightly weaker result is presented in the language of VAS. Given a VASS of dimension and two states , consider the set such that iff is reachable from via a run that takes transition exactly times. Then the image of every morphism is effectively semilinear.
In our setting, take to be the VASS obtained in Section 3.2 with the states and . Let be the morphism that counts the number of transitions used to initialize for all , and recall that by the correctness of the construction of , we have that along a run from to , the value of after the Initialization Component corresponds to the length of the segment in an accepted word. Then, [15, Corollary 4.3] readily shows that the set is effectively semilinear. The previous theorem implies that 2-OCN languages over a singleton alphabet are semilinear. In addition, we have the following:
Corollary 10.
The languages and , known to be 2-DOCA languages [29], are not 2-OCN languages.
Sweeping 2-OCN.
We note that Theorem 9 also applies to sweeping 2-OCN without the bounded language restriction, with a similar proof.
Theorem 11.
For any sweeping 2-OCN over and , is effectively semilinear.
However, unlike the case of bounded languages, here we can show that the Parikh image of sweeping 2-OCN language are not always semilinear.
Proposition 12.
There are nonsemilinear languages recognized by sweeping 2-DOCN.
Proof.
Consider the Dyck language over , i.e., well-parenthesized expressions where corresponds to an opening parenthesis and to a closing one. Write for the prefixes of that language. Clearly, is a 1-DOCN language. Write for the language where and swap roles. We can construct a sweeping 2-DOCN that recognizes using the closure of 2-DOCN under intersection [26] (which preserves the sweeping property). In any word in , if is the length of the first block of ’s, then . Finally, using a few more sweeps, we can express:
Indeed, this requires checking that the number of ’s is equal to the number of ’s in the first block, the number of ’s in the last block, and the number of infixes.
Now, for a fixed value , the longest word in that can appear after is , of length , showing that this language is not semilinear.
We will show, in Section 5, that there is a 1-OCN language (thus a sweeping 2-OCN language) this is not expressible using a 2-DOCN. Conversely, Proposition 12 provides an easy way to show that:
Corollary 13.
There is a 2-DOCN language that is not expressible using a sweeping 2-OCN.
Proof.
Consider the language over the alphabet defined as:
By applying the morphism that erases , and , we have , which can be seen as multiplication.
The language is recognizable by a 2-DOCN by repeatedly using a similar approach to Example 1:
-
Check that the input has the form ,
-
Check that the number of ’s is the same as the number of ’s,
-
Check that the first sequence of ’s is as long as the sequence of ’s,
-
Check that the lengths of all sequences of ’s are equal, by checking that each neighboring sequence satisfies ,
-
Check for each sequence that , and the same for the last segment where is replaced with .
This 2-DOCN proceeds segment by segment, always making sure that the number of ’s increases by exactly . This, together with the initial test that the number of segments is , forces the number of ’s to be exactly .
However, the lengths of words in this language do not form a semilinear set, so by Theorem 11 it cannot be recognized by a sweeping 2-OCN.
Over bounded languages, the following exact characterization holds:
Theorem 14.
A bounded language is recognized by a sweeping 2-OCN iff it is semilinear.
Proof.
The fact that any semilinear bounded language can be expressed with a sweeping 2-OCN is easily deduced from the fact that semilinear sets are those expressible with quantifier-free first-order formulas with addition, order, and modulo. Putting such a formula in DNF, we simply need to guess which conjunction is to hold, and the conjunction can be checked by a sweeping 2-DOCN using the closure of 2-DOCN under intersection [26] (which preserves the sweeping property).
We turn to the fact that bounded languages expressed by sweeping 2-OCN are semilinear. We split the proof into two parts.
Constant number of sweeps.
Let us first assume that the sweeping 2-OCN executes a constant number of sweeps, say , with odd, for each input word (we assume here that the machine ends at the right end-marker). With , consider the following transformation that produces a word over the alphabet :
That is, the input is replicated times, with every other copy reversed, and each copy on its own alphabet. Let us call the replica of . Since makes at most sweeps, we can build a 1-OCN that reads the replica of any word and accepts if and only if is accepted by the 2-OCN (and the input is of the format ). Naturally, also accepts words that are not replicas.
We circumvent this problem by using an “external” semilinear set, as follows. Let be the Parikh image of the language where for all , each appears the same number of times; clearly, a word in is a replica iff its Parikh image is in . Since is a 1-OCN language (and in particular context-free), the Parikh image of its language is semilinear by Parikh’s Theorem [28]. It is then easy to check that is exactly the set of Parikh images of replicas of words in , showing that the Parikh image of itself is semilinear (as a projection of the former).
Any number of sweeps.
If is sweeping but does not do a constant number of sweeps for each input, we adapt the recurring idea of Sections 3.2 and 3.3. Let be the number of states of . After sweeps, a state is repeated at the left end-marker; if the counter effect between the two repetitions is nonpositive, then that run is not useful for acceptance, and can be disregarded. If the counter increases, then it can be increased arbitrarily, and the behavior of the 2-OCN is the same as a 2-NFA from that point on. Consider the 1-OCN constructed above, with . We can additionally have guess the state that repeats, if any, and check that it indeed repeats. Next, assumes that the counter strictly increases in the repetition, and branches into a DFA for . In order to “kill” the runs of where this assumption is incorrect, any transition that takes, in between the first and second appearance of the repeated state, should be followed by input symbols indicating the counter effect, increasing () or decreasing (), and by how much. then verifies that the correct effect is matched to the transition of it guesses. For instance, may read the following replica of with extra counter information (here the original alphabet is and the replica alphabet has as indices):
As mentioned, this modified version of may jump into a DFA for when it should not. However, we can augment the external semilienar set , checking that the input word is a replica, to also check that the number of + is strictly greater than the number of -. This corresponds to the counter updates between the state repetitions having a strict positive impact, and that correctly jumps into ’s simulation. To conclude, let again be the (semilinear) Parikh image of the language of . The vectors in correspond precisely to the replicas (with possibly explicit counter updates) that are accepted by , hence the language of is semilinear.
5 2-DOCN vs 1-OCN: A Pumping Lemma for 2-DOCN
In [7] it is shown that the language is 2-OCN recognizable but not 2-DOCN recognizable. We strengthen this result in two directions: first we present a general pumping lemma that we can then use to establish that various languages are not 2-DOCN-recognizable. Second, using our lemma we are able to find a language that is not 2-DOCN recognizable, but is already 1-OCN recognizable. This shows that nondeterminism can be used to compensate for two-wayness, in some settings.
Lemma 15.
Let be an alphabet and . For every 2-DOCN-recognizable language there exists such that for every , if then there exists such that as well.
We give a rough sketch of the main ideas of the proof, see the full version for the complete arguments and definitions. Consider a 2-DOCN , and think of as some large constant. Our goal is to devise a way to pump certain infixes of a word , so that we can reason about the resulting run of on it. Naturally, the challenge lies in synchronizing the behaviors of forward and backward head movements in the run.
We think of the infix as partitioned to three parts: two short segments referred to as outer left ’s and outer right ’s, and a long segment in the middle, called the inner ’s. We depict the word as
We then consider the form of the run of on . Specifically, we divide runs according to whether they cross into the inner ’s. We show that runs that do reach the inner ’s, must continue to the end of the tape (either from right or left). Runs that do not cross the inner ’s must therefore get stuck in a small part of the tape. By depicting runs as
going from top to bottom upon head reversal, we have e.g., the type
We define a round trip to be a concatenation of runs of the form
A crux of the proof lies in handling Form (3) above. The main idea is the following: consider a run with several round-trips that ends due to a counter becoming negative. Since the inner ’s are a long infix, we can find two indices that are visited with the same sequence of states in the round trips. We can then attempt to “pump” or “cut” the infix between these indices, but this may cause the counter to ultimately increase, thus yielding an accepting run from a previously-rejecting one.
We therefore carefully analyze the effect of the counter between these two indices on each pass of the form
Corollary 16.
is 1-OCN but not 2-DOCN recognizable.
Proof.
is 1-OCN recognizable by guessing whether or , and checking by either increasing the counter on and decreasing on , or increasing on and decreasing on (with another decrease at the end to make the inequality strict). Assume by way of contradiction that for a 2-DOCN . Let be the constant provided by Lemma 15, then . Then, Lemma 15 guarantees that there exists such that , but , since either or , a contradiction. We remark that we can similarly show that is also not 2-DOCN recognizable (but is recognizable by a sweeping 2-OCN), giving an alternative proof to the result in [26].
6 Research Directions
Separations.
The classes we consider, 1-OCN, 1-DOCN, 2-OCN, 2-DOCN, sweeping and nonsweeping, over bounded languages or not, are all provably distinct, except for the following: are all bounded 2-OCN languages expressible with a sweeping machine? We conjecture that they are, and leave this question open (this is known for 2-DOCN from [7]).
Closure.
The examples of languages outside 2-DOCN readily show that the class of languages recognized by 2-DOCN is not closed under union nor complement, but is closed under intersection (also shown in [26]). For 2-OCN, the class is closed under union, but we conjecture that it is closed under neither intersection nor complement. We leave these questions open, together with the same questions for sweeping 2-OCN.
Decidability.
Beyond emptiness, our proofs imply that it is decidable whether the intersection of two (sweeping or bounded) 2-OCN is empty. The next natural question is then the decidability of inclusion. We conjecture that this problem is decidable for sweeping and bounded 2-OCN.
References
- [1] Shaull Almagor, Guy Avni, Henry Sinclair-Banks, and Asaf Yeshurun. Dimension-minimality and primality of counter nets. In International Conference on Foundations of Software Science and Computation Structures, volume 14575, pages 229–249. Springer, Springer, 2024. doi:10.1007/978-3-031-57231-9_11.
- [2] Shaull Almagor, Udi Boker, Piotr Hofman, and Patrick Totzke. Parametrized universality problems for one-counter nets. In 31st International Conference on Concurrency Theory (CONCUR 2020). Schloss Dagstuhl – Leibniz-Zentrum für Informatik, 2020. doi:10.4230/LIPIcs.CONCUR.2020.47.
- [3] Shaull Almagor and Asaf Yeshurun. Determinization of one-counter nets. In 33rd International Conference on Concurrency Theory (CONCUR 2022). Schloss Dagstuhl – Leibniz-Zentrum für Informatik, 2022. doi:10.4230/LIPIcs.CONCUR.2022.18.
- [4] Setsuo Arikawa. On some properties of length-growing functions on two-way pushdown automata. Memoirs of the Faculty of Science, Kyushu University. Series A, Mathematics, 22(2):110–127, 1968.
- [5] Mohamed Faouzi Atig and Pierre Ganty. Approximating petri net reachability along context-free traces. In IARCS Annual Conference on Foundations of Software Technology and Theoretical Computer Science (FSTTCS), pages 152–163. Dagstuhl, Germany: Leibniz-Zentrum für Informatik, 2011. doi:10.4230/LIPIcs.FSTTCS.2011.152.
- [6] Leonard Berman. The complexity of logical theories. Theoretical Computer Science, 11(1):71–77, 1980. doi:10.1016/0304-3975(80)90037-7.
- [7] Tat-hung Chan. On two-way weak counter machines. Mathematical systems theory, 20(1):31–41, 1987. doi:10.1007/BF01692057.
- [8] Wojciech Czerwiński, Sławomir Lasota, Ranko Lazić, Jérôme Leroux, and Filip Mazowiecki. The reachability problem for petri nets is not elementary. Journal of the ACM (JACM), 68(1):1–28, 2020. doi:10.1145/3422822.
- [9] Wojciech Czerwiński and Łukasz Orlikowski. Reachability in vector addition systems is ackermann-complete. In 2021 IEEE 62nd Annual Symposium on Foundations of Computer Science (FOCS), pages 1229–1240. IEEE, 2022. doi:10.1109/FOCS52979.2021.00120.
- [10] Zhe Dang, Oscar H Ibarra, and Zhi-Wei Sun. On two-way nondeterministic finite automata with one reversal-bounded counter. Theoretical computer science, 330(1):59–79, 2005. doi:10.1016/j.tcs.2004.09.010.
- [11] Marzio De Biasi and Abuzer Yakaryılmaz. Unary languages recognized by two-way one-counter automata. In International Conference on Implementation and Application of Automata, pages 148–161. Springer, 2014. doi:10.1007/978-3-319-08846-4_11.
- [12] Pavol Duris and Zvi Galil. Fooling a two way automation or one pushdown store is better than one counter for two way machines. Theoretical Computer Science, 21(1):39–53, 1982. doi:10.1016/0304-3975(82)90087-1.
- [13] Zvi Galil. Some open problems in the theory of computation as questions about two-way deterministic pushdown automaton languages. Mathematical systems theory, 10(1):211–228, 1976. doi:10.1007/BF01683273.
- [14] James N Gray, Michael A Harrison, and Oscar H Ibarra. Two-way pushdown automata. Information and Control, 11(1-2):30–70, 1967. doi:10.1016/S0019-9958(67)90369-5.
- [15] Dirk Hauschildt and Matthias Jantzen. Petri net algorithms in the theory of matrix grammars. Acta Informatica, 31:719–728, 1994. doi:10.1007/BF01178731.
- [16] Piotr Hofman, Richard Mayr, and Patrick Totzke. Decidability of weak simulation on one-counter nets. In 2013 28th Annual ACM/IEEE Symposium on Logic in Computer Science, pages 203–212. IEEE, 2013. doi:10.1109/LICS.2013.26.
- [17] Piotr Hofman and Patrick Totzke. Trace inclusion for one-counter nets revisited. In Reachability Problems: 8th International Workshop, RP 2014, Oxford, UK, September 22-24, 2014. Proceedings 8, pages 151–162. Springer, 2014. doi:10.1007/978-3-319-11439-2_12.
- [18] Oscar H Ibarra and Zhe Dang. On two-way fa with monotonic counters and quadratic diophantine equations. Theoretical computer science, 312(2-3):359–378, 2004. doi:10.1016/j.tcs.2003.10.027.
- [19] Oscar H Ibarra, Tao Jiang, Nicholas Tran, and Hui Wang. On the equivalence of two-way pushdown automata and counter machines over bounded languages. In STACS 93: 10th Annual Symposium on Theoretical Ascpects of Computer Science Würzburg, Germany, February 25–27, 1993 Proceedings 10, pages 354–364. Springer, 1993. doi:10.1007/3-540-56503-5_36.
- [20] Oscar H Ibarra, Tao Jiang, Nicholas Tran, and Hui Wang. New decidability results concerning two-way counter machines. SIAM Journal on Computing, 24(1):123–137, 1995. doi:10.1137/S0097539792240625.
- [21] Oscar H Ibarra and Jianwen Su. Counter machines: decision problems and applications. In Jewels are Forever: Contributions on Theoretical Computer Science in Honor of Arto Salomaa, pages 84–96. Springer, 1999.
- [22] Hans Kleine Büning, Theodor Lettmann, and Ernst W. Mayr. Projections of vector addition system reachability sets are semilinear. Theoretical Computer Science, 64(3):343–350, 1989. doi:10.1016/0304-3975(89)90055-8.
- [23] Jérôme Leroux. The reachability problem for petri nets is not primitive recursive. In 2021 IEEE 62nd Annual Symposium on Foundations of Computer Science (FOCS), pages 1241–1252. IEEE, 2022. doi:10.1109/FOCS52979.2021.00121.
- [24] Daniel Martin and John Gwynn. Two results concerning the power of two-way deterministic pushdown automata. In Proceedings of the ACM annual conference, pages 342–344, 1973. doi:10.1145/800192.805729.
- [25] Marvin Lee Minsky. Computation. Prentice-Hall Englewood Cliffs, 1967.
- [26] Satoru Miyano. Two-way deterministic multi-weak-counter machines. Theoretical Computer Science, 21(1):27–37, 1982. doi:10.1016/0304-3975(82)90086-X.
- [27] Burkhard Monien. Deterministic two-way one-head pushdown automata are very powerful. Information processing letters, 18(5):239–242, 1984. doi:10.1016/0020-0190(84)90001-2.
- [28] Rohit J. Parikh. On context-free languages. J. ACM, 13(4):570–581, 1966. doi:10.1145/321356.321364.
- [29] Holger Petersen. Two-way one-counter automata accepting bounded languages. ACM SIGACT News, 25(3):102–105, 1994. doi:10.1145/193820.193835.
- [30] Michael O Rabin and Dana Scott. Finite automata and their decision problems. IBM journal of research and development, 3(2):114–125, 1959. doi:10.1147/rd.32.0114.
- [31] John C Shepherdson. The reduction of two-way automata to one-way automata. IBM Journal of Research and Development, 3(2):198–200, 1959. doi:10.1147/rd.32.0198.
- [32] Moshe Y. Vardi. A note on the reduction of two-way automata to one-way automata. Information Processing Letters, 30(5):261–264, 1989. doi:10.1016/0020-0190(89)90205-6.
Appendix A Detailed Construction of in Section 3.2
We follow the intuition given in Section 3.2. Let . Recall that . When constructing we refer to vector entries as counters, and we specify vector operations on individual counters, e.g., if is a specific counter, then refers to the vector that is all apart from in the entry corresponding to . Similarly, we can combine several counter operations in a single vector (e.g., ).
We start by describing the counters of and their intuitive roles.
-
c – tracking the counter of
-
c_freeze_1 – a copy of c, to be frozen at the beginning of a positive loop.
-
c_freeze_2 – a copy of c, to be frozen at the end of a positive loop.
-
and – a pair of counters marking the position of the head from the Left and from the Right of each segment.
We now describe the states and transitions of by listing the information stored in each state, and its intuitive meaning. We divide the states according to the components of .
Initialization Component.
This component consists of a single state . The transitions include a self loops for , each with operation . These transitions allow to “charge” the number of letters in each segment. Another transition (with operation ) leads to the simulation component with current state and head position on (i.e., current segment , see below).
Simulation Component.
A state contains the following information:
-
– the current state of .
-
– the current segment, with and representing and , respectively.
-
– how many times did we move from segment to segment (bounded by ).
-
– the border between segments where we guess a loop occurs, or if this is not guessed yet.
-
– the state where we guess a loop occurs, or if this is not guessed yet.
The transitions within segments are induced by those of : if and , then each transition induces a transition to a state with and the counter operations . Additionally, if then c_freeze_1 behaves like c.
The transition between segments are similarly induced, but instead of updating L[i] and R[i], if then guesses that we move to a state with . Then, R[i] is 0-tested and is increased by 1, if possible (otherwise there is no transition). The left moves ( are dual).
In addition, upon moving between segments, may nondeterministically record the current state and segments in loop_statea and loop_seg_move respectively. Note that from then on, c_freeze_1 no longer changes. If those are already recorded then it may check whether the current state and segments match the recorded ones, in which case we move to the 2-NFA Component (which also freezes c_freeze_2).
If at any point we have , we move to the Finalizing Component.
2-NFA Component.
A state contains the following information:
-
– the current state of .
-
– the current segment, with and representing and , respectively.
-
– how many times did we move from segment to segment (bounded by ).
-
– the border between segments where we guess a loop occurs, or if this is not guessed yet.
-
– the state where we guess a loop occurs, or if this is not guessed yet.
The transitions are similar to those of the Simulation Component, with the exception that , c_freeze_2 are no longer changed (i.e., the counter of is no longer tracked). Note that upon reaching the 2-NFA Component, the function num_moves is reset to to begin a fresh count.
Finalizing Component.
This component consists of two states: and . has a self loop with operation , and a transition to with operation . Lastly, has a self loop allowing to decrease each counter separately, except for c_freeze_1.
We prove the correctness of the construction in Appendix A.
Lemma 17.
iff is reachable from in .
Proof.
It is not hard to see that by construction, the states of the Simulation Component of correctly track the word guessed by in the Initialization Component provided no more than segment moves occur. Thus, in order to prove correctness, it is enough to prove that if accepts some word , then it also accepts with at most moves between segments and for all . Unfortunately, this is not always the case. However, note that if more than moves between segments and occur, then there must be a state that is repeated in two of those moves. If the counter effect of this loop is nonpositive, then the loop can be cut to obtain a “better” run (i.e., one whose counter values are not lower). Otherwise, if the counter effect of the loop is positive, then it can be repeated to make the counter of arbitrarily high. Then, accepts the word iff the 2-NFA obtained from by discarding the counter effects is accepted from the loop state and the position of the head at the end of segment . Moreover, such an accepting run can be assumed not to loop, since otherwise a shorter accepting run can be found. Thus, In the latter case, the 2-NFA component tracks an accepting run, if one exists.
Finally, note that the finalization component can reach with only if , i.e., the loop taken was indeed strictly positive.
Appendix B Proof of Theorem 6
We present the detailed construction of from , starting with the counters of and their intuitive roles.
-
– the counter value before each forward sweep.
-
– the counter value before each backward sweep.
-
f_loop_1 – the counter before the first forward sweep that starts with a loop state .
-
f_loop_2 – the counter before the second forward sweep that starts with a loop state .
We now turn to describe the states and transitions of , split into three components.
Initialization Component.
This component starts in state and has other states containing the following information:
-
– the state beginning each forward sweep, or if the sweep is not taken.
-
– the state ending each backward sweep, or if the sweep is not taken.
-
q_loop – a state that appears twice in the forward sweeps.
The transitions from guess the components in the next state, with the following restrictions:
-
if appears at some entry, then all later entries are also (i.e., once a certain sweep is not taken, not further sweeps are taken).
-
the first forward state is forced to be and if then the last forward state that is not is .
-
for all , i.e., the backward sweep ends with the same state the next forward sweep starts with.
-
The field q_loop is guessed in a consistent way, i.e., it must be the first state that appears twice in the qf[i] states.
Each initialization state has self loops to charge the counters with the following operations: for every , we have a loop with operations , corresponding to the fact that the counter with which the -th backward sweep ends is the same as the counter with which the -th forward sweep starts. In addition, if for some , then is applied together with and together with . Thus enforcing the semantics mentioned above.
The initialization states can nondeterministically move to the Simulation Component.
Simulation Component.
The initialization component keeps track of the forward and backward runs of , as well as acceptance in the DFA equivalent to from the loop state q_loop as follows. Each state in this component carries the same information as the initialization component, but updates it using the transition function.
First, we obtain by defining (observe that we omit transitions on , this is explained below). Thus, essentially reverses the runs of . Furthermore, for each state we obtain from a DFA by discarding the counter effects of , setting the initial state to and converting the resulting 2-NFA to a DFA [31, 30, 32] of single-exponential size.
The main behavior in this component is as follows. From state of , we guess a letter (the guesses of and have special status, see below). The transition then guesses, for each state qf[i] in , a transition on , and updates the corresponding f[i] counter with the transition effect. Dually, for each we guess a transition on in and update b[i] accordingly. Finally, we update using the DFA with the transition on .
To treat the end-markers, we note that reading an end-marker is joint between the forward and backward runs, and we therefore only want to simulate each end-marker in one of them, and we choose the forward runs (which is why we omit these transitions from ). We force the first letter guessed to be , and we only update the forward elements (the qf[i] states and f[i] counters). Upon guessing , we again only update the forward elements, and we transition to the Finalizing Component.
Finalizing Component.
In order to verify that the sweeps are concatenated correctly, we proceed as follows. In state we first check that , i.e., that the forward and backward sweeps meet at the same state. We then have loops with operation for all . Note that such transitions can be taken to reach iff the counters are the same, implying that the forward and backward sweeps meet with the same counter value.
The above checks that the forward and backward sweeps form a valid run of on the guessed word. If the last forward state is , this ensures the word is accepted. It remains to check acceptance in the case of a loop. To this end, if , we check that the state reached in q_loop is an accepting state of . However, we also need to check that the loop taken is indeed positive. To achieve this we move to a state with operation , and then a final transition with only the operation to . Then, reaching ensures that the loop was indeed positive, as the counter increased between the first and second visits to the loop states.
The correctness of the construction follows from the observation that if , then either accepted within forward sweeps, or a positive loop is reached, and from this loop there is a run to an accepting state. In this case, the loop can be arbitrarily pumped so that the run to the accepting state can be taken in the corresponding DFA.