The traveling salesman problem (or TSP for short) is a well-studied problem in computer science and graph theory: Given a graph and a cost function on the edges , what is the cheapest cycle that visits all vertices of the graph?
Finding such cycle is very useful on its own, but it also has many applications and usages in other problems. An obvious use case would be planning the route for package delivery service at the beginning of the day, such that all packages are delivered and the minimum amount of fuel is used. Another more interesting application would be the design of a network or circuit board, aiming to optimize the efficiency and data transition times, while minimizing the size and costs. TSP also appears as a sub-problem in many other fields that may not seem related at all at first, such as DNA sequencing and astronomy.[1]
TSP is HARD.
Theoretical computer scientists love to study problems and the relations between them, in terms of their complexity. This field of computer science is called computational complexity theory. It turns out that the TSP problem is hard (NP-Hard). Not only that, the problem of determining if a given cycle (which visits all vertices of the graph) is the cheapest cycle, is hard too (NP-Complete). And if that wasn’t enough, it has been proven that TSP can’t be approximated ( approximation in polynomial time), even with “easier” versions of TSP when we allow some constraints on the structure of the graph and the cost function. This fact is known as the inapproximability of the TSP problem.[2]
So, is all hope lost?
Well, not exactly. In this article, I will present the Christofides Algorithm[3], which is a classic approximation algorithm in the field of combinatorial optimization. The algorithm does not solve the general TSP problem (because it’s hard!), but rather the Metric TSP Problem, which is an easier variant of the problem that provides some constraints on the input graph and cost function. Since we are talking in the context of graph theory, I present those constraints in their graphic definition:
The graph is a clique, meanings that there is an edge between every two vertices in . A formal way to say that would be that , where denotes the cartesian product of two groups.
The costs function satisfies the triangle inequality: when are vertices .
Although it is not as general as the non-metric TSP problem, Metric TSP is still a very interesting problem with many applications. In fact, most of the applications I’ve listed in the introduction are actually cases where solving the Metric TSP is sufficient. For example, it is not difficult to prove that the following two distance functions between two points on a plane and satisfy the inequality:
Throughout this article, I will use the phrases cost, weight or distance interchangeably. All of them refer to the same function . The cost of a collection of edges (cost of a path or a cycle) is defined as the sum of costs over all edges in the collection.
Approximation Algorithms
In the field of theoretical computer science, and especially in combinatorial optimization, we usually try to solve hard (NP-Hard) problems using approximation algorithms. I won’t dive too deeply into computational complexity here, but in general, when (and if) we know that a certain optimization problem is hard to solve exactly in polynomial time, we can instead solve it approximately. Such a solution may not be the best solution, but we would still want to prove some bound on “how good the solution is with respect to the optimal solution”. There are multiple ways that one can do so, but in this article, we will use an approximation factor.[6]
We say that an algorithm is an -approximation algorithm (factor approximation) for a problem if and only if for every instance of the problem, our algorithm will find a solution within a factor of of the optimum solution.
Since TSP is a minimization problem (we want to find a TSP cycle with minimal cost), then , and such algorithm will always find a cycle whose total cost is at most times the cost of the optimal TSP cycle.[7]
Cycle Shortcutting
One very useful property that we will use in the algorithm analysis is that under the metric setting, any two consecutive edges in a path can be shortened into a shorter path by skipping over the middle vertex. This is possible since the graph is complete, so an edge always exists, and because of the triangle inequality, we know that .
From now on, given a graph we will denote an optimal TSP Cycle of it with . Such a path is an ordered set of edges of the graph, where each edge is directed, and every two adjacent edges in the cycle are of the form , for some (notably, adjacent edges share a vertex). Additionally, of course, the first and last vertices are the same.
The cycle must visit every vertex at least once (by definition), so it must contain at least edges. On the other hand, if contains more than edges, then there exists a vertex that appears twice in , and one of those appearances can be skipped, resulting in a shorter (or equally weighted) cycle. Hence, there must exist an optimal TSP cycle with exactly edges.
Bounding TSP with MST
It turns out that the cost of the minimum spanning tree of a graph is actually a lower bound on , meaning that . This is because removing any edge from results in a tree whose edges are . This tree spans all vertices of the original graph and thus is a spanning tree. By definition, the weight of is minimal across all spanning trees of , and thus . From that we easily show that the cost of is at least :
Unlike TSP, finding the MST of a graph is considered easy, and there are multiple efficient, popular algorithms that do that (Prim’s, Kruskal’s, Borůvka’s). This is very promising! We now have a building block (MST) that we can easily compute, which resembles a TSP cycle very well and is guaranteed to have a lower cost than the optimal TSP.
An Approximation Using MSTs
Using this fact, we can now show a fairly straightforward -approximation for the Metric TSP problem:
- Find the MST of , and denote it as .
- Construct a new graph where is the multiset that contains every edge of exactly twice. In other words, is the tree where all of its edges are duplicated.
- Find an Eulerian cycle of and denote it as .
- Shortcut the cycle to a cycle containing exactly unique vertices.
Correctness
It is known that an Eulerian cycle can be found in a connected graph if and only if the degree of all vertices is even. Since is the tree where each edge is duplicated, the degree of all vertices of must be even. Besides that, every Eulerian cycle of a connected graph with vertices of even degree is a valid TSP Cycle since it must visit all vertices. We apply shortcutting only if it leaves the cycle a valid TSP Cycle, and thus the algorithm yields a valid TSP Cycle as required.
Approximation Factor
Hence this simple approximation algorithm actually returns a cycle that costs at most twice as the optimal TSP cycle.
Runtime Analysis
Since both finding an MST and an Eulerian cycle of a graph are easy, polynomial problems, and , , , and are all of polynomial size relative to the size of the input graph , the resulting algorithm is polynomial as well; I will leave it to the reader to provide a strict bound.
The Problem of the Naïve Approximation
We have the MST as a lower bound on the optimal TSP Cycle. The problem is that the MST is not a cycle; by duplicating the edges of the tree and finding the Eulerian cycle in the new graph , we convert the tree, in a fairly clever manner, into a cycle as desired.
Sadly, this conversion costs a lot in the approximation factor. As mentioned before, all that is required from is that all vertices of it are of even degree. By duplicating all edges of we obviously guarantee that, but do we have to duplicate all edges of the MST? Why not leave even vertices untouched, and modify only the bad vertices? Additionally, vertices with degree 5 (for example) would be of degree 10 after duplication, but degree 6 would suffice, as that is also even.
The Handshaking Lemma
The Handshaking lemma states that in any undirected graph, the number of vertices of odd degree is even. The following is a sketch of the proof; Consider a general undirected graph . Since every edge in a graph contributes exactly twice to the degree of some two vertices in the graph, we get that
meaning that the sum of degrees across all vertices in the graph is even. If we partition into two sets , then we get
Hence must be even too. By definition, all elements in the sum are even, meaning that must be even, as required.[8]
Recall that in the naïve algorithm, we have the MST and we want to convert it to a graph with even vertices only. By the Handshaking lemma, we know that the number of bad vertices (odd degree vertices) must be even, which is a crucial observation towards Christofides’ Algortihm.
Finally… Christofides’ Algorithm
Christofides’ algorithm is an elegant and revolutionary algorithm that provides a -approximation for the Metric TSP problem. In other words, it outputs a cycle of the graph where the cost of the cycle (which is defined by the sum of all costs of edges in the cycle), is at most 50% more than the cost of the shortest cycle possible.
The algorithm steps follow:
- Find the MST of , and denote it as .
- Denote with the set of vertices with odd degree in relation to .
- Find a Minimum Weight Perfect Matching (MPM) of vertices , and denote such matching with .
- Construct a new graph , merging the MST with the MPM, where is a multiset that may contain the same edge more than once.
- Find an Eulerian cycle of and denote it as .
- Shortcut the cycle to a cycle containing exactly unique vertices.
Correctness
The key to the correctness of the algorithm is the handshaking lemma, which states that in all undirected graphs, the number of vertices of an odd degree is even. This means that a perfect matching of the odd degree vertices always exists since the graph is a clique.
The matching adds exactly one edge endpoint to all vertices of odd degree in the MST , making the union of the MST and MPM an undirected graph with even degree vertices only. This, as discussed before, is sufficient for finding an Eulerian cycle in , which is a valid TSP cycle. The way we apply shortcutting keeps the cycle a TSP cycle and thus the algorithm yields a valid TSP Cycle as required.
Approximation Factor
Consider an optimal TSP cycle . By shortcutting, we can transform such cycle to a new cycle of even length , that visits only vertices of odd degree of (meaning that they are ) and skip over vertices of even degree. We only use shortcuts to get to from , and hence .
We can now partition the cycle into two different matchings of vertices . Without loss of generality, denote where is even. Then,
By this construction, we get that the edges of the cycle are exactly . Since both and are perfect matchings of all vertices , they both serve as upper bounds on the minimum weight perfect matching of those vertices. We get that:
Hence we get that . Since the algorithm yields an Eulerian cycle of edges of the MST joined with , we get
Hence the algorithm yields a cycle which costs at most times the cost of the optimal TSP cycle , meaning that the algorithm has an approximation factor of as required.
Runtime Analysis
The analysis is similar to the naïve algorithm analysis, with the addition of finding the minimum weight perfect matching. Using Edmond’s Blossom algorithm, it is possible to find the minimum weight perfect matching in a polynomial time. I won’t dive into Edmond’s algorithm in this article; It turns out that finding the matching is actually the bottleneck of Christofides’ algorithm, and it too runs in .
Christofides in Practice
When I first saw Christofides’ algorithm, I thought to myself that the 1.5 approximation factor is pretty 💩! I mean, if I was the head of some delivery company, I would surely not be happy to know that the route planning algorithm can be improved by 33%. The practical implications of how close we can approximation TSP is huge in the real world.
Well, remember that 1.5 is an upper bound on the approximation ratio. In practice, especially on large dense graphs, the algorithm yields a cycle that is not that far from the optimal cycle on average. Try it yourself! play with the simulation above, and try to get it to output a cycle that is clearly not close to the optimal. Note that since computing the actual optimal TSP cycle is exponentially hard, comparing the approximation to the optimal TSP cycle is emitted from the simulation.[9]
Does a better approximation exist?
Christofides published his paper in 1976, and for almost 50 years (!) it was the best-known approximation algorithm for the Metric TSP problem. In 2020 however, a paper that consists of 90 pages was published with the following abstract: “For some we give a randomized approximation algorithm for metric TSP.”[10]
Although the algorithm is much more complex (I did not even try to understand it), and the approximation improvement is clearly not sufficient, it actually proves that is not the lower bound of the approximation factor. This gives hope that an efficient and elegant algorithm exists whose factor is a magnitude better than , and we just haven’t discovered it yet. The paper was published at STOC’21 and for this reason, received the best paper award.
References and Further Reading
I was heavily inspired to write this post after a lecture about the topic of Prof. Feldman Moran in the course Combinatorial Optimization at the University of Haifa, spring 2023. Proofreading was done by Almog Ben Chen. Thanks!
- Travelling salesman problem Wikipedia ↩
- New Inapproximability Bounds for TSP Marek Karpinski, Michael Lampis, Richard Schmied ↩
- Worst-case analysis of a new heuristic for the traveling salesman problem Nicos Christofides ↩
- Taxicab geometry Wikipedia ↩
- Euclidean distance Wikipedia ↩
- Approximation Algorithms Wikipedia ↩
- Discrete Mathematics and Algorithms (Lecture notes) Reza Zadeh ↩
- Handshaking Lemma Wikipedia ↩
- A Probabilistic Analysis of Christofides' Algorithm Markus Blaser, Konstatinos Panagiotou, B. V. Raghavendra Rao ↩
- A (Slightly) Improved Approximation Algorithm for Metric TSP Anna R. Karlin, Nathan Klein, Shayan Oveis Gharan ↩