用Python实现Dijkstra算法用来寻找两点之间的最短路径 (Implementation of Dijkstra in Python)
2015-06-18 21:36
1416 查看
这么简单一个算法,懒得花时间去自己实现,然后就想在网上搜搜看是否有现成可用的。谁知试了几个,真头疼:写得真是太好(垃)用(圾)了。不是没有注释,就是不规范看起来巨不爽,更甚bug满天飞根本不能执行。也怪自己懒,算了不抱怨了,因为下边我贴出的例子也是基于GitHub上一个写得较为顺眼的例子,然后自己包了一下,并解析了一下原作的返回内容,使得它符合我的需求:输入一个src-dst
pair,返回他们之间的 distance 与 shortest path。废话不多说,有图有真相:可以运行。需要的拿走用就是了。
====== A. Code: (The dijkstra_raw(...) is the borrowed function, the dijkstra(...) is my design.)
====== B. The topology used in the following use-case:
====== C. Use-case:
====== D. 执行结果:
Davy
2015--6-18
pair,返回他们之间的 distance 与 shortest path。废话不多说,有图有真相:可以运行。需要的拿走用就是了。
====== A. Code: (The dijkstra_raw(...) is the borrowed function, the dijkstra(...) is my design.)
from collections import defaultdict from 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,path for 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 = -1 ret_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 = ',length print 'The shortest path is ',Shortest_path
====== D. 执行结果:
Davy
2015--6-18
相关文章推荐
- python 抓取网页
- Python学习 之 运算符&表达式
- 字符编码以及python的编码解释
- ThinkPython-编程基础
- python下的web服务器模块
- Python 3程序开发指南(第2版 修订版)笔记
- Python python2和python3的安装及切换使用
- python写的日志监控程序,关键字统计,日志大小监控,关键字出现报警并截取日志
- Python 安装使用模块
- 【python练习】
- “Python Version 2.7 required which was not found in the registry”
- 负数的除法和取模运算(Python 2.7和C的比较)
- PYTHON基础
- Python WebDriver自动化测试
- PYTHON进行URL解码
- Python下载百度贴吧帖子里面的图片
- python最简单的爬取邮箱地址
- python 小记
- ConfigParser
- 转:Python之全局变量