深度优先搜索算法用于解决TSP问题
发布时间
阅读量:
阅读量
问题描述
import math # 回溯法求解旅行商问题
'''
path中最开始只有源点1,对顶点按照从小到大的全排列顺序一个一个判断是否到达叶子节点时,再回到源点有最短路径
深度优先搜索,模拟解答树过程
'''
bestcost = math.inf # 最短路径
nowcost = 0 # 当前的路径长度
x = [1, 2, 3, 4, 5, 6] # 顶点序号
path = [1] # 模拟open表中过程
arc = [] # 存放
def main():
n = int(input()) # 顶点个数
graph = [] # 图的邻接矩阵
for i in range(n):
graph.append(list(map(int, input().split())))
tsp(graph, 1, n)
print(bestcost, end=": ")
print(arc[:n])
def tsp(graph, s, n): # 深度优先搜索,模拟解答树过程
global nowcost, bestcost, arc
全部评论 (0)
还没有任何评论哟~
