迪克斯拉特算法:

1、找出代价最小的节点,即可在最短时间内到达的节点;

2、更新节点的邻居的开销;

3、重复这个过程,直到图中的每个节点都这样做了;

4、计算最终路径。

'''
迪克斯特拉算法:
1、以字典的方式更新图,包括权重
2、创建开销字典,关键在于起点临近的点开销为实际数值,其他点为暂时未到达,开销为无穷,随后更新
3、创建父节点列表保存每个点的父节点,以便记录走过的路径
'''
from queue import LifoQueue graph = {}
graph['start'] = {}
graph['start']['a'] = 6
graph['start']['b'] = 2
graph['a'] = {}
graph['a']['end'] = 4
graph['b'] = {}
graph['b']['a'] = 3
graph['b']['c'] = 2
graph['c'] = {}
graph['c']['end'] = 3
graph['end'] = {}
print(graph) infinity = float('inf')
costs = {}
costs['a'] = 6
costs['b'] = 2
costs['c'] = infinity
costs['end'] = infinity parents = {}
parents['a'] = 'start'
parents['b'] = 'start'
parents['c'] = 'b'
parents['end'] = None processed = [] def find_lowest_cost_node(costs):
lowest_cost = float('inf')
lowest_cost_node = None
for node in costs:
cost = costs[node]
if (cost < lowest_cost and node not in processed):
lowest_cost = cost
lowest_cost_node = node
return lowest_cost_node node = find_lowest_cost_node(costs)
while(node is not None):
cost = costs[node]
neighbors = graph[node]
for n in neighbors.keys():
new_cost = cost + neighbors[n]
if costs[n] > new_cost:
costs[n] = new_cost
parents[n] = node
processed.append(node)
node = find_lowest_cost_node(costs) #输出最短路径
p = 'end'
path = LifoQueue()
while(True):
path.put(p)
if(p == 'start'):
break
p = parents[p] while not path.empty():
print(path.get())

最新文章

  1. Linux C语言解析.bmp格式图片并显示汉字
  2. js中自己实现each方法来遍历多维数组
  3. PPC MPC85xx e500学习笔记
  4. 【Java每日一题】20161116
  5. 切身体验苹果Reminders的贴心设计
  6. CSS select样式优化 含jquery代码
  7. 周赛-Expression 分类: 比赛 2015-08-02 09:35 3人阅读 评论(0) 收藏
  8. ConfigurationManager配置操作
  9. 九度OJ 1499 项目安排 -- 动态规划
  10. 关于Hibernate框架的面试题
  11. WordPress Tweet Blender插件跨站脚本漏洞
  12. 使用QJM部署HDFS HA集群
  13. QtWebkit2.2.0 HTML5.0支持情况
  14. c语言,string库函数itoa实现:将int转换为char*
  15. Mariadb Galera Cluster 群集 安装部署
  16. 使用EasyNetQ组件操作RabbitMQ消息队列服务
  17. css,解决文字与图片对齐的问题
  18. WPF中的数据绑定(初级)
  19. Python入门到精通学习书籍推荐!
  20. 题解——ATCoder AtCoder Grand Contest 017 B - Moderate Differences(数学,构造)

热门文章

  1. 使用ui给定的字体,通过css引入字体库
  2. MySQL安装/卸载
  3. [CSP-S模拟测试60]题解
  4. if语句里面continue和break的区别
  5. HTML5: HTML5 Canvas
  6. seleniumIDE command命令
  7. PouchContainer 容器技术演进助力阿里云原生升级
  8. 发送验证码倒计时60s
  9. 14. Django MTV及Django模型
  10. EF复合主键