用Python实现Dijkstra算法用来寻找两点之间的最短路径 (Implementation of Dijkstra in Python)
来源:互联网 发布:java调用短信接口 编辑:程序博客网 时间:2024/04/30 04:31
这么简单一个算法,懒得花时间去自己实现,然后就想在网上搜搜看是否有现成可用的。谁知试了几个,真头疼:写得真是太好(垃)用(圾)了。不是没有注释,就是不规范看起来巨不爽,更甚bug满天飞根本不能执行。也怪自己懒,算了不抱怨了,因为下边我贴出的例子也是基于GitHub上一个写得较为顺眼的例子,然后自己包了一下,并解析了一下原作的返回内容,使得它符合我的需求:输入一个src-dst pair,返回他们之间的 distance 与 shortest path。废话不多说,有图有真相:可以运行。需要的拿走用就是了。
====== A. Code: (The dijkstra_raw(...) is the borrowed function, the dijkstra(...) is my design.)
from collections import defaultdictfrom heapq import *def dijkstra_raw(edges, from_node, to_node):g = defaultdict(list)for l,r,c in edges:g[l].append((c,r))q, seen = [(0,from_node,())], set()while q:(cost,v1,path) = heappop(q)if v1 not in seen:seen.add(v1)path = (v1, path)if v1 == to_node:return cost,pathfor c, v2 in g.get(v1, ()):if v2 not in seen:heappush(q, (cost+c, v2, path))return float("inf"),[]def dijkstra(edges, from_node, to_node):len_shortest_path = -1ret_path=[]length,path_queue = dijkstra_raw(edges, from_node, to_node)if len(path_queue)>0:len_shortest_path = length## 1. Get the length firstly;## 2. Decompose the path_queue, to get the passing nodes in the shortest path.left = path_queue[0]ret_path.append(left)## 2.1 Record the destination node firstly;right = path_queue[1]while len(right)>0:left = right[0]ret_path.append(left)## 2.2 Record other nodes, till the source-node.right = right[1]ret_path.reverse()## 3. Reverse the list finally, to make it be normal sequence.return len_shortest_path,ret_path
====== B. The topology used in the following use-case:
====== C. Use-case:
### ==================== Given a list of nodes in the topology shown in Fig. 1.list_nodes_id = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20];### ==================== Given constants matrix of topology.M=99999# This represents a large distance. It means that there is no link.### M_topo is the 2-dimensional adjacent matrix used to represent a topology.M_topo = [[M, 1,1,M,1,M, 1,1,1,M,M, M,M,M,M,M, M,M,M,M,M],[1, M,1,M,M,1, M,M,M,M,M, M,M,M,M,M, M,M,M,M,M],[1, 1,M,1,M,M, M,M,M,M,M, M,M,M,M,M, M,M,M,M,M],[M, M,1,M,1,M, M,M,M,M,M, M,M,M,M,M, M,M,M,M,M],[1, M,M,1,M,M, M,M,M,1,1, 1,M,M,M,M, M,M,M,M,M],[M, 1,M,M,M,M, 1,M,M,M,M, M,M,M,M,M, M,M,M,M,M],[1, M,M,M,M,1, M,1,M,M,M, M,M,M,M,M, M,M,M,M,M],[1, M,M,M,M,M, 1,M,1,M,M, M,M,M,M,M, M,M,M,M,M],[1, M,M,M,M,M, M,1,M,1,M, M,1,M,M,M, M,M,M,M,M],[M, M,M,M,1,M, M,M,1,M,M, 1,M,M,M,M, M,M,M,M,M],[M, M,M,M,1,M, M,M,M,M,M, 1,M,1,M,M, M,M,M,M,M],[M, M,M,M,1,M, M,M,M,1,1, M,M,1,1,M, M,M,M,M,M],[M, M,M,M,M,M, M,M,1,M,M, M,M,M,1,M, M,M,M,M,M],[M, M,M,M,M,M, M,M,M,M,1, 1,M,M,1,M, M,1,1,M,M],[M, M,M,M,M,M, M,M,M,M,M, 1,1,1,M,1, 1,M,M,M,M],[M, M,M,M,M,M, M,M,M,M,M, M,M,M,1,M, 1,M,1,1,M],[M, M,M,M,M,M, M,M,M,M,M, M,M,M,1,1, M,M,M,M,1],[M, M,M,M,M,M, M,M,M,M,M, M,M,1,M,M, M,M,1,M,M],[M, M,M,M,M,M, M,M,M,M,M, M,M,1,M,1, M,1,M,1,M],[M, M,M,M,M,M, M,M,M,M,M, M,M,M,M,1, M,M,1,M,1],[M, M,M,M,M,M, M,M,M,M,M, M,M,M,M,M, 1,M,M,1,M]]### --- Read the topology, and generate all edges in the given topology.edges = []for i in range(len(M_topo)):for j in range(len(M_topo[0])):if i!=j and M_topo[i][j]!=M:edges.append((i,j,M_topo[i][j]))### (i,j) is a link; M_topo[i][j] here is 1, the length of link (i,j).
print "=== Dijkstra ==="print "Let's find the shortest-path from 0 to 9:"length,Shortest_path = dijkstra(edges, 0, 9)print 'length = ',lengthprint 'The shortest path is ',Shortest_path
====== D. 执行结果:
Davy
2015--6-18
0 1
- 用Python实现Dijkstra算法用来寻找两点之间的最短路径 (Implementation of Dijkstra in Python)
- Python-通过Dijkstra计算两点之间的最短路径
- dijkstra算法求两点之间最短路径
- python最短路径算法,基于dijkstra
- python 实现 Dijkstra最短路径问题
- Dijkstra单源最短路径实现 及 Floyd任意两点之间的最短路径
- dijkstra算法--求两点之的最短路径
- Dijkstra算法的最短路径实现
- dijkstra最短路径算法的实现
- Dijkstra算法--寻找最短路径
- Matalab代码 实现 Dijkstra求 有向图及无向图之间,任意两点之间的最短路径
- 两点之间最短路径算法(Single-Dijkstra-shortest path)
- 最短路径算法—Dijkstra(迪杰斯特拉)算法分析与实现(Python)
- Dijkstra 最短路径算法C#实现
- 最短路径实现(Dijkstra算法)
- dijkstra最短路径算法C实现
- 最短路径Dijkstra算法实现
- 【Python排序搜索基本算法】之Dijkstra最短路径算法(Dijkstra's Shortest-Path Algorithm)
- 运行windows命令
- 用位运算来代替乘法、除法以及取模
- java实现ftp上传下载
- 黑马程序员——java基础——数组
- 20150618-Java项目心得1(里面内容均摘自我的有道云笔记)
- 用Python实现Dijkstra算法用来寻找两点之间的最短路径 (Implementation of Dijkstra in Python)
- ACTIVEX。不支持在Internet Explorer中添加WebBrowser
- Android中蓝牙通信的实现
- 面向对象和面向过程的区别
- glibc的几个有用的处理二进制位的内置函数
- python 抓取网页
- 二分图匹配
- 设计模式之-----策略模式
- 数据结构学习之双向循环链表的基本操作(非递归实现)