The cheapest route through a day of stops, with proof.
I am Vice President of an import and distribution business, so the routing question is familiar territory. The question itself is simple: what is the cheapest way to visit every stop once and return, and how do I know that nothing cheaper exists?
Move a stop on the map and the day is solved again. The shortest round is now units; driving to the nearest stop each time is , and no round can be shorter than . Drag a stop, or focus one and use the arrow keys.
Instinct against the exact optimum
A driver's instinct, always heading to the nearest unvisited stop, produces a route that looks fine. This page computes that route, improves it with 2-opt where distances are symmetric, then solves the single-vehicle problem exactly by dynamic programming over every subset of stops and shows the gap. A lower bound from an independent relaxation is drawn beside the optimum so the proof rests on a second, independent argument.
The delivery day you edit
The depot, stops and distances are made up, and everything runs in your browser: nothing you type is stored or sent.
The depot, every stop, its demand, the vehicle capacity and the distance convention are editable in the JSON source, for up to 12 stops. Loading an example or resetting replaces your edits and clears the results.
Edit the depot, stops, capacity and distances
Edit stops[].x, y and demand, or vehicle.capacity. Set distances to "euclidean" for rounded straight-line legs, or to { "matrix": [[...]] } for an explicit integer matrix over the depot then the stops in input order. Asymmetric matrices are allowed. Stop order defines the tie-breaking order.
1 to 12 stops; coordinates are integers 0 to 10,000; demand 0 to 1,000; capacity 1 to 100,000; matrix entries 0 to 1,000,000 with a zero diagonal; 50,000 source characters. Unknown and duplicate JSON fields are refused. IDs start with a letter, and each stop needs its own, distinct from the depot's.
JavaScript is required to compute. No calculation has run.
The result and its certificate
Lengths are exact integer sums of integer legs. Milliseconds are a wall-clock measurement and the only nondeterministic value on this page.
Both tours on the map
Scroll horizontally to inspect the full map on a small screen. Exact legs are in the tour table below.
Splitting the load into trips
When the stops need more than one load, the vehicle makes several trips from the depot. Every group of stops that fits is costed by its own exact tour, and a search over all ways of dividing the stops finds the fewest trips, then the shortest total length among those. When a split with more trips is shorter, it is listed too.
| Trip | Stops | Load | Tour order | Length |
|---|
Legs and loads on the optimal tour
The vehicle leaves with the total demand on board and delivers at each stop. Every leg distance comes from the matrix below; the cumulative column reaches the optimum on the return leg.
| Leg | From | To / source | Leg distance | Cumulative | Delivered | Load after |
|---|
How the heuristics compare with the optimum
Positive gaps are distance the heuristic wastes. The lower bound sits at or below the optimum by construction; when it is tight the bound alone certifies the tour, otherwise the certificate is the exhaustive dynamic programme itself.
| Method | Status | Length | Gap to optimum | Gap % | Tour order |
|---|
Certificate detail
Inside the dynamic programme
The programme stores, for every set of stops still to visit and every stop the vehicle could be at, the cheapest way to finish the tour. The optimal tour is read back from that table one position at a time: from where the vehicle stands, each open stop costs its leg plus the stored finish from there, and the cheapest total is the next stop. Step through the positions to see every entry the reconstruction consults.
| Next stop | Leg | Cheapest finish | Total | Outcome |
|---|
The distance matrix
Where each stop sits in the source
JSON pointers identify each node in the exact source snapshot. Coordinates and demands are the values you supply.
| Index | ID / label | Kind | x | y | Demand | JSON pointer |
|---|
Exact source text for this calculation
Whitespace and field order are preserved. Any change to the editor, including a silent programmatic edit, invalidates this run. Export checks the live editor again before creating a file.
Method and limits
Distances and integer arithmetic
With distances: "euclidean" each leg is nint(sqrt(dx² + dy²)), the TSPLIB EUC_2D convention, so every leg is an integer and every tour length is an exact integer sum. Rounding each leg separately can break the triangle inequality by a unit, for example three collinear points on a diagonal. An explicit matrix is used as supplied, in input order with the depot first; it may be asymmetric and need not satisfy the triangle inequality.
The triangle audit checks every ordered triple of distinct nodes for a detour shorter than its direct leg and lists up to 12 examples. Violations do not affect the exact optimum. The 1-tree bound remains valid on any symmetric matrix and the assignment bound on any matrix, because neither uses the triangle inequality. Only the informal picture of 2-opt removing crossing legs depends on it.
Gaps are integer differences. Percentages are gap divided by optimum, rounded to four decimals for display, and are null when the optimum is zero. No floating arithmetic enters any length.
Exact optimum, bounds and heuristics
The optimum is computed by the Held and Karp 1962 dynamic programme over subsets. A state is one pair of a set of stops still to visit and the stop the vehicle is at, holding the cheapest way to finish from there; for n stops the programme evaluates n times 2ⁿ⁻¹ such states, at most 12 times 2,048 = 24,576, inside an array of n times 2ⁿ entries. The recorded state and transition counts are exact. The programme is oriented from each stop back to the depot so that the forward reconstruction can take the lowest input index at every position; the reported tour is therefore the lexicographically smallest optimal tour by stop input index, and the same input always yields the same tour.
For a symmetric matrix the certificate is a 1-tree: a Prim minimum spanning tree over the stops plus the two cheapest depot edges to distinct stops. Any tour minus its two depot edges is a spanning path, so the bound never exceeds the optimum; with one stop the single depot edge is counted twice, which equals the only tour. For an asymmetric matrix the certificate is the assignment relaxation solved by the Hungarian method with self-edges forbidden: a minimum-cost cycle cover, which every tour satisfies. Both bounds are checked against the optimum; a bound above the optimum would stop the run as an internal error.
Nearest neighbour starts at the depot and always moves to the closest unvisited stop, ties to the lowest input index. 2-opt reverses tour segments while a reversal strictly shortens the integer length and runs on symmetric matrices only, because a reversed segment keeps its length only when distances are symmetric. Both heuristics are there to be measured against the optimum.
The step-through reads the same table the optimum came from. At each position it lists every open stop with its leg, the stored cheapest finish and their sum; the chosen stop is the lowest input index whose sum equals the value being reconstructed, and the trace is checked to reproduce the tour. In the greedy-trap example all four first stops tie at 5,000, because every optimal tour on one road runs to one end and back, so S1 goes first by index.
Capacity first decides whether one tour can serve the day. When total demand exceeds it, ceil(total / capacity) is only a lower bound on trips, so the page searches exactly. Every group of stops whose demand fits is costed by its Held-Karp tour, read from the same table, and a dynamic programme over sets of stops builds each trip around the lowest-index stop left, which meets every partition once: at most 3¹² = 531,441 pairs of sets for 12 stops. The answer is the fewest trips, then the shortest total length; the shortest split with any number of trips is reported beside it. Three stops of demand 6 with a capacity of 10 have a lower bound of 2 and need 3. A stop that needs more than the capacity has no split, and the page names it. Trips are made one after another by the same vehicle; fleets, time windows and driver hours stay outside the model.
Source schema and units
Required top-level fields: fictional: true, scenario, depot, stops, vehicle, distances. The depot requires id, label, x, y; each stop requires id, label, x, y, demand; the vehicle requires capacity. Unknown, missing or duplicate fields are errors. JSON nesting is limited to 16 levels. The loaded examples are complete and editable.
1 to 12 stops. Coordinates are integers from 0 to 10,000 in arbitrary distance units, demands integers from 0 to 1,000, capacity an integer from 1 to 100,000. An explicit matrix must have exactly one row and one column per node, integer entries from 0 to 1,000,000 and a zero diagonal. IDs are case sensitive, start with an ASCII letter and have at most 24 letters, digits, underscores or hyphens; a stop may not reuse the depot ID and no two stops share an ID. Scenario: up to 120 characters; labels: 80. Text cannot contain ASCII or C1 control characters, invisible format characters (zero-width characters, bidirectional overrides, line and paragraph separators, byte order mark) or unpaired Unicode surrogates.
Scope, implementation and checks
Real distribution adds road networks, one-way streets, time windows, driver hours, several vehicles, traffic and cost, all outside this model. Nothing here is a Rehan Spices route, customer list or performance figure. A proven shortest tour on supplied numbers is a demonstration of method, never a dispatch plan.
The engine and the interface are plain JavaScript with no dependencies; the same engine runs in Node and the browser. Edits disappear on reload. A download contains the complete supplied JSON, the matrix, every tour, the reconstruction trace, any trip split and the certificate, and replays with RV_LOGISTICS.analyze(exactInputText).
I checked the engine against hand-worked cases (a square, a collinear road, the greedy trap, an asymmetric matrix and the capacity abstention), against brute-force enumeration of every tour on random instances, against an independent Kruskal spanning tree for the 1-tree bound and against derangement enumeration for the assignment bound. Those checks cover the arithmetic.