【Python学习系列二十六】networkx库图最短路径求解
2017-07-24 10:42
513 查看
场景:基于python库networkx来求解图最短路径,相关算法基础参考
http://blog.csdn.net/fjssharpsword/article/details/52931373 http://blog.csdn.net/fjssharpsword/article/details/52953640
可看networkx源码了解其内部算法用的原理。
代码:
# -*- coding: utf-8 -*-
import networkx as nx
import matplotlib.pyplot as plt
#读取文件,获取节点和边
f = open("D:\\tmp\\gy_contest_link_top.txt", "r")
nodelist=[]
edgelist=[]
while True:
line = f.readline()
if line:
pass # do something here
line=line.strip()
node=line.split(';')[0]#获取图节点
nodelist.append(node)
in_nodes=line.split(';')[1].split('#')#获取图边,该节点是终点
for ins in range( len(in_nodes) ) :
if in_nodes[ins].strip() !='':
in_edge=(in_nodes[ins],node)
if in_edge not in edgelist:
edgelist.append(in_edge)
out_nodes=line.split(';')[2].split('#')#获取图边,该节点是起点
for ins in range( len(out_nodes) ) :
if out_nodes[ins].strip() !='':
out_edge=(node,out_nodes[ins])
if out_edge not in edgelist:
edgelist.append(out_edge)
else:
break
f.close()
del nodelist[0] #删除表头生成的节点
del edgelist[0]
del edgelist[0] #删除表头生成的边
#print len(nodelist) #图节点
#print len(edgelist) #边数
#有向图构建
G=nx.DiGraph()
G.add_nodes_from(nodelist)
G.add_edges_from(edgelist)
#任意两点间最短路径
'''
try:
n=nx.shortest_path_length(G,1,4)
print n
except nx.NetworkXNoPath:
print 'No path'
'''
path=nx.all_pairs_shortest_path(G)
#for item in path.items():
# print item
print path.popitem()
#强联通和弱联通
for c in nx.weakly_connected_components(G):#弱联通
print c
con = nx.strongly_connected_components(G)#强联通
print con
print type(con)
print list(con)
#有向图绘制
nx.draw_networkx(G, pos=None, arrows=True, with_labels=False)#with_labels=False不带节点名
#plt.savefig('D:\\tmp\\it.png')
plt.show()
http://blog.csdn.net/fjssharpsword/article/details/52931373 http://blog.csdn.net/fjssharpsword/article/details/52953640
可看networkx源码了解其内部算法用的原理。
代码:
# -*- coding: utf-8 -*-
import networkx as nx
import matplotlib.pyplot as plt
#读取文件,获取节点和边
f = open("D:\\tmp\\gy_contest_link_top.txt", "r")
nodelist=[]
edgelist=[]
while True:
line = f.readline()
if line:
pass # do something here
line=line.strip()
node=line.split(';')[0]#获取图节点
nodelist.append(node)
in_nodes=line.split(';')[1].split('#')#获取图边,该节点是终点
for ins in range( len(in_nodes) ) :
if in_nodes[ins].strip() !='':
in_edge=(in_nodes[ins],node)
if in_edge not in edgelist:
edgelist.append(in_edge)
out_nodes=line.split(';')[2].split('#')#获取图边,该节点是起点
for ins in range( len(out_nodes) ) :
if out_nodes[ins].strip() !='':
out_edge=(node,out_nodes[ins])
if out_edge not in edgelist:
edgelist.append(out_edge)
else:
break
f.close()
del nodelist[0] #删除表头生成的节点
del edgelist[0]
del edgelist[0] #删除表头生成的边
#print len(nodelist) #图节点
#print len(edgelist) #边数
#有向图构建
G=nx.DiGraph()
G.add_nodes_from(nodelist)
G.add_edges_from(edgelist)
#任意两点间最短路径
'''
try:
n=nx.shortest_path_length(G,1,4)
print n
except nx.NetworkXNoPath:
print 'No path'
'''
path=nx.all_pairs_shortest_path(G)
#for item in path.items():
# print item
print path.popitem()
#强联通和弱联通
for c in nx.weakly_connected_components(G):#弱联通
print c
con = nx.strongly_connected_components(G)#强联通
print con
print type(con)
print list(con)
#有向图绘制
nx.draw_networkx(G, pos=None, arrows=True, with_labels=False)#with_labels=False不带节点名
#plt.savefig('D:\\tmp\\it.png')
plt.show()
相关文章推荐
- python包NetworkX学习——最短路径dijkstra_path和dijkstra_path_length
- python中networkx包学习——最短路径函数shortest_path及shorest_path_length
- 021 模拟退火算法学习(一)-----求解最短连通路径
- python 利用模拟退火算法求解TSP最短路径
- 最短路径(Python)networkx 包
- Python基于Floyd算法求解最短路径距离问题实例详解
- Python使用Dijkstra算法实现求解图中最短路径距离问题详解
- python 利用爬山法和迪杰斯特拉算法求解TSP最短路径
- BFS,Dijkstra,Floyd,DP求解最短路径问题
- arcgis api for js入门开发系列十三地图最短路径分析
- Python学习笔记-模块介绍(三)-模块包和搜索路径
- 使用栈求解迷宫最短路径
- python学习系列一:python快速入门
- [Qt][Floyd算法] 动态规划求解最短行驶路径 源码及演示程序
- Floyd算法:用动态规划求解多源(全源)最短路径
- python学习系列(五)--- 正则表达式基本函数
- PYTHON学习路径计划图整理
- 【Python学习系列一】Windows下Python及其IDE(eclipse+pydev)安装
- 【Python学习系列二】Python默认编码和Eclipse环境的冲突问题