当前位置: 首页 > news >正文

白洋湾做网站公司网络策划方案

白洋湾做网站公司,网络策划方案,国内软件外包公司,中考复读学校网站怎么做文章目录 [toc]问题描述回溯算法Python实现时间复杂性 问题描述 给定一组城市和它们之间的距离矩阵,找到一条距离最短的路径,使得旅行商从一个城市出发,经过所有城市恰好一次,并最终回到出发城市 回溯算法 旅行售货员问题的解空间…

文章目录

    • @[toc]
      • 问题描述
      • 回溯算法
      • `Python`实现
      • 时间复杂性

问题描述

  • 给定一组城市和它们之间的距离矩阵,找到一条距离最短的路径,使得旅行商从一个城市出发,经过所有城市恰好一次,并最终回到出发城市

回溯算法

  • 旅行售货员问题的解空间是一棵排列树

  • i = n i = n i=n时,算法搜索至叶结点,其相应的路径长度为 c d cd cd,如果 c d < b e s t d cd < bestd cd<bestd,则表示当前解优于当前最优解,此时更新 b e s t d bestd bestd

  • i < n i < n i<n时,当前扩展结点位于排列树的第 i i i层,图 G G G中存在从顶点 x [ i ] x[i] x[i]到顶点 x [ i + 1 ] x[i + 1] x[i+1]的边时, x [ 1 : i + 1 ] x[1 : i + 1] x[1:i+1]构成图 G G G的一条路径,且当 x [ 1 : i + 1 ] x[1 : i + 1] x[1:i+1]的路径长度小于当前最优值时算法进入排列树的第 i + 1 i + 1 i+1层,否则将剪去相应的子树


Python实现

import numpy as npdef backtrack_tsp(cities):n = len(cities)visited = [False] * n  # 记录城市是否已经被访问shortest_path = []shortest_distance = float('inf')def distance(city1, city2):x1, y1 = city1x2, y2 = city2return np.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2)# 创建距离矩阵dist_matrix = np.zeros((n, n))for i in range(n):for j in range(n):dist_matrix[i][j] = distance(cities[i], cities[j])def backtrack(path, distance):nonlocal shortest_path, shortest_distanceif len(path) == n:  # 所有城市都已经访问过distance += dist_matrix[path[-1]][path[0]]  # 回到起点的距离if distance < shortest_distance:  # 更新最短路径和最短距离shortest_path = path[:]shortest_distance = distancereturnlast_city = path[-1] if path else 0  # 上一个访问的城市for next_city in range(n):if not visited[next_city]:visited[next_city] = Truepath.append(next_city)distance += dist_matrix[last_city][next_city]backtrack(path, distance)# 恢复回溯前状态distance -= dist_matrix[last_city][next_city]path.pop()visited[next_city] = False# 开始回溯搜索visited[0] = Truebacktrack([0], 0)return shortest_path, shortest_distancecities = [(0, 0), (1, 5), (2, 3), (5, 2), (6, 4)]
shortest_path, shortest_distance = backtrack_tsp(cities)print(f'最短路径: {shortest_path}')
print(f'最短距离: {shortest_distance}')
最短路径: [0, 2, 1, 4, 3]
最短距离: 18.56187155119086

时间复杂性

  • 回溯算法解TSP问题的时间复杂性为 O ( n ! ) O(n!) O(n!)

http://www.hengruixuexiao.com/news/24590.html

相关文章:

  • 龙岗营销网站建设公司哪家好seo优质友链购买
  • 网页框架图百度快速排名优化工具
  • 深圳高端做网站公司宣传推广渠道有哪些
  • 有回定ip怎么做网站不花钱网站推广
  • 郑州网站建设选微锐x网站页面分析
  • 泰州市网站制作公司南宁seo优化公司
  • 国内投资咨询网站 html模板四年级下册数学优化设计答案
  • 网站建设百强企业天猫代运营
  • 织梦网站做视频安卓优化大师官方版本下载
  • 鸡西网站制作百度大搜数据多少钱一条
  • 做网站的风险分析seo外包优化服务商
  • 北京网站制作百度推广网络推广赚钱项目
  • 美国地址生成器在线沈阳沈河seo网站排名优化
  • 贵阳网站建设有限公司河南省网站
  • 网站开发技术分享ppt免费网站或软件
  • 永久空间网站推广怎么做才可以赚钱
  • 昆山便宜做网站如何网站优化排名
  • 网站客户端ip做爬虫关键词难易度分析
  • 那家财经网站做的好网站推广和精准seo
  • 做网站业务员怎么样现在广告行业好做吗
  • github建网站百度首页
  • 北京开发网站公司网站推广技巧
  • 在凡科做网站火蝠电商代运营靠谱吗
  • 外贸网站用什么空间如何网络推广新产品
  • txt做网站 插入图片如何进行seo
  • 大连龙彩科技的网站在谁家做seo培训师
  • 做网站要多少知识 java网站建设网络推广公司
  • 绍兴建设用地使用权网站一个企业该如何进行网络营销
  • 阿里云服务器做电影网站网页制作的软件
  • 个人网页制作与网站建设网站推广引流最快方法