python中networkx包学习——最短路径函数shortest_path及shorest_path_length
来源:互联网 发布:什么软件支持货到付款 编辑:程序博客网 时间:2024/06/03 15:56
函数调用
shortest_path(G, source=None, target=None, weight=None)#寻找最短路径shortest_path_length(G, source=None, target=None, weight=None)#求最短路径长度
参数表:
————————————————————————————————————————————————————————————————————————
例子:
import networkx as nximport pylab import numpy as np#自定义网络row=np.array([0,0,0,1,2,3,6])col=np.array([1,2,3,4,5,6,7])print('生成一个空的有向图')G=nx.DiGraph()print('为这个网络添加节点...')for i in range(0,np.size(col)+1): G.add_node(i)print('在网络中添加带权中的边...')for i in range(np.size(row)): G.add_edges_from([(row[i],col[i])])print('给网路设置布局...')pos=nx.shell_layout(G)print('画出网络图像:')nx.draw(G,pos,with_labels=True, node_color='white', edge_color='red', node_size=400, alpha=0.5 )pylab.title('Self_Define Net',fontsize=15)pylab.show()'''shortest_path function'''p=nx.shortest_path(G,source=0,target=7)print('源节点为0,终点为7:', p)distance=nx.shortest_path_length(G,source=0,target=7)print('源节点为0,终点为7,最短距离:', distance)p=nx.shortest_path(G,source=0) # target not specifiedprint('只给定源节点0:', p[7])distance=nx.shortest_path_length(G,source=0) # target not specifiedprint('只给定源节点0, 最短距离:', distance[7])p=nx.shortest_path(G,target=7) # source not specifiedprint('只给定终点7:', p[0])distance=nx.shortest_path_length(G,target=7)# source not specifiedprint('只给定终点7,最短距离:', distance[0])p=nx.shortest_path(G) # source,target not specifiedprint('源节点,终点都为给定:', p[0][7])distance=nx.shortest_path_length(G) # source,target not specifiedprint('源节点,终点都为给定,最短距离:', distance[0][7])
——————————————————————————————————————————————
输出结果
runfile('D:/project/python_instruct/Networkx.py', wdir='D:/project/python_instruct')生成一个空的有向图为这个网络添加节点...在网络中添加带权中的边...给网路设置布局...画出网络图像:源节点为0,终点为7: [0, 3, 6, 7]源节点为0,终点为7,最短距离: 3只给定源节点0: [0, 3, 6, 7]只给定源节点0, 最短距离: 3只给定终点7: [0, 3, 6, 7]只给定终点7,最短距离: 3源节点,终点都为给定: [0, 3, 6, 7]源节点,终点都为给定,最短距离: 3
参考
更多详细介绍和案例可参考网站1和2
阅读全文
0 0
- python中networkx包学习——最短路径函数shortest_path及shorest_path_length
- python包NetworkX学习——最短路径dijkstra_path和dijkstra_path_length
- 最短路径(Python)networkx 包
- python中networkx包学习——画网络图像
- Python Networkx 包常用的方法函数
- 学习dijk最短路径中
- python中的networkx包学习——简单的网络画图入门
- python中的networkx包学习——简单的网络画图入门
- 最短路径—flody
- 最短路径——迪杰斯特拉算法 学习笔记
- 最短路径——迪杰斯特拉算法 学习笔记
- 最短路径——迪杰斯特拉算法 学习笔记
- 最短路径算法 python
- 学习笔记---街区最短路径问题算法及优化
- 最短路径算法学习
- 最短路径学习总结
- 【Python学习系列二十六】networkx库图最短路径求解
- 最短路径——SPFA
- HUD 2222 AC自动机摸 解法:1.暴力.2.指针型代码.3.数组型代码
- 神经网络的直观解释(转载)
- Hdu2068 RPG的错排
- 中断+异常处理【学习笔记】
- vue2.0使用vue-router
- python中networkx包学习——最短路径函数shortest_path及shorest_path_length
- 将二叉搜索树转换为双向链表
- HBase连接池技术
- Apache服务器和nginx的优缺点(转)
- Java多线程学习
- 134 Gas Station
- 【GTX 10x0 Ti】此nvidia驱动程序与此windows版本不兼容
- SAPUI5教程——查看系统安装的SAPUI5版本
- Android单元测试