← 🤖 Algorithms & AI
Click empty space to add a city · drag a city to move it · right-click to delete

About this 2D Travelling Salesman visualizer

This is the flat-canvas companion to the 3D Travelling Salesman simulation: the same three classic heuristics — Nearest Neighbour, 2-opt local search and Simulated Annealing — run here on a plain 2D canvas instead of a WebGL scene, with the tour redrawn every frame as it improves.

Click empty canvas space to add a city, drag an existing one to move it, or right-click to remove it. Pick an algorithm, set the Speed (steps per animation frame) and press Run. For Simulated Annealing, the Start temp and Cooling sliders control the temperature schedule; the panel's live stats show the current and best tour length, iteration count, temperature and how many moves actually improved the tour.

Frequently Asked Questions

How is this different from the 3D TSP simulation?

Same underlying algorithms and city-tour math, but rendered on a flat 2D canvas with plain 2D drawing instead of a WebGL/Three.js scene. It is a separate, self-contained implementation, not a screenshot or reskin of the 3D version.

What move does 2-opt and Simulated Annealing actually make?

Both pick two positions in the tour and reverse the segment between them (a "2-opt swap"). 2-opt only keeps a reversal if it shortens the tour; Simulated Annealing can also accept a reversal that lengthens the tour, with probability exp(-Δ/T), so it can escape local optima that trap pure 2-opt.

What do the Start temp and Cooling sliders do?

Start temp sets the initial temperature T0 (slider value × 500). Cooling sets the per-step multiplier alpha, roughly between 0.9995 and 0.99995 — closer to 1 means slower cooling and a longer, more thorough search before T drops near zero and the run stops.

Are the tours found here optimal?

No — all three are heuristics. Nearest Neighbour can end up 25% or more above the optimal tour; 2-opt and Simulated Annealing usually get much closer but offer no guarantee of finding the true shortest tour.

Why does the tour sometimes get longer before it gets shorter?

That only happens under Simulated Annealing: it deliberately accepts some worse moves (governed by the current temperature) so it can jump out of a dead end that plain 2-opt would get stuck in. The best tour ever seen is tracked separately and always drawn in green.

📍 2D Travelling Salesman — Nearest Neighbour, 2-opt & Simulated Annealing

The flat-canvas companion to 3D Travelling Salesman — TSP: click to place cities, then compare Nearest Neighbour, 2-opt local search and real Simulated Annealing as they shorten the tour live.

🔬 What it demonstrates

TSP asks for the shortest closed tour visiting every city once. It's NP-hard, so this simulation uses three heuristics of increasing sophistication to approach near-optimal answers fast.

🎮 How to use

Click empty space to add cities, drag to reposition them, right-click to delete. Switch algorithms and watch the current tour (blue) and the best tour ever found (green) update.

💡 Did you know?

With just 20 cities there are 20!/2 ≈ 1.2 × 10¹⁸ possible tours — brute force is hopeless, which is exactly why heuristics like these exist.