Origin-equivalence of two-way word transducers is in PSPACE

We consider equivalence and containment problems for word transductions. These problems are known to be undecidable when the transductions are relations between words realized by non-deterministic transducers, and become decidable when restricting to functions from words to words. Here we prove that decidability can be equally recovered by adopting a slightly different, but natural semantics, called origin semantics and introduced by Bojanczyk in 2014. Specifically, we prove that the equivalence and containment problems for two-way word transducers in the origin semantics are PSPACE-complete. We also consider a variant of the containment problem where two-way transducers are compared under the origin semantics, but in a more relaxed way, by allowing distortions of the origins. The possible distortions are described by means of a resynchronization relation. We propose a logical formalism for describing a broad class of resynchronizations, while preserving the decidability of the variant of the containment problem.


Introduction
Finite-state transducers over words were studied in computer science very early, at the same time as finite-state automata, see e.g.[Sch61,AHU69,Eil74,Ber79]. A transducer defines a binary relation between words by associating an output with each transition.It is called functional if this relation is a partial function.Whereas the class of functions defined by oneway transducers, i.e. transducers that process their input from left to right, has been extensively considered in the past, the study of two-way transducers is quite recent.Connections to logic, notably through the notion of graph transformations definable in monadic second-order logic (MSO) [Cou97,Eng97], have shown that the functions realized by two-way word transducers can be equally defined in terms of MSO transductions [EH01].This result is reminiscent of Büchi-Elgot characterizations that hold for many classes of objects (words, trees, traces, etc).For this reason, transductions described by functional two-way word transducers are called regular functions.For a recent, nice survey on logical and algebraic properties of regular word transductions the reader is referred to [FR16].
Non-determinism is a very natural and desirable feature for most types of automata.However, for word transducers, non-determinism means less robustness.As an example, nondeterministic transducers are not equivalent anymore to NMSOT (the non-deterministic version of MSO transductions), however the latter is equivalent to non-deterministic streaming transducers [AD11].A major problem is the undecidability of the equivalence of non-deterministic, one-way word transducers [Gri68] (also called NGSM, and capturing the class of rational relations).In contrast, equivalence of functional, two-way word transducers is decidable [CK87], even in PSpace.This complexity is mainly based on the fact that it can be checked in PSpace if two non-deterministic, two-way automata are equivalent (see e.g.[Var89]).
The equivalence test is one of the most widely used operation on automata, so that it becomes a natural question to know what is needed to recover decidability of equivalence for rational relations, and even for regular relations, which are transductions defined by nondeterministic, two-way transducers.The main result of our paper is that equivalence of nondeterministic, two-way transducers is decidable if one adopts a semantics based on origin information.According to this origin semantics [Boj14], each letter of the output is tagged with the input position that generated it.Thus, a relation in the origin semantics becomes a relation over Σ * × (Γ × N) * .Surprisingly, the complexity of the equivalence test turns out to be as low as it could be, namely in PSpace (thus PSpace-complete for obvious reasons).
As a second result, we introduce a class of MSO-definable resynchronizations for regular relations.A resynchronization [FJLW16] is a binary relation over Σ * × (Γ × N) * that preserves the input and the output (i.e., the fist two components), but can change the origins (i.e., the third component).Resynchronizations allow to compare transducers under the origin semantics in a more relaxed way, by allowing distortions of origins.Formally, given two non-deterministic, two-way transducers T 1 , T 2 , and a resynchronization R, we want to compare T 1 , T 2 under the origin semantics modulo R. Containment of T 1 in T 2 modulo R means that for each tagged input/output pair σ ′ generated by T 1 , there should be some tagged input/output pair σ generated by T 2 such that (σ, σ ′ ) ∈ R. In other words, the resynchronization R describes possible distortions of origin, and we ask whether T 1 is contained in R(T 2 ).The resynchronizations defined here correspond to MSO formulas that describe the change of origin by mainly considering the input (and to some small extent, the output).The containment problem under such resynchronizations turns to be undecidable, unless we enforce some restrictions.It is decidable for those resynchronizations R that use formulas satisfying a certain (decidable) 'boundedness' property.In addition, if R is fixed, then the containment problem modulo R is solvable in PSpace, thus with the same complexity as the origin-equivalence problem.This is shown by providing a twoway transducer T ′ 2 that is equivalent to R(T 2 ) (we say that R(T 2 ) is realizable by a transducer), and then we check containment of T 1 in T ′ 2 using our first algorithm.We conjecture that our class of resynchronizations captures the rational resynchronizations of [FJLW16], but we leave this for future work.
Related work and discussion.The origin semantics for transducers has been introduced in [Boj14], and was shown to enjoy several nice properties, in particular a Myhill-Nerode characterization that can be used to decide the membership problem for subclasses of transductions, like first-order definable ones.The current state of the art counts quite a number of results related to the origin semantics of transducers.In [BDGP17] a characterization of the class of origin graphs generated by (functional) streaming transducers is given (the latter models were studied in [AC10,AD11]).Decision problems for tree transducers under the origin semantics have been considered in [FMRT18], where it is shown that origin-equivalence of top-down tree transducers is decidable.Note that top-down transducers correspond on words to one-way transducers, so the result of [FMRT18] is incomparable with ours.
As mentioned before, the idea of resynchronizing origins of word transducers has been introduced by Filiot et al. in [FJLW16] for the case of one-way transducers.Rational resynchronizers as defined in [FJLW16] are one-way transducers R that read sequences of the form where u 1 • • • u n represents the input and v 1 • • • v n the output, with the origin of v i being the last letter of u i .It is required that any image of u 1 v 1 u 2 v 2 • • • u n v n through R leaves the input and the output part unchanged, thus only origins change.The definition of resynchronizer in the one-way case is natural.However, it cannot be extended to two-way transducers, since there is no word encoding of tagged input-output pairs realized by arbitrary two-way transducers.Our approach is logic-based: we define MSO resynchronizations that refer to origin graphs.More precisely, our MSO resynchronizations are formulas that talk about the input and, to a limited extent, about the tagged output.
Overview.After introducing the basic definitions and notations in Section 2, we present the main result about the equivalence problem in Section 3. Resynchronizations are considered in Section 4.

Preliminaries
Given a word w = a 1 . . .a n , we denote by dom(w) = {1, . . ., n} its domain, and by w(i) its i-th letter, for any i ∈ dom(w).
Automata.To define two-way automata, and later two-way transducers, it is convenient to adopt the convention that, for any given input w ∈ Σ * , w(0) = ⊢ and w(|w| + 1) = ⊣, where ⊢, ⊣ / ∈ Σ are special markers used as delimiters of the input.In this way an automaton can detect when an endpoint of the input has been reached and avoid moving the head outside.
A two-way automaton (2NFA for short) is a tuple A = (Q, Σ, ∆, I, F ), where Σ is the input alphabet, right} is the transition relation.The partitioning of the set of states is useful for specifying which letter is read from each state: left-reading states read the letter to the left, whereas right-reading states read the letter to the right.A transition (q, a, q ′ , d) ∈ ∆ is leftward (resp.rightward ) if d = left (resp.d = right).Of course, we assume that no leftward transition is possible when reading the left marker ⊢, and no rightward transition is possible when reading the right marker ⊣.We further restrict ∆ by asking that (q, a, q ′ , left) ∈ ∆ implies q ′ ∈ Q ≺ , and (q, a, q ′ , right) ∈ ∆ implies q ′ ∈ Q ≻ .
To define runs of 2NFA we need to first introduce the notion of configuration.Given a 2NFA A and a word w ∈ Σ * , a configuration of A on w is a pair (q, i), with q ∈ Q and i ∈ {1, . . ., |w| + 1}.Such a configuration represents the fact that the automaton is in state q and its head is between the (i − 1)-th and the i-th letter of w (recall that we are assuming w(0) = ⊢ and w(|w| + 1) = ⊣).The transitions that depart from a configuration (q, i) and read a are denoted (q, i) − a − → (q ′ , i ′ ), and must satisfy one of the following conditions: • q ∈ Q ≺ , a = w(i − 1), (q, a, q ′ , right) ∈ ∆, and i ′ = i, • q ∈ Q ≺ , a = w(i − 1), (q, a, q ′ , left) ∈ ∆, and i ′ = i − 1. q 0 q 1 q 2 q 3 q 4 q 5 q 6 q 7 q 8 q 9 a1 a2 a3 a2 a1 ⊢ a1 a2 a3 i m+1 ) of configurations connected by transitions.The figure to the right depicts an input w = a 1 a 2 a 3 (in blue) and a possible run on it (in red), where q 0 , q 1 , q 2 , q 6 , q 7 , q 8 ∈ Q ≻ and q 3 , q 4 , q 5 ∈ Q ≺ , and 1, 2, 3, 4 are the positions associated with the various configurations.A run is successful if it starts with an initial configuration and ends with a final configuration.The language of A is the set [[A]] ⊆ Σ * of all words on which A has a successful run.
When A has only right-reading states (i.e.Q ≺ = ∅) and rightward transitions, we say that A is a one-way automaton (NFA for short).
Transducers.Two-way transducers are defined similarly to two-way automata, by introducing an output alphabet Γ and associating an output from Γ * with each transition rule.So a twoway transducer (2NFT for short) T = (Q, Σ, Γ, ∆, I, F ) is basically a 2NFA as above, but with a transition relation ∆ , and describes a move of the transducer from configuration (q, i) to configuration (q ′ , i ′ ) that reads the input letter a and outputs the word v.The same restrictions and conventions for two-way automata apply to the transitions of two-way transducers, and configurations and runs are defined in a similar way.
The output associated with a successful run ρ = (q 1 , i 1 ) of all the pairs (u, v) such that v is the output of some successful run ρ of T on u.Throughout the paper, a transducer is non-deterministic and two-way.
Origin semantics.In the origin semantics for transducers [Boj14], the output is tagged with information about the position of the input where it was produced.If reading the i-th letter of the input we output v, then all letters of v are tagged with i, and we say they have origin i.We use the notation (v, i) to denote that all positions in v have origin i.The outputs associated with a successful run ρ = (q 1 , i 1 ) Under the origin semantics, the relation defined by T , denoted [[T ]] o , is the set of pairs σ = (u, ν) -called synchronized pairs-such that u ∈ Σ * and ν ∈ (Γ × N) * is the output of some successful run on u.Take as example the 2NFA run depicted in the previous figure, and assume that any transition on a letter a ∈ Σ outputs a, while a transition on a marker ⊢ or ⊣ outputs the empty word ε.The output associated with that run in the origin semantics is (a Given two transducers T 1 , T 2 , we say they are Regular outputs.The transducers we defined just above consume input letters while outputting strings of bounded length.In order to perform some crucial constructions laternotably, to shortcut factors of runs with empty output-we need to slightly generalize the notion of output associated with a transition, so as to allow producing arbitrarily long words on reading a single letter.Formally, the transition relation of a transducer with regular outputs is allowed to be any subset ∆ of Q×(Σ⊎{⊢, ⊣})×2 Γ * 2 Γ * 2 Γ * ×Q×{left, right} such that, for all q, q ′ ∈ Q, a ∈ Σ, d ∈ {left, right}, there is at most one language L ⊆ Γ * such that (q, a, L, q ′ , d) ∈ ∆; moreover, this language L must be non-empty and regular.A transition (q, i) − a|L − − → (q ′ , i ′ ) means that the transducer can move from configuration (q, i) to configuration (q ′ , i ′ ) while reading a and outputting any word v ∈ L. Accordingly, the outputs that are associated with a successful run ρ = (q 1 , i 1 ) ) in the origin semantics are the words of the form ν = (v 1 , j 1 )(v 2 , j 2 ) • • • (v m , j m ), where v k ∈ L k and j k = i k or j k = i k−1 depending on whether q k ∈ Q ≻ or q k ∈ Q ≺ .We say that two runs ρ 1 , ρ 2 are originequivalent if they have the same sets of associated outputs.Clearly, the extension with regular outputs is only syntactical, and it preserves the expressiveness of the class of transducers we consider.In the remaining of the paper, we will tacitly refer to above notion of transducer.
PSpace PSpace PSpace-constructibility.As usual, we call the size of an automaton or a transducer the number of its states, input symbols, transitions, plus, if present, the sizes of the NFA descriptions of the regular output languages associated with each transition rule.
In our complexity analysis, however, we will often need to work with online presentations of automata and transducers.For example, we may say that an automaton or a transducer can be computed using a polynomial amount of working space (at thus its size would be at most exponential) w.r.t. a given input.The terminology introduced below will be extensively used throughout the paper to describe the computational complexity of an automaton, a transducer, or a part of it, in terms of a specific parameter.
Given a parameter n ∈ N, we say that an automaton or a transducer has PSpace-constructible transitions w.r.t.n if its transition relation can be enumerated by an algorithm that uses working space polynomial in n, and in addition, when the device is a transducer, every transition has at most polynomial size in n.In particular, if a transducer has PSpace-constructible transitions, then the size of the NFA representing every output language is polynomial.Similarly, we say that an automaton is PSpace-constructible w.r.t.n if all its components, -the alphabets, the state set, the transition relation, etc.-are enumerable by algorithms that use space polynomial in n.

Equivalence of transducers with origins
We focus on the equivalence problem for two-way transducers.In the classical semantics, this problem is known to be undecidable even if transducers are one-way [Gri68] (called NGSM in the latter paper).We consider this problem in the origin semantics.We will show that, in this setting, equivalence becomes decidable, and can even be solved in PSpace -so with no more cost than equivalence of non-deterministic two-way automata: Theorem 1. Containment and equivalence of two-way transducers under the origin semantics is PSpace-complete.
The proof of this result is quite technical.As a preparation, we first show how to check origin-equivalence for transducers in which all transitions produce non-empty outputs, namely, where the transition relation is of the form We call busy any such transducer.

Origin-equivalence of busy transducers
An important feature of our definition of transducers is that, along any possible run, an input position is never read twice in a row.In other words, our transducers do not have "stay" transitions.For a busy transducer, this implies that the origins of outputs of consecutive transitions are always different.As a consequence, runs of two busy transducers can be only origin-equivalent if they visit the same sequences of positions of the input and have the same possible outputs transition-wise.To give the intuition, we note that origin-equivalence of busy classical transducers, with single output words associated with transitions, can be reduced to a version of equivalence of 2NFA, where we ask that runs have the same shape.Already the last condition does not allow to apply the PSpace algorithm for equivalence of 2NFA of [Var89], and the naive algorithm would be of exponential time.Some more complications arise when we assume regular outputs, which will be required when we shall deal with non-busy transducers.We introduce now the key notions of transition shape and witness procedure.
Let T 1 , T 2 be busy transducers over the same input alphabet, with T i = (Q i , Σ, Γ i , ∆ i , I i , F i ) for i = 1, 2. We say that two transitions t 1 ∈ ∆ 1 and t 2 ∈ ∆ 2 , with t i = (q i , a i , q ′ i , L i , d i ), have the same shape if a 1 = a 2 , q 1 ∈ Q 1,≺ ⇔ q 2 ∈ Q 2,≺ , and We assume that there is a non-deterministic procedure W, called witness procedure, that does the following.Given a transition t 1 = (q 1 , a 1 , q ′ 1 , L 1 , d 1 ) of T 1 , W returns a set X ⊆ ∆ 2 of transitions of T 2 satisfying the following property: for some word v ∈ L 1 , we have and t 2 has same shape as t 1 .
Note that W is non-deterministic: it can return several sets based on the choice of v.If t 1 ∈ ∆ 1 and X is a set that could be returned by W on t 1 , we write X ∈ W(t 1 ).However, if T 1 and T 2 were classical transducers, specifying a single output word for each transition, then W could return only one set on t 1 ∈ ∆ 1 , that is, the set of transitions of T 2 with the same shape and the same output as t 1 .
The intuition behind the procedure W is the following.Consider a successful run necessarily means that for all possible outputs produced by the transitions of ρ 1 , there is some run ρ 2 of T 2 on u that has the same shape as ρ 1 and the same outputs, transition-wise.Procedure W will precisely provide, for each transition t 1 of T 1 with output language L ⊆ Γ + , and for each choice of v ∈ L, the set of all transitions of T 2 with the same shape as t and that could produce the same output v.
We introduce a last piece of terminology.Given a run ρ 1 = t 1 . . .t m of T 1 of length m and a sequence ξ = X 1 , . . ., X m of subsets of ∆ 2 (witness sequence), we write ξ ∈ W(ρ 1 ) whenever The following result is crucial: Proposition 2. Let T 1 , T 2 be two busy transducers over the same input alphabet, and W a witness procedure.Then and only if for every successful run ρ 1 of T 1 , and for every witness sequence ξ ∈ W(ρ 1 ), there is a successful run ρ 2 of T 2 which is ξ-compatible.
there must be a successful run ρ 2 = t ′ 1 . . .t ′ m of T 2 on the same input u that enables the same ν as output.In other words, for this produced by ρ 1 .We want to show that ν can also be produced by a successful run of T 2 on the same input u.According to the description of the witness procedure W, for each k there is a set X k ∈ W(t k ) containing precisely the transitions t ′ of T 2 that can output v k and that have the same shape as t k .Let ξ = X 1 , . . ., X m .By the hypothesis of the claim, there is a successful run This means that the transitions in ρ 2 have the same shapes as those in ρ 2 -in particular, they read the same input letters-and can produce the same outputs v 1 , . . ., v m , tagged with the same origins j 1 , . . ., j m .Thus, ρ 2 is over the same input u and enables the same output ν ] o to the emptiness problem of a one-way automaton (NFA) B. In this reduction, the NFA B can be exponentially larger than T 1 , T 2 , but is PSpaceconstructible under suitable assumptions on T 1 , T 2 , and W. Lemma 3. Given two busy transducers T 1 , T 2 with input alphabet Σ and a witness procedure W, one can construct an NFA B that accepts precisely the words u ∈ Σ * for which there exist a successful run ρ 1 of T 1 on u and a witness sequence ξ ∈ W(ρ 1 ) such that no ξ-compatible run ρ 2 of T 2 is successful.
Moreover, if T 1 , T 2 have a total number n of states and PSpace-constructible transitions w.r.t.n, and W uses space polynomial in n, then B is PSpace-constructible w.r.t.n.
Before proving the lemma, let us state an immediate consequence of it and of the previous Proposition 2: Corollary 4. Given two busy transducers T 1 , T 2 with a total number n of states, and given a witness procedure that uses space polynomial in n, the problem of deciding ] o amounts to check emptiness of the NFA B from Lemma 3. The latter problem can be decided in PSpace w.r.t.n, by using the constructibility of B to enumerate all transitions departing from any given state.
We finally turn to the proof of the lemma: The goal is as expected: on input u, the NFA B needs to verify the existence of a successful run ρ 1 = t 1 , . . ., t m of A 1 on u, and of a witness sequence ξ ∈ W(ρ 1 ), such that there is no successful, ξ-compatible run ρ 2 = t ′ 1 , . . ., t ′ m of A 2 (the fact that ρ 2 is over the same input u as ρ 1 follows, as usual, from ξ-compatibility).
The goal is achieved by processing the input u from left to right, while guessing pieces of the run ρ 1 of T 1 that are 'induced' by prefixes of u.At the same time, B uses the procedure W to 'guess' the witness sequence ξ.The sequence ξ is used to track induced pieces of runs of T 2 with the same shape as in ρ 1 , such that once we choose an output word for every transition of T 1 , we are guaranteed to follow all pieces of runs of T 2 that can produce the same output words.
Recall that successful runs start at the leftmost position and end at the rightmost position.We say that a run is induced by the first i letters of the input u if it visits only the positions {1, . . ., i}, possibly ending in position i + 1 (in particular, if i ≤ |u|, then such a run will never read the right marker ⊣).We further restrict our attention to induced runs that end at the rightmost position, i.e. i + 1, and that start at any of the two extremities, i.e. 1 or i + 1.Based on the starting position, we distinguish between left-to-right runs and right-to-right runs.For example, if we refer to the example of run given in Section 2 and consider the first two letters of the input, we would have one induced left-to-right run (q 0 , 1) − a1 − → (q 1 , 2) − a2 − → (q 2 , 3), and one induced right-to-right run (q 3 , 3) The NFA B will guess runs of T 1 induced by longer and longer prefixes of the input (more precisely, for each prefix, there will be exactly one left-to-right run starting at an initial state and any number of right-to-right runs).At the end, B expects to see only one left-to-right induced run of T 1 , that must be successful.At the same time, B will also maintain the set of all runs of T 2 that are induced by the consumed prefix and that have been constructed using transitions provided by W. Concerning these latter runs, B will check that none of the left-to-right runs of T 2 are successful.In fact, as we will see below, all the guessed runs are abstracted by states or pairs of states, so as to maintain at each step only a bounded, polynomial-size information.This will require some care, since, for instance, the same pair of states can represent many right-to-right runs of T 1 , with different shapes.The key observation is that it is sufficient to associate at most one left-to-right run with each state of T 1 , and at most one right-to-right run with each pair of states of T 1 .This is because if, say, ρ 1 has two right-to-right runs on some prefix, that start and end, respectively, in the same states, then we can replace one run by the other, and do the same for ρ 2 .The property that no ξ-compatible run of T 2 is ultimately successful, will be preserved.
The states of the NFA B are tuples consisting of four different objects (note that the size of a state is polynomial in the total number n of states of T 1 , T 2 ): • a right-reading state q of T 1 , • a set P of pairs of left-reading and right-reading states of T 1 , • a set S of right-reading states of T 2 , • a relation R between pairs of states of T 1 and pairs of states of T 2 such that for all (q 1 , q ′ 1 ), (q 2 , q ′ 2 ) ∈ R, q 1 and q 2 are left-reading, and q ′ 1 and q ′ 2 are right-reading.
The intended meaning of the above objects is explained by the following invariant.After reading a prefix u[1, i] of the input, the state (q, P, S, R) reached by B must satisfy the following properties: 1. there is a left-to-right run ρ q of T 1 induced by u[1, i] and ending in q, and a witness sequence ξ q in W(ρ q ), 2. for each pair (q, q ′ ) ∈ P , there is a right-to-right run ρ q,q ′ of T 1 that is induced by u[1, i], starts in q, ends in q ′ , and a witness sequence ξ q,q ′ in W(ρ q,q ′ ), 3. if there is a left-to-right run ρ ′ of T 2 that is induced by u[1, i] and is ξ q -compatible (cf.first item), then S contains the last state of ρ ′ , 4. for each pair (q, q ′ ) ∈ P , if there is a right-to-right run ρ ′ of T 2 induced by u[1, i], ξ q,q ′compatible (cf.second item), starting in r and ending in r ′ , then ((q, q ′ ), (r, r ′ )) ∈ R.
The initial states of B are the tuples of the form (q, P, S, R), with q ∈ I 1 , P = ∅, and S = I 2 .Similarly, the final states are the tuples (q, P, S, R) such that q ∈ F 1 , P = ∅, and S ∩ F 2 = ∅.
Assuming that Properties 1.-4.are satisfied, this will imply that B accepts some input u iff there exist a successful run ρ 1 of T 1 on u and a witness sequence ξ ∈ W(ρ 1 ), but no successful, ξ-compatible run of T 2 on u.
Figure 1: Constraints between q and q′ , and between P and P ′ .
We now give the transitions of B that preserve Properties 1.-4.These are of the form and must satisfy a certain number of constraints between the various components of the source and target states.We first focus on the constraints between the first two components, i.e. q, P and q′ , P ′ , which guarantee Properties 1.-2.For this, we basically apply a variant of the classical crossing-sequence construction [She59] for simulating a 2NFA by an NFA.In the following we omit output languages in transitions, since they are determined by the source/target state, the letter and the direction.
As before, we write for short q ❀ q 1 ❀ q 2 • • • ❀ q 2k ❀ q ′ (to distinguish the notations for the former and the latter property, it suffices to check whether the first state is rightreading or left-reading).
As concerns the constraints between S, R and S ′ , R ′ , we first lift the previous properties and notations to pairs of runs of T 1 and T 2 , thus writing, for instance, On top of this, we restrict the runs of T 2 to be ξ-compatible with the runs of T 1 , for the corresponding witness sequence ξ.Formally, we require the following: • Assume that s ∈ S and that R contains the pairs (q 2i−1 , q 2i ), (s 2i−1 , s 2i ) , for all 1 ≤ i ≤ k.Let t = (q, a, q 1 , left), t ′ = (q 2k , a, q′ , right), and t i = (q 2i−1 , a, q 2i , left), for all 2 ≤ i ≤ k, be the transitions of T 1 that are used to connect q to q 1 , q 2k to q′ , etc.
Using W, choose some witness sets X ∈ W(t), X ′ ∈ W(t ′ ), and • Assume that (q, q ′ ) ∈ P ′ and R contains the pairs (q 2i−1 , q 2i ), (s 2i−1 , s 2i ) , for all 1 ≤ i ≤ k.As before, let t, t ′ , t i be the transitions of T 1 that connect q to q 1 , q 2k to q ′ , q 2i to q 2i+1 , for all 1 ≤ i ≤ k.
Using W, choose some witness sets X ∈ W(t)), X ′ ∈ W(t ′ ), and It is routine to check that the above constraints guarantee all the Properties 1.-4.stated above.
To conclude, we observe that the NFA B can be constructed by an algorithm that uses the subprocedures for enumerating the transitions of T 1 , T 2 and the non-deterministic PSpace procedure W, plus additional polynomial space for storing (temporarily) the sets X, X ′ , X i .

Origin-equivalence of arbitrary transducers
We now consider transducers that are not necessarily busy.To show that origin-equivalence remains decidable in PSpace, we will modify the transducers so as to make them busy, and reduce in this way the origin equivalence problem to the case treated in Section 3.1.
A naive idea would be to modify the transitions that output the empty word ε and make them output a special letter #.This however would not give a correct reduction towards originequivalence with busy transducers.Indeed, a transducer may produce non-empty outputs, say v 1 , v 2 , . . ., with transitions that occur at the same position, say i, and that are interleaved by runs traversing other positions of the input but producing only ε.In that case, we would still need to compare where the words v 1 , v 2 , . . .were produced, and see that they may form a contiguous part of the output with origin i.The above idea is however useful if we first normalize our transducers in such a way that maximal subruns generating empty outputs are unidirectional.Paired with the fact that the same input position is never visited twice on two consecutive transitions, this will give the following characterization: two arbitrary transducers are origin-equivalent if and only if their normalized versions, with empty outputs replaced by #, are also origin-equivalent.
For simplicity, we fix a single transducer T = (Q, Σ, Γ, ∆, I, F ), which could be thought of as any of the two transducers T 1 , T 2 that are tested for origin-equivalence.To normalize T we consider runs that start and end in the same position, and that produce empty output.Such runs are called lazy U-turns and are formally defined below.We will then abstract lazy U-turns by pairs of states, called U-pairs for short.
Definition 5. Given an input word u, a left (resp.right) lazy U-turn at position i of u is any run of T on u of the form (q 1 , i 1 ) For brevity, we shall often refer to a left/right lazy U-turn without specifying the position i and the word u, assuming that these are clear from the context.
The pair (q 1 , q m+1 ) of states at the extremities of a left/right lazy U-turn is called a left/right U-pair (at position i of u).We denote by U ý i (resp.U ý i ) the set of all left (resp.right) U-pairs at position i (again, the input u is omitted from the notation as it usually understood from the context).
Note that we have Accordingly, we define the word u ý over 2 Q≺×Q≻ that has the same length as u and labels every position i with the set U ý i of left U-pairs.This u ý is seen as an annotation of the original input u, and can be computed from T = (Q, Σ, Γ, ∆, I, F ) and u using the following recursive rule: The annotation u ý with right U -pairs satisfies a symmetric recursive rule.
Of course, the annotations u ý , u ý are over alphabets of exponential size.This does not raise particular problems concerning the complexity, since we aim at deciding origin-equivalence in PSpace, and towards this goal we could work with automata and transducers that have PSpace-constructible transitions.In particular, we can use the recursive rules for u ý and u ý to get the following straightforward lemma (proof omitted): Lemma 6.Given a transducer T , one can compute an NFA U that has the same number of states as T and such that [[U]] = {u ⊗ u ý ⊗ u ý : u ∈ Σ * }, where ⊗ denotes the convolution of words of the same length.The NFA U is PSpace-constructible w.r.t. the size of T .
To normalize the transducer T it is convenient to assume that the sets of left and right U-pairs can be read directly from the input (we refer to this as annotated input ).For this, we introduce the transducer T U , that is obtained from T by extending its input alphabet from Σ to Σ × 2 Q≺×Q≻ × 2 Q≻×Q≺ , and by modifying the transitions in the obvious way, that is, from (q, a, L, q ′ , d) to q, (a, U ý , U ý ), L, q ′ , d for any Note that T U does not check that the input is correctly annotated, i.e. of the form u ⊗ u ý ⊗ u ý (this is done by the NFA U).Further note that T U has exponential size w.r.t.T , but its state space remains the same as T , and its transitions can be enumerated in PSpace.
We are now ready to describe the normalization of T U , which produces an origin-equivalent transducer Norm(T U ) with no lazy U-turns.The transducer Norm(T U ) is obtained in two steps.First, using the information provided by the annotation of the input, we shortcut all runs of T U that consist of multiple transitions outputting at the same position and interleaved by lazy U-turns.The resulting transducer is denoted Shortcut(T U ).After this step, we will eliminate the lazy U -turns, thus obtaining Norm(T U ).
Formally, Shortcut(T U ) has for transitions the tuples of the form q, (a, U ý , U ý ), L, q ′ , d , where L is the smallest language that contains every language of the form L 1 • L 2 • • • L k for which there are q 1 , q ′ 1 , . . ., q k , q ′ k and d 1 , . . ., d k , with q = q 1 , q ′ k = q ′ (and hence and  (q ′ i , q i+1 ) ∈ U ý ∪ U ý for all i (see the figure to the right).Note that there is no transition q, (a, U ý , U ý ), L, q ′ , d when there are no languages L 1 , L 2 , . . ., L k as above.The output languages associated with the transitions of Shortcut(T U ) can be constructed using a classical saturation mechanism, which is omitted here, they are regular, and their NFA representations are polynomial-sized w.r.t. the size of the NFA representations of the output languages of T U .This implies that Shortcut(T U ) has PSpace-constructible transitions w.r.t. the number of its states, exactly like T U .
Recall that two runs are origin-equivalent if they produce the same synchronized pairs.The next lemma shows that, on correctly annotated inputs, Shortcut(T U ) is origin-equivalent to T U , even when avoiding U-turns.
Lemma 7. Let w = u ⊗ u ý ⊗ u ý be an arbitrary input annotated with left and right U-pairs.Every successful run of T U on w is origin-equivalent to some successful run of Shortcut(T U ) on w without lazy U-turns.Conversely, every successful run of Shortcut(T U ) on w is origin-equivalent to some successful run of T U on w.

Proof. Consider an arbitrary run of T
The above run can also be seen as a run of Shortcut(T U ), since for every transition of T U outputting v there is a similar transition of Shortcut(T U ), between the same states and outputting the same word v.We prove the claim by induction on the number of lazy U-turns in ρ.If ρ has no lazy U-turn, then the claim follows trivially.Now, for the inductive step, suppose that the factor that starts in (q k , i k ) and ends in (q h , i h ) is a maximal left lazy U-turn, which thus occurs at position i = i k = i h (the case of a right lazy U-turn case is symmetric).By definition of U-turn, we know that q k is left-reading and q h is right-reading, and hence the transitions that immediately precede and follow the U-turn read the same input letter, i.e. w(i) = b k−1 = b h , and output respectively the words v k−1 and v h .By construction, Shortcut(T U ) admits a transition t that moves from configuration (q k−1 , i k−1 ) to configuration (q h , i h ), reading w(i) and producing v k−1 • v h as output.We can replace the U-turn of ρ and the surrounding transitions with the latter transition t, thus obtaining an origin-equivalent run of Shortcut(T U ) with a smaller number of lazy U-turns.This proves the inductive step, and thus the first claim of the lemma.
For the second claim, consider an arbitrary run of Shortcut(T U ) on w: Consider a transition along ρ, say t : . By construction, there must be a sequence of states r 1 , r ′ 1 , . . ., r h , r ′ h and directions d 1 , . . ., d h , for some h ≥ 2, such that The latter transitions, interleaved with some U-turns corresponding to the U-pairs (r ′ 1 , r 2 ), . . ., (r ′ h−1 , r h ) ∈ U ý ∪ U ý , form a valid run ρ ′ t of T U on w, which connects (q k , i k ) to (q k+1 , i k+1 ) and outputs v k .This means that in the run ρ we can safely replace the transition t by the run ρ ′ t .Doing this simultaneously for all transitions in ρ results in an origin-equivalent run of T U .The next step of the normalization consists of restricting the runs of Shortcut(T U ) so as to avoid any lazy U-turn.This is done by simply forbidding the shortest possible lazy U-turns, namely, the transitions that output ε and that remain on the same input position.On the one hand, since every lazy U-turn contains a transition of the previous form, forbidding this type of transitions results in forbidding arbitrary lazy U-turns.On the other hand, thanks to Lemma 7, this will not affect the semantics of Shortcut(T U ). Formally, we construct from Shortcut(T U ) a new transducer Norm(T U ) by replacing every transition rule (q, a, L, q ′ , d) with (q, a, L ′ , q ′ , d), where L ′ is either L or L \ {ε}, depending on whether q ∈ Q ≺ ⇔ q ′ ∈ Q ≺ or not.We observe that Norm(T U ) has the same set of states as the original transducer T , and PSpace-constructible transitions w.r.t. the size of T .The proof of the following result is straightforward and thus omitted.
Lemma 8. T U and Norm(T U ) are origin-equivalent when restricted to correctly annotated inputs, i.e.: We finally come to the last step of the reduction.This amounts to consider two normalized transducers Norm(T 1,U ) and Norm(T 2,U ) that read inputs annotated with the left/right U-pairs of both T 1 and T 2 , and replacing, in their transitions, the empty output by a special letter # / ∈ Γ. Formally, in the transition relation of Norm(T i,U ), for i = 1, 2, we replace every tuple (q, a, L, q ′ , d), where q is left-reading iff q ′ is left-reading, with the tuple (q, a, L ′ , q ′ , d), where L ′ is either (L \ {ε}) ∪ {#} or L, depending on whether ε ∈ L or not.The transducer obtained in this way is busy, and is thus called Busy(T i,U ).Moreover, the states of Busy(T i,U ) are the same as those of T i , and its transitions are PSpace-constructible.The proposition below follows immediately from the previous arguments, and reduces origin-containment between T 1 and T 2 to an origin-containment between Busy(T 1,U ) and Busy(T 2,U ), but relativized to correctly annotated inputs.
Proposition 9. Given two transducers T 1 and T 2 , where R = [[U ′ ]] × (Γ × N) * and U ′ is an NFA that recognizes inputs annotated with left/right U-pairs of both T 1 and T 2 .
It remains to show that, given the transducers T 1 , T 2 , there is a PSpace witness procedure for Busy(T 1,U ), Busy(T 2,U ): Proposition 10.Let T 1 , T 2 be transducers with a total number n of states, and Busy(T i,U ) = (Q i , Σ, Γ, ∆ i , I i , F i ) for i = 1, 2 (the input alphabet Σ is the same as for U ′ ).There is a nondeterministic procedure W that works in polynomial space in n and returns on a given transition ) such that for some v ∈ L 1 : and t 2 has same shape as t 1 .
Proof.Given transition t 1 = (q 1 , a, q ′ 1 , L 1 , d 1 ) of Busy(T 1,U ), we first use the PSpace enumeration procedure of the transitions of Busy(T 2,U ) to generate the set Z of all t 2 = (q 2 , a, q ′ 2 , L 2 , d 2 ) ∈ ∆ 2 with the same shape as t 1 .Note that Z has polynomial size, and its representation is polynomial as well.The procedure W guesses first a subset Z 0 of Z. Then it guesses on-the-fly a word v ∈ L 1 , and verifies that v belongs to the output language of every t 2 ∈ Z 0 , and to no output language of any t 2 ∈ (Z \ Z 0 ).Recall that Busy(T 1,U ) and Busy(T 2,U ) have PSpaceconstructible transitions w.r.t.n, and thus, by definition, the output languages are represented by NFAs of size polynomial in n.Basically W checks a condition of the form v ∈ i D i \ j D ′ j , for polynomially many NFAs D i , D ′ j of size polynomial in n, which can be done in PSpace.If successful, then W returns the set Z 0 .
We can now conclude with the proof of our main result, which we recall here: Theorem 1. Containment and equivalence of two-way transducers under the origin semantics is PSpace-complete.
Proof.The lower bound follows from a straightforward reduction from the classical equivalence problem of NFA.For the upper bound, in view of Proposition 9, we can consider origin containment between the transducers Busy(T 1,U ) and Busy(T 2,U ), obtained from T 1 and T 2 , respectively.We recall that Busy(T i,U ) has at most n i = |T i | states and PSpace-constructible transitions w.r.t.n i , for i = 1, 2. We then apply Proposition 2 and Lemma 3 to reduce the containment problem to an emptiness problem for the intersection of two PSpace-constructible NFA, i.e.B and U ′ .We observe that U ′ is basically the product of two NFA obtained from Lemma 6 by letting T = T i , once for i = 1 and once for i = 2. Finally, we use the same arguments as in the proof of Corollary 4 to conclude that the latter emptiness problem is decidable in PSpace w.r.t.n = n 1 + n 2 .

Containment modulo resynchronization
In this section we aim at generalizing the equivalence and containment problems for transducers with origins.The goal is to compare the origin semantics of any two transducers up to 'distortions', that is, differences in the origin tagging each position of the output.

Recall that [[T ]]
o is the set of synchronized pairs σ = (u, ν), where u ∈ Σ * is a possible input for the transducer T and ν ∈ (Γ × N) * is an output (tagged with origins) produced by a successful run of T on u.Given a pair σ = (u, ν) ∈ [[T ]] o , we denote by in(σ), out(σ), and orig(σ) respectively the input word u, the output word obtained by projecting ν onto the finite alphabet Γ, and the sequence of input positions obtained by projecting ν onto N.This notation is particularly convenient for describing resynchronizations, that is, relations between synchronized pairs.Following prior terminology from [FJLW16], we call resynchronization any relation R between synchronized pairs that preserves input and output words, but can modify the origins, namely, such that (σ, σ ′ ) ∈ R implies in(σ) = in(σ ′ ) and out(σ) = out(σ ′ ).
The containment problem modulo a resynchronization R [FJLW16] is the problem of deciding, given two transducers or in other words if every synchronized pair of T 1 can be seen as a distortion of a synchronized pair of T 2 .In this case we write for short T 1 ⊆ R T 2 .We remark that, despite the name 'containment' and the notation, the relation ⊆ R is not necessarily transitive, in the sense that it may happen that We propose a class of resynchronizations that can be described in monadic second-order logic (MSO ).The spirit is that, as synchronized pairs are (special) graphs, graph transformations à la Courcelle and Engelfriet [CE12] are an adequate tool to define resynchronizers.However, we cannot directly use MSO logic over origin graphs, since this would result in an undecidable containment problem.Our MSO resynchronizers will be able to talk about (regular) properties of the input word and the output word, and say how origins are distorted.We will show that containment modulo MSO resynchronizations is decidable, assuming a (decidable) restriction on the change of origins.• γ is a function that maps any element τ ∈ Γ × B n , with B = {0, 1}, to an MSO formula γ(τ )(I, y, z) over the input signature that has a tuple of free monadic variables I (input parameters) and two free first-order variables y, z (called source and target, respectively), • δ is a function that maps any pair of elements τ, τ ′ ∈ Γ × B n , with B = {0, 1}, to an MSO formula δ(τ, τ ′ )(I, z, z ′ ) over the input signature that has a tuple of free monadic variables I (input parameters) and two free first-order variables z, z ′ (called targets).
The input and output parameters I, O that appear in the formulas of an MSO resynchronizer play the same role as the parameters of an NMSO-transduction [EH01].They allow to express regular properties of the input and output word, respectively, through the first two formulas, α(I) and β(O).The other two formulas are used to describe how the origin function is transformed.They depend on the input and output parameters, in particular, on the "type" of the positions of the output word, as defined next.
Example 13.We provide a few examples of MSO resynchronizers: 1.The most unconstrained resynchronization, called universal resynchronization, groups any two synchronized pairs with the same input and output strings.This is readily defined by an MSO resynchronizer R univ = (α, β, γ, δ) without parameters, where 2. Consider a resynchronization that displaces the origin in a synchronized pair by one position to the left or to the right.An instance of this resynchronization is shown to the right, where solid arrows represent origins in the initial synchronized pair, and dashed arrows represent origins in the modified synchronized pair.This transformation can be defined by a parameterless MSO resynchronizer R ±1 = (α, β, γ, δ), where α = β = true, γ(τ )(y, z) = (z = y − 1) ∨ (z = y + 1), and δ(τ, τ ′ )(z, z ′ ) = true for all τ, τ ′ ∈ Γ.Note that the reflexive and transitive closure of 3. We give a variant of the previous resynchronizer R ±1 , where the direction of movement of the origin is controlled by a property of the output position, e.g. the parity of the number of b's that precede that position in the output.We see an instance of the resynchronization to the right (as usual, solid and dashed arrows represent initial and modified origins).The transformation can be defined by an MSO resynchronizer with a single output parameter O that encodes the parity condition (an interpretation of O is shown in the figure as an annotation of the output over B = {0, 1}).Formally, we let R ′ ±1 = (α, β, γ, δ), where α = true, , and γ and δ are defined on the basis of the output-types τ, τ ′ ∈ Γ × B, as follows: γ(τ )(y, z) enforces either z = y + 1 or z = y − 1 depending on whether τ ∈ Γ× {0} or τ ∈ Γ × {1}, and δ(τ, τ ′ )(z, z ′ ) = true.

Let us now consider a resynchronization that does not
modify the origins, but only constrain them so as to obtain a 'regular' subset of synchronization pairs.Here the allowed synchronization pairs are those that correspond to the process of applying a rational substitution f : Σ → 2 Γ * to an input over Σ.The figure to the right describes a possible synchronized pair for f defined by Below, we show how to define such a resynchronization for an arbitrary rational substitution f .We fix, for each letter a ∈ Σ, an NFA A a that recognizes the regular language f (a).We then define an MSO resynchronizer R f that uses one output parameter O a,t for every letter a ∈ Σ and every transition t of A a , plus two additional output parameters O first and O last .By a slight abuse of notation, given the output-type τ of a position x, we write τ [a, t] = 1 whenever x ∈ O a,t , and similarly for τ [first ] and τ [last ].The first formula α of the resynchronizer holds vacuously, as we have no restriction on the input.The second formula β requires that the parameters O a,t form a partition of the output domain in such a way that if a position x is labeled by a letter c ∈ Γ and x ∈ O a,t , then t is a c-labeled transition of A a .In addition, β requires that the parameters O first and O last are singletons consisting of the first and the last output position, respectively.The third component γ of the resynchronizer is defined by γ(τ )(y, z) = a(y) ∧ (y = z) whenever τ [a, t] = 1 (the origin is not modified, and the transition annotating the output position must belong to the correct NFA).The last component δ restricts further the parameters and the origins for consecutive output positions, so as to simulate successful runs of the NFA.Formally, δ(τ, τ ′ )(z, z ′ ) enforces the following constraints: , and z = z ′ , then the target state of t coincide with the source state of t ′ , namely, tt ′ forms a factor of a run of A a , • if τ [a, t] = 1, τ ′ [a ′ , t ′ ] = 1, and z < z ′ , then the target state of t must be final, the source state of t ′ must be initial, and every input letter strictly between z and z ′ is mapped via f to a language that contains ε, • if τ [first ] = 1 and τ [a, t], then the source state of t must be initial, and every input letter strictly before z is mapped via f to a language that contains ε, • if τ ′ [last ] = 1 and τ ′ [a, t], then the target state of t must be final, and every input letter strictly after z ′ is mapped via f to a language that contains ε.
5. We conclude the list of examples with a resynchronization that moves the origin of an arbitrarily long output over an arbitrarily long distance.This resynchronization contains the pairs (σ, σ ′ ), where σ (resp.σ ′ ) maps every output position to the ] is also 'one-way', in the sense that it contains only synchronized pairs that are admissible outcomes of runs of one-way transducers.However, [[R 1st−to−last ]] is not captured by the formalism of one-way rational resynchronizers from [FJLW16].
Recall the Example 13.1 above, which defines the universal resynchronization [[R univ ]].The containment problem modulo [[R univ ]] boils down to testing classical containment between transducers without origins, which is known to be undecidable [Gri68].Based on this, it is clear that in order to compare effectively transducers modulo resynchronizations, we need to restrict further our notion of MSO resynchronizer.Intuitively, what makes the containment problem modulo resynchronization undecidable is the possibility of redirecting many sources to the same target.This possibility is explicitly forbidden in the definition below.Also observe that, since [[R univ ]] is the reflexive and transitive closure of [[R ±1 ]], we cannot take our resynchronizations to be equivalence relations.
Note that all resynchronizers from Example 13 but R univ are bounded.For instance, R 1st−to−last is 1-bounded and R ±1 is 2-bounded.
Proposition 15. t is decidable to know whether a given MSO resynchronizer is bounded.
Proof.Let R = (α, β, γ, δ) be a resynchronizer as above.Since we can work with words over Σ ′ = Σ × B m+1 , where the annotation over B m+1 encodes the interpretation of the input parameters I and the target z of γ(I, y, z), verifying whether R is bounded amounts to verify whether an MSO formula γ(y) with one free variable satisfies the following property: There is some k such that for every word w ∈ Σ ′ * , there are at most k positions y ∈ dom(w) such that w, y γ.
To check the above property we assume that γ is given by a deterministic finite automaton (DFA) A. The DFA processes words with a single marked position, i.e. words over Σ ′ × B.
From A = (Q, Σ ′ × B, ∆, q 0 , F ) we build an NFA B that recognizes the projection of A over Σ ′ .Formally, the NFA B has Σ ′ as input alphabet, and two copies of Q as set of states, i.e.Q ′ = Q ⊎ Q.The two copies of Q have transitions of the form (q, a, q ′ ) and (q, a, q′ ), for any q, (a, 0), q ′ ) ∈ ∆.In addition, if q, (a, 1), q ′ ∈ ∆, then we add the transition (q, a, q′ ) that moves from Q to Q.An NFA is said to be k-ambiguous if it admits at most k successful runs on every given input.It is finitely ambiguous if it is k-ambiguous for some k.From the above constructions it follows that B is finitely ambiguous if and only if the MSO resynchronizer is bounded.To conclude, we recall that one can decide in polynomial time whether a given NFA is finitely ambiguous [WS91].
The goal is to reduce the problem of containment modulo a bounded MSO resynchronizer to a standard containment problem in the origin semantics.For this, the natural approach would be to show that transducers are effectively closed under bounded MSO resynchronizers.However, this closure property cannot be proven in full generality, because of the (input) parameters that occur in the definition of resynchronizers.More precisely, two-way transducers cannot guess parameters in a consistent way (different guesses could be made at different visits of the same input position).We could show the closure if we adopted a slightly more powerful notion of transducer, with so-called common guess [BDGP17].Here we prefer to work with classical two-way transducers and explicitly deal with the parameters.Despite the different terminology, the principle is the same: parameters are guessed beforehand and accessed by the two-way transducer as explicit annotations of the input.Given a transducer T over an expanded input alphabet Σ × Σ ′ and an NFA A over Σ × Σ ′ , we let

In other words, [[T ]]
] o by restricting the inputs of T via A, and then projecting them on Σ.
The following result is the key to reduce containment modulo bounded MSO resynchronizers to containment in the origin semantics.
Theorem 16.Given a bounded MSO resynchronizer R with m input parameters, a transducer T with input alphabet Σ × Σ ′ and an NFA A over Σ × Σ ′ , one can construct a transducer T ′ with input alphabet Σ × Σ ′ × B m and an NFA A ′ over Σ × Σ ′ × B m such that Proof.To prove the claim, it is convenient to assume that R has no input nor output parameters.If this were not the case, we could modify T in such a way that it reads inputs over Σ × Σ ′ × B m , exposing a valuation of the parameters I, and produces outputs over Γ × B n , exposing a valuation O.We could then apply the constructions that follow, and finally modify the resulting transducer T ′ by projecting away the input and output annotations.We observe that the projection operation on the output is easier and can be implemented directly at the level of the transitions of T ′ , while the projection of the input requires the use of the notation [[T ′ ]] o ⇂ A ′ Σ , for the reasons that we discussed earlier.In both cases the complexity bounds are preserved.
Let us begin with the resynchronizer R α = (α, true, γ id , δ true ).In this case the origins are not modified: the only effect of R α is to restrict the set of possible inputs.Formally, we need to check that an input u ∈ Σ * , not only is accepted by the NFA A, but also satisfies the formula α.This can be checked by an NFA A ′ that is obtained from intersecting A with an NFA equivalent to α.In this case, the transducer T ′ is the same as T , and the construction of A ′ takes time polynomial in |A|, assuming that α is fixed.In particular this satisfies the complexity bounds given in the second claim of the theorem.
Let us now consider the resynchronizer R β = (true, β, γ id , δ true ), which restricts the set of possible outputs.In this case, since no further restriction is enforced on the input, we can take the NFA A ′ to be the same as A. To construct T ′ , we proceed as follows.We first construct an NFA B equivalent to β.For every pair of states q, q ′ of B, let L q,q ′ be the language of words that induce runs of B from q to q ′ .We use the latter languages to modify T and simulate a successful run of B on the output.Formally, the control states of T are paired with the control states of B, and the new transition rules are of the form (p, q) − a|L∩L q,q ′ −−−−−−→ (p ′ , q ′ ), whenever p − a|L − − → p ′ is a transition rule of T and L ∩ L q,q ′ = ∅.The initial and final states of T ′ are defined accordingly.As before, the construction of T ′ takes polynomial time in |T |, assuming that the formula β is fixed.
We now consider the most interesting case, that of R γ = (true, true, γ, δ true ), which modifies the origins.The rough idea here is that T ′ has to simulate an arbitrary run of T , by displacing the origins of any output letter with type τ from a source y to a target z, as indicated by the formula γ(τ )(x, y).Since a factor of an output of T that originates at the same input position can be broken up into multiple sub-factors with origins at different positions, here it is convenient to assume that T outputs at most a single letter at each transition.This assumption can be made without loss of generality, since we can reproduce any longer word v that is output by some transition (q, i) − a|L − − → (q ′ , i ′ ) letter by letter, with several transitions that move back and forth around position i.
The idea of the construction is as follows.Whenever T outputs a letter b with origin in y, T ′ non-deterministically moves to some position z that, together with y, satisfies the formula γ(b) (note that b is also the output-type of the produced letter).Then T ′ produces the same output b as T , but at position z.Finally, it moves back to the original position y.For the latter step, we will exploit the fact that R is bounded.Now, for the details, we construct from γ a finite monoid (M, •), a monoid morphism h : (Σ × Σ ′ × B × B) * → M , and some subsets F τ of M τ , for each output-type τ ∈ Γ, such that (u, x, y) γ(τ ) if and only if h(u x,y ) ∈ F τ , where u x,y is the encoding on u of the positions x and y, namely, u x,y (i) = (u(i), b i=x , b i=y ) for all 1 ≤ i ≤ |u|, and b i=x (resp.b i=y ) is either 1 or 0 depending on whether i = x (resp.i = y) or not.Similarly, we denote by u ∅,∅ the encoding on u of two empty monadic predicates.For all 1 ≤ i ≤ j ≤ |u|, we then define The elements ℓ i and r i associated with each position i of the input u are functionally determined by u.In particular the word ℓ 1 . . .ℓ |u| (resp.r 1 . . .r |u| ) can be seen as the run of a deterministic (resp.co-deterministic) automaton on u.Without loss of generality, we can assume that the values ℓ i and r i are readily available as annotations of the input at position i, and checked by means of a suitable refinement A ′ of the NFA A.
We now describe how T ′ simulates a transition of T , say q − a|b − − → q ′ , that originates at a position y and produces the letter b (the simulation of a transition with empty output is • it non-deterministically chooses a language among L ε and L b ′ , for any b ′ ∈ Γ, where • if it chose to output L ε , then, assuming L ε = ∅, it simulates the change of control state of the transition of Busy(T U ), moves from z ′ to z ′′ , and updates the stored monoid element from m z,z ′ to m z,z ′′ = m z,z ′ • h(a), where a is the input letter at position z ′′ ; • if it chose to output L b ′ , then, assuming L b = ∅, it simulates the change of control state of the transition of Busy(T U ), moves from z ′ to z ′′ , updates the stored monoid element from m z,z ′ to m z ′′ ,z ′′ = h(ε), and finally changes the most recent output letter to b ′ .
In particular, the above behaviour guarantees the following two properties: 1. δ(v(x), v(x + 1))(z ′ , z ′ ) holds for all pairs of consecutive output positions x and x + 1 that are produced at the same input position z ′ .
2. δ(b, v(x + 1))(z, z ′ ) holds for all pairs of consecutive output positions x and x + 1 that have origins respectively in z -i.e. the last input position where a non-empty output ending with b = v(x) was emitted-and z ′ -i.e. the current input position, where a new non-empty output is going to be emitted.The above result is used to reduce the containment problem modulo a bounded MSO resynchronizer R to a containment problem with origins (relativized to correctly annotated inputs).That is, if T 1 , T 2 are transducers with input alphabet Σ, and T ′ 2 , A ′ are over the input alphabet Σ × Σ ′ and constructed from T 2 using Theorem 16, then where R ′ = [[A ′ ]]×(Γ×N) * and ↾ Σ ′ is the inverse of the input-projection operation, i.e.

[[T ]]
We also recall from Section 3 that the latter containment reduces to emptiness of a PSpace-constructible NFA, which can then be decided in PSpace w.r.t. the sizes of T 1 and T 2 .We thus conclude with the following result: Corollary 17.The problem of deciding whether T 1 ⊆ R T 2 , for any pair of transducers T 1 , T 2 and for a fixed bounded MSO resynchronizer R, is PSpace-complete.

Conclusions
We studied the equivalence and containment problems for non-deterministic, two-way word transducers in the origin semantics, and proved that the problems are decidable in PSpace, which is the lowest complexity one could expect given that equivalence and containment of NFA are already PSpace-hard.This result can be contrasted with the undecidability of equivalence of non-deterministic, one-way word transducers in the classical semantics.
We have also considered a variant of containment up to 'distortions' of the origin, called containment modulo a resynchronization R, and denoted ⊆ R .We identified a broad class of resynchronizations, definable in MSO, and established decidability of the induced containment problem.In fact, we obtained an optimal fixed-parameter complexity result: testing a containment T 1 ⊆ R T 2 modulo a bounded MSO resynchronization R is PSpace-complete in the size of the input transducers T 1 , T 2 , where the fixed parameter is the size of the formulas used to describe R.
Our logical definition of resynchronizations talks implicitly about origin graphs.Since we cannot encode the origin semantics of arbitrary two-way transducers by words, we have chosen to work directly with origin graphs defined by two-way transducers.A classical way to define resynchronizations of origin graphs would be to use logical formalisms for graph transformations.Unfortunately, the classical MSO approach [Cou97,Eng97] does not work in our setting, since satisfiability of MSO over origin graphs of two-way transducers is already undecidable, which means that the definable resynchronizations would not be realizable by two-way transducers.
Another possibility would be to use a decidable logic over origin graphs, like the one introduced by Filiot et al. in [DFL18].Their logic is not suited either for our purposes, since it allows predicates of arbitrary arity defined using MSO over the input word.The reason is that single head devices would not be able to move between the tuples of positions related by those definable predicates, and thus, in particular, we would not be able to guarantee that the definable resynchronizations are realizable by two-way transducers.
Yet an alternative approach to the above problem consists in viewing tagged outputs as data words, and using transducers to transform data words.Durand-Gasselin and Habermehl introduced in [DGH16] a framework for transformations of data words, where MSO transductions, deterministic two-way transducers and deterministic streaming transducers have the same expressive power.However, this approach would be unsatisfactory, because the transformation does not take the input into account.
We conjecture that the bounded MSO resynchronizers defined here strictly capture the rational resynchronizers introduced in [FJLW16].In particular, although MSO resynchronizers do not have the ability to talk about general origin graphs, they presumably can describe 'regular' origin graphs of one-way transducers, i.e., graphs expressed by regular languages over sequences that alternate between the input and the output word.We also recall that the MSO resynchronizer R 1st−to−last from Example 13.5 contains only origin graphs of one-way transducers, but cannot be defined by a rational resynchronizer.
Another natural question that we would like to answer concerns compositionality: are bounded MSO resynchronizers closed under relational composition?In other words, given two bounded MSO resynchronizers R, R ′ , is it possible to find (possibly effectively) a bounded MSO resynchronizer
R is fixed, T has n states and PSpace-constructible transitions w.r.t.n, and A is PSpace-constructible w.r.t.n, then T ′ and A ′ have similar properties, namely, T ′ has a number of states polynomial in n and PSpace-constructible transitions w.r.t.n, and A ′ is PSpace-constructible w.r.t.n.