How this calculation works
The traveling-salesman problem seeks a least-cost cycle that visits every supplied vertex exactly once before returning to its starting vertex. This calculator uses subset dynamic programming for a bounded cost matrix, tracking which vertices have been visited and the current endpoint. It supports directed asymmetric costs and forbidden moves rather than assuming geographic symmetry or filling missing routes with shortest-path distances. A reconstructed tour is checked for a single visit to every vertex and for agreement between its leg sum and the optimal dynamic-programming value.
D[S,j]=min over i in S without j of D[S without j,i]+cᵢⱼ; close the tour by adding cⱼ₁.A worked example
For the default four-vertex matrix, tour 1→2→4→3→1 costs 10+25+30+15=80. The reverse tour also costs eighty because this example is symmetric. Tour 1→2→3→4→1 costs 10+35+30+20=95. Choosing the cheapest next leg without considering the remaining visit requirements is not a proof of the optimum. Starting at vertex one fixes rotational copies of the same round trip; it does not require a different optimization problem for every possible starting label.
Good to know
Square integer cost matrices of order 1–13, with entries within ±1,000,000 and X for forbidden directed moves. Diagonal entries are ignored; one vertex has a zero-length round trip. Negative costs are permitted because supported tours have a fixed finite number of legs. No intermediate vertex revisits, vehicle capacities, service times, time windows, multiple vehicles or automatic road-network distances are modeled. If no Hamiltonian round trip exists, no partial route is presented as a complete result. Returns one optimum, not all tied tours.
Reference: MIT 6.006 — dynamic-programming lecture notes (opens in a new tab) ↗
Visited sets distinguish a complete tour from an ordinary shortest path
A state records both the endpoint and the subset already visited. Two routes ending at the same vertex can have different legal futures when they have visited different subsets. Conversely, among routes with the same endpoint and visited set, only the cheapest accumulated cost is needed: every remaining transition has the same options. The final stage requires every vertex and an allowed return edge to the chosen start.
Forbidden moves are not treated as merely expensive. They are absent transitions, so a matrix can have many finite entries but still fail to admit a complete round trip. The output preserves the original directed leg costs, and its reconstruction checks that each vertex appears once before closure. This is different from joining pairwise shortest routes, which could pass through another required vertex early or revisit vertices in ways this Hamiltonian model excludes.
Exact small-instance optimization needs a visible size boundary
Subset dynamic programming grows exponentially with the number of vertices. The hard order limit keeps the supported search exact instead of replacing larger inputs with a greedy guess while retaining an optimality label. The reported state count describes computed partial-tour states, not all factorially many full permutations. A tie can produce a different equally cheap tour, especially in a symmetric matrix where reversing a tour preserves its cost.
Costs can represent distance, time or another consistent additive quantity, but the calculator cannot validate that the entries model the same conditions. Asymmetry is supported directly; it is not corrected by averaging directions. The Shortest path calculator for a weighted graph edge list optimizes a journey between selected endpoints and may skip most vertices. The Minimum spanning tree calculator from weighted edges connects the whole network without requiring a round trip. Their lower totals are therefore not competing answers to this visit-once tour question.
A few questions, answered
Can a tour use a vertex twice to avoid a forbidden move?
No. This page solves a Hamiltonian round trip, visiting each listed vertex once before closure. Repeated-visit route planning needs a different model.
Why is a negative travel cost accepted?
It is a signed mathematical cost, not necessarily a physical distance. A tour has finitely many required legs, so negative entries do not permit an infinitely repeated improving walk.