Tutorials › Graph Theory (Undergraduate) › Metric TSP And Triangle-Inequality Shortcutting

The Traveling Salesman Problem · Tutorial 529 of 799

Metric TSP And Triangle-Inequality Shortcutting

Learn how the triangle inequality lets a metric TSP route that repeats vertices be replaced by a Hamiltonian tour without increasing its cost.

Advanced 10 min read

What You'll Learn

  • Define metric edge weights on a complete graph for the Traveling Salesman Problem.
  • Distinguish a closed walk visiting every vertex from a Hamiltonian tour.
  • Prove that the cost of a direct edge is no greater than the cost of a walk between its endpoints.
  • Construct a Hamiltonian tour by retaining vertices in order of first appearance.
  • Check the shortcutting cost bound on explicit metric examples.
  • Identify how a nonmetric edge weight can make shortcutting increase the cost.

Metric Costs and Repeated Visits

A route that visits every city need not visit each city exactly once. It may return to a city already visited, then continue to a city it has not yet reached. For metric Traveling Salesman Problems, repeated visits can be removed without increasing the route cost. The essential condition is the triangle inequality: travelling directly between two vertices costs no more than travelling between them through an intermediate vertex.

Throughout, let \(n\ge 3\). The underlying graph is the complete undirected simple graph \(G=(V,E)\), where \(|V|=n\) and \(E\) contains every unordered pair of distinct vertices. Thus there is an edge between every pair of different vertices. An edge cost is a function \(c\) assigning a real number to each such edge.

Definition: The edge costs are metric if they come from a function \(c:V\times V\to\mathbb{R}\) satisfying \(c(u,u)=0\), \(c(u,v)=c(v,u)>0\) whenever \(u\ne v\), and \(c(u,w)\le c(u,v)+c(v,w)\) for every \(u,v,w\in V\). The cost of a route is the sum of the costs of its traversed edges, counted with multiplicity.

A closed walk visiting all vertices is a sequence \(w_0,w_1,\ldots,w_\ell\) with \(w_\ell=w_0\), consecutive vertices adjacent, and every vertex of \(V\) appearing in the sequence. It may repeat vertices and edges. In contrast, a Hamiltonian tour has the form \(v_0,v_1,\ldots,v_{n-1},v_0\), where \(v_0,\ldots,v_{n-1}\) are distinct and include every vertex. Its cost is \(\sum_{i=0}^{n-1}c(v_i,v_{i+1})\), with \(v_n=v_0\). The final return to \(v_0\) is part of the tour; a longer closed walk with further visits is not a Hamiltonian tour.

Cost Along a Walk

The triangle inequality applies not only to one intermediate vertex, but to an entire walk. This fact is what permits a whole section of a route to be replaced by one edge.

Lemma: If \(x_0,x_1,\ldots,x_k\) is a walk in a metric complete graph, then \(c(x_0,x_k)\le \sum_{j=0}^{k-1}c(x_j,x_{j+1})\).

Proof. We use induction on the number \(k\ge1\) of edges in the walk. If \(k=1\), the two sides are equal. Suppose \(k\ge2\) and the assertion holds for walks with \(k-1\) edges. The triangle inequality, applied with intermediate vertex \(x_{k-1}\), gives \(c(x_0,x_k)\le c(x_0,x_{k-1})+c(x_{k-1},x_k)\). By the induction hypothesis, \(c(x_0,x_{k-1})\le\sum_{j=0}^{k-2}c(x_j,x_{j+1})\). Adding \(c(x_{k-1},x_k)\) proves the claimed bound. The argument allows repeated vertices: the metric definition includes \(c(u,u)=0\), so such repetitions cause no exception. \(\square\)

Shortcutting to a Hamiltonian Tour

Theorem: In a complete graph with metric edge costs, every closed walk visiting all \(n\ge3\) vertices can be replaced by a Hamiltonian tour of no greater cost.

Proof. Let \(w_0,w_1,\ldots,w_\ell\) be a closed walk visiting all vertices, with \(w_\ell=w_0\). For each vertex, record its first appearance among \(w_0,\ldots,w_{\ell-1}\). List these first appearances in order as \(w_{i_0},w_{i_1},\ldots,w_{i_{n-1}}\), where \(i_0=0<i_1<\cdots<i_{n-1}<\ell\). This list contains every vertex exactly once. Form the tour that follows this order and then returns from \(w_{i_{n-1}}\) to \(w_0\).

For each \(r\) with \(0\le r<n-1\), the part of the original walk from \(w_{i_r}\) to \(w_{i_{r+1}}\) has cost \(\sum_{j=i_r}^{i_{r+1}-1}c(w_j,w_{j+1})\). By the Lemma, this is at least \(c(w_{i_r},w_{i_{r+1}})\), the cost of the tour edge that replaces it. The final part of the walk runs from \(w_{i_{n-1}}\) to \(w_\ell=w_0\); the Lemma shows that its cost is at least \(c(w_{i_{n-1}},w_0)\). These parts partition all edges of the original walk. Adding their inequalities shows that the constructed Hamiltonian tour costs no more than the walk. Its listed vertices are distinct and include all of \(V\), and its last edge returns to its starting vertex, so it is a Hamiltonian tour as defined. \(\square\)

The construction keeps the first occurrence of each vertex and replaces each intervening section of the walk by a direct edge. Completeness guarantees that each replacement edge exists; the metric condition guarantees that its cost does not exceed the cost of the section it replaces.

Worked Example: Shortcutting a Walk on a Line Metric

Let \(V=\{a,b,c,d\}\), let \(E\) contain all six pairs of distinct vertices, and assign coordinates \(a=0,b=1,c=3,d=4\). Give each edge \(uv\) cost \(c(u,v)=|\text{coordinate}(u)-\text{coordinate}(v)|\). The pair costs are \(c(a,b)=1\), \(c(a,c)=3\), \(c(a,d)=4\), \(c(b,c)=2\), \(c(b,d)=3\), and \(c(c,d)=1\). Absolute distance on the line is symmetric, positive for distinct coordinates, and satisfies the triangle inequality, so these costs are metric.

Consider the closed walk \(a,b,c,b,d,a\). Its five traversed edge costs, in order, are \(1,2,2,3,4\), so its total cost is \(1+2+2+3+4=12\). The first appearances give the order \(a,b,c,d\), hence the shortcut tour is \(a,b,c,d,a\). Its edge costs are \(c(a,b)=1\), \(c(b,c)=2\), \(c(c,d)=1\), and \(c(d,a)=4\), giving total cost \(1+2+1+4=8\).

Check: The original walk splits into sections \(a\) to \(b\), \(b\) to \(c\), \(c,b,d\), and \(d\) to \(a\), with costs \(1,2,5,4\). Their sum is \(12\). The corresponding tour edges cost \(1,2,1,4\), with sum \(8\); the only nontrivial replacement uses \(c(c,d)=1\le c(c,b)+c(b,d)=2+3=5\). The computed tour has four distinct vertices before its return to \(a\), so it is Hamiltonian.

Worked Example: Several Repeated Visits

Let \(V=\{x_0,x_2,x_5,x_9,x_{14}\}\), with \(E\) containing all ten pairs of distinct vertices. Assign each vertex its subscript as its coordinate on the real line and define \(c(x_i,x_j)=|i-j|\). These costs are metric by the triangle inequality for absolute value. Consider the closed walk \(x_0,x_2,x_5,x_2,x_9,x_{14},x_9,x_0\).

The successive edge costs are \(2,3,3,7,5,5,9\), so the walk costs \(2+3+3+7+5+5+9=34\). Its first appearances give the order \(x_0,x_2,x_5,x_9,x_{14}\). The resulting tour has costs \(2,3,4,5,14\), for a total of \(2+3+4+5+14=28\).

Check: The segment from \(x_5\) through \(x_2\) to \(x_9\) costs \(3+7=10\), while the replacement edge \(x_5x_9\) costs \(4\). The segment \(x_{14},x_9,x_0\) costs \(5+9=14\), equal to the closing edge \(x_{14}x_0\). The other retained consecutive sections have costs \(2\) and \(3\), unchanged. Thus the tour total is \(2+3+4+5+14=28\le34\), and it visits each of the five vertices exactly once before returning.

Why the Metric Condition Matters

The theorem depends on the triangle inequality, not merely on having costs assigned to every edge. Without that condition, an edge that skips part of a walk can be more expensive than the section it replaces. The next example shows that even positive, symmetric costs on a complete graph are not enough.

Worked Example: Shortcutting Can Fail Without the Triangle Inequality

Let \(V=\{a,b,c\}\), \(E=\{ab,ac,bc\}\), and assign costs \(c(a,b)=1\), \(c(a,c)=1\), and \(c(b,c)=10\). These costs are positive and symmetric, but they are not metric because \(10=c(b,c)>c(b,a)+c(a,c)=2\).

The closed walk \(a,b,a,c,a\) visits all three vertices and has cost \(1+1+1+1=4\). Retaining vertices in order of first appearance gives the Hamiltonian tour \(a,b,c,a\), of cost \(1+10+1=12\). The other orientation, \(a,c,b,a\), also costs \(1+10+1=12\), so every Hamiltonian tour costs more than this particular covering walk.

Check: The increase from \(4\) to \(12\) occurs when the two-edge section from \(b\) through \(a\) to \(c\), of cost \(1+1=2\), is replaced by edge \(bc\), of cost \(10\). This replacement violates the inequality required by the Lemma. Therefore the metric hypothesis cannot simply be omitted.

What Shortcutting Does—and Does Not—Say

One consequence is that, in a metric TSP instance, allowing a closed route to repeat vertices does not produce a lower minimum cost than requiring a Hamiltonian tour. Every Hamiltonian tour is itself a closed walk visiting all vertices. In the other direction, the theorem converts any such closed walk into a tour of no greater cost. Thus the least cost among Hamiltonian tours is no greater than the cost of any covering closed walk, while tours are already among the covering closed walks. These two comparisons give equality of the minimum costs.

This conclusion concerns the complete graph with metric costs. If a graph is not complete, a shortcut edge may not exist. If the costs are not metric, that edge may exist but be too expensive. Nor does shortcutting say that every repeated visit can be removed while keeping an arbitrary prescribed order of all other visits. The construction instead takes the vertices in their order of first appearance and uses the metric inequality to bound the resulting tour cost.

Common mistake. It is incorrect to argue that a repeated-vertex walk can always be shortened because “the direct edge is shorter.” In the nonmetric example, the direct edge \(bc\) costs \(10\), while the route \(b,a,c\) costs \(1+1=2\). The valid correction is to invoke the triangle inequality for each replaced section; without it, no nonincrease is guaranteed. It is also important to check the endpoint of the tour: after listing the \(n\) distinct vertices, the sequence must return immediately to its starting vertex, with no extra visits.

Check Your Understanding

Use the definitions and the shortcutting argument to answer the following.

  1. State all the conditions on \(c\) that make edge costs metric.
  2. For a closed walk that visits every vertex, how are the retained vertices ordered in the construction?
  3. Why does the proof compare each new tour edge with an entire section of the original walk rather than only with one edge?
  4. In the nonmetric example, identify the precise triangle inequality that fails and the cost increase it permits.
  5. Explain why the constructed sequence \(v_0,\ldots,v_{n-1},v_0\) is a Hamiltonian tour, rather than merely a longer closed walk that happens to contain every vertex.