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,分别代表特征个数.训练样本个数.隐藏层神经元个数.输 ...
随机推荐
- BZOJ1503:[NOI2004]郁闷的出纳员——题解
http://www.lydsy.com/JudgeOnline/problem.php?id=1503 (题面复制的洛谷的,因为洛谷好看) 题目描述 OIER公司是一家大型专业化软件公司,有着数以万 ...
- BZOJ2823 [AHOI2012]信号塔 【最小圆覆盖】
题目链接 BZOJ2823 题解 最小圆覆盖模板 都懒得再写一次 #include<iostream> #include<cstdio> #include<cmath&g ...
- HDU.2734 Quicksum
Quicksum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Subm ...
- 项目管理---git----快速使用git笔记(七)------coding.net项目管理多人操作的流程规范--合并代码审核
我们在前面已经介绍了coding.net和本地git的基本用法. 但是多人协作开发时情况会复杂得多,所以我们最好有一些规范来保证项目多人开发顺利进行. 比如说 规范一 master代码分支 需要开启 ...
- c#中文件流的读写
文件流读入:第一static void Main(string[] args) { //C#文件流写文件,默认追加FileMode.Append string msg = "okffffff ...
- warning: React does not recognize the xxx prop on a DOM element
这是React不能识别dom元素上的非标准attribute报出的警告,最终的渲染结果中React会移除这些非标准的attribute. 通常{...this.props}和cloneElement( ...
- HashMap & SparseArray & ArrayMap 简单说明
HashMap 使用有限一维拉链数组存储结构,鉴于所用Entry结构{key, value, nextExtry},Key的hash值用于取余获得所属的数组行下标,通过链表方式顺序存放所有余数相同的各 ...
- Centos +django+nginx
WSGI配置 #!/usr/bin/python """ WSGI config for rana project. It exposes the WSGI callab ...
- SFM
1.相机模型,内参数和外参数矩阵,相机标定: 2.极线约束和本征矩阵:特征点提取与匹配:提取到的特征点计算本征矩阵(五对以上的点)findEssentialMat(),需啊要点对,焦距参数,cx,cy ...
- Codeforces Round #342 (Div. 2) A
A. Guest From the Past time limit per test 1 second memory limit per test 256 megabytes input standa ...