traveling_salesman_problem(G, weight='weight', nodes=None, cycle=True, method=None)
This function allows approximate solution to the traveling salesman problem on networks that are not complete graphs and/or where the salesman does not need to visit all nodes.
This function proceeds in two steps. First, it creates a complete graph using the all-pairs shortest_paths between nodes in nodes
. Edge weights in the new graph are the lengths of the paths between each pair of nodes in the original graph. Second, an algorithm (default: christofides
for undirected and asadpour_atsp
for directed) is used to approximate the minimal Hamiltonian cycle on this new graph. The available algorithms are:
christofides
greedy_tsp
simulated_annealing_tsp
threshold_accepting_tsp
asadpour_atsp
Once the Hamiltonian Cycle is found, this function post-processes to accommodate the structure of the original graph. If :None:None:`cycle`
is False
, the biggest weight edge is removed to make a Hamiltonian path. Then each edge on the new complete graph used for that analysis is replaced by the shortest_path between those nodes on the original graph.
A possibly weighted graph
collection (list, set, etc.) of nodes to visit
Edge data key corresponding to the edge weight. If any edge does not have this attribute the weight is set to 1.
Indicates whether a cycle should be returned, or a path. Note: the cycle is the approximate minimal cycle. The path simply removes the biggest edge in that cycle.
A function that returns a cycle on all nodes and approximates the solution to the traveling salesman problem on a complete graph. The returned cycle is then used to find a corresponding solution on G
. :None:None:`method`
should be callable; take inputs G
, and :None:None:`weight`
; and return a list of nodes along the cycle.
Provided options include christofides
, greedy_tsp
, simulated_annealing_tsp
and threshold_accepting_tsp
.
If :None:None:`method is None`
: use christofides
for undirected G
and threshold_accepting_tsp
for directed G
.
To specify parameters for these provided functions, construct lambda functions that state the specific value. :None:None:`method`
must have 2 inputs. (See examples).
If G
is a directed graph it has to be strongly connected or the complete version cannot be generated.
Find the shortest path in G
connecting specified nodes
>>> tsp = nx.approximation.traveling_salesman_problem
... G = nx.cycle_graph(9)
... G[4][5]["weight"] = 5 # all other weights are 1
... tsp(G, nodes=[3, 6]) [3, 2, 1, 0, 8, 7, 6, 7, 8, 0, 1, 2, 3]
>>> path = tsp(G, cycle=False)
... path in ([4, 3, 2, 1, 0, 8, 7, 6, 5], [5, 6, 7, 8, 0, 1, 2, 3, 4]) True
Build (curry) your own function to provide parameter values to the methods.
>>> SA_tsp = nx.approximation.simulated_annealing_tspSee :
... method = lambda G, wt: SA_tsp(G, "greedy", weight=wt, temp=500)
... path = tsp(G, cycle=False, method=method)
... path in ([4, 3, 2, 1, 0, 8, 7, 6, 5], [5, 6, 7, 8, 0, 1, 2, 3, 4]) True
The following pages refer to to this document either explicitly or contain code examples using this.
networkx.algorithms.approximation.traveling_salesman.traveling_salesman_problem
Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.
Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)
SVG is more flexible but power hungry; and does not scale well to 50 + nodes.
All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them