Two-Way Automata over Locally Finite Semirings

. Two-way transducers or weighted automata are in general more powerful than one-way ones. We show that two-way automata over locally ﬁnite semirings may have undeﬁned behaviour. We prove that it is decidable whether this behaviour is deﬁned, and, if it is, we show that two-way automata over locally ﬁnite semirings are equivalent to one-way automata.


Introduction
Weighted two-way automata and transducers have been recently intensively studied for their interest in verification [3]. Their expressiveness is in general larger than the expressiveness of one-way models. We consider in this paper two-way automata over locally finite semirings. Finite or locally finite semirings occur in many models, like distributive lattices or fuzzy logic. One-way automata over these semirings have been studied for many decades. For instance, the first proof of the limitedness problem [4] relies on automata on the idempotent semiring {0, 1, ω, ∞} (where 1 means "something" and ω means "a lot").
It is folklore that every one-way automaton over a locally finite semiring is equivalent to a deterministic finite automaton where the weight of the run only depends on the state where the run stops. For two-way automata over locally finite semirings, the situation is not as simple. For instance, if the weights belong to Z/2Z, then the weight of an input depends on the parity of the number of accepting runs; since in a two-way automaton, there may exist an infinite number of runs accepting some input, this weight may be not defined.
For every two-way automaton over a locally finite semiring, we build an automaton that describes the potentially infinite family of weights of runs on every input. From this object, knowing which infinite sums are defined in the locally finite semiring is sufficient to decide whether the behaviour of the twoway automaton is defined. We also prove that every two-way automaton over a locally finite semiring with a defined behaviour is equivalent to a one-way automaton.
In Section 2, we consider locally finite semirings. In particular, we study how the additive order allows to encode infinite sums. In Section 3, we introduce weighted two-way automata over locally finite semirings and we show that they can be normalized in such a way that the weight of every run only depends on the final state, and the move of the input head is fully characterized by the current state. In Section 4, we use an extension of crossing sequences [6,7] to convert a two-way automaton into a one-way automaton. It leads to a deterministic oneway automaton where each final state describes the (potentially infinite) family of weights of runs of the two-way automaton on the input. It is then decidable whether the weight of every input is defined, and, if it is, then the deterministic automaton can be turned into a (deterministic) one-way automaton equivalent to the two-way automaton.

Locally Finite Semirings
A semiring (K, +, .) is a set K endowed with two associative operations: a commutative addition and a multiplication that is distributive over the addition. Moreover, the semiring contains at least two distinct elements which are respectively neutral for each of these operations: 0 K for the addition and 1 K for the multiplication; it is also required for 0 K to be an annihilator for the multiplication.
A semiring K is locally finite if, for every finite subset F of K containing both 0 K and 1 K , the semiring generated by F is finite.
Moreover, we assume that K is endowed with a partially defined operator for countable sums: I x, where I is a countable set and x = (x i ) i∈I a family of elements of x. If I is finite, then I x is always defined and equal to the sum of elements of x. We also assume that two families equal up to a permutation have the same sum.
In this paper, we deal with weighted automata where a finite number of elements of some locally finite semiring occur. These elements generate a finite semiring. In the sequel, we assume that the semiring K is finite. Example 1. Let (L, ∨, ∧) be an infinite distributive lattice; L is a semiring with ∨ as addition and ∧ as multiplication. 0 L is the minimum element of L and 1 L is the maximum element. The infinite sum of a family of elements of L is the supremum of this family; if it exists. In lattices, the supremum of families with a finite number of distinct elements is always defined.
⊓ ⊔ Example 2. Let K 1 = {o, i, x} be the finite semiring where o is the zero, i is the unit, i + i = o, and r + x = r.x = x for every r in {i, x}. Intuitively, this semiring allows to count values modulo 2, and x stands for values where the parity information is lost. In K 1 , the sum of any family which contains at least one x is defined and equal to x, and a family that does not contain any x is summable if and only if the number of occurrences of i is finite.

⊓ ⊔
Since K is finite and families are unordered, a family is totally characterized by the number of occurrences of each element of K. Hence, we represent such a family s by a vector v in (N∪{∞}) K such that, for every x in K, v x is the number of occurrences of x in s. The evaluation of v is the sum of s if it is defined. Notice that if s and s ′ are two families respectively represented by vectors v and v ′ , then the union of s and s ′ is represented by v + v ′ .
In Section 4.3, a one-way deterministic K-automaton equivalent to a two-way K-automaton is built. The states of this automaton store vectors representing families. In order to build a finite number of states, we need to define a finite set of representatives. Two vectors v and v ′ in (N ∪ {∞}) K are equivalent if for every vector u, v + u and v ′ + u have the same evaluation. This property will be required during a determinization step.
We show that every vector in (N ∪ {∞}) K is equivalent to a vector where finite entries are bounded. We consider the natural external product : for every (k, x) in N × K, k.x is the sum of k elements x. Likewise, if it is defined, the infinite sum of x is denoted ∞.x. Definition 1. Let x be an element of a locally finite semiring K. The additive order of x is the minimal couple (n x , p x ) of integers (with p x > 0) verifying The additive order of x is always defined in a locally finite semiring. Actually, the set {k.x | k ∈ N} is finite and the sequence s = (k.x) k∈N is ultimately periodic: if s i = s j , then for every k s i+k = i.x + k.x = j.x + k.x = s j+k . Thus, p x is the minimal distance between two occurrences of the same value in s and n x is the smallest i such that s i appears infinitely often in s. If (n x , p x ) is the order of x, then for every (m, q) with m larger than or equal to k and q multiple of p x , it also holds ∀k m, (k + q).x = k.x. The additive order of a finite semiring is then the minimal pair which is admissible for every element of the semiring. Definition 2. Let K be a finite semiring. The additive order of K is the couple where lcm X is the least common multiple of elements of X. For every k in N ∪ {∞}, the value of k modulo (n, p) is defined as Hence, k mod(n, p) is in 0; n + p − 1 ∪ {∞} and if the additive order of a finite semiring K is (n, p), then for every x in K, k.x = (k mod(n, p)).x.
If (n, p) is the additive order of K, then every vector v in (N∪{∞}) K is equivalent to the vector where each entry is considered modulo (n, p). The number of distinct vectors modulo (n, p) is equal to (n + p + 1) |K| (some entries may be equal to ∞).
In the sequel, N n,p is the semiring 0; n + p − 1 ∪ {∞} where the operations between finite integers are modulo (n, p).

Definition and Behaviour
We follow in this paper the definition of two-way weighted automata given in [5]. Contrary to the classical definition of two-way finite automata [7,6], the move of the reading head does not depend on the transitions but on the states. This model was first used in [1] and was there proved to be equivalent to the classical one.
Definition 3. Let K be a locally finite semiring, A an alphabet and a special symbol, called endmarker, which does not belong to For every state p, we note The head of a two-way automaton can not go beyond endmarkers, hence for every pair (p, q) of states, if δ(p)δ(q) = 1, then E(p, , q) = 0 K . A run of length k in the automaton is a triple (p, (e i ) i∈ 1;k , q), where p and q are in F and (e i ) i∈ 1;k is a sequence of transitions such that, for every i in 1; k − 1 , τ (e i ) = σ(e i+1 ); if k > 0, then p = σ(e 1 ) and q = τ (e k ); otherwise p = q. This run admits a label w = w 1 . . . w n in A * if there exists a mapping π : 1; k → 0; n + 1 such that π(1) = 1, π(k) = n, and -for every i in 2; k , π(i) = π(i − 1) + δ(σ(e i )), -for every i in 1; k , λ(e i ) = w π(i) if π(i) is in 1; n , and λ(e i ) = otherwise. The function π is the position mapping, giving the position of the reading head during the computation. The run (p, (e i ) i∈ 1;k , q) meets k + 1 states. The sequence of these states is defined by p 0 = p and p j = τ (e j ) for every j in 1; k . The position of the j-th state of the run is defined by pos(0) = 0, and The positions of states and transitions are related; for every j ∈ 1; k , pos(j) = π(j) if δ(p j ) = 1, and pos(j) = π(j) − 1 otherwise. The weight of a run (p, (e i ) i∈ 1;k , q) is The weight of a word w in a two-way weighted automaton is defined if the family of the weights of runs with label w is summable.  Remark 1. If B = ∅, then the two-way K-automaton is actually one-way, and every one-way K-automaton can be described as a two-way K-automaton where B = ∅.
Example 4. Let A 1 be the two-way K 1 -automaton of Figure 1. The final weight of the states is represented by an arrow outgoing from states. States p, q, s and t are forward, r is backward. States p, q and r are not final, which means that their final weight is equal to 0 K1 = o. The weight of every transition is 1 K1 = i: it is a characteristic automaton. Figure 1(b) shows a run of A 1 . State p appears at position 0 and 1. State r appears twice at the same position; this is an unmoving circuit: the part of the run between the two occurrences of r can be repeated in order to get longer runs over the same word.
⊓ ⊔ Remark 2. In the sequel, we state results for two-way automata over finite semirings. If A is a two-way automaton over a locally finite semiring K, then it can be considered as an automaton over the finite semiring K ′ where K ′ is the semiring generated by the weights occurring in A.

Characteristic and δ-normalized Two-way Automaton
Like for one-way automata, we show here that two-way automata with weights in a finite semiring are equivalent to characteristic two-way automata where the weight of the run only depends on the final state. In our model, in such an automaton, the weights of all transitions as well as initial weights are all equal to 1 K .
Proposition 1. Let A be a two-way automaton over a locally finite semiring. There exists a characteristic two-way automaton B equivalent to A.
Actually, through a run, the product of weights spans over a finite set and this can be stored in states. Hence, if K is the finite semiring generated by the weights of A, then states of B belong to Q × K. Figure 1(b) shows that transitions during a run can be classified into four types: forward transitions, backward transitions, and forward and backward halfturns. We consider now δ-normalized two-way automata, where the type of a transition depends on its source. To convert A into B, every state of A is split into two copies, the outgoing transitions that go to a forward state are assigned to the first copy, those that go to a backward state are assigned to the second one. This construction applied to a characteristic automaton gives a characteristic automaton. From now on, we assume that two-way automata are characteristic and δnormalized.

Counting Paths
To convert two-way K-automata into one-way automata, we use a variant of the method of crossing sequences initiated in [6]. Nevertheless, the method must be improved in order to keep records of the number of paths. More precisely, let (n, p) be the order of K. The algorithm builds a (oneway) deterministic automaton B, where each state is a crossing sequence (that may have states repeating at most once). B is then used to build a one-way automaton. The idea is to build from B an automaton that "counts" (modulo (n, p)) the number of runs with a given weight ending in the last state of a crossing sequence.

Crossing Sequences
Definition 6. Let ρ be a run with label w in a two-way automata, and let (p j ) j∈ 0;k be the sequence of states met by ρ. For every i in 0; |w| , the crossing sequence of ρ at position i is the subsequence of states p j such that pos(j) = i. Example 6. On Figure 1(b), crossing sequences are (p), (p) and (q, r, s, r, s).

Remark 3.
Crossing sequences are sequences of states with odd length. More precisely, the first state is in F , and there is an alternation of states in F and B. Using a regular expression, we can say that crossing sequences are in (F B) * F .
If a crossing sequence of ρ contains (at least) two occurrences of the same state, it means that during the run, the automaton comes back to the same state with the input head at the same position; we call this an unmoving circuit. Such a circuit can be removed from ρ in order to obtain a valid run for the same input. On the other hand, it can be iterated to produce an infinite number of valid runs. In a characteristic automaton, all these runs have the same weight.
In the classical crossing sequence construction [6], reduced crossing sequences, that are crossing sequences without repetitions, are considered; we have to consider also crossing sequences with 1 repetition in which no state appears more than twice, in order to detect unmoving circuits.

Proposition 3 ([2]
). Let ρ be a run of a δ-normalized two-way K-automaton over a word w, and let (c i ) i∈ 0,|w| be the list of the crossing sequences of ρ. Then ρ is characterized by w and (c i ) i∈ 0,|w| .
Notice that this proposition is independent from the weights on transitions and that it is true whether or not the two-way automaton is characteristic. We describe briefly how, starting with two consecutive crossing sequences c i−1 and c i , it is possible to build the unique list of transitions in position i which is consistent with these crossing sequences, and the i-th letter of w denoted a.
The algorithm scans sequences c i−1 and c i . If the current state p of c i−1 is in F + , there is a transition from p to the current state q of c i ; if it is in F − , there is a transition from p to the next state p ′ of c i−1 . The algorithm deals then with the next states. If the current states are in B, the analysis is based on the nature of the current state in c i . This process produces the list of transitions in position i in the run. It is formally described in [2].
If c i−1 and c i are not successive crossing sequences of a run, the algorithm may fail or build a list of triples which are not transitions of the automaton. c 1 = (p i ) i∈ 1;2h+1 is an final crossing sequence if p 2h+1 is final, and, for every i in 1; h , (p 2i−1 , , p 2i ) is a transition of A.
Let S be the infinite one-way automaton of crossing sequences of the δnormalized two-way K-automaton A. Its states are the elements of (F B) * F . A state c is initial if c is an initial crossing sequence, it is final if c is a final crossing sequence, and there is a transition from c to c ′ with label a if c ′ is a successor of c by a. This automaton accepts the same words as A, and there is a bijection between runs of S and runs of A. S can be turned into a K-automaton: for every final crossing sequence c = (p i ) i∈ 1;2h+1 , the final weight of c can be set as the final weight of p 2h+1 in A. This gives an infinite one-way K-automaton equivalent to A. Notice that the K-automaton of crossing sequences of a twoway K-automaton A is not always equivalent to A, in particular if K is not commutative. Here, the fact that A is characteristic is crucial.
To convert two-way automata (without weights) to NFA, it is sufficient to restrict S to states without any repetition; this is a classic construction of oneway NFA from two-way NFA.
We want to check whether there is an infinite number of runs that end in a given state. As we have seen, as soon as a run has an unmoving circuit, this circuit can be iterated to get an infinite number of runs. Therefore, it is unnecessary to keep track of crossing sequences with more than two occurrences of the same state. If a crossing sequence c of a run ρ contains three times the same state, then the unmoving circuit between the first and the second one (or between the second one and the third one) can be removed; the resulting path has still an unmoving circuit. Lemma 1. Let A be a δ-normalized characteristic two-way K-automaton, and let ρ be a run on A. If ρ admits a crossing sequence at position i in wich a state p appears more than two times, then there exists a run ρ ′ in which p appears only twice in the crossing sequence at position i, and ρ ′ and ρ end in the same state.
Each crossing sequence with 1 repetition is the witness of an infinite number of runs with the same weight. Lemma 1 tells that every run of A is either a run without any unmoving circuit, or a run which admits such a witness.

Automaton of Crossing Sequences with One Repetition
Let A = (F, B, E, I, T ) be a two-way K-automaton. Let C 1 (resp. C 2 ) be the sequences of (F B) * F where each state appears at most once (resp. twice). Let (n, p) be the order of the finite semiring generated by the coefficients of A. Let B be the one-way automaton over N n,p with states R = C 1 ∪ C 2 defined by: if c is an initial crossing sequence, then c is an initial state of B, with weight a successor of c by a, then (c, a, c ′ ) is a transition of B, with weight ∞ if c is in C 1 and c ′ in C 2 , and weight 1 otherwise; if c is a final crossing sequence, then c is final in B, with weight 1.
The only interesting part of B is its trim part formed with states that can actually occur in some runs. Thus, our construction only requires to build the accessible part of B.
Automaton B is a variant of the crossing sequence automaton. On the one hand, there is a bijection between runs with weight 1 in B and runs without unmoving circuits in A. On the other hand, every run in B with weight ∞ is the witness of an infinite number of runs in A ending in the same state. Notice that some runs of A may admit several witnesses, but all these witnesses show that there are infinitely many runs for the same final state. A run of B ends in a final crossing sequence; the weight of the corresponding run in A is given by the last state of this crossing sequence.
Example 7. Four different crossing sequences may appear in the runs of A 1 ; this leads to the automaton B 1 on Figure 2. The weight of every transition in this automaton is equal to 1, except the transition that goes to the crossing sequence with repetitions where the weight is equal to ∞. For instance, this automaton shows that there is an infinite number of runs over a in A 1 that start in state p and stop in state s. ⊓ ⊔

Gathering Runs with the Same Label
On some locally finite semirings, the automaton B is sufficient to decide whether the two-way automaton is valid and to build an equivalent one-way automaton. In these cases (Case 1 and 2), the last step of the algorithm can be avoided; it leads to a simpler construction. Case 1: Every infinite sum is defined. A non deterministic characteristic oneway K-automaton C equivalent to the two-way K-automaton can be built from the automaton B of crossing sequences with 1 repetition. The weight of a run is ∞ as soon as the weight ∞ is met (otherwise it is 1); this information can be stored in the states of C that belong to R × {1, ∞}, where R is the set of states of B. The accessible part of C is inductively defined as: if p is initial in B with weight k, then (p, k) is initial in C; if (p, a, q) is a transition in B with weight k and (p, r) is a state of C, then ((p, k), a, (q, k.r)) is a transition of C; -if p is final in B, then p is a final crossing sequence whose last state is final with weight x in K; every state (p, k) of C is final with weight k.x.
Case 2: No infinite sum is defined. The two-way K-automaton is valid if and only if no transition with weight ∞ appears in any run of B. If there is no such transition, then the previous construction applies.
Case 3: Some infinite sums are defined, some are not. To decide whether the family of weights of all runs labelled by a given word is defined, all these runs must be gathered. We use a determinization-like algorithm to build an automaton that computes, for each word w, the number (modulo (n, p)) of runs that end in each final crossing sequence. The determinization D of B gathers the vectors corresponding to all runs on every input. Each state of D is a vector in N R n,p , where R is the set of states of B.
the initial vector of D is I, where I c is the initial weight of c in B; if X is a state of D, then the succesor of X by letter a is the state Y defined by: -A state X is final if there exists a final crossing sequence c such that X c is different from 0.
We consider the projection π from the final crossing sequences of R onto F which maps c onto the last state of c; this projection allows to map every final state X of D onto a vector v in N F n,p : For every word w, if the run on w in D ends in state X, then for every c, X c is the number (modulo (n, p)) of runs on w in A whose last crossing sequence is c.
Hence, v f is the number (modulo (n, p)) of runs on w in A that end in state f . Therefore, the weight of w in A is equal to the evaluation of v; if this evaluation is not defined, then the weight of w in A is not defined. If, for every final state X of D, the evaluation of the corresponding vector is defined, this weight can be assigned to X as a final weight. This turns D to a characteristic and deterministic one-way K-automaton equivalent to A. Theorem 1. Over locally finite semirings, the validity of two-way automata is decidable, and every valid two-way automaton is equivalent to a one-way automaton.
The construction follows the algorithm outlined in this paper. Given a two-way automaton A over a locally finite semiring semiring, we can first consider it as an automaton over K, the finite semiring generated by the weights of the p : 1 automaton. A δ-normalized characteristic two-way K-automaton A ′ equivalent to A is then constructed. The automaton of crossing sequences with 1 repetition of A ′ , B, allows to compute the number of runs that stop in each state. The end of the algorithm depends on the cases described in Section 4.3. In Cases 1 and 2 the validity is directly decidable on B and a one-way K-automaton equivalent to A can also be derived from B. In Case 3, a determinization step is required. As explained above, this deterministic automaton allows both to decide the validity and to build a one-way K-automaton equivalent to A, if there exists one.

Remark 4.
The fact that the weight of a word w is defined in A only depends on the state ending the run over w in D. Hence the language of words with an undefined weight is regular.
Example 8. We denote X = (q, r, q, r, s) and Y = (q, r, s); hence the states of automaton B 1 are p, t, X and Y . The states of the determinization of B 1 are vectors in N {p,t,X,Y } n,p ; the determinization is drawn on Figure 3. Only non zero entries are written in each state. The initial state is not final since it does not contain any final crossing sequence.
We replace now each vector in N {p,t,X,Y } n,p by the corresponding vector in N K1 n,p (the entry corresponding to o is not shown since it does not influence the evaluation). Consider for instance the vector [p : 1, X : ∞, Y : 1, t : 1]. p is not a final crossing sequence and does not contribute; the last state of X and Y is s with final weight x, hence, x appears with multiplicity ∞ + 1 = ∞ and the final weight of t is i; finally, the family of weights for this state is [x : ∞, i : 1] and the final weight is ∞.x + 1.i = x. We obtain the automaton of Figure 4. Notice that states with vector 2.i are not final since i + i = o = 0 K . The construction fails because one state corresponds to the vector ∞.i and the infinite sum of i is not defined, thus no final weight can be defined for this state. For every word leading to this state (for instance abb), there is in A 1 an infinite number of runs and the weight of each of these runs is equal to i. Therefore, the two-way automaton A 1 is not valid.  Fig. 4. The automaton A1 is not valid.

Conclusion
This paper describes an algorithm to decide whether a two-way K-automaton is valid, and, if it is, to build an equivalent one-way K-automaton. The construction involves several steps. The first two -getting a characteristic K-automaton and then a δ-normalized K-automaton -have low state complexity: respectively |K||Q| and 2|Q|. In contrast, the last two have huge complexity. The state complexity of the automaton of crossing sequences with 1 repetition is in 2 O(|Q| log |Q|) and the determinization is in (n + p) |Q| , where Q is the set of states of the automaton on which each construction is applied and (n, p) is the order of the finite semiring of weights. Finally the state complexity of the construction is a double exponential. This work encompasses all locally finite semirings.
It is an open question to know whether there exist in general a more efficient construction. For particular classes of semirings, this construction can certainly be improved.