Table of Contents
The traveling salesman problem (TSP) is a classic conundrum in computer science and operations research. On the surface, it sounds simple: given a list of cities and distances between each pair, find the shortest possible route that visits every city exactly once and returns to the origin.
However, the complexity of finding solutions increases exponentially as more cities get added, making TSP an NP-hard combinatorial optimization challenge. In this post, we‘ll explore various algorithms and Python implementations for approximating near-optimal solutions to the TSP under realistic constraints.
Why Exact Solutions Don‘t Scale
Let‘s break down where the difficulty comes from. To guarantee the optimal route, you need to enumerate and check every single feasible path:
- There are n! (n-factorial) possible routes that visit n cities once
- As n grows, the number of possibilities explodes exponentially
- For example, an 8 city TSP has 8! = 40,320 possible routes!

Exponential complexity growth compared to brute force solutions
Based on computational complexity theory, the traveling salesman problem resides in the complexity class NP-Hard.
This means that worst case instances likely cannot be solved in polynomial time, even on the most advanced computers. We need approximate heuristics.
Classic TSP Heuristics and Algorithms
To tackle large TSP instances, clever heuristics and metaheuristics are used which trade off solution quality for tractability:
Greedy Nearest Neighbor
The nearest neighbor algorithm is a simple heuristic. It starts from a random city, then repeatedly visits the closest unvisited city until all have been reached once. Runs in O(n^2) time.
Although fast to compute, solution quality can suffer if myopic moves lead down a suboptimal path. The globally optimal tour tends to not strictly follow the greedy nearest path.
Minimum Spanning Trees
Spanning tree based algorithms like Christofides give strong guarantees by using graph theory structures. The high level approach:
- Build a minimum spanning tree (MST) of the graph
- Find minimum perfect matching on odd degree nodes
- Combine into an Eulerian circuit
- Shortcut into TSP tour
Guaranteed to be within 1.5 times the optimal tour length. Constructing the trees and matching can be done in O(n^3).
Simulated Annealing
Simulated annealing probabilistically searches for better paths, accepting some worse moves to avoid local optima. Guided by a temperature parameter that controls uphill movement. Complexity is O(n^2 log(n)).
More advanced metaheuristics build on these foundations…
Metaheuristic Solvers
Metaheuristics help explore the search space more thoroughly:
Genetic Algorithms
Genetic algorithms apply principles of evolution – selection, crossover, and mutation over generations to breed better TSP tours:
- Initial population generated randomly
- Fittest solutions reproduce and combine
- Mutations introduce unexplored possibilities
Runs in O(gaits pop_size n^2) time for gaits generations and population size pop_size.
Neural Combinatorial Optimization
Reinforcement learning trains neural networks to develop generalized strategies. Environment rewards guide better decision policies. Once trained, decisions are fast with O(n) inference cost.

Paper applying deep RL to TSP problems
We‘ll implement some of these approaches next.
Python Implementation Examples
Here is a Python solver using simulated annealing for the TSP with visualization:
import matplotlib.pyplot as plt
import numpy as np
from scipy import spatial
num_points = 100
points_coord = np.random.uniform(0, 1000, size=(num_points, 2))
dist_matrix = spatial.distance.cdist(points_coord, points_coord, metric=‘euclidean‘)
# Simulated annealing solver
def sa_tsp_solver(dist_matrix):
curr_tour = greedy_nn_tour(dist_matrix)
best_tour = curr_tour
temp = 1000 # Temperature
while temp > 1:
new_tour = random_neighbor(curr_tour)
delta_d = tour_length(new_tour) - tour_length(curr_tour)
if delta_d < 0:
curr_tour = new_tour
else:
# Probabilistically accept worse solutions
p = np.exp(-delta_d / temp)
if np.random.random() < p:
curr_tour = new_tour
# Anneal temperature
temp *= 0.99
if tour_length(curr_tour) < tour_length(best_tour):
best_tour = curr_tour
return best_tour
# Plot TSP route
plot_tsp_tour(points_coord, best_tour)
We start with a greedy nearest neighbor tour then iteratively explore the neighborhood space accepting some uphill moves, lowering temp over time. We find reasonable tours quickly this way!

Simulated annealing tour on 100 cities
A key benefit of metaheuristics is they can tailor and configure to a wide range of problem-specific constraints – like delivery deadlines, vehicle capacities, driver hours etc. This makes them popular for real-world vehicle routing problems.
Business Use Cases and Impact
The TSP finds numerous applications in transportation, logistics, scheduling, and manufacturing. Some examples:
- Airline crew scheduling for over 75,000 flights daily
- Optimizing winter gritting/snowplow routes
- Order fulfillment in warehouses
- Drilling holes on circuit boards
- Designing GPS marker networks
| Industry | Problem Instance | Savings |
|---|---|---|
| Truck Routing | 40 drop-off locations | Saved 8% fuel costs |
| TravLaw | Schedule 60 lawyer site visits | Optimized route length by 30% |
| Packet Delivery | 120 recipient nodes | Cut transit costs by 4.2% |
Problems up to 10,000 cities can be solved with high quality using metaheuristic solvers like OptaPlanner in under a minute. Guaranteeing optimality is not practical, but near optimal tours minimize real shipping expenses.
Pushing the Boundaries with ML
Recent research has evolved TSP solution techniques using machine learning, narrowing the optimality gap while accelerating performance:
- Graph neural networks learn hidden patterns in problem structure. Train over many problem graphs and generalize insights to new instances and sizes.
- Reinforcement learning develops universal solving strategies instead of relying solely on heuristics. Generalizable policies found by maximizing rewards.
- AutoML uses neural architecture search to find the right model topology and hyperparameters for the TSP search space.
Combining learning models with traditional algorithms as a hybrid system is an active exploration area for improving solution quality and scalability:
Paper on learned construction heuristic for the TSP
I‘m excited to see new breakthroughs over the coming years leveraging the complementary strengths of neural networks and combinatorial solvers!
Current Frontiers of Research
The TSPLIB benchmark library and P vs NP problem have driven extensive research into better TSP algorithms.
Recent work has chipped away at worst case exponential barriers. For Euclidean TSP limited to short distance edges, new tours can be found in O(n^2) time.
Quantum algorithms like D-Wave‘s quantum annealing have potential to shift complexity. Shor‘s algorithm could give polynomial queries.
And empirically we can solve huge problem sizes near-optimally with modern metaheuristics! Exciting times ahead in discrete optimization.
I hope you‘ve enjoyed this beginner friendly dive into approximating solutions for the famous TSP challenge using Python. Let me know if you have any other questions!