python复杂网络分析库NetworkX_第1页
python复杂网络分析库NetworkX_第2页
已阅读5页,还剩11页未读 继续免费阅读

付费下载

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

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

2、nximportmatplotlib.pyplotnx.draw(G)plt.savefig("wuxiangtu.png")plt.show()G=nx.Graph()G.add_node(1)G.add_edge(2,3)3)G.add_edge(3,2)边printprintprint#建立一个空的无向图G#添加一个节点1#添加一条边2-3(隐含着添加了两个节点2、#对于无向图,边3-2与边2-3被认为是一条"nodes:",G.nodesO"edges:",G.edgesO"numberofedges:",

3、#输出全部的节点:1,2,3#输出全部的边:(2,3)G.number_of_edges()#输出边的数量:1输出1nodes:1,2,32edges:(2,3)3numberofedges:1例2:骑#-*-coding:utf8-*-importnetworkxasnximportmatplotlib.pyplotaspitG=nx.DiGraph()G.add_node(l)G.add_node(2)#加点G.add_nodes_from(3,4,5,6)#加点集合G.add_cycle(l,2,3,4)#加环G.add_edge(l,3)G.add_edges_from(3,5),(3

4、,6),(6,7)#加边集合nx.draw(G)plt.savefig("youxiangtu.png")plt.show()回到顶部有向图例1:电#!*coding:utf8*importnetworkxasnximportmatplotlib.pyplotaspitG=nx.DiGraph()G.add_node(l)G.add_node(2)G.add_nodes_from(3,4,5,6)G.add_cycle(l,2,3,4)G.add_edge(l,3)G.add_edges_from(3,5),(3,6),(6,7)nx.draw(G)pit.savefig(

5、"youxiangtu.png")pit.show()侖#!*coding:utf8*importnetworkxasnximportmatplotlib.pyplotaspitG=nx.DiGraph()G.add_node(l)G.add_node(2)G.add_nodes_from(3,4,5,6)G.add_cycle(l,2,3,4)G.add_edge(l,3)G.add_edges_from(3,5),(3,6),(6,7)G=G.to_undirected()nx.draw(G)pit.savefig("wuxiangtu.png")p

6、it.show()注意区分以下2例例3-1#-*-coding:utf8-*-importnetworkxasnximportmatplotlib.pyplotaspitG=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.add_nodes_from(road_nodes.

7、iteritems()G.add_edges_from(road_edges)nx.draw(G)plt.savefig("youxiangtu.png")plt.show()#-*-coding:utf8-*-importnetworkxasnximportmatplotlib.pyplotaspitG=nx.DiGraph()#road_nodes='a':1,'b':2,'c':3road_nodes='a':1:1,'b':2:2,'c':3:3road_edges=(&

8、#39;a','b'),('b','c')G.add_nodes_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*importnetworkxasnxi

9、mportmatplotlib.pyplotaspitG=nx.Graph()#建立一个空的无向图GG.add_edge(2,3)#添加一条边23(隐含着添加了两个节点2、3)G.add_weighted_edges_from(3,4,3.5),(3,5,7.0)#对于无向图,边3-2与边2-3被认为是一条边printG.get_edge_data(2,3)printG.get_edge_data(3,4)printG.get_edge_data(3,5)nx.draw(G)pit.savefig("wuxiangtu.png")pit.show()输出'weigh

10、t':3.5'weight':7.0经典图论算法计算计算1:求无向图的任意两点间的最短路径#*coding:cp936*importnetworkxasnximportmatplotlib.pyplotaspit#计算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)printpath1计算2:找图中两个点的最短路径鬲importnetworkxasnxG=nx.Graph()G.a

11、dd_nodes_from(1,2,3,4)G.add_edge(1,2)G.add_edge(3,4)try:n二nx.shortest_path_length(G,1,4)printnexceptnx.NetworkXNoPath:print'Nopath'回到顶部强连通、弱连通强连通:有向图中任意两点v1、v2间存在v1到v2的路径(path)及v2到v1的路径。弱联通:将有向图的所有的有向边替换为无向边,所得到的图称为原图的基图。如果一个有向图的基图是连通图,则有向图是弱连通图。距离例1:弱连通#*coding:utf8*importnetworkxasnximport

12、matplotlib.pyplotasplt#G=nx.path_graph(4,create_using二nx.Graph()#0123G=nx.path_graph(4,create_using二nx.DiGraph()#默认生成节点0123,生成有向变0->1,1->2,2->3G.add_path(7,&3)#生成有向边:7>8>3forcinnx.weakly_connected_components(G):printcprintlen(c)forcinsorted(nx.weakly_connected_components(G),key=le

13、n,reverse二True)nx.draw(G)plt.savefig("youxiangtu.png")plt.show()执行结果set(0,1,2,3,7,8)6例2:强连通电#-*-coding:utf8-*-importnetworkxasnximportmatplotlib.pyplotasplt#G=nx.path_graph(4,create_using二nx.Graph()#0123G=nx.path_graph(4,create_using二nx.DiGraph()G.add_path(3,&1)#forcinnx.strongly_conne

14、cted_components(G):# printc#printlen(c)forcinsorted(nx.strongly_connected_components(G),key=len,reverse二True)con=nx.strongly_connected_components(G)printconprinttype(con)printlist(con)nx.draw(G)pit.savefig("youxiangtu.png")pit.show()执行结果generatorobjectstrongly_connected_componentsat0x00000

15、00008AAlD80>type'generator'>set(8,1,2,3),set(0)回到顶部子图#-*-coding:utf8-*-importnetworkxasnximportmatplotlib.pyplotaspitG=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)pit.savefig("youxiangtu.png")pit.show()回到顶部条件过滤#

16、原图#-*-coding:utf8-*-importnetworkxasnximportmatplotlib.pyplotaspltG=nx.DiGraph()road_nodes='a':'id':l,'b':'id':l,'c':'id':3,'d':'id':4road_edges二('a',,b'),('a',,c'),('a',,d'),(,b',,d,)G.add_nodes_

17、from(road_nodes)G.add_edges_from(road_edges)nx.draw(G)pit.savefig("youxiangtu.png")pit.show()#过滤函数#*coding:utf8*importnetworkxasnximportmatplotlib.pyplotaspitG=nx.DiGraph()defflt_func_draw():flt_func=lambdad:d'id'!=1returnflt_funcroad_nodes='a':'id':l,'b':&#

18、39;id':l,'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(nforn,dinG.nodes_iter(data二True)iff

19、lt_func(d)nx.draw(part_G)pit.savefig("youxiangtu.png")pit.show()图Figure!刁|Icx=0,579437y=0-842171回到顶部wl»l-»l»|Q|s|iapred,succ#-*-coding:utf8-*-importnetworkxasnximportmatplotlib.pyplotaspitG=nx.DiGraph()road_nodes='a':'id':1,'b':'id':l,'c':'id':3road_edges=('a','b'),('a','c'),('c','d')G.add_nodes_from(road_nodes.iteritems()G.add_edges_from(road_edges)printG.nodes()printG.edges()print"a'spred",G.pred'a'pr

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论