Traveling Salesman Solver
Build a closed Euclidean tour through 3–50 coordinates using nearest neighbor plus deterministic 2-opt improvement.
Privacy: your files never leave your device. All processing happens locally in your browser.
How to use
- 1.Enter 3–50 locations as name, x, y, placing the desired start in the first row.
- 2.Build the approximate tour and compare the initial and improved distances.
- 3.Inspect the closed route and assumptions, then copy it only if straight-line Euclidean distance fits the task.
About Traveling Salesman Solver
Traveling Salesman Solver builds an approximate closed tour through named points on a two-dimensional coordinate plane. Enter one location per line as name, x, y. The first row is the fixed starting location. The result lists a route that visits every point once, returns to the start, and reports total straight-line distance. It also shows the nearest-neighbor starting distance and the number of 2-opt passes, making improvement visible rather than hiding the heuristic.
The traveling salesperson problem asks for a shortest closed route that visits all locations. Google OR-Tools explains the same routing objective and cautions that practical solvers can return non-optimal results because routing problems are computationally difficult. This page deliberately uses a bounded, inspectable heuristic instead of claiming an exact solution for arbitrary inputs.
Construction begins at the first input point. At each step, nearest neighbor selects the closest unvisited point using Euclidean distance, sqrt((x2−x1)²+(y2−y1)²). Exact distance ties keep original input order. After a complete tour exists, 2-opt examines two route edges. If reconnecting them by reversing the intervening segment strictly shortens the tour, the exchange is applied and scanning restarts. Passes continue until no improving exchange remains or the defensive limit is reached.
The returned tour is locally improved under those two-edge exchanges, but it is not guaranteed to be globally shortest. A different starting location or construction rule can lead to another local minimum. The solver fixes the first row as start so repeated runs are deterministic. Eight small geometric golden cases—triangles, squares, rectangles, collinear points, and a center point—verify exact perimeter distances, closure, one visit per location, and that 2-opt never makes the initial route longer.
Coordinates are abstract planar values. They can be meters on a local drawing, pixels in a layout, or unitless classroom coordinates if both axes use the same scale. They are not latitude and longitude. Applying Euclidean distance directly to degrees distorts real Earth distance, especially across large regions or the antimeridian. Convert geographic points to an appropriate local projection or use a routing service designed for geodesic distance.
The page also ignores roads, one-way streets, traffic, travel time, obstacles, appointments, vehicle capacity, and multiple drivers. A straight segment between two points is not a claim that a vehicle can travel that way. Real route planning needs an actual distance or time matrix and the relevant constraints. No external map API is called, so there is no paid request and no location data leaves the browser.
Inputs require 3 through 50 distinct coordinate pairs. Duplicate coordinates are rejected because two separately named points at exactly the same location make route display ambiguous. Names may not contain commas, coordinates must be finite and bounded, and the point limit keeps quadratic local search responsive. The copied result contains the full closed name sequence plus the calculated distance.
Use this tool to learn TSP heuristics, explore spatial ordering, plan an approximate inspection sequence on a local coordinate system, or create reproducible test fixtures. Compare several starting-point orders if solution quality matters. For dispatch, navigation, cost commitments, or safety-critical routing, use a validated solver with real network data and independently verify the route rather than treating this local approximation as operational authority.
Methodology & sources
Validate 3–50 unique bounded planar points, fix the first point as start, construct a stable nearest-neighbor closed tour, then repeatedly apply the first strictly improving 2-opt edge exchange. Sum Euclidean hypot distances including return to start; never claim global optimality.
Frequently asked questions
- Does this find the globally shortest route?
- No. Nearest Neighbor plus 2-opt is a deterministic heuristic and can stop at a local minimum.
- Can I enter latitude and longitude?
- Not directly for reliable geographic distance. The solver treats coordinates as a flat Euclidean plane.
- Does it follow roads or traffic?
- No. It uses straight-line coordinate distance and calls no map or routing API.
- Why is the first location special?
- It fixes the starting point and deterministic tie behavior; the tour returns to that location at the end.
Related tools
- GPS Coordinates ConverterConvert coordinates between decimal degrees and DMS.
- Minimum Spanning Tree SolverFind an exact minimum-weight tree connecting every named node with deterministic Kruskal selection and explicit disconnected-graph errors.
- Assignment Problem SolverFind the exact minimum-cost one-to-one pairing between equally sized worker and task sets with the deterministic Hungarian algorithm.
- Absolute Value CalculatorInstantly find |x| — the absolute value of any number, free.
- Age CalculatorYour exact age in years, months, and days — updated live
- Angle ConverterConvert degrees, radians, gradians, turns, arcmin & arcsec
Calculators guides
View all- How to Calculate Cube Volume from One Side Length
- How to Calculate Cube Root for Any Number
- How to Change Clothing Size on Amazon: Convert Sizes
- How to Calculate Circumference From Radius or Diameter
- How to Calculate Circle Area: A = πr² Made Simple
- Convert Your Cat's Age to Human Years the Vet-Approved Way
- Calculate Brinell Hardness from Load, Ball and Indent
- How to Calculate Bra Size Accurately at Home
- Measure Body Fat at Home Without Scales or Calipers
- Calculate Your BMR in Seconds with Two Trusted Formulas
- How to Calculate BMI in Seconds with a Free Online Tool
- How to Calculate Online Without a Physical Calculator
- How to Calculate Your Blood Alcohol Content Accurately
- Predict Your Baby's Eye Color Odds in One Click
- How to Calculate Average Fast – Free Online Mean, Median & Mode Tool
- Convert Any Area to Square Feet in Seconds – Free Online Tool
- How to Calculate Annular Area Quickly – Free Online Tool
- How to Convert Degrees to Radians in Seconds – Free Online Tool
- How to Calculate Age Between Two Dates in Excel – Instant Online Tool
- How to Calculate Absolute Value Fast – Free Online Tool