python实现Floyd算法

yipeiwu_com5年前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中利用opencv简单做图片比对的方法

下面代码中利用了两种比对的方法,一 对图片矩阵(m x m)求解特征值,通过比较特征值是否在一定的范围内,判断图片是否相同。二 对图片矩阵(m x m)中1求和,通过比较sum和来比较图...

windows10系统中安装python3.x+scrapy教程

windows10系统中安装python3.x+scrapy教程

官网下载就好, https://www.python.org/downloads/release/python-352/ 用installer下载比较方便,它直接把环境变量都帮你配了。...

Python 字典dict使用介绍

Python字典的创建 方法一: >>> blank_dict = {} >>> product_dict = {'MAC':8000,'Ipho...

Python常用随机数与随机字符串方法实例

随机整数: 复制代码 代码如下: >>> import random >>> random.randint(0,99) 21 随机选取0到100间的...

python TF-IDF算法实现文本关键词提取

TF(Term Frequency)词频,在文章中出现次数最多的词,然而文章中出现次数较多的词并不一定就是关键词,比如常见的对文章本身并没有多大意义的停用词。所以我们需要一个重要性调整系...