Find the shortest route visiting all cities exactly once and returning to origin
1. Problem Definition
2. City Network
3. Distance Matrix
4. Algorithm & Solution
Traveling Salesman Problem
The Traveling Salesman Problem (TSP) is a classic algorithmic problem in computer science and operations research. It focuses on optimization to find the shortest possible route that visits each city exactly once and returns to the origin city.
Problem Definition
Input
A set of cities and the distances between each pair of cities.
Objective
Find the shortest possible route that visits each city exactly once and returns to the origin city.
Complexity
TSP is NP-hard, meaning there is no known polynomial-time solution for large problem sizes.
Applications
Logistics, route planning, circuit board drilling, genome sequencing, and more.
Solution Approaches
Exact Algorithms
Brute force, dynamic programming, branch and bound. Guarantee optimal solution but are computationally expensive.
Approximation Algorithms
Christofides algorithm, nearest neighbor, minimum spanning tree based approaches.
Heuristic Methods
Genetic algorithms, simulated annealing, ant colony optimization, tabu search.
Our Implementation
We use a combination of nearest neighbor heuristic and 2-opt optimization for a good balance of speed and solution quality.
Ready to Start?
Click the button below to begin setting up your TSP problem.
City Network Setup
Select cities to include in your TSP problem. Distances will be automatically calculated based on coordinates.
Select Cities
Hold Ctrl/Cmd to select multiple cities
Current Cities
No cities added yet
Network Preview
Resize the diagram for better view using zoom and pan controls
100%
Distance Matrix
Review the distance matrix that will be used to solve the TSP problem.
Distance Matrix
City
Problem Summary
Number of Cities
0
Possible Routes
0
Matrix Information
Symmetric TSP
The distance from city A to city B is the same as from B to A. Our matrix is symmetric.
Distance Calculation
Distances are calculated using the Haversine formula based on city coordinates.
Matrix Properties
Diagonal elements are zero (distance from a city to itself). Matrix is symmetric (d[i][j] = d[j][i]).
Problem Complexity
For n cities, there are (n-1)! / 2 possible routes (for symmetric TSP).
Algorithm Information
We will use a combination of nearest neighbor heuristic and 2-opt optimization to find a near-optimal solution.
Time complexity: O(n²) for nearest neighbor + O(n²) per 2-opt iteration
Algorithm & Solution
Run the TSP algorithm and visualize the optimal route.
Algorithm:Nearest Neighbor + 2-opt
Algorithm Steps
Step 1: Initialization
Select a random starting city and initialize the tour with that city.
Step 2: Nearest Neighbor
Repeatedly add the closest unvisited city to the current tour until all cities are visited.
Step 3: Return to Start
Complete the tour by returning to the starting city.
Step 4: 2-opt Optimization
Improve the tour by swapping pairs of edges to reduce the total distance.
Step 5: Termination
Stop when no improving 2-opt moves can be found.
Current Tour
No tour computed yet. Run the algorithm to see the tour.
Tour Statistics
Tour Length
-
Improvement
-
TSP Solution Visualization
100%
Optimal Solution
No solution computed yet. Run the algorithm to see the optimal tour.