TravelingSalesman - Maple Help
For the best experience, we recommend viewing online help using Google Chrome or Microsoft Edge.

Online Help

All Products    Maple    MapleSim


GraphTheory

  

TravelingSalesman

 

Calling Sequence

Parameters

Options

Description

Examples

Compatibility

Calling Sequence

TravelingSalesman(G, opts)

TravelingSalesman(G, M, opts)

Parameters

G

-

a connected graph or digraph

M

-

(optional) a Matrix containing edge weights

opts

-

zero or or more options as specified below

Options

• 

metric = positive number or one of the symbols Euclidean, Manhattan, discrete, geographical, or infinity.

  

The parameter metric must be a positive number or one of the symbols listed above. This specifies the metric to be used in computing distances when the option vertexpositions is set to a non-default value.

  

When metric=geographical, the vertex positions must be 2-D and are interpreted as geographical positions on the Earth given as latitudes and longitudes in degrees. Entries in the weight matrix correspond to the distance between points in kilometers.

  

For more information on norms, see LinearAlgebra[Norm].

  

The default metric is 2, the metric induced by the Euclidean norm.

• 

seed = integer or none

  

Seed for the random number generator.

• 

solver=one of concorde or legacy

  

Specifies the solver to use. The default, solver=concorde, uses the Concorde TSP solver, a well-known and highly efficient optimization library for TSP instances. Specifying solver=legacy dispatches to a pure-Maple TSP solver.

• 

startvertex=a valid vertex in G

  

Specifies the starting vertex for the Hamiltonian cycle. If provided, the algorithm will only consider cycles or paths beginning at startvertex.

• 

vertexpositions=truefalse, Matrix, Array, or listlist

  

When set to true or a list, Array, or Matrix, this specifies that the edge weights used in computing a tour correspond to the geometric distances between the vertex positions in a particular layout. The metric used in computing these distances is controlled by the metric option.

  

When a Matrix, Array, or list is provided corresponding to a set of 2-D or 3-D points, this is taken to specify the positions of the vertices in vertex order.

  

When vertexpositions=true, any existing vertex positions stored in G are used, if they exist.

  

When vertexpositions=false, the edge weights from the graph or from the specified matrix M are used. This is the default.

Description

• 

TravelingSalesman(G) finds a traversal of least weight through all the vertices of a graph G, ending at the starting point. This is known as the traveling salesman problem.

• 

If G is not a weighted graph, then all adjacent vertices are considered to have an edge weight of 1.

• 

If a second argument M is specified, it is used for the weights instead of the existing edge weights of G.  If an edge from vertex u to v is not in G then, regardless of the specified edge weight in M, its edge weight is considered to be infinity.

• 

Instead of specifying a weight matrix M, the vertexpositions option may be used to indicate the weights should be interpreted as the geometric distance between the positions of the graph vertices.

• 

The command returns two objects, w of type numeric and the second C a list which is a permutation of the vertices. The first output is the total weight of an optimal tour, and the second is a Hamiltonian cycle which achieves this optimum.

• 

The problem of finding an optimal tour for a weighted graph is is NP-complete, meaning that no polynomial-time algorithm is presently known.

Examples

withGraphTheory:

GGraph1,2,2,1,4,2,2,3,2,3,4,2

GGraph 1: an undirected weighted graph with 4 vertices and 4 edge(s)

(1)

TravelingSalesmanG

,

(2)

AddEdgeG,1,3,1,2,4,1

Graph 1: an undirected weighted graph with 4 vertices and 6 edge(s)

(3)

DrawGraphG,style=circle

w,tourTravelingSalesmanG

w,tour6,1,3,4,2,1

(4)

HighlightTrailG,tour,red

DrawGraphG,style=circle

GCompleteGraph10:

MMatrix0,68,37,95,57,30,1,25,71,84,68,0,9,26,90,26,97,29,47,78,37,9,0,84,59,11,67,61,75,35,95,26,84,0,1,99,55,63,19,8,57,90,59,1,0,61,66,18,7,48,30,26,11,99,61,0,93,10,14,54,1,97,67,55,66,93,0,47,20,95,25,29,61,63,18,10,47,0,28,52,71,47,75,19,7,14,20,28,0,92,84,78,35,8,48,54,95,52,92,0:

OptimalValue,OptimalPathTravelingSalesmanG,M

OptimalValue,OptimalPath142,1,8,6,2,3,10,4,5,9,7,1

(5)

OptimalPath

1,8,6,2,3,10,4,5,9,7,1

(6)

Perform a tour through 10 cities by using the vertexpositions option

BavarianCitiesImportexample/bayern10.csv,base=datadir,output=Matrix

BavarianCitiesMünchen48.13722211.575556Nürnberg49.45555611.078611Augsburg48.37166710.898333Regensburg49.01722212.096944Ingolstadt48.7641511.42434Würzburg49.794419.92937Fürth49.477410.98844Erlangen49.59636111.004311Bamberg49.89166710.891667Bayreuth49.947511.5775

(7)

GGraphTheory:-CompleteGraphconvertBavarianCities..,1,list

GGraph 2: an undirected graph with 10 vertices and 45 edge(s)

(8)

GraphTheory:-TravelingSalesmanG,vertexpositions=BavarianCities..,2..3,metric=geographical

631.542932745210,München,Augsburg,Ingolstadt,Nürnberg,Fürth,Erlangen,Würzburg,Bamberg,Bayreuth,Regensburg,München

(9)

Compatibility

• 

The GraphTheory[TravelingSalesman] command was updated in Maple 2023.

• 

The metric, seed, solver, startvertex and vertexpositions options were introduced in Maple 2023.

• 

For more information on Maple 2023 changes, see Updates in Maple 2023.

See Also

AllPairsDistance

IsHamiltonian

WeightMatrix