Traveling Salesman Problem
Optimization
Algorithm
Operations Research
Computational Mathematics

Traveling salesman example with known global optimum

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

Introduction to the Traveling Salesman Problem

The Traveling Salesman Problem (TSP) is a classic optimization problem that has been extensively studied in the field of computer science and operations research. It poses a simple question: Given a list of cities and the distances between each pair of cities, what is the shortest possible route that visits each city exactly once and returns to the origin city?

The challenge lies in the exponential growth of possible routes, as the number of cities increases. The TSP is known to be NP-hard, which means there is no known algorithm that can solve all instances of the problem efficiently (in polynomial time). However, specific instances with known global optima can help us understand potential solutions and optimization techniques.

Technical Explanation

Problem Representation

The TSP can be represented mathematically as follows:

Cities: c1,c2,,cn{c_1, c_2, \ldots, c_n}Distances: d(ci,cj)d(c_i, c_j) is the distance between city cic_i and city cjc_j.

Objective: Find a permutation of cities (σ(1),σ(2),,σ(n))(\sigma(1), \sigma(2), \ldots, \sigma(n)) that minimizes the total travel distance:

Minimizei=1n1d(σ(i),σ(i+1))+d(σ(n),σ(1))\text{Minimize} \quad \sum_{i=1}^{n-1} d(\sigma(i), \sigma(i+1)) + d(\sigma(n), \sigma(1))

Known Global Optimum Example

One widely discussed example with a known global optimum is the Groetschel 49-city problem. It includes 49 cities with specified Euclidean distances and an established minimum tour length.

Key Features of the Groetschel Example

Number of cities: 49 • Type: Symmetric TSP (distances are equal in both directions) • Optimal tour length: 21282.5 (unit distance)

The optimal solution for this instance helps benchmark TSP solving algorithms and validates their accuracy.

Solving Techniques

While finding the exact solution for larger instances remains computationally challenging, several strategies can be employed:

Exact Algorithms

  1. Branch and Bound: A tree-based search strategy that prunes paths based on estimated lower bounds.
  2. Dynamic Programming: Known as the Held-Karp algorithm, it solves the problem with a time complexity of O(n22n)O(n^2 2^n).

Approximation Algorithms

  1. Christofides’ Algorithm: Guarantees a tour no longer than 1.5 times the optimal tour length for metric TSPs.
  2. Nearest Neighbor: Constructs a path by iteratively visiting the nearest unvisited city.
  3. Genetic Algorithms and Simulated Annealing: These heuristic-based methods explore solution spaces stochastically.

Visualization and Practical Example

Consider a simple 5-city TSP example with the following distances:

From/ToCity 1City 2City 3City 4City 5
City 1010152025
City 2100352515
City 3153503020
City 4202530010
City 5251520100

The optimal tour for this specific setup can be evaluated manually or with an exact algorithm. The minimal path can be achieved by visiting the cities in a specific sequence to minimize the return distance to the start.

Summary Table

Here's a summarized comparison of various TSP strategies:

MethodTypeTime ComplexityApplicability
Branch & BoundExactVariesSuitable for small to medium-sized problems
Dynamic ProgrammingExactO(n22n)O(n^2 2^n)Used for precise solutions to smaller instances
Christofides’ AlgorithmApprox.O(n3)O(n^3)Provides guarantees within an approximation factor; useful for metric TSPs
Nearest NeighborHeuristicO(n2)O(n^2)Fast but not optimal; typically forms part of more complex heuristics
Genetic AlgorithmsHeuristicO(gen×pop)O(gen \times pop)Useful for larger instances where optimality can be relaxed for speed

Conclusion

Understanding the Traveling Salesman Problem with examples of known global optima allows researchers to refine techniques and evaluate the performance of algorithms. As computing capabilities grow, new methods continue to improve the feasibility of solving larger instances accurately and efficiently. The TSP serves as a classic problem that embodies the challenges of combinatorial optimization in computing.


Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.