The Time Complexity of Permutation Routing via Matching, Token Swapping and a Variant

The problems of Permutation Routing via Matching and Token Swapping are reconfiguration problems on graphs. This paper is concerned with the complexity of those problems and a colored variant. For a given graph where each vertex has a unique token on it, those problems require to find a shortest way to modify a token placement into another by swapping tokens on adjacent vertices. While all pairs of tokens on a matching can be exchanged at once in Permutation Routing via Matching, Token Swapping allows only one pair of tokens can be swapped. In the colored version, vertices and tokens are colored and the goal is to relocate tokens so that each vertex has a token of the same color. We investigate the time complexity of several restricted cases of those problems and show when those problems become tractable and remain intractable.


Introduction
Alon et al. [1] have proposed a problem called Permutation Routing via Matching as a simple variant of routing problems. 1 Suppose that we have a simple graph where each vertex is assigned a token. Each token is labeled with its unique goal vertex, which may be different from where the token is currently placed. We want to relocate every misplaced token to its goal vertex. What we can do in one step is to pick a matching and swap the two tokens on the ends  [1] and following papers are mostly interested in the maximum number of steps, denoted rt(G), needed to realize the goal configuration from any initial configuration for an input graph G. For example, Alon et al. have shown rt(K n ) = 2 for complete graphs K n , Zhang [21] has shown rt(T ) = 3n/2+(log n) for trees T of n vertices, and Li et al. [13] have shown rt(K m,n ) ∈ ⌊3m/2n⌋+O(1) for bipartite graphs K m,n with m ≥ n and rt(C n ) = n − 1 for n ≥ 3 for cycles C n . This paper is concerned with the problem where an initial configuration also constitutes an input and discusses its computational complexity. We will show the following results, which were independently obtained by Banerjee and Richards [2].
• Permutation routing via matching is NP-complete even to decide whether an instance admits a 3 step solution (Theorem 12).
• To decide whether 2 step solution exists can be answered in polynomialtime (Theorem 15).
In addition, we present a polynomial-time algorithm that approximately solves Permutation Routing via Matching on paths whose output is at most one larger than that of the exact answer (Theorem 19). Token swapping, introduced by Yamanaka et al. [19], can be seen as permutation routing via "edges". In this setting we can swap only two tokens on an edge at each step. Figure 1 shows that we require 4 steps in Token Swapping to realize the goal configuration, while 2 steps are enough in Routing via Matching. Yamanaka et al. have presented several positive results on this problem in addition to classical results which can be seen as special cases [9]. Namely, graph classes for which Token Swapping can be solved in polynomial-time are paths, cycles, complete graphs and complete bipartite graphs. They showed that Token Swapping for general graphs belongs to NP. The NP-hardness is recently shown in preliminary versions [11,12] of this paper and by Miltzow et al. [14] and Bonnet et al. [3] independently. On the other hand, some polynomial-time approximation algorithms are known for different classes of graphs including the general case [8,14,19]. Our NP-hardness result is tight with respect to the degree bound, as the problem can be solved in polynomial-time if input graphs have vertex degree at most 2.
• Token swapping is NP-complete even when graphs are restricted to bipartite graphs where every vertex has degree at most 3 (Theorem 6).
Moreover, we present two polynomial-time solvable subcases of Token Swapping. One is of lollipop graphs, which are combinations of a complete graph and a path. The other is the class of graphs which are combinations of a star and a path.
A variant of Token Swapping is c-Colored Token Swapping. Tokens and vertices in this setting are colored by one of c admissible colors. We decide how many swaps are required to relocate the tokens so that each vertex has a token of the same color. Yamanaka et al. [20] have shown that 3-Colored Token Swapping is NP-complete while 2-Colored Token Swapping is solvable in polynomial time. This problem and a further generalization are also studied in [3]. In this paper we consider the colored version of Routing via Matching and show that it is also NP-complete.
• 2-Coloring Routing via Matching is NP-complete even to decide whether an instance admits a 3-step solution (Theorem 23).
• 3-Coloring Routing via Matching is NP-complete even to decide whether an instance admits a 2-step solution (Theorem 25).
The former result contrasts the fact that the 2-Colored Token Swapping is solvable in polynomial-time [20]. The latter contrasts that to decide whether 2-step solution exists for Permutation Routing is in P (Theorem 15). In addition, we present another contrastive result.
• It is decidable in polynomial-time whether a 2-step solution exists in 2-Coloring Routing via Matching (Theorem 31).
One may consider permutation routing on graphs as a special case of the Minimum Generator Sequence Problem [6]. The problem is to determine whether one can obtain a permutation f on a finite set X by multiplying at most k permutations from a finite permutation set Π, where all of X, f , k and Π are input. The problem is known to be PSPACE-complete if k is specified in binary notation [9], while it becomes NP-complete if k is given in unary representation [6]. In our settings, permutation sets Π are restricted to the ones that have a graph representation. However, this does not necessarily mean that the NP-hardness of Permutation Routing via Matching implies the hardness of the Minimum Generator Sequence Problem, since the description size of all the admissible parallel swaps on a graph is exponential in the graph size.

Time Complexity of Token Swapping
We denote by G = (V, E) an undirected graph whose vertex set is V and edge set is E. More precisely, elements of E are subsets of V consisting of exactly two distinct elements. A configuration f (on G) is a permutation on V , i.e., bijection from V to V . By [u] f we denote the orbit { f i (u) | i ∈ N } of u ∈ V under f . We call each element of V a token when we emphasize the fact that it is in the range of f . We say that a token v is on a vertex u in f if v = f (u). A swap on G is a synonym for an edge of G, which behaves as a transposition.
For a configuration f and a swap e ∈ E, the configuration obtained by applying e to f , which we denote by f e, is defined by For a sequence e = e 1 , . . . , e m of swaps, the length m is denoted by | e|. For i ≤ m, by e |≤i we denote the prefix e 1 , . . . , e i . The configuration f e obtained by applying e to f is (. . . ((f e 1 )e 2 ) . . . )e m . We say that the token f (u) on u is moved to v by e if f e(v) = f (u). We count the total moves of each token u ∈ V in the application as Clearly move(f, e, u) ≥ dist(f −1 (u), (f e) −1 (u)), where dist(u 1 , u 2 ) denotes the length of a shortest path between u 1 and u 2 , and u∈V move(f, e, u) = 2| e|. We denote the set of solutions for a configuration f by TS(G, f ) = { e | e is a swap sequence on G such that f e is the identity } .
The length of an optimal solution is denoted by ts(G, f ).

Problem 1 (Token Swapping).
Instance: A connected graph G, a configuration f on G and a natural number k.

Token Swapping Is NP-complete
This subsection proves the NP-hardness of Token Swapping by a reduction from the 3DM, which is known to be NP-complete [10].
Question: Is there M ⊆ T such that |M | = |A 1 | and every element of For notational convenience we write a ∈ t if a ∈ A occurs in t ∈ T by identifying t with the set of the elements of t. We construct an instance (G T , f ) of Token Swapping as follows. The vertex set of The edge set E T is given by We call the subgraph induced by The initial configuration f is defined by In the initial configuration f , all and only the tokens in V A are misplaced. Each token u k,i ∈ V A on the vertex u ′ k,i must be moved to u k,i via (a part of) t j -cycle for some t j ∈ T in which a k,i occurs. To design a short solution for (G T , f ), it is desirable to have swaps at which both of the swapped tokens get closer to the destination. If (A, T ) admits a solution, then one can find an optimal solution for (G T , f ) of length 21n, where 9n of the swaps satisfy this property as we will see in Lemma 4. On the other hand, such an "efficient" solution is possible only when (A, T ) admits a solution as shown in Lemma 5. Figure 2 shows the graph and initial configuration reduced from the 3DM instance (A, T ). This instance (A, T ) has a solution M = {t 1 , t 3 }. The proof of Lemma 4 will give how to find an optimal solution for the reduced TSP instance corresponding to M . A part of the solution is illustrated in Figure 3.  Proof. We show in the next paragraph that for each t j ∈ T , there is a sequence σ j of 21 swaps such that gσ j is identical to g except (gσ j )(u k,i ) = g(u ′ k,i ) and (gσ j )(u ′ k,i ) = g(u k,i ) if a k,i occurs in t j for any configuration g. If M ⊆ T is a solution, by collecting σ j for all t j ∈ M , we obtain a swap sequence σ M of length 21n such that f σ M is the identity.
Let t j = {a 1,i1 , a 2,i2 , a 3,i3 }. We first move each of the tokens u k,i k on the vertex u ′ k,i k to the vertex v j,k and the tokens u ′ k,i k on u k,i k to v ′ j,k . We then move the tokens u k,i k on v j,k to the opposite vertex v ′ j,k of the t j -cycle for each k ∈ {1, 2, 3} while moving u ′ k,i k on v ′ j,k to v j,k in the opposite direction simultaneously. At last we make swaps on the same 6 edges we used in the first phase. The above procedure consists of 21 swaps and gives the desired configuration.
Proof. We first show that 21n is a lower bound on ts(G T , f ). Suppose that f σ is the identity. For each token u k,i ∈ V A , we have The adjacent vertices of the vertex u ′ k,i are v j,k such that a k,i ∈ t j . Among those, let τ (u k,i ) ∈ V T be the vertex to which u k,i goes for its first step, i.e., the first occurrence of u ′ k,i in σ is as {u ′ k,i , τ (u k,i )}. This means that move(f, σ, τ (u k,i )) ≥ 2, since the token τ (u k,i ) must once leave from and later come back to the vertex τ (u k,i ). The symmetric discussion holds for all tokens u ′ k,i . Therefore, noting The optimal solution given in the proof of Lemma 4 that exchanges u k,1 and u ′ k,1 for all k ∈ {1, 2, 3} via the t 1 -cycle is illustrated here, where we suppress vertex names. By swapping the tokens on the bold edges in each configuration, we obtain the succeeding one pointed by an arrow. The number by each arrow shows the number of swaps. The swap sequence consists of 21 swaps in total. By doing the same on t 3 -cycle with respect to u 1,2 , u 2,2 , u 3,2 , u ′ 1,2 , u ′ 2,2 , u ′ 3,2 , we obtain the goal configuration. that τ is an injection, we obtain This has shown that if f σ is the identity and |σ| ≤ 21n, then (2) and (1).
It is known that the 3DM is still NP-complete if each a ∈ A occurs at most three times in T [7]. Assuming that T satisfies this constraint, it is easy to see that G T is a bipartite graph with maximum vertex degree 3.

Theorem 6. Token swapping is NP-complete even on bipartite graphs with maximum vertex degree 3.
The NP-hardness of Token Swapping was independently proven by Miltzow et al. [14] and by Bonnet et al. [3]. The graphs obtained by the reduction of Miltzow et al. have a degree bound but it is not as small as our constraint. Our bound 3 is tight, as Token Swapping on graphs with degree at most 2, i.e., paths and cycles, is solvable in polynomial-time. Bonnet et al. [3] have given no degree constraint but their graphs have tree-width 2 and diameter 6. Therefore, their and our results are incomparable.

PTIME Subcases of Token Swapping
In this subsection, we present two graph classes on which Token Swapping can be solved in polynomial time. One is that of lollipop graphs, which are obtained by connecting a path and a complete graph with a bridge. That is, a lollipop graph is L m,n = (V, E) where V = { −m, . . . , −1, 0, 1, . . . , n } and The other class consists of graphs obtained by connecting a path and a star. A star-path graph is Q m,n = (V, E) such that V = { −m, . . . , −1, 0, 1, . . . , n } and

Permutation Routing via Matching
Permutation routing via matching can be seen as the parallel version of Token Swapping. Definitions and notation for Token Swapping are straightforwardly generalized as follows. A parallel swap S on G is a synonym for an involution which is a subset of E, or for a matching of G, i.e., S ⊆ E such that For a configuration f and a parallel swap S ⊆ E, the configuration obtained by applying S to f is defined by

Problem 7 (Permutation Routing via Matching).
Instance: A connected graph G, a configuration f on G and a natural number k.

Routing Permutations via Matching Is NP-complete
We show that routing permutations via matching is NP-hard by a reduction from a restricted kind of the satisfiability problem, which we call PPN-Separable 3SAT (Sep-SAT for short). For a set X of (Boolean) variables, ¬X denotes the set of their negative literals. A 3-clause is a subset of X ∪ ¬X whose cardinality is at most 3. An instance of Sep-SAT is a finite collection F of 3-clauses, which can be partitioned into three subsets F 1 , F 2 , F 3 ⊆ F such that for each variable x ∈ X, the positive literal x occurs just once in each of F 1 , F 2 and never in F 3 , and the negative literal ¬x occurs just once in F 3 and never in F 1 nor F 2 . Note that one can find a partition {F 1 , F 2 , F 3 } of a Sep-SAT instance F in linear time.
Proof. See Appendix C.
We give a reduction from Sep-SAT to Permutation Routing via Matching. For a given instance F = {C 1 , . . . , C n } over a variable set X = {x 1 , . . . , x m } of Sep-SAT, we define a graph G F = (V F , E F ) in the following manner. Let F be partitioned into F 1 , F 2 , F 3 where each of F 1 and F 2 has just one occurrence of each variable as a positive literal and F 3 has just one occurrence of each negative literal. Define The edge set E F is the least set that makes G F contain the following paths of length 2 and 3: The fact that G F is bipartite can be seen by partitioning Figure 4: The instance of Permutation Routing via Matching obtained from the Sep-SAT instance F of Example 10. By moving misplaced tokens along the bold edges, the goal configuration is realized in 3 steps. The reduction graph described in the proof for Theorem 13 has essentially the same shape. and the rest. Vertices v j and v ′ j have degree at most 3 for j ∈ {1, . . . , n}, while u i,k has degree 4 for i ∈ {1, . . . , m} and k ∈ {1, 2, 3}. The initial configuration f is defined to be the identity except for each i ∈ {1, . . . , m} and j ∈ {1, . . . , n}.
Moreover, F 1 and F 2 have only positive literals and F 3 has only negative literals. Therefore, F is a Sep-SAT instance. Figure 4 shows the reduction from F . The formula F is satisfied by assigning 1 to x 1 , x 3 and 0 to x 2 . Corresponding to this assignment, by moving misplaced tokens along the bold edges in Figure 4, the goal configuration is realized in 3 steps.
We will show that F is satisfiable if and only if this lower bound is achieved. Here we describe an intuition behind the reduction by giving the following observation between a 3-step solution for (G F , f ) and a solution for F : • tokens u i and u ′ i pass vertices u i,1 and u i,2 iff x i should be assigned 0, while they pass over u i,3 and u i,4 iff x i should be assigned 1, Of course it is contradictory that a clause C j ∈ F 1 is satisfied by x i ∈ C j which is assigned 0. This impossibility corresponds to the fact that there are no i, j such that both u i and v j with C j ∈ F 1 go to their respective goals via u i,1 in a 3-step solution.
Conversely, suppose that (G F , f ) admits a solution S 1 , S 2 , S 3 . Since the token on u i is moved to u ′ i by the three steps, the path that u ′ i takes should be either In other words, S 2 contains at least one of {u i,1 , u i,2 } and {u i,3 , u i,4 }. We prove that F is satisfied by the assignment φ : For each C j ∈ F 1 , the token on v j must be moved to v ′ j via u i,1 for some i such that Hence C j is satisfied by φ. Almost the same arguments show that clauses in F 2 and F 3 are also satisfied by φ.
Theorem 12. For any fixed p ≥ 3, to decide whether rt(G, f ) ≤ p is NPcomplete even when G is restricted to be a bipartite graph with maximum vertex degree 4.
Proof. Lemma 11 proves the theorem for p = 3. We show the lemma for p = 3+h with h > 0 by inserting paths into appropriate places in the graph constructed above. We add the following paths of length h: Figure 5: Gadget used to show Theorem 12, where Tokens on their goal vertices are omitted.
The tokens on those paths in the initial configuration will be Then, concerning the vertices where u i and u ′ i are put in the initial and goal configurations, The same argument in the proof of Lemma 11 works.
Banerjee and Richards [2] have shown Theorem 12 using a different reduction.
One can modify our reduction so that every vertex has degree at most 3 by dividing vertices u i,k into two vertices of degree at most 3. Let The new graph G ′ F contains the following paths of length 5 and 4: The initial configuration f is defined in the same manner as the previous con- . . , m} and j ∈ {1, . . . , n}. The formula F is satisfiable if and only if rt(G ′ F , f ) = 5. Theorem 13. For any fixed k ≥ 5, to decide whether rt(G, f ) ≤ k is NPcomplete even when G is restricted to be a bipartite graph with maximum vertex degree 3.

PTIME Subcases
In this subsection we discuss tractable subcases of Permutation Routing via Matching. In contrast to Theorem 12, it is decidable in polynomial time whether an instance of Permutation Routing via Matching admits a 2-step solution. In addition, we present an approximation algorithm for finding a solution for Permutation Routing via Matching on paths whose length can be at most one larger than that of an optimal solution.

2-Step Permutation Routing via Matching
It is well-known that any permutation can be expressed as a product of 2 involutions, which means that any problem instance of Permutation Routing via Matching on a complete graph has a 2-step solution. Graphs we treat are not necessarily complete but the arguments by Petersen and Tenner [16, Lemma 2.3] on involution factorization lead to the following observation, which is useful to decide whether rt(G, f ) ≤ 2 for general graphs G.
Proof. Suppose G and f are given. One can compute in polynomial time all the orbits [·] f . Let us denote the subgraph of G induced by a vertex set U ⊆ V by G U and the sub- can be computed in polynomial time by Proposition 14. It is clear that rt(G, f ) ≤ 2 if and only if there is a subset Γ ⊆ Γ f in which every orbit occurs exactly once. This problem is a very minor variant of the problem of finding a perfect matching on a graph, which can be solved in polynomial time [5].
One can calculate the number of 2-step solutions in RT(K n , f ) for any configuration f on the complete graph K n using Petersen and Tenner's formula [16]. However, it is hard for general graphs.
Theorem 16. It is a #P-complete problem to calculate the number of 2-step solutions in RT(G, f ) for bipartite graphs G.
Proof. We show the theorem by a reduction from the problem of calculating the number of perfect matchings in a bipartite graph H, which is known to be #P-complete [17].

Approximation Algorithm for the Permutation Routing via Matching on Paths
We present an approximation algorithm for the Permutation Routing via Matching on paths which outputs a parallel swap sequence whose length is no more , and moreover, a parallel swap sequence S = S 1 , . . . , S m is reasonable w.r.t. f if every e ∈ S j is reasonable w.r.t. f S 1 , . . . , S j−1 for all j ∈ {1, . . . , m}. The parallel swap sequence S 1 , . . . , S m output by Algorithm 3 is reasonable and satisfies the condition which we call the odd-even condition: for each odd number j, all swaps in S j are of the form {2i − 1, 2i} for some i ≥ 1, and for each even number j, all swaps in S j are of the form {2i, 2i + 1} for some i ≥ 1. Our algorithm computes a reasonable odd-even parallel swap sequence in a greedy manner.
. . , S m ∈ RT(P n , f ), at some step we must exchange the positions of the tokens f (i) and Then for any j ∈ {1, . . . , m}, f j and g j = g S ′ 1 , . . . , S ′ j are identical except when j < k the positions of tokens f (i) and f (i + 1) are switched.
Let us denote the output of Algorithm 3 by AP(P n , f 0 ). Clearly AP(P n , f 0 ) ∈ RT(P n , f 0 ).

Algorithm 3 Approximation algorithm for Permutation Routing via Matching on paths
Corollary 18. For any odd-even solution S ∈ RT(P n , f 0 ), we have |AP(P n , f 0 )| ≤ | S|.
Proof. By Corollary 18, it is enough to show that every swap sequence S = S 1 , . . . , S m admits an equivalent odd-even sequence S ′ such that | S ′ | ≤ | S|+1. Without loss of generality we assume that S j ∩ S j+1 = ∅ for any j (in fact, any reasonable parallel swap sequence meets this condition). For a parallel swap sequence S = S 1 , . . . , S m , define OE( S) = S ′ 1 , . . . , S ′ m+1 by delaying swaps which do not meet the odd-even condition, that is, for j = 1, . . . , m + 1 assuming that S 0 = S m+1 = ∅. By the parity restriction, each S ′ j is a parallel swap. It is easy to show by induction on j that for each j ∈ {1, . . . , m + 1}, which implies that f S = f OE( S). Therefore, for an optimal reasonable solution S 0 , we have | S 0 | + 1 = |OE( S 0 )| ≥ |AP(P n , f 0 )|.

Coloring Routing via Matching
Colored Token Swapping is a generalization of Token Swapping, where each token is colored and different tokens may have the same color. By swapping tokens on adjacent vertices, the goal coloring configuration should be realized. More formally, a coloring is a map f from V to N. The definition of a swap application to a configuration can be applied to colorings with no change. We say that two colorings f and g are consistent if |f −1 (i)| = |g −1 (i)| for all i ∈ N. Since the problem is a generalization of Token Swapping, obviously it is NP-hard. Yamanaka et al. [20] have investigated subcases of Colored Token Swapping called c-Colored Token Swapping where the codomain of colorings is restricted to {1, . . . , c}. Along this line, we discuss the colored version of Permutation Routing via Matching in this section.

Problem 21 (c-Coloring Routing via Matching).
Instance: A graph G, two consistent c-colorings f and g, and a number k ∈ N.
Question: Is there S with | S| ≤ k such that f S = g?
Define rt(G, f, g) = min{ | S| | f S = g } for two consistent colorings f and g. Since rt(G, f, g) can be bounded by rt(G, h) for some configuration h, the c-Coloring Routing via Matching belongs to NP.

Hardness of the c-Coloring Routing via Matching
Yamanaka et al. have shown that the 3-colored Token Swapping is NP-hard by a reduction from the 3DM. It is not hard to see that their reduction works to prove the NP-hardness of the 3-Coloring Routing via Matching. We then obtain the following theorem as a corollary to their discussion.
Theorem 22. To decide whether rt(G, f, g) ≤ 3 is NP-hard even if G is restricted to be a planar bipartite graph with maximum vertex degree 3 and f and g are 3-colorings. Proof. We prove the theorem by a reduction from Sep-SAT. We use a slight modification of the graph used in the proof of Lemma 11 to show the theorem for p = 3. We now define The edge set E F is the least set that makes G F contain the following paths of length 3: The initial and goal colorings f and g are defined to be f (w) = 1 and g(w) = 1 for all w but f (u Figure 6 illustrates the gadget related to a variable x 1 that occurs positively in C 1 ∈ F 1 , C 2 ∈ F 2 and negatively in C 3 ∈ F 3 , where each vertex w with f (w) = 2 has a black box on it and one with g(w) = 2 is represented with a bold rim.
If F is satisfiable, then essentially the same parallel swap sequence in the proof of Lemma 11 witnesses rt(G F , f, g) = 3. It is enough to show that if g = f S with | S| ≤ 3, then the token colored 2 on u i is moved to u ′ i for each x i ∈ X, the one on v j is moved to v ′ j for C j ∈ F 1 ∪F 3 , and the one on v ′ j is moved to v j for C j ∈ F 2 . The token on v j must go to a vertex w such that g(w) = 2 and dist(v j , w) ≤ 3. For C j ∈ F 1 ∪ F 3 , the only vertex that meets the condition is v ′ j . On the other hand, for each C j ∈ F 2 , the vertex v j requires a token colored 2 moved from somewhere w, i.e., f (w) = 2 and dist(v j , w) ≤ 3. The only possibility is the vertex v ′ j . Therefore, the token on u i for i ∈ {1, . . . , m} can be moved to neither v ′ j nor v j for any j ∈ {1, . . . , n}. The unique possible destination of u i is u ′ i . We can also show the following using the ideas for proving Theorems 13 and 23. Theorem 24. For any fixed p ≥ 4, to decide whether rt(G, f, g) ≤ p is NP-hard even if G is a bipartite graph with maximum vertex degree 3 and f and g are 2-colorings.
Proof. The theorem is shown based on the reduction from Sep-SAT used for Theorems 12, 13 and 23 again. The gadget we use for this theorem is shown on the right in Figure 6 for p = 4. Suppose that F is satisfied by some φ. If φ(x i ) = 0, then the vertex on u i , u i,1 and u i,3 will go to u i,2 , u ′ i and u 1,4 , respectively. Otherwise, they will go to u i,4 , u i,2 and u ′ i . Then each v j can be moved to v ′ j within 4 steps using an edge on the u i -u ′ i path if either x i ∈ C j ∈ F 1 ∪ F 2 and φ(x i ) = 1 or ¬x i ∈ C j ∈ F 3 and φ(x i ) = 0. Figure 6 illustrates the case where φ(x i ) = 1, x i ∈ C 1 ∈ F 1 , x i ∈ C 2 ∈ F 2 and ¬x i ∈ C 3 ∈ F 3 . Numbers labeling edges show when they are used in a 4-step solution S 1 , . . . , S 4 .
On the other hand, suppose that rt(G, f, g) ≤ 4. Considering the destination of the token on the vertex v j for C j ∈ F 1 ∪ F 3 , the unique vertex w such that Therefore, all the tokens on v j are moved to the vertex v ′ j . This means that the only possible destinations of the token on u i are u i,2 and u i,4 . If u i is moved to u i,2 , then the only possible destination of the token on u i,1 is u ′ i , and thus the token on u i,3 must go to u i,4 . It is now clear that F is satisfied by φ such that φ(x i ) = 0 if and only if the token on u i goes to u i,2 .
The theorem for p > 4 can be shown by inserting extra paths into appropriate places.
The next theorem contrasts the result on Theorem 15, which shows that Theorem 25. It is NP-hard to decide whether rt(G, f, g) ≤ 2 for a graph G of vertex degree at most 4 and 3-colorings f and g.

Proof.
We use the graph used in the proof of Lemma 11. Recall that we have , and E F containing the following paths The initial and goal 3-colorings f and g are such that Figure 7 illustrates the reduction where the values of f and g are shown in rectangles and circles, respectively. Suppose that F is satisfied by an assignment φ. There is a function ψ : F → X such that ψ(C j ) ∈ C j and φ(ψ(C j )) = 1 if C j ∈ F 1 ∪ F 2 , and ¬ψ(C j ) ∈ C j and φ(ψ(C j )) = 0 if C j ∈ F 3 . Define It is easy to see that g = f S 1 , S 2 . We now suppose the converse, S 1 , S 2 ∈ RT(G F , f, g). For each u i , the only vertices w such that dist(u i , w) ≤ 2 and f (u i ) = g(w) = 2 are u i,2 and u i,4 . The only u i -u i,k -path of length at most 2 is (u i , u i,k−1 , u i,k ) for k ∈ {2, 4}. Thus, either {u i , u i,1 } ∈ S 1 and {u i,1 , u i,2 } ∈ S 2 or {u i , u i,3 } ∈ S 1 and {u i,3 , u i,4 } ∈ S 2 . We will show that φ defined by The paths of length at most 2 between v j and v ′ j are of the form (v j , u i,k , v ′ j ) for some i and k such that

2-Step 2-Coloring Routing via Matching Is Easy
In the previous subsection we have shown that c-Coloring Routing via Matching is hard even to decide whether a p-step solution exists if c ≥ 3 and p ≥ 2 or c ≥ 2 and p ≥ 3. We will show that it is easy if c, p ≤ 2. If c = 1, we have nothing to do. If p = 1, the problem is reduced to perfect matching, which can be solved in polynomial time [5]. Suppose that S 1 , S 2 is a 2-step solution for (G, f, g) where f and g are consistent 2-colorings on G = (V, E). We say that a swap {u, v} is vacuous for f if f (u) = f (v).
Lemma 26. If (G, f, g) admits a 2-step solution, then there is S 1 , S 2 ∈ RT(G, f, g) such that • no swaps in S 1 and in S 2 are vacuous for f and for f S 1 , respectively, • S 1 ∪ S 2 gives a path matching in G.
Proof. The first two items are trivial. Assuming S 1 , S 2 satisfies the first two, we show the last. If {u, v}, {v, w} ∈ S 1 ∪ S 2 with v = w, then either {u, v} ∈ S 1 and {v, w} ∈ S 2 or {u, v} ∈ S 2 and {v, w} ∈ S 1 . This implies that G ′ = (V, S 1 ∪ S 2 ) has degree bound 2. Moreover, if G ′ has a cycle, then the size must be even. We show that if G ′ contains a cycle (u 1 , v 1 , u 2 , . . . , u n , v n , u 1 ), then f (u i ) = g(u i ) and f (v i ) = g(v i ) for all i. That is, those edges in the cycle can be removed from S 1 and S 2 . Hereafter, by u j we mean u i such that 1 ≤ i ≤ n and i ≡ j (mod n). Without loss of generality, assume {u i , v i } ∈ S 1 for all i and {v i , u i+1 } ∈ S 2 for all i.
Similarly we have f (u i ) = g(u i ) for all i. Those tokens need not be moved at all.
Hereafter we consider only 2-step solutions that satisfy the condition of Lemma 26.
then (P n , f, g) admits a 2-step solution.
We will reduce the concerned problem to the Vertex-Disjoint Path Problem, which can be solved in polynomial-time [18].

A Proof that Token Swapping on Lollipop Graphs
Is in P This appendix gives a proof that Algorithm 1 computes an optimal swapping sequence on lollipop graphs. We will give an evaluation function on configurations on lollipop graphs L m,n such that any swap changes the value by one, every swap by the algorithm reduces the value by one, and the value is 0 if and only if the configuration is the identity. Algorithm 1 first moves non-negative tokens to the goal vertices on the path and then moves negative ones in the clique. The number of swaps needed to move a token j ∈ {0, . . . , n} is evaluated by where swaps to move the non-negative tokens to the goal vertices in total. We then move the negative tokens in the clique. For a configuration f ′ such that f ′ (j) = j for all j ≥ 0, the number of swaps needed is That is, Note that if c = max( i) > f (0), then c ≥ 0 and thus ( i[f (0)/c]; f (1), . . . , f (k) ) is a pseudo configuration. Our evaluation function Φ is given as Note that ν is defined on pseudo configurations but π and Φ are defined on (proper) configurations. It is clear that Φ(f ) ≥ 0 for any configuration and the equation holds if and only if f is the identity.
Lemma 32. For any i, j, there is a sequence i ′ consisting of the m smallest elements from i · j, where i · j denotes the concatenation of i and j, such that for any k ν( i; j · k) = ν( i ′ ; k) , Proof. The lemma can be shown by induction on | j| just following the definition of ν.
Now we are going to prove that any possible swap on the graph changes the value of Φ by one. We have three cases depending on where a swap takes place. First we consider the case where a swap takes inside the clique.
Lemma 34. Let f = ( i; j) and g = ( i ′ ; j) be pseudo configurations such that i ′ = i[a/b, b/a] for some distinct tokens a, b. Then Proof. We show this by induction on | j|. If j is not empty, the claim follows the induction hypothesis immediately. If j is empty, f and g are configurations on the clique of {−1, . . . , −m}.
The following lemma is concerned with the value of Φ when a swap takes at the joint of the clique and the path.
Proof. We may assume without loss of generality that h = −1 for the symmetry. Let Without loss of generality we assume a > b. Case 1. Suppose a, b < 0. Clearly π(f ) = π(g) and max( i) ≥ 0. We have |ν(f ) − ν(g)| = 1 by applying Lemma 34 to the fact All the m elements of b · i are smaller than a, which are among m + a tokens smaller than a. Therefore, j contains exactly a tokens smaller than a, which means π(g, a) = a. On the other hand, π(f, a) = a + 1 by definition. For all other positive tokens k, π(f, k) = π(g, k) holds.
It remains to show π(f, k) = π(g, k) for all positive tokens k, which is clear for k = a. By definition π(f, a) = a + 1. The fact c > a implies at most m − 1 tokens in b · i are smaller than a, which are among m + a tokens smaller than a. Hence j contains at least a + 1 tokens smaller than a, which means π(g, a) = a + 1.
All in all, |Φ(f ) − Φ(g)| = 1. Case 3. Suppose a > b ≥ 0. This case is almost identical to Case 2 except that we need to confirm π(f, b) = π(g, b) in addition. The fact a > b implies at most m − 1 tokens in a · i are smaller than b, and j contains at least b + 1 tokens smaller than b, which means π(f, b) = π(g, b) = b + 1.
The last case we consider is when a swap takes on the path.
By Lemma 32, there exists i ′ consisting of the m smallest tokens from i · j 1 such that Without loss of generality we assume a > b. Case 1. Suppose a, b < 0. Clearly π(f ) = π(g). For the two largest tokens c and d in i ′ with c > d, we have In this case, we have π(g, a) = Inv(g, a) = Inv(f, a) − 1 = π(f, a) − 1 and thus π(f ) = π(g) + 1. The fact that b · j 2 contains at most a tokens smaller than a implies that i · j 1 contains at least m tokens smaller than a. That is, all of i ′ are smaller than a. By Lemma 33, we have All in all, Φ(f ) = Φ(g) + 1. Case 2.2. Suppose Inv(f, a) = a + 1. In this case, we have π(f, a) = a + 1, π(g, a) = Inv(g, a) = a and thus π(f ) = π(g) + 1. The fact that b · j 2 contains exactly a + 1 tokens smaller than a implies that i · j 1 contains exactly m − 1 tokens smaller than a. That is, all of i ′ are smaller than a except one token c = max( i ′ ). Therefore, All in all, Φ(f ) = Φ(g) + 1. Case 2.3. Suppose Inv(f, a) > a + 1. In this case, we have π(f, a) = π(g, a) = a + 1 and π(f ) = π(g). The fact that b · j 2 contains at least a + 2 tokens smaller than a implies that i · j 1 contains at most m − 2 tokens smaller than a. That is, the two largest tokens c and d in i ′ are bigger than a. Therefore,  Suppose that the algorithm moves a token a < 0. Then Case 1 of the proof of Lemma 34 applies. We conclude Φ(f ) = Φ(g) + 1.
Therefore, our algorithm gives a solution of Φ(f ) steps, which is optimal by Corollary 38.
Theorem 40. Token swapping on lollipop graphs can be solved in polynomial time.

B Proof that Token Swapping on Star-Path Graphs Is in P
This appendix gives a proof that Algorithm 2 computes an optimal swapping sequence on star-path graphs Q m,n in a manner similar to Appendix A. The number of swaps needed to move non-negative tokens to the goal vertices is evaluated by the same function π. On the other hand, the number of swaps needed to relocate negative tokens is evaluated differently from the case of lollipop graphs. The algorithm involves two types of swaps: the ones in the inner while loop and the others. Let us call the former Type A and the latter Type B. The negative tokens which must be moved are in Among those, some are on a non-negative vertex and some are on a negative vertex. Tokens of the former type will be forced to move to 0 by the moves of non-negative tokens (Type B) and then go to the goal vertex by one step (Type A). Moves of Type B of those tokens are counted by π. On the other hand, tokens i of the latter type form equivalence classes [i] f ⊆ N f , which require [i] f + 1 swaps to be relocated to the goal vertices. Let This value µ(f ) correctly evaluates the number of swaps required to relocate negative tokens in the star graph [15,19]. One might think π(f ) + µ(f ) could be the right evaluation for ts(Q m,n , f ). However, when the vertex 0 is occupied by a negative token i < 0 and the vertex i is occupied by the positive token j which is the largest among the tokens on negative vertices, then the move of i to i (Type A) causes the right move of j to 0, which reduces the number of swaps required to move j to the goal. That is, actually π overestimates the number of swaps for j. We must discount the evaluation from π(f ) + µ(f ). For a pseudo configuration f = ( i; j) = ( i 1 , . . . , i m ; j 1 , . . . , j k ) and c = max( i), define Note that if j 1 < 0, then c ≥ 0. The discount function δ is well-defined, since the sum of the number of the misplaced tokens in i and the length of j decreases by one on the right-hand side in the above definition when c ≥ 0.
Our evaluation function Ψ is given as It is clear that Ψ(f ) = 0 if f is the identity. For a pseudo configuration ( i 1 , . . . , i m ; a), let us define The function γ simulates the while loop of Algorithm 2 in the sense that if the algorithm has ( i; a · j) as the value of f at the beginning of the while loop, it will be ( i ′ ; a ′ · j) when exiting the loop for ( i ′ ; a ′ ) = γ( i; a).
Lemma 41. Let γ( i; a) = ( i ′ ; b). Then Proof Since a < 0 ≤ max( i) and i −a < max( i), we have max( i) = max( i[a/i −a ]). By the induction hypothesis, we obtain the lemma.
Lemma 42. For any i, j, there are an integer α ≤ 0 and a sequence i ′ consisting of the m smallest elements from i · j such that for any k δ( i; j · k) = δ( i ′ ; k) + α provided that ( i; j · k) is a pseudo configuration.
Lemma 43. If i · j 1 contains m or more tokens smaller than k ≥ 0, then Proof. We show the lemma by induction on the definition of δ. Let c = max( i).
If c < 0, then δ( i; j 1 · k · j 2 ) = δ( i; j 1 · j 2 ) = 0. Suppose c ≥ 0. If j 1 is empty, k > c ≥ 0 by the assumption. The equation holds immediately by definition. If j 1 is not empty, the recursive definition of δ gives i ′ and j ′ 1 such that To apply the induction hypothesis, it suffices to show that i ′ · j ′ 1 contains at least m tokens smaller than k ≥ 0. The only non-trivial case is that the number of tokens smaller than k in i ′ · j ′ 1 is smaller than that in i · j 1 . In such a case, for the first element j 0 of j 1 , either j 0 > c ≥ 0 and j 0 < k (j 0 is absent in i ′ · j ′ 1 ) or c > j 0 ≥ 0 and c < k (c is absent in i ′ · j ′ 1 ) holds. In the former case, k > c implies that all the m tokens in i ′ = i are smaller than k.
In the latter case, k > c > j 0 implies all the m tokens in i ′ = i[j 0 /c] are smaller than k.
In particular if i contains negative tokens only, δ(f ) = 0.
Proof. Recall that there exist just k + m tokens smaller than k. The fact Inv(f, k) ≤ k means that j 2 contains at most k tokens smaller than k, so i · j 1 must have at least m such tokens. Lemma 43 applies.

B.1 Ψ Evaluates Our Algorithm
Lemma 45. Suppose that our algorithm changes f to g at a point in the run. Then Ψ(g) = Ψ(f ) − 1.
Proof. We have two types of swaps.

B.2 Ψ Is the Right Evaluation Function
Now we are going to prove that any possible swap on the graph changes the value of Ψ by one. We have 6 cases depending on the signs of swapped tokens and the vertices where the swap takes place. Namely we discuss cases where the tokens are both non-negative (Lemma 49), where one is non-negative and the other is negative (Lemma 50) and where both are negative (Lemma 51). Each case has two subcases depending on whether one of the tokens is on a negative vertex or not. Lemmas 47 and 48 are useful to prove those lemmas.
Lemma 47. Let ( i; j) and ( i ′ ; j) be pseudo configurations such that i contains two distinct non-negative numbers a, b ≥ 0 and i ′ = i[a/b, b/a]. Then Note that j cannot be empty, since i · j is a pseudo configuration.
Proof. It is enough to show that for any a, b ≥ 0, i, j and d, We show this claim by induction on the definition of δ. If d / ∈ {−1, −2}, the proof is trivial. For the symmetry, we discuss the case where d = −1 only. Without loss of generality we assume a < b. Let c = max( i). Case 1. In the case where b > c, we have The claim holds. Case 2. In the case where b < c, The claim follows the induction hypothesis.
Let f = ( i; j) be a pseudo configuration where i contains a negative token a. The a-resolution of i is defined by where k is the least natural number such that either f k+1 (a) = a or f k (a) ≥ 0. That is, we relocate tokens a, f (a), . . . , f k−1 (a) on negative vertices to their respective goals and push f k (a) out to a, which is actually its goal if f k+1 (a) = a. We also call g = ( i ′ ; j) the a-resolution of f . If γ( i; a) = ( j; b) and a < 0, it is easy to see that j is the a-resolutions of i[a/b].
Lemma 48. If g is the a-resolution of f , then δ(f ) = δ(g).
Proof. By induction on the definition of δ.
Proof. Suppose that non-negative tokens a and b are swapped. Obviously µ(g) = µ(f ). We have two cases depending on where those tokens are swapped. Case 1. The swap takes place on two non-negative vertices. That is, for some a, b ≥ 0. Without loss of generality we assume a < b. We have π(f, i) = π(g, i) for all i ∈ {0, . . . , n} − {b}, while Inv(g, b) = Inv(f, b) + 1. By Lemma 42, there exists i ′ consisting of the m smallest tokens from i · j 1 such that In this case, we have π(f, b) = Inv(f, b), π(g, b) = Inv(g, b) and thus π(g) = π(f ) + 1. Corollary 44 applies to both f and g and we obtain and Ψ(g) = Ψ(f ) + 1.
In this case, we have π(g) = π(f ) + 1. Since j 2 contains b tokens smaller than b, on the other hand i · j 1 contains exactly m − 1 tokens smaller than b. Let c be the unique element of i ′ which is bigger than b. Then Hence δ(g) = δ(f ) and Ψ(g) = Ψ(f ) + 1. Case 1.3. Inv(f, b) > b and Inv(g, b) > b + 1. In this case, π(f, b) = π(g, b) = b + 1 and π(g) = π(f ). Since j 2 contains at least b + 1 tokens smaller than b, i · j 1 contains at most m − 2 tokens smaller than b. Hence i ′ contains at least 2 tokens bigger than b. Let c 1 and c 2 be the biggest and second biggest in i ′ , respectively. Then By Lemma 47, we obtain |δ(g) − δ(f )| = 1 and |Ψ(g) − Ψ(f )| = 1. Case 2. The swap takes place between a negative vertex and 0. Without loss of generality we may assume that the negative position is −1 and f (−1) < g(−1).
That is, where 0 ≤ a < b. By definition π(f, a) = a + 1 and π(g, b) = b + 1. Since there are a + m tokens smaller than a, of which at most m − 1 tokens can be in b · i, we have Inv(g, a) ≥ a + 1. That is, π(g, a) = π(f, a). On the other hand, since there are b + m tokens smaller than b, of which at most m tokens can be in a · i, In this case, all the elements of i must be smaller than b. We have δ(f ) = δ(g) = δ(a · i; j) .
Lemma 50. Suppose that g is obtained from f by swapping a non-negative token and a negative one. Then |Ψ(g) − Ψ(f )| = 1.
It is enough to show |δ(g) − δ(f )| = 1. Since j 2 contains at least b + 1 tokens smaller than b, i · j 1 contains at most m − 2 tokens smaller than b. Hence i ′ contains at least 2 tokens bigger than b. Let c 1 and c 2 be the biggest and second biggest in i ′ , respectively. Let ( i ′′ ; d) = γ( i ′ ; a). If d = c 1 , then by Lemma 41, If d = c 2 , then by Lemma 41, Suppose π(f, b) = b, in which case π(g) = π(f ) + 1.
By Lemma 47, |δ(g) − δ(f )| = 1. Case 2. The swap takes place between a negative vertex and 0. Without loss of generality we may assume f = (a · i; b · j), g = (b · i; a · j), where a, b < 0. For the case where a = −1 or b = −1, we have already proved that |Ψ(g) − Ψ(f )| = 1 in Lemma 45. So we assume a, b = −1. Let ( i b ; b ′ ) = γ(a· i; b). There are negative tokens b 0 , . . . , b k < 0 in a· i such that b i = f i (b) < 0 for all i ≤ k and f (b k ) = b ′ ≥ 0. Similarly for ( i a ; a ′ ) = γ(b · i; a), there are negative tokens a 0 , . . . , a l < 0 in b · i such that a i = g i (a) < 0 for all i ≤ l and g(a l ) = a ′ ≥ 0. Let θ a and θ b be replacements [a 0 /a 1 , . . . , a l−1 /a l , a l /a ′ ] and

C Proof that the PPN-Separable 3SAT Is NPhard
We show the NP-hardness of Sep-SAT by a reduction from the (usual) 3SAT [4]. For a given CNF F on X, we may without loss of generality assume that for each x ∈ X, the positive literal x and the negative one ¬x occur exactly the same number of times in F . Otherwise, if x occurs k more times than ¬x does, we add clauses {¬x, y i , ¬y i } to F for all i ∈ {1, . . . , k} where y i are new Boolean variables. Now, for a given CNF F on X = {x 1 , . . . , x m } such that the positive and negative literals x i and ¬x i occur exactly the same number of times for each Boolean variable x i ∈ X, we construct F ′ = F 1 ∪ F 2 ∪ F 3 on X ′ such that • F is satisfiable if and only if F ′ is satisfiable, • each positive literal x i occurs just once in each of F 1 and F 2 , • each negative literal ¬x i occurs just once in F 3 .
Let n i be the number of occurrences of the positive literal x i in F (thus of the negative literal ¬x i ) for each x i ∈ X.