Token Swapping on Trees††thanks: This work was partially supported by NSERC.
The input to the token swapping problem is a graph with vertices , and tokens with labels , one on each vertex. The goal is to get token to vertex for all using a minimum number of swaps, where a swap exchanges the tokens on the endpoints of an edge.
Token swapping on a tree, also known as “sorting with a transposition tree,” is not known to be in P nor NP-complete. We present some partial results:
An optimum swap sequence may need to perform a swap on a leaf vertex that has the correct token (a “happy leaf”), disproving a conjecture of Vaughan.
Any algorithm that fixes happy leaves—as all known approximation algorithms for the problem do—has approximation factor at least . Furthermore, the two best-known 2-approximation algorithms have approximation factor exactly 2.
A generalized problem—weighted coloured token swapping—is NP-complete on trees, but solvable in polynomial time on paths and stars. In this version, tokens and vertices have colours, and colours have weights. The goal is to get every token to a vertex of the same colour, and the cost of a swap is the sum of the weights of the two tokens involved.
- 1 Introduction
- 2.1 Reconfiguration
- 2.2 Permutation groups and Cayley graphs
- 2.3 Token swapping on graphs
- 2.4 Token swapping on trees
- 2.5 Token swapping on paths
- 2.6 Token swapping on stars
- 2.7 Transposition trees and interconnection networks
- 2.8 Happy leaves
- 2.9 Coloured token swapping
- 2.10 More general token and pebble games
- 3 Counterexample to the Happy Leaf Conjecture
- 4 Lower Bounds on Approximation
- 5 Weighted Coloured Token Swapping is NP-complete
- 6 Weighted Coloured Token Swapping
- 7 Token Swapping on Brooms
- 8 Conclusions and Open Questions
Suppose we wish to sort a list of numbers and the only allowable operation is to swap two adjacent elements of the list. It is well known that the number of swaps required is equal to the number of inversions in the list, i.e., the number of pairs that are out of order. Many other problems of sorting with a restricted set of operations have been studied, for example, pancake sorting, where the elementary operation is to flip a prefix of the list; finding the minimum number of pancake flips for a given list was recently proved NP-complete .
A much more general problem arises when we are given a set of generators of a permutation group, and asked to express a given permutation in terms of those generators. Although there is a polynomial time algorithm to test if a permutation can be generated, finding a minimum length generating sequence was proved PSPACE-complete in 1985 .
This paper is about a problem, known recently in the computer science community as token swapping, that is intermediate between sorting a list by swaps and general permutation generation. The input is a graph with vertices . There are tokens, labelled , and one token is placed on each vertex. The goal is to “sort” the tokens, which means getting token on vertex , for all . The only allowable operation is to swap the tokens at the endpoints of an edge, i.e., if is an edge of the graph and token is at and token is at , then we can move token to and token to . See Figure 1. The token swapping problem is to find the minimum number of swaps to sort the tokens. In terms of permutation groups, the generators are the transpositions determined by the graph edges, and the permutation is if token is initially at vertex ; we want a minimum length generating sequence for the permutation.
Our emphasis is on computing the number of swaps, and the actual swap sequence, needed for a given placement of tokens on a graph. Another interesting problem is to bound the worst-case number of swaps for a given graph, where the worst case is taken over all possible token placements. This can be formulated in terms of the Cayley graph, which has a vertex for each possible assignment of tokens to vertices, and an edge when the two token assignments differ by one swap. The worst case number of swaps is equal to the diameter of the Cayley graph. The Cayley graph applies more generally for any permutation group given by a set of generators, where the generators define the edges of the Cayley graph. This is discussed in more detail in Section 2.2.
In the special case when the graph is a path, the token swapping problem is precisely the classic problem of sorting a list using adjacent swaps, see Knuth . Our paper is about token swapping on a tree. This problem is also known as “sorting with a transposition tree,” and is of great interest in the area of sorting networks because the Cayley graph of a star (a tree with one non-leaf) is a good alternative to a hypercube. Akers and Krishnamurthy  first introduced this idea in 1989, and their paper has been cited more than 1400 times according to Google scholar.
Token swapping on general graphs has been studied by different research communities in math, computer science, and engineering, often unaware of each others’ work. We survey all the results we know of in Section 2 below.
The problem of token swapping on graphs was proved NP-complete , and even APX- hard , in 2016, and further hardness results have appeared since then . There are polynomial time algorithms for paths, cliques , cycles , and stars [1, 33, 31], and some other special cases, as discussed in more detail below.
The token swapping problem has been generalized in several ways. In weighted token swapping each token has a positive weight and the cost of swapping token and token is . The goal is to sort the tokens while minimizing the sum of the costs of the swaps. In coloured token swapping [19, 47] the tokens have colours, and we are given an initial and final assignment of coloured tokens to the vertices. Tokens of the same colour are indistinguishable. The goal is to move from the initial to the final token arrangement using the fewest swaps. The original problem is the case where each token has a distinct colour. Coloured token swapping on graphs is NP-hard for 3 colours  but solvable in polynomial time for 2 colours. In weighted coloured token swapping we have coloured tokens and each colour has a weight. Such a weighted colored version has been studied for string rearrangements under various cost models, which allows swapping non-adjacent elements .
1.1 Our results
A leaf in a tree that already has the correct token is called a happy leaf. One feature of all the algorithms for token swapping on trees—both the poly-time algorithms for special cases and the approximation algorithms for the general case—is that they never swap a happy leaf. In 1991 Vaughan  conjectured that an optimal swap sequence never needs to swap a token at a happy leaf. We give a 10-vertex counterexample to this “Happy Leaf Conjecture” in Section 3.
Furthermore, we show in Section 4 that any algorithm that fixes the happy leaves has approximation factor at least , and we show that the two best-known 2-approximation algorithms have approximation factor exactly 2.
These results provide new insight that the difficult aspect of token swapping on trees is knowing when and how to swap happy leaves. Next, we explore whether this difficult aspect can be used to prove NP-hardness. We show in Section 5 that the generalized version of weighted coloured token swapping is NP-complete for trees. Furthermore, we show in Section 6 that this generalized version remains poly-time on paths and stars, which gives further evidence that trees really are harder than paths and stars.
We say that a token is home if it is at its destination. In a tree, homing a token means swapping it along the (unique) path from its current position to its destination.
We defined the token swapping problem as: move token from its initial vertex to vertex , with associated permutation if token is initially at . An alternative formulation is in terms of an initial and final token assignment. Suppose is an initial assignment of tokens to vertices, and is a final assignment of tokens to vertices. The goal then is to move each token from its initial vertex to its final vertex . The associated permutation is . (Our first formulation just eases notation by assuming that .)
A solution to a token swapping problem is a sequence of swaps, . Our convention is that, starting with the initial token assignment, we perform the swaps starting with and ending with to get the tokens to their final positions. Equivalently, performing the transpositions starting with and ending with generates the associated permutation.
This section contains a thorough summary of results on token swapping and related problems.
Problems of turning one configuration into another using a limited repertoire of steps have a long history, for example in the study of puzzles and permutation groups . Recently, the name “reconfiguration” has been applied to these problems—see the recent surveys by van den Heuvel  and Nishimura . Reconfiguration problems can be formulated in terms of a “reconfiguration graph” that has a vertex for each configuration and an edge for each possible reconfiguration step. As discussed below, when the set of moves form a permutation group the reconfiguration graph is the Cayley graph.
The general questions that are considered in reconfiguration problems are: can any configuration be reconfigured to any other (connectivity); what is the worst case number of steps required (diameter); and what is the complexity of computing the minimum number of steps required to get from one given configuration to another given configuration (distance). In this paper, we concentrate on distance questions, although we will mention some previous work on deciding reconfigurability and on diameter. We return to more general reconfiguration problems in the final subsection.
2.2 Permutation groups and Cayley graphs
Given a group , a subset is a generator of , if every element of can be expressed as the product of finitely many elements of and their inverses. Given a group and a generator of , the Cayley graph has the elements of as vertices and any two vertices are adjacent, if there exists an element such that . In our context, we are interested in the Cayley graph of the symmetric group that consists of all permutations of the element set . Given an graph on vertices, we define the generating set as the set of all transpositions corresponding to edges of . The Token Swapping problem corresponds to finding the shortest path in the Cayley graph from a given permutation to the identity permutation. Note that this shortest path corresponds to the minimum length generating sequence of by elements of . The worst case number of swaps corresponds to the diameter of the Cayley graph.
2.3 Token swapping on graphs
Token swapping on a connected graph of vertices takes at most swaps—take a rooted spanning tree and, for vertices in leaf-first order, successively home the token that goes to that vertex, where homing a token means swapping it along the unique path to its final location. This bound is tight for a path with tokens in reverse order. The token swapping problem on graphs (to compute the minimum number of swaps between two given labellings of the graph) is NP-complete, and in fact, APX-complete, as proved by Miltzow et al. . They complemented these hardness results with a polynomial-time 4-approximation algorithm, and an exact exponential time algorithm that is best possible assuming ETH. These results extend to coloured token swapping. Bonnet at al.  showed that token swapping is W-hard parameterized by number of swaps, but fixed parameter tractable for nowhere dense graphs. This result extends to coloured token swapping and even to a further generalization called “subset token swapping”.
There are many special classes of graphs on which token swapping can be solved via exact polynomial time algorithms. These include (in historical order): cliques , paths , cycles , stars [1, 33, 31], brooms [41, 23], complete bipartite graphs , and complete split graphs . See the survey by Kim .
2.4 Token swapping on trees
Various efficient but non-optimal algorithms for token swapping on a tree have been presented in the literature. Most of them are 2-approximations—i.e., they use at most twice the optimum number of swaps—although this was not always noted. Several of the algorithms are expressed in terms of the paths that tokens should take. For any token , there is a unique path from its initial vertex to its final vertex . Let be the length (the number of edges) of the path , and let .
Happy swap algorithm. The earliest algorithm we are aware of is due to Akers and Krishnamurthy in 1989 . Their algorithm involves two operations that we will call a “happy swap” and a “shove.” Let be an edge with token on and token on . A happy swap exchanges and if includes and includes , i.e., the two tokens want to travel in opposite directions across the edge as the first steps in their paths. A shove exchanges and if includes and is home. Akers and Krishnamurthy show that: (1) one of these operations can always be applied; and (2) both operations decrease where is the number of vertices and is the number of cycles in the permutation defined by if token is initially at . Note that if (i.e., is home) this forms a trivial cycle which counts in . Both aspects (1) and (2) of the proof are fairly straightforward. For (2) they prove that a shove does not change but decreases , whereas a happy swap decreases by 2 and changes by at most 1. Their proof implies that is an upper bound on the minimum number of swaps. They do not claim that is at most twice the minimum, but this follows from the easy observation that and is a lower bound on the minimum number of swaps, since a single swap decreases by at most 2.
Miltzow et al.  gave a 4-approximation algorithm for [coloured] token swapping on general graphs. In case the graph is a tree, their algorithm is the same as the one of Akers and Krishnamurthy and they prove that it is a 2-approximation.
Vaughan’s algorithm. Independently of the work by Akers and Krishnamuthy, Vaughan  in 1995 gave an algorithm for token swapping on a tree that uses a number of swaps between and (in her notation is called “PL”). Her algorithm involves three operations: A, a happy swap; B, a version of a happy swap that alters the final token assignment; and C, a variant of a shove. Her operations construct the swap sequence by adding swaps at the beginning and the end of the sequence, whereas the other algorithms construct the sequence from the start only.
Operation B applies when there is an edge and tokens and such that the destination of is and the destination of is and includes and includes , i.e., the two tokens want to travel in opposite directions across the edge as the last steps in their paths. The operation exchanges the final destinations of and , computes a swap sequence for this subproblem, and then the adds the swap of and at the end of the sequence.
Operation C applies in the following situation. Suppose there is an edge with token on and token on , where includes and token is home. Suppose furthermore that there is a token whose destination is and whose path includes . (Note that this is a more restrictive condition than for a shove.) The operation exchanges tokens and and exchanges the final destinations of and . Recursively solve this subproblem. The swap sequence consists of the swap of and , followed by the sequence computed for the subproblem, followed by the swap of and .
Vaughan proves that if operations and do not apply, then operation does, and she proves that each operation decreases the sum of the distances by 2.
Cycle algorithm. The first explicit description of a 2-approximation algorithm for token swapping on trees was given by Yamanaka et al. , who gave an algorithm that sorts the cycles of the permutation one-by-one. Consider a cycle of tokens in the permutation . For their algorithm swaps token along the path from its current vertex to the vertex currently containing token —but stops one short of the destination. Finally, token is swapped from its current vertex to its (original) destination.
We now outline their proof of correctness and the bound on the number of swaps. Suppose that token is currently at vertex and that the first edge it wishes to travel along is . Let be the minimum index, such that wishes to travel in the opposite direction along (and observe that exists). Then the cycle is equivalent to followed by , where the second cycle is empty if . Also, the algorithm performs the same swaps on these two cycles as on the original. Thus it suffices to prove that their algorithm correctly solves the cycle . This cycle has the special feature that no tokens besides and wish to traverse edge . Yamamoto et al. prove that their algorithm “almost” achieves the property that just before step (the step in which moves) tokens are at their final destinations and all other tokens, including the non-cycle tokens, are at their initial positions. “Almost” means that there is the following exception. Let be the vertex containing , and let be the next vertex on the path from to . All the tokens on the path from to are one vertex away from their desired positions—–they should all be one vertex closer to . With this exception, the property is obvious for and and can be proved by induction, which implies that the algorithm is correct. Because tokens are only “off-by-one” it can be argued that the number of swaps performed in step of the algorithm is bounded by the original distance from to its destination. This implies that the total number of swaps is at most the sum of the distances of labels in the cycle, which gives the factor 2 approximation.
Comparisons. None of the algorithms will swap a token at a happy leaf, so there is an instance (see Section 3) where the algorithms are not optimal. The three algorithms differ in how far they allow a token to stray from its path . In the Happy Swap algorithm no token leaves the set of vertices consisting of its path together with the vertices at distance 1 from its destination. In the Cycle algorithm, no token moves more than distance 1 from its path. In Vaughan’s algorithm, a vertex may go further away from its path.
2.5 Token swapping on paths
Token swapping on a path is the classic problem of sorting a list by transposing adjacent pairs. See Knuth [25, Section 5.2.2]. The minimum number of swaps is the number of inversions in the list. Curiously, a swap that decreases the number of inversions need not be a happy swap or a shove (as described above) and, on the other hand, there does not seem to be any measure analogous to the number of inversions that applies to trees more generally, or even to stars.
2.6 Token swapping on stars
A star is a tree with one non-leaf vertex, called the center vertex. We will need the following known result about token swapping on a star, which expresses the number of swaps as a function of the number of cycles in the permutation . The formula is often written with a delta term whose value depends on whether the center vertex is happy or not, but we will express it more compactly.
The optimum number of swaps to sort an initial placement of tokens on a star is , where is the number of unhappy leaves and is the number of cycles in the permutation that have length at least 2 and do not involve the center vertex.
Consider a cycle of length at least 2 in the permutation of tokens and consider the corresponding vertices of the star. If the center vertex is not in then the number of swaps to sort is its number of leaves plus one. If the center vertex is in then the number of swaps is the number of leaves in . Because the cycles are independent, we can sum over all non-trivial cycles, which yields the stated formula. ∎
It follows that the diameter of the Cayley graph for a star is , which arises when all cycles have length 2. Further properties of Cayley graphs of stars were explored by Qiu et al. . Portier and Vaughan  analyzed the number of vertices of the Cayley graph at each distance from the distinguished “sorted” source vertex (see also ). Pak  gave a formula for the number of shortest paths between two vertices of the Cayley graph.
2.7 Transposition trees and interconnection networks
The network community’s interest in token swapping on trees (“transposition trees”) stems from the use of the corresponding Cayley graphs as interconnection networks, an idea first explored by Akers and Krishnamurthy . Cayley graphs of transposition trees have the following desirable properties: they are large graphs ( vertices) that are vertex symmetric, with small degree (), large connectivity (the same as the degree), and small diameter. In particular, the diameter is when the tree is a star. The commonly used hypercube has vertices and diameter , so the diameter is logarithmic in the size. By contrast, the Cayley graph of a star has sublogarithmic diameter.
Akers and Krishnamurthy proved a bound on the diameter of the Cayley graph of a transposition tree, specifically, the maximum over all permutations of the bound which was discussed above. This bound cannot be computed efficiently since it involves the maximum over permutations. Vaughan  also gave upper and lower bounds on the diameter of the Cayley graph, though neither easy to state nor to prove.
Follow-up papers by Ganesan , Chitturi  and Kraft  have lowered the diameter bound and/or the time required to compute the bound. To give a flavour of the results, we mention a polynomial-time computable upper bound, , due to Chitturi  that is defined recursively as follows: if the tree is a star, use the known diameter bound; otherwise choose a vertex that maximizes the sum of the distances to the other vertices, increase by the maximum distance from to another vertex and recurse on the smaller tree formed by removing the leaf .
2.8 Happy leaves
As mentioned in the introduction above, Vaughan  conjectured that a happy leaf in a tree need not be swapped in an optimal swap sequence. In fact she made a stronger conjecture [40, Conjecture 1] that if a tree has an edge such that no token wishes to cross (i.e., no path from a token to its destination includes edge ) then there is an optimal swap sequence in which no token swaps across . The Happy Leaf Conjecture is the special case where is a leaf.
Smith [37, Theorem 9] claimed something stronger than the happy leaf conjecture: that no optimal swap sequence would ever swap a happy leaf. But later he found an error in the proof , and gave an example of a small tree where there is an optimal swap sequence that performs a swap on a happy leaf. In his example, there is also an optimal swap sequence that does not swap the happy leaf so he did not disprove the happy leaf conjecture.
2.9 Coloured token swapping
Many natural reconfiguration problems involve “coloured” elements, where two elements of the same colour are indistinguishable. Token swapping for coloured tokens was considered by Yamanaka et al.  (journal version ). They proved that the coloured token swapping problem is NP-complete for colours even for planar bipartite graphs of max degree 3, but for the problem is solvable in polynomial time, and in linear time for trees. On complete graphs, coloured token swapping is NP-complete  but fixed parameter tractable in the number of colours . The complexity of coloured token swapping on trees is open.
2.10 More general token and pebble games
There are many problems similar to token swapping, some of which were studied long ago. We mention some results but do not attempt a complete survey.
The classic 15-puzzle (see [12, 39] for surveys) is a version of token swapping on a grid graph where only one special token (the “hole”) can be swapped. As a consequence, only half the token configurations (the alternating group) can be reached. Generalizing beyond the grid to general graphs, Wilson  in 1974 gave a complete characterization of which token configurations on which graphs can be reached via this more limited set of moves. Minimizing the number of moves is NP-complete  even for grid graphs . Recently, a version with coloured tokens has also been considered .
Several papers explore generalizations where there is more than one “hole”. As in the 15-puzzle, a swap must involve a hole. The tokens may be labelled or unlabelled. For labelled tokens, Kornhauser et al.  in 1984 gave a polynomial time algorithm to decide if reconfiguration between two label placements is possible, and proved a tight bound of on the diameter of the associated Cayley graph.
For unlabelled tokens and holes Auletta et al.  gave a linear time algorithm to decide reconfigurability on any tree, and this was generalized to linear time for any graph (even for coloured tokens) by Goraly and Hassin . Călinescu et al.  gave a polynomial time algorithm to minimize the number of swaps for any graph, but showed that the problem becomes APX-hard if the objective is to minimize the number of times a token moves along a path of holes. Fabila-Monroy et al.  showed that the diameter of the Cayley graph (which they call the “token graph”) is at most times the diameter of the original graph.
Papadimitriou et al.  considered a “motion planning” version where all the tokens are unlabelled “obstacles” except for one labelled “robot” token which must move from a start vertex to a destination vertex; as before, a swap must involve a hole. They showed that minimizing the number of swaps is NP-complete for planar graphs but solvable in polynomial time for trees. The run time for trees was improved in .
A unifying framework is to replace holes by labelled or coloured tokens that are “privileged,” and to require that every swap must involve a privileged token.
In another variant, token movement must be carried out by a single robot walking along the graph edges and carrying at most one token at a time. Graf  includes a good summary.
Rather than token swapping across an edge, an alternative motion is rotation of tokens around a simple cycle in the graph. This is of interest in the robotics community since it models movement of robots with the restriction that no two robots can travel along the same edge at the same time. When all cycles in a graph may be used, there are polynomial time algorithms to decide if reconfiguration is possible [53, 15]. See also  for hardness of optimization and  for practical approaches. If rotation is only allowed around the cycles of a cycle basis (e.g., the faces of a planar graph) Scherphuis  provided a characterization (similar to Wilson’s for the 15-puzzle generalization) of which graph/cycle-basis/token- placement combinations permit reconfiguration (see also Yang , who showed that Wilson’s result reduces to this result).
Although the term “pebbling” is sometimes used for token swapping, the more standard distinction is that pebbling games are played on directed acyclic graphs and involve rules to add and remove pebbles, modelling memory usages or dependencies in computation. Nordström  wrote a thorough survey of pebbling games and their relevance to complexity theory.
3 Counterexample to the Happy Leaf Conjecture
In this section we disprove the Happy Leaf Conjecture by giving a tree with initial and final token placements such that any optimal swap sequence must swap a token on a happy leaf (recall that a happy leaf is one that has the correct token). Our counterexample has vertices and is shown in Figure 2(a). This is the smallest possible counterexample—we have verified by computer search that all trees on less than 10 vertices satisfy the Happy Leaf Conjecture. Our counterexample can easily be generalized to larger , and we give more counterexamples in the next section. Our tree consists of a path and one extra leaf joined by an edge to vertex . The initial token placement has token 10 at (so is a happy leaf) and tokens in that order along the path .
If token 10 does not leave vertex (i.e., we fix the happy leaf), then we must reverse the order of the tokens on a path of length 9, which takes swaps. However, as we now show, there is a swap sequence of length 34.
Initially, we perform 3 swaps to move token 10 to giving the configuration shown in Figure 2(b). Next, we ignore leaf and perform the sequence of swaps that homes tokens . The result of homing tokens 9 and 8 is shown in Figure 2(c), and the result of homing all of them is shown in Figure 2(d). It is easy to verify that this takes 7 swaps for token 9, 6 for token 8, , 2 for token 4, which adds up to swaps.
Finally, we perform the following swaps to complete the sort: home token 10 in 3 swaps, then home token 1 in 1 swap. In total, this uses swaps.
The idea of why this saves swaps is as follows. To reverse the order of edges on a path, every token must swap with every other token. By contrast, the above approach saves swaps whenever two tokens occupy vertices and . For example, tokens 8 and 7 never swap with each other, nor do 7 and 6, etc. We need so that this saving exceeds the cost of the initial set-up and final clean-up.
4 Lower Bounds on Approximation
In this section we prove that if an algorithm for token swapping on a tree never swaps a token on a happy leaf, then it has worst case approximation factor at least . In Section 2.4 we described three 2-approximation algorithms for token swapping on a tree: the Happy Swap algorithm, Vaughan’s algorithm, and the Cycle algorithm. All three algorithms fix the happy leaves, so the lower bound applies. For the Happy Swap algorithm and the Cycle algorithm we improve the approximation lower bound to 2. To do this we use further properties of those algorithms, in particular, constraints on how far a token may stray from the path between its initial and final positions. It is an open question whether Vaughan’s algorithm has an approximation factor better than 2.
Any algorithm that does not move tokens at happy leaves has an approximation factor of at least .
Define a tree to have a path of vertices, together with a set of leaves adjacent to the center vertex, , of the path. The tokens at and the vertices of are already home, i.e., they have the same initial and final positions. The tokens on the path should be reversed, i.e., the token initially at vertex of the path has final position and vice versa. See Figure 3 for an illustration.
Any algorithm that fixes happy leaves must reverse the path which takes swaps.
We now describe a better swap sequence . Let denote the token at vertex , let denote the tokens initially at , let denote the tokens initially at vertices of , and let denote the tokens initially at . The plan is to perform the following 4 steps: (1) exchange the tokens with the tokens ; (2) exchange with ; (3) exchange with ; and (4) adjust the order of tokens .
For clarity of explanation, we will implement each step in a way that is suboptimal but efficient enough for our purposes. For step (1), we implement the exchange of the two token sets as follows: first move token to using swaps, then for move token to vertex using swaps, and finally, move token from back to using swaps. This places tokens on the first half of the path in the correct order ( on ), and tokens on vertices in reversed order ( on ). The total number of swaps is .
Step (2) acts similarly on and , after which tokens are home, and tokens are on vertices in the correct order ( on ). Finally, after step (3), tokens of and are home, and tokens are on but in reverse order. For step (4) we must sort the tokens on which means solving a tree swapping problem on a star of leaves, where the permutation consists of cycles each of length 2 ( must be exchanged with ). Each cycle takes 3 swaps for a total of swaps.
The total number of swaps used is . This is an upper bound on the optimum number of swaps. The ratio of number of swaps used by the algorithm to optimum number of swaps is at least . ∎
The Happy Swap and the Cycle algorithms do not have an approximation factor less than .
Our example generalizes the one in the preceding proof to have paths of vertices rather than two paths. For every and every odd we define a tree together with initial and final token placements. The tree consists of paths of vertices all joined to a center vertex . Additionally, there is a set of leaf vertices adjacent to . See Figure 4 for an illustration.
We denote the paths of vertices by respectively. Let be the tokens initially on path , let be the tokens initially on , and let be the token on .
In the final token placement the tokens are on path , indices modulo . Each token in should have the same distance to in the initial and final token placements. The tokens and should stay where they are.
Idea of the proof.
Any solution for token swapping on this input has to accomplish two tasks. Each set of tokens has to be moved from its initial path to its final path . This takes roughly swaps, as tokens have to move steps. But each those swaps could accomplish something else for another token, so only half of those swaps should be charged to this task. The other task is to reverse the order of each set . Now, we know that this takes swaps on a path. But if we can use the happy leaves, this can be done with swaps and thus is asymptotically free.
More precisely, we will first give a clever solution using happy leaves to show that the optimum numer of swaps, , satisfies . Then we will argue that the number of swaps, , used by either algorithm is at least .
To complete the proof, observe that for , and . Then for every there is a such that . This shows that the algorithms do not have an approximation factor better than 2.
We now fill in the details of the clever solution, and the analyses of the algorithms.
A Clever Solution.
We describe a procedure to swap every token to its target position using only swaps. See Figure 5 for an illustration.
As a first step, exchange the tokens with the tokens . The exchange is implemented as described in the proof of Theorem 2 and takes swaps. In the second step, exchange the tokens with the tokens . This places the tokens of on path in the correct order, and again takes swaps. In general, the step exchanges tokens (currently on the vertex set ) with the tokens , moving onto the path in the correct order. In the step, tokens are exchanged with tokens , moving onto the path . If is odd, the tokens will end up in the correct order on , and the total number of swaps is .
Behavior of the Happy Swap algorithm.
We will prove that the Happy Swap algorithm uses at least swaps. The tokens of must move from path to path . Suppose has vertices where is adjacent to . Suppose has vertices where is adjacent to . Let be the token whose initial vertex is and whose final vertex is . The path from initial to final vertex is , and its length is . We note the following properties:
(*) Until reaches , happy swaps and shoves can only move along its path. Once reaches , a shove can move to or to , after which it must move back to .
This implies, in particular, that cannot leave .
On the long path the tokens must reverse their order. Because none of these tokens leave the long path, the algorithm must perform a swap for each of the inversions. The algorithm must also perform some swaps to move tokens from to . We need a way to count these separately because a single swap can make progress towards both goals. We will use a charging argument.
Each swap involves two tokens, and pays to each of those two tokens. The number of swaps is the sum, over all tokens, of the payments to that token. Now we will charge the work performed to the tokens. Each of the edges of ’s path charges to token . Each inversion , charges 1 to . The amount charged to is .
We will prove below that for every token , the payment is at least the charge. This implies that the total number of swaps is at least since there are tokens.
The payments to are at least the charges to .
The first time reaches its destination, it has been paid for each edge on its path. This balances the charges for the path.
It remains to account for the inversions charged to . Consider an inversion , . Some swap must change the order of these tokens in the path , and, in this swap, moves in a direction opposite to the direction of its path from initial to final vertex. Thus, by property (*), this swap can only happen after has reached its destination. In particular, there are two possibilities for this swap: (1) token is home at and token is at and a shove exchanges them; or (2) token is at and token is at and a happy swap exchanges them. In case (1), there must be a subsequent swap where returns to its home. In case (2) there must have been a previous swap where was shoved from its home to . In both cases the extra swap moves in the direction of its path, and cannot fix an inversion. Thus in both cases, there are two swaps to pay for the inversion. ∎
Behavior of the Cycle algorithm.
We will prove that the Cycle algorithm uses at least swaps. The Cycle algorithm solves the cycles independently. Each non-trivial cycle has tokens, one on each path. Consider the action of the algorithm on the cycle of tokens at distance from . Let be the token on path at distance from . Suppose without loss of generality that the algorithm moves first. It swaps along to the center vertex , which takes swaps, and then along from the center vertex to the vertex just before the one containing , which takes swaps. The positions of the other tokens on the cycle have not changed. Thus, each takes swaps, and finally, takes swaps. The total over all cycles is . ∎
5 Weighted Coloured Token Swapping is NP-complete
In the coloured token swapping problem, we have coloured tokens, one on each vertex of the graph, and each vertex has a colour. The goal is to perform a minimum number of swaps to get each token to a vertex that matches its colour. We assume that the number of tokens of each colour is equal to the number of vertices of that colour. The standard token swapping problem is the special case when all colours are distinct.
In the weighted coloured token swapping problem, each colour has a weight , and the cost of swapping tokens of colours and is . The goal is to reach the target configuration with minimum total cost.
In this section we prove that weighted coloured token swapping is NP-complete. We reduce from Vertex Cover by constructing a long path with some ‘green’ tokens initially at the right end of the path. The final configuration has green tokens at the left end of the path. We can save the cost of moving all those green tokens the whole length of the path by dislodging some happy green tokens from a subtree that dangles off the path; we construct this dangling subtree from the vertex cover instance in such a way that there is a cost savings if and only if there is a small vertex cover.
Weighted coloured token swapping on trees is NP-complete.
We will reduce the NP-complete problem vertex cover  to weighted coloured token swapping on trees. Let the input to vertex cover be a graph with vertices, and a number . The problem is whether has a vertex cover of size at most .
We first construct a tree with root and children of the root corresponding to the vertices of , e.g., see Figures 6(a)-(b). We will refer to these as the -vertices of . For each edge of , we create two vertices in . We add one of them as a child of the -vertex corresponding to and the other as a child of the -vertex corresponding to . We will refer to these new leaf vertices as the -vertices of .
We place tokens on as follows. We place a darkgray token on each -vertex. We set the weight of the darkgray tokens to be . On the root of we place a blue token with weight 1. We assign each of the two -vertices the colour , and set the weight to . In the schematic representation (see Figure 6(c)) the tokens of -vertices are shown in green, but note that they are in fact a collection of different colours. We will refer to these colours as edge-colours.
We then construct a path of length such that
the first vertices have red tokens of weight 1,
the next vertices have blue tokens,
the next vertices have tokens of edge-colours,
Let , and identify the root of with the vertex of the path. Let be the resulting tree (see Figure 6(d)).
We now define the initial and final token configurations on as illustrated in Figures 6(d) and (g), respectively (ignore the intermediate figures for now). Note that the vertices that originally belonged to are ‘happy’ vertices.
Let and . In the following we show that has a vertex cover of size if and only if the weighted coloured token swapping problem on has a solution of cost at most .
From Vertex cover to Weighted coloured Token Swapping:
Assume that has a vertex cover of size . We reach the target configuration as follows:
- Fig. 6(d)–(e):
Move the darkgray tokens from those -vertices that correspond to the vertex cover to some adjacent leaves. Since each edge of is incident to a vertex in the vertex cover, we can swap distinct edge-coloured tokens with blue tokens on the path. This costs for moving the darkgray tokens, and for swapping the edge-coloured tokens with the blue tokens.
- Fig. 6(e)–(f):
Move the red tokens all the way to the right end of . This costs .
- Fig. 6(f)–(g):
Swap the edge-coloured tokens with the blue tokens that are on the - and -vertices, which costs . Finally, move the darkgray tokens back to the -vertices, which costs .
From Weighted coloured Token Swapping to Vertex cover:
Assume that the weighted coloured token swapping problem on has a solution of cost at most . We now show how to find a vertex cover of size at most .
Observe first that we need to pay at least a cost of for moving the red tokens to the right end of .
If does not move the tokens of - and -vertices, then we still need to move the green tokens, i.e., the edge-coloured tokens, to the left end of . If we move any such green token to one of the leftmost positions of , then would require at least a cost of . Since (for every ), we have . Consequently, all the green tokens that are to the left of in the final configuration must come from the -vertices. Since the darkgray tokens are ‘blocking’ the -vertices, must have moved some of these darkgray tokens.
We now claim that cannot move more than darkgray tokens. If moves or more darkgray tokens, then the cost would be at least . Since (for every ), we will have a cost of more than .
Let be vertices of that correspond to the darkgray tokens that have been moved by . It now suffices to show that is a vertex cover. Suppose for a contradiction that there is an edge that has not been covered. Then no edge-coloured token with colour can move from an -vertex to the left of in the final configuration. Thus this edge-coloured token must come from the green block that was initially on , which implies a cost of at least , which is strictly larger than . This completes the NP-hardness proof. ∎
6 Weighted Coloured Token Swapping
In this section we give polynomial-time algorithms for weighted coloured token swapping on paths and stars. Recall from the previous section our convention that we have coloured tokens and coloured vertices, with one token at each vertex, and with the number of tokens of each colour equal to the number of vertices of that colour. The goal is to perform swaps to get each token to a vertex that matches its colour. Each colour has a weight and the cost (or weight) of performing a swap on two tokens of colour and is . The objective is to minimize the total cost (weight) of the swaps. Note that standard token swapping is the special case where all the colours are distinct and all the weights are , since each swap moves two tokens.
The main issue in [weighted] coloured token swapping is to decide which token should go to which vertex. After fixing such a “token-vertex assignmen” the problem becomes [weighted] token swapping without colours. In some situations—including for paths and stars—it turns out that the optimum token-vertex assignment does not depend on the weights. In these situations we can combine an algorithm for coloured token swapping and an algorithm for weighted token swapping to obtain an algorithm for weighted coloured token swapping.
Such a separation of colours and weights does not hold for trees in general, as the NP-hardness proof in the previous section shows. However, when the number of colours is 2, the weights and colours do separate—we should never swap two tokens of the same colour, and therefore every swap costs where and are the two colours. This means that, for 2 colours, weighted coloured token swapping is no harder than coloured token swapping. Yamanaka et al.  gave a polynomial-time algorithm for 2-coloured token swapping on general graphs. Thus, weighted 2-coloured token swapping can also be solved in polynomial time.
Our main result in this section is an algorithm for weighted coloured token swapping on stars. Before that, we give a brief solution for paths.
6.1 Weighted coloured token swapping on paths
As mentioned above, we should never swap two tokens of the same colour. As noted by Yamanaka et al. , for the case of paths, this constraint imposes a unique assignment of tokens to vertices: the token of colour along the path must be assigned to the the vertex of colour .
It remains to solve the weighted token swapping problem on paths. As in the unweighted case, the required swaps correspond precisely to the inversions, i.e., the pairs of tokens whose order in the initial token placement differs from their order in the final token placement. The minimum weight of a swap sequence is the sum, over all inversions of .
6.2 Weighted coloured token swapping on stars
In this section we give a polynomial time algorithm for the weighted coloured token swapping problem on a star. As announced above, we will show that weights and colours can be dealt with separately.
6.2.1 Weighted token swapping on a star
In this subsection we assume that every token has a distinct colour so we know exactly which vertex every token must move to. Each token has a weight and the cost of swapping tokens and is . Let and be the sets of tokens initially on the happy and unhappy leaves, respectively. Let , the set of active tokens, be all tokens except those in , i.e., is plus the token at the center vertex.
In the token permutation, the cycle that contains the token at the center vertex of the star will be called the unlocked cycle, and all other cycles will be called locked cycles. Using this terminology, Lemma 1 states that the optimum number of swaps to solve the unweighted token swapping problem is , where and is the number of non-trivial locked cycles. The intuition for the lemma, and the reason for our terminology, is that every locked cycle must be ‘unlocked’ by an external token, introducing one extra swap per locked cycle.
The number of swaps performed in the weighted case must be at least and we will show that an optimum solution uses either this lower bound or two extra swaps. The idea is the following: each of the locked cycles must be unlocked by some other token, and we want to use the cheapest possible token for this. Either we will use an active token and perform swaps, or we will introduce two extra swaps that bring and return a globally cheapest token from an initially happy leaf to the star center and use this token to unlock all the locked cycles.
Notation. The following notation will be used throughout Section 6.
Let be the unlocked cycle. Let be a minimum weight token in , be a minimum weight token in , and be a minimum weight token in ( might not exist if there are no happy leaves). Observe that . As above, let denote the number of non-trivial locked cycles in the input token permutation. Finally, let be the distance of token from its home and let . Observe that is a lower bound on the cost of weighted token swapping.
Before presenting the algorithm, we give an alternative formula for . We will use this in the forthcoming section on weighted coloured stars. Also, it implies that in the case of unit weights, , which will clarify how the present algorithm generalizes the unweighted case. For vertex , recall our notation that is the initial token at , and is the final token at . Thus, a leaf vertex is happy if and only if .
is an unhappy leaf.
If is an unhomed token whose initial and final vertices are both leaves, then it contributes to both sides of the equation. If is a token whose initial vertex is the center vertex and whose final vertex is a leaf, then it contributes to both sides. Similarly, a token whose initial vertex is a leaf and whose final vertex is the center, contributes to both sides. Finally, a token that is home contributes 0 to both sides. ∎
When the weights are all 1, .
We now describe the algorithm for weighted token swapping on a star. The algorithm uses the best of three possible strategies, all of which begin the same way:
Stategy 1. Begin solving the unlocked cycle by repeatedly swapping the token from the star center to its home until the token is on the star center. Next, use to unlock and solve all the locked cycles. Finally, complete solving . The total weight is .
Strategy 2. This strategy only applies when , in which case . Begin solving the unlocked cycle by repeatedly swapping the token from the star center to its home until the token is on the star center. Then swap with . Suppose was in the locked cycle . Use to unlock and solve all the other locked cycles, leaving tokens of and fixed. Then use to solve cycle , which will return to the center token. Finally, complete solving . The effect is that one locked cycle is unlocked by at a cost of and cycles are unlocked by at a cost of , for a total cost of .
Strategy 3. This strategy only applies when exists. Begin solving the unlocked cycle by repeatedly swapping the token from the star center to its home until the token is on the star center. Then swap with . Use to unlock and solve all the locked cycles, leaving tokens of fixed. Then swap and . Finally, complete solving . The total weight is
To decide between the strategies we find the minimum of , , and use the corresponding strategy 1, 2, or 3, respectively, achieving a total weight of .
The above algorithm finds a minimum weight swap sequence and the weight of the swap sequence is:
Observe that in the case of unit weights, by Corollary 7, so the theorem says that the minimum number of token moves is , i.e., the number of swaps is , which matches what we know for the unweighted case.
To prove the theorem, we will need the following result about the unweighted star.
Any swap sequence on an unweighted star that moves a happy leaf does at least two more swaps than an optimal swap sequence.
By Lemma 1, solving the unweighted problem on a star optimally takes swaps, where is the number of unhappy leaves and is the number of non-trivial locked cycles. It suffices to check that after swapping a happy token with the center token the value given by the formula is increased by one. Indeed, the number of non-trivial locked cycles stays the same and the number of unhomed leaves increases by one, hence, the net change is +1. ∎
We now prove Theorem 8.
The swap sequence found by the algorithm realizes the formula given in the theorem. It remains to show that the formula provides a lower bound on the weight of any swap sequence.
To reach its home, each token must contribute weight at least , for a total over all tokens of . If then is empty so and the formula evaluates to , which is a lower bound. Assume from now on that . In addition to the moves accounted for in , there must be at least other token moves, two for each locked cycle. Furthermore, there must be a first move that swaps some token of with a token outside . This swap can only happen when is at the center vertex. Since will then be unhomed, there must be a move that returns token back to the center vertex. The minimum weight for each of these moves is , and this provides the term in the lower bound. Subtracting these two moves from the required moves leaves moves still to be accounted for.
We now consider two cases depending whether a token of is moved or not. If no token of is moved in the swap sequence, then the best we can do for the remaining moves is to use a minimum weight token from , so the weight is at least .
Next, consider swap sequences that move a token of . By Lemma 9, the sequence must do at least two extra swaps, i.e., at least 4 extra token moves. Thus the number of moves (beyond those for ) is at least . As argued above, we need two moves for a token of , costing at least each. We also need two moves for a token of (to leave its home and then return) costing at least each. This leaves further moves. This gives weight at least . If then this lower bound is higher than the previous ones and becomes irrelevant. If , then the bound becomes .
Thus, combining these possibilities, we have a lower bound of , which completes the proof. ∎
6.2.2 Coloured token swapping on a star
Recall that in the coloured token swapping problem, tokens and vertices are assigned colours, possibly with multiple tokens and vertices sharing the same colour, and the aim is to move the tokens to vertices of corresponding colours using a minimum number of swaps.
Our algorithm will find a token-vertex assignment that maps each token of colour to a vertex of colour , with the interpretation that token should move to vertex in the token swapping problem. Such an assignment yields a standard token-swapping problem which, by Lemma 1, requires swaps, where is the number of unhappy leaves and is the number of non-trivial locked cycles. Thus, we want a token-vertex assignment that minimizes . Note that minimizing is the same as maximizing , the number of happy leaves since , where is the number of vertices in the star.
We will find an optimum token-vertex assignment using an auxiliary multigraph that has a vertex for each colour and one edge for each vertex of the star: for a vertex of colour with an initial token of colour , add a directed edge from to in . In case this edge is a loop. See Figure 7 for an example.
Let be the number of leaf loops of —loops corresponding to leaf vertices of the star. Any leaf loop corresponding to leaf vertex and token can be turned into a happy leaf by assigning token to vertex . This maximizes the number of happy leaves, .
In the input, the number of vertices of colour is equal to the number of tokens of colour . Thus, each vertex of has in-degree equal to out-degree, which implies that any connected component in is strongly connected and has a directed Eulerian tour. We call a connected component trivial if it has one vertex. Let be the number of non-trivial connected components of not counting the component that contains the edge corresponding to the center vertex of the star.
The algorithm for coloured token swapping on a star is as follows:
Find a token-vertex assignment:
Construct the multigraph .
For each of the leaf-loops, assign its token to its vertex.
Remove the leaf-loops from to obtain . Observe that is unchanged, and is still Eulerian. For each connected component of find an Eulerian tour that traverses all the edges of the component. Convert each Eulerian tour to a token-vertex assignment as follows: Suppose the edges of the tour are labelled by vertices (we are freely re-labelling vertices to ease the notation), and suppose that the edge of labelled goes from colour to colour (subscript addition modulo ). Then vertex has colour and the colour of its initial token, say , is . The next edge in the tour corresponds to vertex of colour . Assign token to vertex . Note that both have colour . This assignment is well-defined since the edges of the walk correspond to distinct vertices with distinct initial tokens. Note that this token-vertex assignment introduces a cycle in the corresponding token permutation.
Solve the (un-coloured) token swapping problem determined by the computed token- vertex assignment.
This algorithm produces a token-vertex assignment with happy leaves, and non-trivial locked cycles, one for each non-trivial connected component of except the component that contains the edge corresponding to the center vertex of the star. In other words, , and . Thus the number of swaps is by Lemma 1.
Our goal in the remainder of this subsection is to prove that the algorithm uses the minimum number of swaps which means showing that any token-vertex assignment results in at least swaps. The following lemma will help us do that. (Note: the third statement in the lemma will be useful in the next section.)
Any token-vertex assignment has the following properties:
has at most happy leaves.
has at least non-trivial locked cycles.
If has happy leaves then the tokens in the unlocked cycle of are a subset of , where is the set of tokens that are in the unlocked cycle resulting from the above algorithm.
1. Happy leaves only arise from leaf loops so has at most happy leaves.
2. The token permutation corresponding to can be expressed as a set of cycles. We claim that each cycle corresponds to a closed walk of the same size in and that every edge of is in for some . This will prove property 2, because it implies that we need at least one cycle for each connected component in , and more precisely, that we need at least one non-trivial locked cycle for each of the components counted in .
Consider an edge of , say the edge corresponding to the vertex whose initial token is . Token appears in some cycle , say . (We are freely re-naming tokens, vertices, and colours in this proof.) Suppose token has colour and is initially at vertex . Then the cycle moves token to vertex (subscript addition modulo ). Since the token-vertex assignment respects the colours, vertex has colour . Also, vertex has initial token of colour . Thus there is a corresponding edge in . Therefore, the cycle corresponds to a closed walk in . Also, this closed walk uses the edge we began with, the one whose initial token is .
3. The unlocked cycle of is the one that contains the token initially on the center vertex . By the argument above, the tokens in the unlocked cycle must come from the connected component of that contains the edge labelled with . This set of tokens consists of together with some tokens of leaf-loops. But if has happy leaves, then all the leaf-loops have been turned into happy leaves, so the set of tokens is reduced to . Thus, the tokens of the unlocked cycle are a subset of . ∎
We are now ready to prove that the algorithm is optimal:
The above algorithm uses swaps and this is the minimum possible.
6.2.3 Weighted coloured token swapping on a star
Our algorithm for weighted coloured token swapping on a star is as follows:
Ignore the weights and find a token-vertex assignment as in Step 1 of the algorithm in the previous section.
Using this token-vertex assignment and the original token weights, run the algorithm for the (uncoloured) weighted star.
In order to show that this algorithm is correct, we will first show that any optimum token-vertex assignment must turn all leaf-loops into happy leaves. After that we only need to compare the solution found by the algorithm to solutions with this property.
Suppose is a token-vertex assignment and there is a leaf-loop consisting of a leaf vertex with token such that both and have colour , but the token-vertex assignment does not assign to . Then is not optimum for the weighted problem.
By Theorem 8, the cost of is
where , and depend on . We will construct a new token-vertex assignment that assigns to and has .
Since is not assigned to , must be part of some non-trivial cycle in the token permutation determined by . Suppose that the cycle contains tokens in that order (possibly ), with initial vertices , respectively. Define a new token-vertex assignment that assigns to , i.e., becomes a happy leaf, and shortcuts the rest of by assigning token to vertex . This is valid because token and vertex both have colour , the same as . The new cycle is formed by deleting from . We will compare and by looking at the quantities and .
First of all, no leaf becomes unhappy, so no token leaves and does not increase. Furthermore, becomes happy, so by Claim 6, decreases by at least .
Next we show that does not increase. That would only happen if leaves the set . Then must be the unlocked cycle. Since is at a leaf vertex, the token from the center vertex remains in , so is the new unlocked cycle. Furthermore, token , which is a ‘twin’ of in the sense that it has the same colour and weight, remains in , so remains the same.
Finally we must consider and . Here we will separate out one special case—when and exchanges two leaf tokens, in which case becomes a trivial locked cycle. If we are not in the special case then either is a non-trivial locked cycle, or is the unlocked cycle. In either case has the same status, so is unchanged and ’s twin remains in the active set so does not increase. Thus when we are not in the special case.
It remains to consider the special case when exchanges two leaf tokens. Then was a non-trivial locked cycle, but is a trivial locked cycle. Thus decreases by 1. Furthermore, by Claim 6, decreases by at least since two leaves become happy. If does not increase then we are fine. If it does increase then and was the minimum weight element in . Because we are in the special case, both and its twin token have left and joined . If is determined by we are again fine. Hence we only need to provide an additional argument if . Since we now have a token of weight in , Strategy 3 gives a swap sequence for of weight at most . Thus even in the special case.
With this claim in hand, we are ready to prove that the algorithm is correct.
The above algorithm solves the weighted coloured token swapping problem on a star optimally.
By Theorem 8, the cost of a token-vertex assignment