Token swapping is a graph problem where each vertex has exactly one token, and each token has a target vertex. A move swaps the tokens on the two endpoints of an edge.
The goal is to find a sequence of adjacent swaps that transforms the initial token placement into the target placement. The optimization version asks for as few swaps as possible.
This model appears in routing, scheduling, reconfiguration, and puzzle-like movement problems where objects can only exchange places along allowed connections.
What is token swapping?
Token swapping takes a graph, an initial token placement, and a target token placement. Each vertex contains one token. During each step, you choose an edge and swap the two tokens on that edge.
A solution is valid when every token ends on its target vertex. A shorter solution is usually better because it means fewer adjacent exchanges.
NpSolv returns a candidate swap sequence and lets you replay it from the initial arrangement to the target arrangement. The replay is useful for checking whether the sequence behaves the way you expect.
How the graph model works
The graph defines which swaps are allowed. Vertices are positions, locations, slots, or states. Edges are the allowed adjacencies between those positions.
- Each vertex must have exactly one starting token.
- Each token must have exactly one target vertex.
- A swap is allowed only between vertices connected by an edge.
- The graph must be connected for every token to be able to reach every vertex.
If the graph is disconnected, a token may be trapped in the wrong component. In that case, no sequence of adjacent swaps can solve the full instance.
What information do you need for token swapping?
1. Vertices
Add one vertex for each position in the problem. Vertices can be arranged visually on the graph canvas to make the structure easier to understand.
2. Edges
Add edges between vertices where a direct swap is allowed. The solver treats swaps as undirected: if two vertices are connected, the two tokens on those vertices may be exchanged.
3. Initial placement
Assign the token currently sitting on each vertex. In NpSolv, the visual graph shows token labels, while the placement table is the source of truth for editing token assignments.
4. Target placement
Assign where each token should end. The target placement must use the same set of tokens as the initial placement.
Worked example: swapping tokens on a small graph
Suppose you have four vertices arranged in a cycle:
1 -- 2
| |
4 -- 3The initial placement is:
| Vertex | Initial token | Target token |
|---|---|---|
| 1 | A | B |
| 2 | B | C |
| 3 | C | D |
| 4 | D | A |
A valid solution might rotate tokens around the cycle using a sequence of adjacent swaps. The important check is that applying every returned swap to the initial placement produces the target placement.
Build a graph, enter initial and target placements in the table, then replay the returned swap sequence.
How to model a token swapping instance
Step 1: Draw the graph
Add vertices for the positions in your problem, then connect the pairs where a direct exchange is allowed.
Step 2: Check connectivity
Make sure the graph is connected. If it is not, tokens cannot move between disconnected components.
Step 3: Fill the placement table
Enter the starting token at each vertex and the desired target token at each vertex. The initial and target columns should contain the same token labels.
Step 4: Solve and inspect the sequence
Run the solver and review the swap count, replay animation, error messages, and step-by-step swap table.
Step 5: Replay the swaps
Use the replay controls to step through the sequence. This helps catch modeling mistakes and makes the result easier to explain.
How should you read the result?
The result summary reports the number of swaps returned by the solver and the elapsed runtime. The visualization shows the graph state as swaps are applied from initial placement to target placement.
The result details table lists each adjacent swap in order. If you apply those swaps one by one to the initial placement, the final placement should match the target placement.
NpSolv returns a generated swap sequence for the graph and token placements you provide. Treat that sequence as a candidate result, not a proof that no shorter sequence exists for every input.
Why can token swapping be hard?
A swap that helps one token may temporarily move another token away from its target. Local choices can interact across the graph, especially when many tokens need to pass through the same small set of vertices.
- Graph structure: Paths, cycles, bottlenecks, and branches all change which swaps are useful.
- Coupled movement: Every swap moves two tokens, so progress for one token can affect another.
- Ordering choices: Moving tokens in a different order can change the total number of swaps.
Practical token swapping methods are useful because they can find valid swap sequences for general graphs without requiring an exhaustive search for the exact optimum.
When should you use a token swapping solver?
Use a token swapping solver when you can model your problem as labeled objects occupying graph vertices, and each move exchanges objects across an allowed edge.
NpSolv provides a token swapping tool where you can build the graph, assign initial and target placements, generate a swap sequence, and replay the swaps visually.
Try Token SwappingFrequently asked questions
What is a token in token swapping?
A token is a labeled object occupying a vertex. The goal is to move each token to its target vertex through adjacent swaps.
What counts as one swap?
One swap exchanges the two tokens on the endpoints of a single graph edge.
Does the solver always return the minimum number of swaps?
No. The current solver returns a candidate sequence. It should be treated as a valid generated sequence, not a proof that no shorter sequence exists.
Why does the graph need to be connected?
If the graph is disconnected, tokens cannot cross between components. A token whose target is in another component cannot be moved there by adjacent swaps.
Can two vertices have the same token?
No. Token swapping assumes each token appears once in the initial placement and once in the target placement.
What is the replay visualization for?
It lets you step through the returned sequence so you can see which two tokens are swapped at each step and confirm the final arrangement.
Further reading: Tillmann Miltzow, Lothar Narins, Yoshio Okamoto, Günter Rote, Antonis Thomas, and Takeaki Uno, Approximation and Hardness of Token Swapping, ESA 2016.

