b2KIT

Traveling Salesman Visualizer

Place cities and watch nearest-neighbor, greedy, and simulated annealing algorithms find short tours. Compare path lengths.

Tested tool guide Tested browser tools Checked August 16, 2026

What Traveling Salesman Visualizer does, with a checked example

Place cities by clicking the canvas, run the three classic traveling-salesman heuristics - nearest neighbor, greedy edge selection, and simulated annealing - and the tool draws each resulting tour and reports its length so you can compare them. The thing most users misread: this is a heuristic comparison, not an exact solver, so the shortest line on screen is a candidate solution, not a proven optimum. The other surprise is the closing edge: the reported length includes the trip back to the starting city, and that return leg is often what inflates a naive tour.

Worked example

A concrete input and expected output from the current implementation.

Input

Click four cities onto the canvas at the corners of a square four units on a side: (0,0), (4,0), (4,4), (0,4).

Expected output

Nearest neighbor and greedy both draw the perimeter route and report 16 units; simulated annealing converges on the same tour.

Every side of the square is 4 units and every diagonal is about 5.66, so the perimeter tour, 4 + 4 + 4 + 4 = 16, beats any route that uses a diagonal and is the true optimum. The symmetric layout forces all three heuristics onto the same tour; that agreement is a coincidence of symmetry, not a general guarantee.

How the result is produced

1

Nearest neighbor and greedy construction

Nearest neighbor starts at one city and repeatedly steps to the nearest city not yet visited, closing with the edge back to the start. It is deterministic except when candidates tie, and myopic: each local choice looks safe, yet an early short step can force a long closing edge later. Greedy scans every city pair, accepting the shortest edge that keeps the emerging tour valid until a full tour exists.

2

Simulated annealing refinement

Simulated annealing starts from a complete tour and repeatedly tries a small rearrangement, typically swapping the order of two edges, a move known as 2-opt. It accepts every improvement and also some worsening moves, with the acceptance probability shrinking as an annealing temperature cools. That tolerance for uphill steps lets it escape the local minima that trap nearest neighbor; the cost is time, and longer runs usually produce shorter tours.

Good uses

  • Run a classroom demonstration: place the same cities in a tight cluster and then in a long line, and show how much the nearest-neighbor tour degrades relative to annealing.
  • Sketch a quick route for a small real job - a few delivery stops, service calls, or drill-hole positions - when you need a sensible order fast and a provably optimal one is not worth the effort.
  • Stress-test the heuristics deliberately: arrange cities in clusters separated by open space, or along a curve, and watch which algorithm holds up as the layout gets harder.

Limits and checks

  • No algorithm proves optimality. TSP is NP-hard, and the best displayed length is an upper bound on the optimum; a tour all three heuristics agree on can still be improvable.
  • Results are not fully reproducible: equidistant cities create nearest-neighbor ties, and annealing depends on its random start and on how long you let it run, so two runs can report different lengths.
  • Lengths are straight-line Euclidean distances on the canvas. Roads, rivers, one-way streets, and travel time are ignored, so the shortest drawn tour is not necessarily the shortest real route; moving one city can also flip the winner.

Common questions

Why is my nearest-neighbor tour so much longer than the annealing tour?

Nearest neighbor always steps to the closest city and can strand itself with an expensive closing edge; simulated annealing keeps proposing rearrangements and can escape those traps. The gap grows with the number of cities and with elongated or clustered layouts. On small, symmetric layouts the three algorithms often agree, so the difference is easiest to see with more cities.

If all three algorithms return the same length, is that the true optimum?

Probably, but not proven. Agreement is a strong hint on small layouts with a handful of cities, yet heuristics can coincide on a tour that is merely locally optimal. Treat the shortest reported length as a good upper bound on the optimum; only an exact solver can certify that no shorter tour exists.

References and verification

The example and behavioral notes were checked against the browser implementation. Standards and primary references below define the relevant format, formula, or platform behavior.

Related Tools