Chapter 6 · Simulation

Traveling Salesman Problem

← Chapter 6

Ready.

Traveling Salesman Problem

Cities are generated; the fast nearest-neighbor heuristic is compared with an exact brute-force search. Distances are Euclidean distances on the canvas (in pixels).

How to use

  • Choose the number of cities (2–10) and press Generate; the cities are placed at random.
  • Nearest Neighbor shows, at each step, the candidate edges leaving the current city, then extends the tour.
  • Brute Force tries every tour starting at city 0, one by one; the best tour found so far stays visible. It works with at most 9 cities.
  • When both are run on the same cities, the side panel shows how much longer the heuristic tour is than the optimum. Cancel stops the run.

Nearest neighbor

At each step the tour goes from the current city to the nearest unvisited city. It is fast but does not guarantee an optimal tour.

Brute force

Fixes one city as the start and tries every possible tour. It finds the optimum, but because of factorial growth it is only usable on small inputs.

Complexity

MethodTimeResult
Nearest neighborO(n²)Heuristic, not always optimal
Brute forceO(n!) — (n − 1)! toursOptimal