One can also represent the problem using a graph, where the vertices represent cities and the edge weights represent inter-city distances. For non-adjacent vertices, the inter-city distances can be defined using some rule. For example, one might define them to be the shortest path distances in the graph, or one might make them infinite.
A solution to a TSP instance can be expressed as a list of vertices that includes every vertex exactly once. The length of the tour is the sum of the distances linking successive vertices. Alternatively, it can be expressed as a list of the graph edges used to pass through the cities in the list.

Not only is the problem of finding an optimum TSP solution $NP$-hard, the problem of finding a good worst-case approximation is also $NP$-hard. Fortunately, there are important special cases for which approximate solutions are possible, and even in the more general case, there are algorithms that work well on random graphs.
Doubling the edges of an MST yields an Eulerian graph in which every vertex has even degree. An Euler tour of a such a graph is a cycle that uses all its edges, and can be constructed in time proportional to the number of edges. An Euler tour can be converted to a TSP tour by simply listing the vertices in the order in which they first appear in the Euler tour. Since the TSP distances satisfy the triangle inequality, the length of the TSP tour can be no more than the length of the Euler tour, and consequently no more than twice the length of an optimal TSP tour.
Christofides algorithm [Christofides76] is a refinement of the MST approximation that uses a different Eulerian graph. Specifically, Christofides algorithm computes a complete graph on the MST vertices of odd degree and assigning each edge a weight equal to the shortest path distance joining its endpoints in the TSP graph. If the edges in a perfect matching on this graph are added to the edges in the MST, the resulting graph is Eulerian. If the perfect matching has minimum weight, its weight is at most half the length of an optimal TSP tour. So the weight of the Eulerian graph constructed using a minimum weight matching is at most 50% larger than the optimal tour length.
A Javascript implementation of this algorithm appears below.
Note that the TSP problem is specified as a graph which need not be complete and the returned value includes all the edges on the computed tour. If the provided graph is not complete, the tour may include “implicit edges” that were not explicitly present in the graph. Any such edges that are needed to complete the tour are added to the graph and assigned a length equal to the shortest path distance between the endpoints. This ensures that the graph and the tour are consistent with one another.
The following script can be used to demonstrate the program.
let n = 12; let d = 3; let scale = .5; let wtmax = 9;
let [g,seed,seedLength] = tspRandom(n,d,scale,[randomInteger,1,wtmax]);
let [[u0,tour], ts, stats] = tspC(g, 1);
log(ts + `\nseed: ${g.elist2string(seed,0,0,1)} ${seedLength}\n` +
`\nfinal graph: ${g.toString(1)}`);
The function tspRandom generates a random graph with a
built-in “seed tour”.
Edges are assigned a random weight and the edge weights
on the tour are then multipled by a scale factor of 0.5.
Finally, any edge weights that violate the triangle inequality are
adjusted to comply with it. The returned value includes the generated
graph and the length of the built-in tour.
Here is a sample output from the script.
graph: {
a[d:4 f:4] b[c:2 e:4] c[b:2 k:2] d[a:4 i:3]
e[b:4 f:5 j:5] f[a:4 e:5 h:1 j:4 l:4] g[i:3 k:7 l:1] h[f:1 k:1]
i[d:3 g:3] j[e:5 f:4 k:2 l:4] k[c:2 g:7 h:1 j:2 l:6] l[f:4 g:1 j:4 k:6]
}
mst: [bc ck ad eb fa ig hf di jk kh gl] 27
matching: [ek jl] 11
tsp tour: b [bc ck jk lj gl ig di ad fa hf he eb] 36
seed: [fa ad di ig gl lj je eb bc ck kh hf] 34
final graph: {
a[d:4 f:4] b[c:2 e:4] c[b:2 k:2] d[a:4 i:3]
e[b:4 f:5 h:6 j:5] f[a:4 e:5 h:1 j:4 l:4] g[i:3 k:7 l:1] h[e:6 f:1 k:1]
i[d:3 g:3] j[e:5 f:4 k:2 l:4] k[c:2 g:7 h:1 j:2 l:6] l[f:4 g:1 j:4 k:6]
}
The trace output shows the edges in the MST and their total weight, the edges in the matching and the final tour. The seed tour built into the random graph is also shown, along with the final version of the graph. Note that the tour includes edge $he$ which was not explicitly represented in the original graph, but is included in the final version of the graph.
The length of the computed tour is about 6% less than the total weight of the MST and matching and about 6% larger than the length of the seed. The MST implies a lower bound of 27 on the length of an optimal tour. The actual optimal tour length lies somewhere between this lower bound and the length of the seed.
Here are some results for a larger graph (100 vertices and 500 edges) with a scale factor of .1 and a maximum edge weight of 99. The resulting seed tour lengths range from 493 to 547.
100 10 0.1 99 547 {"mst":412,"match":191,"tour":587}
100 10 0.1 99 493 {"mst":357,"match":191,"tour":544}
100 10 0.1 99 501 {"mst":365,"match":169,"tour":524}
100 10 0.1 99 527 {"mst":392,"match":189,"tour":561}
The computed tour lengths range from 524 to 587 and the ratios of the tour lengths to the lower bounds range from 1.39 to 1.43. The ratios of the computed tour lengths to the seed tour lengths range from 1.05 to 1.1.
Karp's algorithm starts by first solving a weighted bipartite matching problem. Specifically, if the TSP is defined by a weighted graph $G=(V,E)$ with $n$ vertices and $m$ edges, the bipartite graph $B=(W,F)$ has $2n$ vertices and $m$ edges, where for each edge $(v_i,v_j)\in E$ there is a corresponding edge $(w_i,w_{n+j})\in F$. A minimum weight matching in $B$ defines a minimum length collection of vertex-disjoint cycles in $G$. Since a TSP tour is a special case of such a collection, the length of the optimum TSP tour can be no smaller than the weight of such a matching. This is illustrated below. Note that the edge lengths are identified by the edge colors.

What remains is to combine the cycles into a single tour using a patching operation. If two distinct cycles contain edges $(u,v)$ and $(x,y)$ and there are also edges cross edges $(u,y)$ and $(x,v)$, the cycles can be combined by replacing $(u,v)$ and $(x,y)$ with the cross edges, at a cost equal to the difference in length between the two edge pairs. This is illustrated below

Karp showed how to select the patching operations by solving a second bipartite matching problem $P$ with a vertex for each of the “small cycles” and another for each of the edges in the large cycle. For every edge $(u,v)$ in a small cycle $c$ that can be used with an edge $(x,y)$ in the large cycle to combine the two, an edge from $c$ to $(x,y)$ is included in $P$. The weight of this edge is the incremental cost of the patching operation. If a maximum size matching of minimum weight on $P$ matches every small cycle, then the matching gives a minimum cost strategy for patching all the small cycles into the large one. The earlier example is continued below, showing the patching graph with edges joining the cycle [de ed] to the large cycle at edge $ca$ and cycle [gf$\,$ fg] at edge $bc$. In this case the patching graph is a matching, and both small cycles can be patched into the large one. The right side of the figure shows how one patching operation replaces the cycle edges $ed$ and $ca$ with the cross edges $ea$ and $cd$.

Completing the second patching operation produces the tour [ab bf fg gc cd de ea] with a total length of 15. Note that the optimum tour is [ae ed dc cg gf fb ba] which has length 12 and that Karp's algorithm produces the same result even if the lengths of edges $cd$ and $gc$ are arbitrarily large. So, it cannot approximate the optimum tour within any constant bound. On the other hand, Karp showed that for very large complete random graphs with uniformly distributed edge weights, the largest cycle defined by the matching includes most of the vertices in the graph with high probability and that the limited number of smaller cycles can patched into the largest with only a small cost penalty.
A Javascript implementation of Karp's algorithm is shown below. This implementation extends Karp's algorithm in two ways. First, if the standard matching process is unable to combine all the cycles into one, then a second attempt is made to merge the remaining small cycles. This second attempt repeatedly looks for the least cost patching opportunity between the small cycles and the large one, and carries out the corresponding patching operation. It can sometimes succeed when the original algorithm fails, since it can take advantage of new patching opportunities that may arise from earlier patching operations.
If the second attempt to complete the tour fails, the remaining cycles are merged with the large one by extending the initial graph with edges of infinite length that can be used as cross edges in patching operations. This is equivalent to the interpretation that the “missing edges” are just a way of representing infinite inter-city distances.
The program can be demonstrated using the following script.
let n = 12; let d = 4; let scale = .5;
let [g,seed,seedLength] = tspRandom(n,d,scale,[randomInteger,1,9],1,0);
let [[u0,tour],ts] = tspK(g,1);
log(ts + `\nseed: ${g.elist2string(tour,0,0,1)} ${seedLength}\n`);
The tspRandom function call uses two additional arguments.
The first specifies that the returned graph should define an
asymmetric instance (implying a directed graph).
The second specifies that the edge lengths need not satisfy
the triangle inequality.
A sample result appear below.
graph: {
a[e:1] b[c:3 k:5] c[d:3 g:4 h:5 i:3 j:3]
d[b:1 e:4 f:5 h:5 k:4] e[c:6 h:6 i:5 k:2] f[a:5 b:4 c:4 i:6]
g[a:8 c:4 f:6 i:8 j:5 k:3 l:5] h[d:6 g:6 i:3] i[c:7 j:3 k:8 l:4]
j[b:4 c:3] k[b:4 e:6 f:2 h:2 i:2] l[a:7 f:5 g:3 h:6 i:1 k:7]
}
initial cycles: b[bc cd dh hi ij jb] a[ae ek kf fa] g[gl lg] 39
potential patching edge sets
ek bc ec bk 6
ek dh eh dk 3
kf dh kh df 0
gl ij gj il 1
lg hi li hg 1
updated cycle sets
a[ae ek kh hi ij jb bc cd df fa] g[gl lg] 39
a[ae ek kh hg gl li ij jb bc cd df fa] 40
tsp tour: a[ae ek kh hg gl li ij jb bc cd df fa] 40
seed: a[ae ek kh hg gl li ij jb bc cd df fa] 40
In this case, the intial collection of cycles includes three cycles, with a total length of 39. Each cycle is shown as a list of edges, preceded by its initial vertex. The edge groups that define potential patching operations are shown, along with the incremental cost of each operation. The first of the small cycles can be patched into the large one in three different ways. The second can be patched in two different ways. The collection of cycles is shown after each patching operation, with the updated length. The computed tour turns out to be the seed tour.
The following script can be used to explore how the algorithm performs under different conditions.
let n = 100; let d = 15; let scale = .1; let wtmax = 99;
let [g,seed,seedLength] = tspRandom(n,d,scale,[randomInteger,1,wtmax],1,0);
let t = Date.now(); let [[u0,tour], ts, stats] = tspK(g); t = Date.now() - t;
log(n, d, scale, wtmax, 'cycleStats=['+stats.cycleStats+']',
'perfRatio='+(stats.tourLength/stats.cycleStats[2]).toFixed(2),
'seedLength='+seedLength, t+'ms');
Some sample results are shown below. Each group of four uses the same
parameters and gives some sense of the performance variability.
As the graph density is increased, the scale factor for the seed tour
is decreased. The cycleStats include the initial length
of the largest cycle, the number of cycles and the total length of the initial
set of cycles. The performance ratio
is the ratio of the computed tour length to the length of the initial cycles.
100 2 0.5 99 cycleStats=[100,1,2215] perfRatio=1.00 seedLength=2215 27ms 100 2 0.5 99 cycleStats=[100,1,2267] perfRatio=1.00 seedLength=2267 27ms 100 2 0.5 99 cycleStats=[100,1,2635] perfRatio=1.00 seedLength=2635 27ms 100 2 0.5 99 cycleStats=[ 68,3,2600] perfRatio=Infinity seedLength=2684 30ms 100 5 0.25 99 cycleStats=[ 54,5,1257] perfRatio=Infinity seedLength=1263 28ms 100 5 0.25 99 cycleStats=[ 90,2,1274] perfRatio=Infinity seedLength=1299 38ms 100 5 0.25 99 cycleStats=[ 40,3,1163] perfRatio=1.06 seedLength=1177 38ms 100 5 0.25 99 cycleStats=[ 78,2,1238] perfRatio=1.08 seedLength=1289 38ms 100 10 0.15 99 cycleStats=[ 51,4, 717] perfRatio=Infinity seedLength= 738 43ms 100 10 0.15 99 cycleStats=[ 38,4, 732] perfRatio=1.07 seedLength= 744 37ms 100 10 0.15 99 cycleStats=[ 81,3, 734] perfRatio=1.11 seedLength= 781 43ms 100 10 0.15 99 cycleStats=[ 97,2, 725] perfRatio=1.00 seedLength= 737 40ms 100 15 0.1 99 cycleStats=[ 55,3, 447] perfRatio=1.16 seedLength= 462 33ms 100 15 0.1 99 cycleStats=[ 49,3, 467] perfRatio=1.03 seedLength= 508 44ms 100 15 0.1 99 cycleStats=[ 43,4, 466] perfRatio=1.23 seedLength= 487 43ms 100 15 0.1 99 cycleStats=[ 48,4, 493] perfRatio=1.12 seedLength= 532 51ms 100 99 0.05 99 cycleStats=[ 46,4, 234] perfRatio=1.12 seedLength= 256 76ms 100 99 0.05 99 cycleStats=[ 89,4, 233] perfRatio=1.26 seedLength= 261 63ms 100 99 0.05 99 cycleStats=[ 43,3, 248] perfRatio=1.03 seedLength= 289 67ms 100 99 0.05 99 cycleStats=[ 68,3, 226] perfRatio=1.04 seedLength= 274 62ms
At very low densities, the algorithm usually discovers the seed tour in the first step. However, when the initial set of cycles is shorter than the seed tour, it is likely to be unable to patch the cycles together without adding infinite length cross edges. This occurs more often at slightly higher densities, but then becomes less frequent as the densities continue to increase (as there are more cross edges available for patching). When the algorithm produces a finite length tour, its length is usually close to the lower bound, regardless of the graph density.