收藏 分销(赏)

python复杂网络分析库NetworkX.doc

上传人:精*** 文档编号:2390291 上传时间:2024-05-29 格式:DOC 页数:18 大小:305.54KB
下载 相关 举报
python复杂网络分析库NetworkX.doc_第1页
第1页 / 共18页
python复杂网络分析库NetworkX.doc_第2页
第2页 / 共18页
python复杂网络分析库NetworkX.doc_第3页
第3页 / 共18页
python复杂网络分析库NetworkX.doc_第4页
第4页 / 共18页
python复杂网络分析库NetworkX.doc_第5页
第5页 / 共18页
点击查看更多>>
资源描述

1、(word完整版)python复杂网络分析库NetworkXpython复杂网络分析库NetworkX阅读目录 无向图 有向图 加权图 经典图论算法计算 强连通、弱连通 子图 条件过滤 pred,succNetworkX是一个用Python语言开发的图论与复杂网络建模工具,内置了常用的图与复杂网络分析算法,可以方便的进行复杂网络数据分析、仿真建模等工作。networkx支持创建简单无向图、有向图和多重图(multigraph);内置许多标准的图论算法,节点可为任意数据;支持任意的边值维度,功能丰富,简单易用。引入模块import networkx as nxprint nx回到顶部无向图例1:

2、!-* coding:utf8*- import networkx as nximport matplotlib.pyplot as pltG = nx.Graph() 建立一个空的无向图GG。add_node(1) 添加一个节点1G。add_edge(2,3) #添加一条边23(隐含着添加了两个节点2、3)G.add_edge(3,2) #对于无向图,边32与边23被认为是一条边print ”nodes:, G.nodes() 输出全部的节点: 1, 2, 3print ”edges:, G。edges() 输出全部的边:(2, 3)print ”number of edges:”, G。n

3、umber_of_edges() #输出边的数量:1nx。draw(G)plt.savefig(wuxiangtu。png)plt。show()输出123nodes: 1, 2, 3edges: (2, 3)number of edges: 1例2:# coding:utf8* import networkx as nximport matplotlib.pyplot as pltG = nx.DiGraph()G。add_node(1)G。add_node(2) 加点G。add_nodes_from(3,4,5,6) 加点集合G。add_cycle(1,2,3,4) 加环G。add_edge

4、(1,3) G.add_edges_from(3,5),(3,6),(6,7)) #加边集合nx。draw(G)plt.savefig(”youxiangtu.png)plt.show()回到顶部有向图例1:#!*- coding:utf8- import networkx as nximport matplotlib。pyplot as pltG = nx。DiGraph()G。add_node(1)G.add_node(2)G。add_nodes_from(3,4,5,6)G。add_cycle(1,2,3,4)G。add_edge(1,3)G.add_edges_from((3,5),(

5、3,6),(6,7)nx.draw(G)plt.savefig(youxiangtu。png”)plt.show()注:有向图和无向图可以互相转换,使用函数: Graph.to_undirected() Graph.to_directed()例2,例子中把有向图转化为无向图:#!* coding:utf8- import networkx as nximport matplotlib.pyplot as pltG = nx.DiGraph()G。add_node(1)G.add_node(2)G。add_nodes_from(3,4,5,6)G。add_cycle(1,2,3,4)G。add_

6、edge(1,3)G.add_edges_from(3,5),(3,6),(6,7))G = G.to_undirected()nx.draw(G)plt。savefig(wuxiangtu。png)plt.show()注意区分以下2例例3-1#-*- coding:utf8-*-import networkx as nximport matplotlib。pyplot as pltG = nx.DiGraph()road_nodes = a: 1, b: 2, c: 3#road_nodes = a:1:1, b:2:2, c:3:3road_edges = (a, b), (b, c)G。

7、add_nodes_from(road_nodes。iteritems())G.add_edges_from(road_edges)nx.draw(G)plt。savefig(”youxiangtu。png”)plt。show()例32- coding:utf8*-import networkx as nximport matplotlib。pyplot as pltG = nx。DiGraph()road_nodes = a: 1, b: 2, c: 3road_nodes = a:1:1, b:2:2, c:3:3road_edges = (a, b), (b, c)G。add_nodes

8、_from(road_nodes.iteritems())G.add_edges_from(road_edges)nx。draw(G)plt.savefig(”youxiangtu。png”)plt.show()回到顶部加权图有向图和无向图都可以给边赋予权重,用到的方法是add_weighted_edges_from,它接受1个或多个三元组u,v,w作为参数,其中u是起点,v是终点,w是权重。例1:#!*- coding:utf8- import networkx as nximport matplotlib.pyplot as pltG = nx。Graph() 建立一个空的无向图GG.ad

9、d_edge(2,3) 添加一条边23(隐含着添加了两个节点2、3)G。add_weighted_edges_from((3, 4, 3。5),(3, 5, 7。0) #对于无向图,边32与边23被认为是一条边print G。get_edge_data(2, 3)print G。get_edge_data(3, 4)print G.get_edge_data(3, 5)nx。draw(G)plt.savefig(wuxiangtu。png)plt.show()输出weight: 3。5weight: 7.0回到顶部经典图论算法计算计算1:求无向图的任意两点间的最短路径# coding: cp9

10、36 -*import networkx as nximport matplotlib。pyplot as plt #计算1:求无向图的任意两点间的最短路径G = nx。Graph()G.add_edges_from(1,2),(1,3),(1,4),(1,5),(4,5),(4,6),(5,6))path = nx.all_pairs_shortest_path(G)print path1计算2:找图中两个点的最短路径import networkx as nxG=nx。Graph()G.add_nodes_from(1,2,3,4)G。add_edge(1,2)G.add_edge(3,4)

11、try: n=nx。shortest_path_length(G,1,4) print nexcept nx。NetworkXNoPath: print No path回到顶部强连通、弱连通 强连通:有向图中任意两点v1、v2间存在v1到v2的路径(path)及v2到v1的路径. 弱联通:将有向图的所有的有向边替换为无向边,所得到的图称为原图的基图.如果一个有向图的基图是连通图,则有向图是弱连通图.距离例1:弱连通#* coding:utf8*- import networkx as nximport matplotlib。pyplot as pltG = nx。path_graph(4, c

12、reate_using=nx。Graph()0 1 2 3G = nx.path_graph(4, create_using=nx。DiGraph()) #默认生成节点0 1 2 3,生成有向变0-1,1-2,23G。add_path(7, 8, 3) #生成有向边:7-8-3for c in nx。weakly_connected_components(G): print cprint len(c) for c in sorted(nx.weakly_connected_components(G), key=len, reverse=True)nx。draw(G)plt。savefig(yo

13、uxiangtu。png”)plt.show()执行结果set(0, 1, 2, 3, 7, 8)6例2:强连通- coding:utf8- import networkx as nximport matplotlib。pyplot as plt#G = nx。path_graph(4, create_using=nx.Graph())0 1 2 3G = nx.path_graph(4, create_using=nx。DiGraph())G.add_path(3, 8, 1)for c in nx。strongly_connected_components(G):# print c#pri

14、nt len(c) for c in sorted(nx.strongly_connected_components(G), key=len, reverse=True)con = nx.strongly_connected_components(G)print conprint type(con)print list(con)nx.draw(G)plt。savefig(”youxiangtu。png)plt。show()执行结果type generatorset(8, 1, 2, 3), set(0)回到顶部子图-* coding:utf8-*- import networkx as nxi

15、mport matplotlib。pyplot as pltG = nx.DiGraph()G。add_path(5, 6, 7, 8)sub_graph = G.subgraph(5, 6, 8)sub_graph = G。subgraph(5, 6, 8) ok 一样nx。draw(sub_graph)plt.savefig(”youxiangtu.png”)plt.show()回到顶部条件过滤#原图-* coding:utf8import networkx as nximport matplotlib。pyplot as pltG = nx。DiGraph()road_nodes = a

16、:id:1, b:id:1, c:id:3, d:id:4road_edges = (a, b), (a, c), (a, d), (b, d)G。add_nodes_from(road_nodes)G。add_edges_from(road_edges)nx。draw(G)plt。savefig(youxiangtu.png)plt.show()图#过滤函数- coding:utf8-import networkx as nximport matplotlib。pyplot as pltG = nx.DiGraph()def flt_func_draw(): flt_func = lambd

17、a d: did != 1 return flt_funcroad_nodes = a:id:1, b:id:1, c:id:3, d:id:4road_edges = (a, b), (a, c), (a, d), (b, d)G.add_nodes_from(road_nodes。iteritems()G.add_edges_from(road_edges)flt_func = flt_func_draw()part_G = G.subgraph(n for n, d in G.nodes_iter(data=True) if flt_func(d)nx.draw(part_G)plt.s

18、avefig(youxiangtu。png)plt。show()图回到顶部pred,succ-*- coding:utf8-import networkx as nximport matplotlib。pyplot as pltG = nx。DiGraph()road_nodes = a:id:1, b:id:1, c:id:3road_edges = (a, b), (a, c), (c, d)G.add_nodes_from(road_nodes。iteritems()G.add_edges_from(road_edges)print G。nodes()print G。edges()pri

19、nt as pred , G。predaprint bs pred ”, G。predbprint cs pred , G.predcprint ds pred ”, G。preddprint ”as succ ”, G。succaprint ”bs succ ”, G.succbprint cs succ , G.succcprint ds succ ”, G.succdnx。draw(G)plt.savefig(”wuxiangtu。png)plt.draw()结果123456789101112a,c,b,d(a,c), (a,b), (c,d)as pred bs pred a: cs pred a: ds pred c: as succ c: , b: bs succ cs succ d: ds succ

展开阅读全文
部分上传会员的收益排行 01、路***(¥15400+),02、曲****(¥15300+),
03、wei****016(¥13200+),04、大***流(¥12600+),
05、Fis****915(¥4200+),06、h****i(¥4100+),
07、Q**(¥3400+),08、自******点(¥2400+),
09、h*****x(¥1400+),10、c****e(¥1100+),
11、be*****ha(¥800+),12、13********8(¥800+)。
相似文档                                   自信AI助手自信AI助手
百度文库年卡

猜你喜欢                                   自信AI导航自信AI导航
搜索标签

当前位置:首页 > 考试专区 > 中考

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        获赠5币

©2010-2024 宁波自信网络信息技术有限公司  版权所有

客服电话:4008-655-100  投诉/维权电话:4009-655-100

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :gzh.png    weibo.png    LOFTER.png 

客服