python代码实现dijkstra算法

求解从1到6的最短路径。
python代码实现:(以A-F代表1-6)
# Dijkstra算法需要三张散列表和一个存储列表用于记录处理过的节点,如下:
processed = [] def build_graph():
"""建立图关系的散列表"""
graph = {}
graph["A"] = {}
graph["A"]["B"] = 1
graph["A"]["C"] = 12
graph["B"] = {}
graph["B"]["C"] = 9
graph["B"]["D"] = 3
graph["C"] = {}
graph["C"]["E"] = 5
graph["D"] = {}
graph["D"]["E"] = 13
graph["D"]["F"] = 15
graph["D"]["C"] = 4
graph["E"] = {}
graph["E"]["F"] = 4
graph["F"] = {}
return graph def build_costs():
"""建立开销的散列表"""
infinity = float("inf") # 无穷大
costs = {}
costs["B"] = 1
costs["C"] = 12
costs["D"] = infinity
costs["E"] = infinity
costs["F"] = infinity
return costs def build_parents():
"""建立存储父节点的散列表"""
parents = {}
parents["B"] = "A"
parents["C"] = "A"
parents["D"] = None
parents["E"] = None
parents["F"] = None
return parents # 下面是正式的算法步骤
def find_low_cost_node(costs):
"""首先要在未处理的节点找出开销最小的节点,在(开销表)中找,如果没有或者全部都已经处理过了,返回初始值None"""
lowest_cost = float("inf")
lowest_cost_node = None
for node,cost in costs.items():
if cost < lowest_cost and node not in processed:
lowest_cost = cost
lowest_cost_node = node
return lowest_cost_node def solve_shortest_route(graph,costs,parents):
"""dijkstra算法求解"""
node = find_low_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_low_cost_node(costs) # 输出结果:
print("graph:",graph)
print("costs:",costs)
print("parents:",parents) if __name__ == '__main__':
solve_shortest_route(build_graph(),build_costs(),build_parents())
python代码实现dijkstra算法的更多相关文章
- dijkstra算法解决单源最短路问题
简介 最近这段时间刚好做了最短路问题的算法报告,因此对dijkstra算法也有了更深的理解,下面和大家分享一下我的学习过程. 前言 呃呃呃,听起来也没那么难,其实,真的没那么难,只要弄清楚思路就很容易 ...
- 单链表反转的原理和python代码实现
链表是一种基础的数据结构,也是算法学习的重中之重.其中单链表反转是一个经常会被考察到的知识点. 单链表反转是将一个给定顺序的单链表通过算法转为逆序排列,尽管听起来很简单,但要通过算法实现也并不是非常容 ...
- Python实现各种排序算法的代码示例总结
Python实现各种排序算法的代码示例总结 作者:Donald Knuth 字体:[增加 减小] 类型:转载 时间:2015-12-11我要评论 这篇文章主要介绍了Python实现各种排序算法的代码示 ...
- 数据关联分析 association analysis (Aprior算法,python代码)
1基本概念 购物篮事务(market basket transaction),如下表,表中每一行对应一个事务,包含唯一标识TID,和购买的商品集合.本文介绍一种成为关联分析(association a ...
- 隐马尔科夫模型,第三种问题解法,维比特算法(biterbi) algorithm python代码
上篇介绍了隐马尔科夫模型 本文给出关于问题3解决方法,并给出一个例子的python代码 回顾上文,问题3是什么, 下面给出,维比特算法(biterbi) algorithm 下面通过一个具体例子,来说 ...
- 【Python排序搜索基本算法】之Dijkstra算法
Dijkstra算法和前一篇的Prim算法非常像,区别就在于Dijkstra算法向最短路径树(SPT)中添加顶点的时候,是按照ta与源点的距离顺序进行的.OSPF动态路由协议就是用的Dijkstra算 ...
- tf–idf算法解释及其python代码实现(下)
tf–idf算法python代码实现 这是我写的一个tf-idf的简单实现的代码,我们知道tfidf=tf*idf,所以可以分别计算tf和idf值在相乘,首先我们创建一个简单的语料库,作为例子,只有四 ...
- tf–idf算法解释及其python代码实现(上)
tf–idf算法解释 tf–idf, 是term frequency–inverse document frequency的缩写,它通常用来衡量一个词对在一个语料库中对它所在的文档有多重要,常用在信息 ...
- 神经网络BP算法C和python代码
上面只显示代码. 详BP原理和神经网络的相关知识,请参阅:神经网络和反向传播算法推导 首先是前向传播的计算: 输入: 首先为正整数 n.m.p.t,分别代表特征个数.训练样本个数.隐藏层神经元个数.输 ...
随机推荐
- POJ1375:Intervals——题解
http://poj.org/problem?id=1375 题目大意:有一盏灯,求每段被圆的投影所覆盖的区间. —————————————————————— 神题,卡精度,尝试用各种方法绕过精度都不 ...
- POJ2987:Firing——题解
http://poj.org/problem?id=2987 题目大意: 炒掉一个人能够获得b收益(b可以<0),但是炒掉一个人必须得炒掉他的下属(然后继续递归). 求最大收益和此时最小裁员. ...
- BZOJ5321 & 洛谷4064 & LOJ2274:[JXOI2017]加法——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=5321 https://www.luogu.org/problemnew/show/P4064 ht ...
- To pack or not to pack – MyISAM Key compression
MyISAM storage engine has key compression which makes its indexes much smaller, allowing better fit ...
- like tp
$where['insurance_order_num'] = array('like',$insurance_order_num.'%'); //右边模糊搜索,2099032902309张三 和 2 ...
- javascript如何判断对象为空
1.自定义jQuery的isEmptyObject()方法. function isEmptyObject(e) { var t; for (t in e) return !1; return !0 ...
- 如何写出高性能DOM?
为什么要写高性能DOM? 一个网站,在页面上承载最多内容的就是DOM,而且无论是我们通过加载JS.加载图片,他们也是通过写HTML标签来实现的.而我们性能优化要做的无非就是几大块: 站点的网络消耗 D ...
- [USACO11FEB] Cow Line
https://www.luogu.org/problem/show?pid=3014 题目描述 The N (1 <= N <= 20) cows conveniently number ...
- zoj 2006 Glass Beads
Glass Beadshttp://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1006 Time Limit: 2 Seconds ...
- python基础--结构篇
在C/C++/Java中,main是程序执行的起点,Python中,也有类似的运行机制,但方式却截然不同: Python使用缩进对齐组织代码的执行,所有没有缩进的代码(非函数定义和类定义),都会在载入 ...