Example of Python Solving the Traveling salesman problem (TSP) based on the backtracking subset tree template

Source: Internet
Author: User
This article mainly introduces the Python-based subset tree template to solve the traveling salesman problem (TSP), describes the traveling salesman problem briefly, and analyzes the relevant implementation steps and operation skills of Python using the backtracking subset tree template to solve the traveling salesman problem, and the required friends can refer to

The example in this paper describes the Python-based subset tree template to solve the traveling salesman problem (TSP). Share to everyone for your reference, as follows:

Problem

The traveler's question (traveling salesman problem,tsp) is that the traveler is going to travel to several cities, the cost of each city is known, and in order to save money, the traveler decides to depart from the city and return to the initial city after each city trip. Ask him what route he should choose to make the total cost of travel shortest?

Analysis

This problem can be described as follows: g= (V,e) is a weighted, forward graph that finds a forward loop that contains each node in V, that is, a travel route, so that the sum of all the edges on the ring is minimized.

The difference between this question and the previous article http://www.jb51.net/article/122933.htm is that the subject is a graph with a right. Just a little bit of modification.

The length of the solution is fixed n+1.

Each node in the diagram has its own adjacency node. For a node, all its neighboring nodes form the state space of the node. When the path reaches this node, its state space is traversed.

Finally, you can find the best solution!

Obviously, continue to apply the backtracking subset Tree template!!!

Code


"Travel quotient problem (traveling salesman problem,tsp)" # represents a weighted graph n = 5 # nodes with adjacency table a,b,c,d,e = Range (n) # node Name graph = [{b:7, C:6, D:1, E:3 }, {a:7, C:3, D:7, E:8}, {a:6, B:3, D:12, e:11}, {a:1, B:7, C:12, e:2}, {a:3, B:8, c:11, d:2}]x = [0]* (n+1) # A solution (n+1 meta Array, fixed length) X = [] # A set of solutions best_x = [0]* (n+1) # The best solution found (path) min_cost = 0 # Minimum Travel # Conflict Detection def conflict (k): Global N,graph,x,bes T_x,min_cost # K-node, whether the previous has traversed if K < n and X[k] in X[:k]: return True # Back to the departure node if k = = N and x[k]! = x[0]: RE Turn True # The amount of travel for the previous part of the solution exceeds the minimum total travel cost that has been found = SUM ([Graph[node1][node2] for node1,node2 in Zip (X[:k], x[1:k+1])]) if 0 < Min_cost < Cost:return True return False # conflict free # travel salesman problem (TSP) def tsp (k): # Reach (x's) k node global n,a,b,c,d,e,graph,x,x , min_cost,best_x if k > N: # The length of the solution exceeds, has traveled n+1 nodes (if not back to the departure node, then k==n) cost = SUM ([Graph[node1][node2] for Node1,node2 in Zip (x[:-1], x[1:])] # Calculate Total travel if Min_cost = = 0 or cost < min_cost:best_x = x[:] Min_cost = Cost #prin T (x) else:for nodeIn Graph[x[k-1]]: # traversal node X[k-1] adjacency node (state space) x[k] = node if not conflict (k): # Pruning TSP (k+1) # test x[0] = C # Departure section Point: The first node of path X (whichever) TSP (1) # Start with the 2nd node in x print (best_x) print (min_cost)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.