tsp_tour

Contents

tsp_tour#

graph_tool.structure.tsp_tour(g, src, weight=None, method='mst', improve=None, t0=None, alpha=0.99, niter=1000)[git][source]#

Return a traveling salesman tour of the graph.

Parameters:
gGraph

Graph to be used. The graph must be undirected.

srcVertex

The source (and target) of the tour.

weightEdgePropertyMap (optional, default: None)

Edge weights.

methodstr (optional, default: "mst")

Construction of the tour: "mst" (preorder of a minimum spanning tree), "christofides", or "greedy" (nearest neighbor).

improvestr (optional, default: None)

If given, the tour is improved with random 2-opt moves by "simulated_annealing" or "threshold_accepting".

t0float (optional, default: None)

Initial temperature (or threshold) of the improvement; if not given, 5% of the mean length of a tour step.

alphafloat (optional, default: 0.99)

Factor by which the temperature (or threshold) is multiplied after every \(V\) moves.

niterint (optional, default: 1000)

Number of sweeps of \(V\) moves of the improvement.

Returns:
tournumpy.ndarray

List of vertex indices corresponding to the tour.

See also

eulerian_path

Path that traverses every edge once

tournament_hamiltonian_path

Hamiltonian path of a tournament

minimum_spanning_tree

Minimum spanning tree used by the approximation

Notes

With method == "mst", the vertices are visited in the preorder of a minimum spanning tree of the graph (not of its metric closure, so that consecutive vertices need not be adjacent), which is guaranteed to be at most twice as long as the optimal tour for complete graphs with metric weights. The algorithm runs with \(O(E\log V)\) complexity.

The other methods work on the metric closure, i.e. on the shortest-path distances between all pairs of vertices, which take time \(O(V E)\) and memory \(O(V^2)\); consecutive vertices of the tour are joined by shortest paths in the graph. Christofides’ algorithm [christofides-worst-1976] adds a minimum-weight perfect matching of the odd-degree vertices of a minimum spanning tree and shortcuts an Eulerian circuit, which is within a factor \(3/2\) of the optimum, in time \(O(V^3)\). The nearest-neighbor tour visits next the closest unvisited vertex, in time \(O(V^2)\). Simulated annealing [kirkpatrick-optimization-1983] and threshold accepting [dueck-threshold-1990] reverse random segments of the tour, accepting increases in length with the Metropolis probability or below a threshold, both of which decrease geometrically; the best tour found is returned.

References

[christofides-worst-1976]

Nicos Christofides, “Worst-case analysis of a new heuristic for the travelling salesman problem”, Technical Report 388, Graduate School of Industrial Administration, Carnegie Mellon University (1976).

[kirkpatrick-optimization-1983]

S. Kirkpatrick, C. D. Gelatt and M. P. Vecchi, “Optimization by simulated annealing”, Science 220, 671-680 (1983), DOI: 10.1126/science.220.4598.671 [sci-hub, @tor]

[dueck-threshold-1990]

Gunter Dueck and Tobias Scheuer, “Threshold accepting: A general purpose optimization algorithm appearing superior to simulated annealing”, Journal of Computational Physics 90, 161-175 (1990), DOI: 10.1016/0021-9991(90)90201-B [sci-hub, @tor]

Examples

>>> g = gt.lattice([10, 10])
>>> tour = gt.tsp_tour(g, g.vertex(0))
>>> print(tour)
[ 0  1  2 11 12 21 22 31 32 41 42 51 52 61 62 71 72 81 82 83 73 63 53 43
 33 23 13  3  4  5  6  7  8  9 19 29 39 49 59 69 79 89 14 24 34 44 54 64
 74 84 91 92 93 94 95 85 75 65 55 45 35 25 15 16 17 18 27 28 37 38 47 48
 57 58 67 68 77 78 87 88 97 98 99 26 36 46 56 66 76 86 96 10 20 30 40 50
 60 70 80 90  0]

The other methods give shorter tours:

>>> g = gt.lattice([10, 10])
>>> d = gt.shortest_distance(g).get_2d_array(range(100))
>>> length = lambda t: d[t[:-1], t[1:]].sum()
>>> for m in ["mst", "greedy", "christofides"]:
...     print(m, length(gt.tsp_tour(g, 0, method=m)))
mst 160
greedy 108
christofides 108
>>> t = gt.tsp_tour(g, 0, method="christofides",
...                 improve="simulated_annealing")
>>> print(length(t))
100

The last tour can be drawn over the lattice:

>>> pos = g.new_vp("vector<double>",
...                vals=[(i % 10, i // 10) for i in range(100)])
>>> h = gt.Graph(list(zip(t[:-1], t[1:])), directed=False)
>>> gt.graph_draw(h, pos, vertex_fill_color="#cccccc",
...               edge_color="#e41a1c", edge_pen_width=2,
...               output="tsp_tour-lattice.pdf")
<...>
../_images/tsp_tour-lattice.png

Tour of a \(10\times 10\) lattice obtained with the Christofides algorithm and improved by simulated annealing, with its edges shown in red.#