How this calculation works
Find a shortest sequence of adjacent swaps that routes distinct labeled tokens to their target vertices.
d(start,target)=minimum number of edge transpositions transforming start into target.A worked example
On the four-vertex path, placement 3,1,2,0 requires five swaps to reach 0,1,2,3; its inversion count is five.
Good to know
Simple graph with 1–8 vertices, two complete zero-based token permutations and at most 28 distinct edges. Exact BFS over at most 40320 placements. Sequential unit-cost swaps only; component-infeasible placements return unreachable.
Reference: Bonnet, Miltzow and Rzążewski: Complexity of Token Swapping and its Variants (opens in a new tab) ↗
Tokens are conserved and distinguishable
Place one distinct token on each graph vertex. A legal move swaps the two tokens at the endpoints of a listed edge. Nothing is created, destroyed or left empty, so the entire placement remains a permutation after every move. The destination is another complete placement of the same labels.
This is a routing problem in a configuration space rather than an ordinary shortest path between two vertices. Moving one token necessarily changes another token’s position. A route that looks shortest for one token in isolation can therefore be incompatible with the shortest sequence for all tokens together.
Compare this with the Graph pebbling reachability calculator from an initial stone distribution. Route a permutation of distinct conserved tokens to prescribed vertices using the fewest adjacent swaps. Pebbling destroys one stone per move and asks reachability of a target, so its states and objective differ.
Enter placements by vertex position
The starting list is indexed by vertex: its first entry is the token currently at vertex 0, its second entry the token at vertex 1, and so on. The target list uses the same convention. Each list must contain every token label from 0 through n−1 exactly once.
An edge is an undirected permission to swap its endpoint tokens. Its row order does not affect the cost, and every legal swap costs one. There is no empty-slot move, simultaneous round or weighted exchange here. Those would produce different state graphs and require a different interpretation of a shortest answer.
Check disconnected components first
A token can move only inside the connected component containing its current vertex. Before exploring placements, the calculator checks whether each token’s target vertex belongs to that same component. If not, the result is unreachable with a structural explanation rather than an invented large swap count.
This component test is also sufficient for reachability: adjacent edge swaps generate every permutation within a connected component. A disconnected graph can therefore still have a valid answer when every component contains precisely the tokens that its targets require. Isolated vertices must already hold their target tokens.
Breadth-first search on placements
Every placement is a search state, and every legal edge swap leads to a neighboring state. Because all moves have unit cost, breadth-first search reaches states in nondecreasing swap count. The first time the target is removed from the queue, its predecessor chain is a shortest legal swap sequence.
At most eight tokens produce at most 8! distinct placements. The implementation stores the predecessor edge for each discovered placement and replays the recovered swaps before reporting success. The visited-state count is a measure of the explored search space, while the minimum-swap count is the actual optimization result.
Work through the path example
The default graph is the path 0–1–2–3, with initial tokens 3,1,2,0 and target tokens 0,1,2,3. One shortest sequence swaps edges 0–1, 1–2, 2–3, 1–2 and 0–1. Replaying it produces the requested target after exactly five exchanges.
On a path with the identity target, adjacent swapping distance equals the number of inversions in the starting token order. Here token 3 precedes three smaller labels, token 1 precedes 0, and token 2 precedes 0. That gives five inversions, supplying an independent lower bound attained by the displayed sequence.
Understand why the graph changes distance
On a complete graph, any pair of positions can be swapped directly. The minimum then equals n minus the number of cycles in the permutation relative to the target. On a path, exchanges are restricted to neighboring positions, so inversion count governs the answer instead.
Adding an allowed edge cannot increase the optimal swap count because every old sequence remains legal. Removing an edge may increase the count or make a required token transfer impossible. These monotonicity checks are useful when experimenting with a graph, but they do not mean that a greedy token-by-token routing rule is always optimal.
Read a shortest witness responsibly
The returned edge sequence is a concrete executable certificate: apply each listed exchange to the two currently present endpoint tokens. It specifies sequential operations, so two adjacent rows must not be treated as simultaneous unless you separately verify that their endpoints are disjoint and that your cost model permits parallelism.
Different shortest sequences may exist, particularly in graphs with cycles or symmetric routes. This page returns one of them and does not count all optimal sequences. Colored indistinguishable tokens, destination subsets, exchange weights and sliding puzzles have different rules; their answers cannot be inferred by relabeling this result without changing the underlying problem.
A few questions, answered
Is a move one token sliding into an empty vertex?
No. Every vertex is occupied, and a move exchanges the two endpoint tokens of an allowed edge.
Why is my disconnected graph unreachable?
At least one token’s current and target vertices lie in different components, which no allowed swap can cross.
Does reversing a swap undo it?
Yes. The same endpoint exchange is its own inverse; an undirected edge permits either direction.
Is the returned sequence unique?
Not necessarily. BFS returns one shortest sequence among potentially several sequences of equal length.