python实现Floyd算法

yipeiwu_com6年前Python基础

下面是用Python实现Floyd算法的代码,供大家参考,具体内容如下

# -*- coding: utf-8 -*- 
""" 
Created on Thu Jul 13 14:56:37 2017 
 
@author: linzr 
""" 
 
## 表示无穷大 
INF_val = 9999 
 
class Floyd_Path(): 
 def __init__(self, node, node_map, path_map): 
  self.node = node 
  self.node_map = node_map 
  self.node_length = len(node_map) 
  self.path_map = path_map 
  self._init_Floyd() 
  
 def __call__(self, from_node, to_node): 
  self.from_node = from_node 
  self.to_node = to_node 
  return self._format_path() 
 
 def _init_Floyd(self): 
  for k in range(self.node_length): 
   for i in range(self.node_length): 
    for j in range(self.node_length): 
     tmp = self.node_map[i][k] + self.node_map[k][j] 
     if self.node_map[i][j] > tmp: 
      self.node_map[i][j] = tmp 
      self.path_map[i][j] = self.path_map[i][k] 
       
  print '_init_Floyd is end' 
 
 
 def _format_path(self): 
  node_list = [] 
  temp_node = self.from_node 
  obj_node = self.to_node 
  print("the shortest path is: %d")%(self.node_map[temp_node][obj_node]) 
  node_list.append(self.node[temp_node]) 
  while True: 
   node_list.append(self.node[self.path_map[temp_node][obj_node]]) 
   temp_node = self.path_map[temp_node][obj_node] 
   if temp_node == obj_node: 
    break; 
   
  return node_list 
     
 
   
 
 
def set_node_map(node_map, node, node_list, path_map): 
 for i in range(len(node)): 
  ## 对角线为0 
  node_map[i][i] = 0 
 for x, y, val in node_list: 
  node_map[node.index(x)][node.index(y)] = node_map[node.index(y)][node.index(x)] = val 
  path_map[node.index(x)][node.index(y)] = node.index(y) 
  path_map[node.index(y)][node.index(x)] = node.index(x) 
 
  
if __name__ == "__main__": 
 node = ['A', 'B', 'C', 'D', 'E', 'F', 'G'] 
 node_list = [('A', 'F', 9), ('A', 'B', 10), ('A', 'G', 15), ('B', 'F', 2), 
     ('G', 'F', 3), ('G', 'E', 12), ('G', 'C', 10), ('C', 'E', 1), 
     ('E', 'D', 7)] 
 
 ## node_map[i][j] 存储i到j的最短距离 
 node_map = [[INF_val for val in xrange(len(node))] for val in xrange(len(node))] 
 ## path_map[i][j]=j 表示i到j的最短路径是经过顶点j 
 path_map = [[0 for val in xrange(len(node))] for val in xrange(len(node))] 
  
 ## set node_map 
 set_node_map(node_map, node, node_list, path_map) 
 
 
 ## select one node to obj node, e.g. A --> D(node[0] --> node[3]) 
 from_node = node.index('A') 
 to_node = node.index('E') 
 Floydpath = Floyd_Path(node, node_map, path_map) 
 path = Floydpath(from_node, to_node) 
 print path 

运行结果为:
the shortest path is: 23
['A', 'F', 'G', 'C', 'E']

相关文章

Python统计日志中每个IP出现次数的方法

本文实例讲述了Python统计日志中每个IP出现次数的方法。分享给大家供大家参考。具体如下: 这脚本可用于多种日志类型,本人测试MDaemon的all日志文件大小1.23G左右,分析用时...

Python数组条件过滤filter函数使用示例

使用filter函数,实现一个条件判断函数即可。 比如想过滤掉字符串数组中某个敏感词,示范代码如下: #filter out some unwanted tags def pass...

Python、Javascript中的闭包比较

同为脚本语言,python和Javascript具有相似的变量作用域,不像php,函数的内部的所有变量和外部都是隔绝的,也就是说,函数要想处理其外部的数据,必须使用参数把需要处理的数据传...

Python修改文件往指定行插入内容的实例

需求:批量修改py文件中的类属性,为类增加一个core = True新的属性 原py文件如下 a.py class A(): description = "abc" 现在有一个...

pycharm修改file type方式

pycharm修改file type方式

原因 添加一个文件 config.ini, 理论上说该文件会以ini文件格式打开,但是不知道咋的手贱用Text格式打开了。 .ini正确打开方式 .Text正确打开方式 手贱操作后的...