python复杂网络分析库NetworkX.docx
《python复杂网络分析库NetworkX.docx》由会员分享,可在线阅读,更多相关《python复杂网络分析库NetworkX.docx(16页珍藏版)》请在冰豆网上搜索。
python复杂网络分析库NetworkX
python复杂网络分析库NetworkX
阅读目录
无向图
有向图
加权图
经典图论算法计算
强连通、弱连通
子图
条件过滤
pred,succ
NetworkX是一个用Python语言开发的图论与复杂网络建模工具,内置了常用的图与复杂网络分析算法,可以方便的进行复杂网络数据分析、仿真建模等工作。
networkx支持创建简单无向图、有向图和多重图(multigraph);内置许多标准的图论算法,节点可为任意数据;支持任意的边值维度,功能丰富,简单易用。
引入模块
importnetworkxasnx
printnx
回到顶部
无向图
例1:
#!
-*-coding:
utf8-*-
importnetworkxasnximportmatplotlib.pyplotasplt
print"numberofedges:
",G.number_of_edges()#输出边的数量:
1nx.draw(G)plt.savefig("wuxiangtu.png")
plt.show()
输出
1
nodes:
[1,2,3]
2
edges:
[(2,3)]
3
numberofedges:
1
例2:
#-*-coding:
utf8-*-
#加点#加点集合
#加环
#加边集合
importnetworkxasnximportmatplotlib.pyplotaspltG=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),(3,6),(6,7)])nx.draw(G)plt.savefig("youxiangtu.png")plt.show()
回到顶部有向图例1:
#!
-*-coding:
utf8-*-
importnetworkxasnx
importmatplotlib.pyplotasplt
G=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),(3,6),(6,7)])nx.draw(G)
plt.savefig("youxiangtu.png")
plt.show()
注:
有向图和无向图可以互相转换,使用函数:
Graph.to_undirected()
Graph.to_directed()
例2,例子中把有向图转化为无向图:
#!
-*-coding:
utf8-*-importnetworkxasnximportmatplotlib.pyplotasplt
G=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),(3,6),(6,7)])
G=G.to_undirected()
nx.draw(G)
plt.savefig("wuxiangtu.png")
plt.show()
注意区分以下2例
例3-1
#-*-coding:
utf8-*-
importnetworkxasnx
importmatplotlib.pyplotasplt
G=nx.DiGraph()
road_nodes={'a':
1,'b':
2,'c':
3}#road_nodes={'a':
{1:
1},'b':
{2:
2},'c':
{3:
3}}road_edges=[('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()
例3-2
#-*-coding:
utf8-*-
importnetworkxasnx
importmatplotlib.pyplotasplt
G=nx.DiGraph()
#road_nodes={'a':
1,'b':
2,'c':
3}road_nodes={'a':
{1:
1},'b':
{2:
2},'c':
{3:
3}}road_edges=[('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()
回到顶部
edges_from,它接w是权重。
#建立一个空的无
#添加一条边2-3
加权图
有向图和无向图都可以给边赋予权重,用到的方法是add_weighted受1个或多个三元组[u,v,w]作为参数,其中u是起点,v是终点,例1:
#!
-*-coding:
utf8-*-importnetworkxasnx
importmatplotlib.pyplotasplt
G=nx.Graph()向图GG.add_edge(2,3)(隐含着添加了两个节点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)
plt.savefig("wuxiangtu.png")plt.show()
输出
{}
{'weight':
3.5}
回到顶部
经典图论算法计算
计算1:
求无向图的任意两点间的最短路径
#-*-coding:
cp936-*-importnetworkxasnximportmatplotlib.pyplotasplt#计算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)
printpath[1]
计算2:
找图中两个点的最短路径
importnetworkxasnx
G=nx.Graph()
G.add_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)
printn
exceptnx.NetworkXNoPath:
print'Nopath'
回到顶部强连通、弱连通
强连通:
有向图中任意两点v1、v2间存在v1到v2的路径(path)及v2到v1的路径。
弱联通:
将有向图的所有的有向边替换为无向边,所得到的图称为原图的基图。
如果
一个有向图的基图是连通图,则有向图是弱连通图。
距离
例1:
弱连通
#-*-coding:
utf8-*-
importnetworkxasnx
importmatplotlib.pyplotasplt
#G=nx.path_graph(4,create_using=nx.Graph())
#0123
G=nx.path_graph(4,create_using=nx.DiGraph())#默认生成节点012
3,生成有向变0->1,1->2,2->3
G.add_path([7,8,3])#生成有向边:
7->8->3
forcinnx.weakly_connected_components(G):
printc
print[len(c)forcinsorted(nx.weakly_connected_components(G),key=len,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())
#0123
G=nx.path_graph(4,create_using=nx.DiGraph())G.add_path([3,8,1])
#forcinnx.strongly_connected_components(G):
#printc
#
#print[len(c)forcinsorted(nx.strongly_connected_components(G),key=len,reverse=True)]
con=nx.strongly_connected_components(G)printcon
printtype(con)
printlist(con)
nx.draw(G)
plt.savefig("youxiangtu.png")
plt.show()
执行结果
[set([8,1,2,3]),set([0])]
回到顶部子图
#-*-coding:
utf8-*-
importnetworkxasnx
importmatplotlib.pyplotasplt
G=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:
utf8-*-
importnetworkxasnx
importmatplotlib.pyplotasplt
G=nx.DiGraph()road_nodes={'a':
{'id':
1},'b':
{'id':
1},'c':
{'id':
3},'d':
{'id':
4}}road_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-*-importnetworkxasnx
importmatplotlib.pyplotasplt
G=nx.DiGraph()
defflt_func_draw():
flt_func=lambdad:
d['id']!
=1
returnflt_func
road_nodes={'a':
{'id':
1},'b':
{'id':
1},'c':
{'id':
3},'d':
{'id':
4}}road_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)ifflt_func(d))nx.draw(part_G)
plt.savefig("youxiangtu.png")
plt.show()
回到顶部
pred,succ
#-*-coding:
utf8-*-
importnetworkxasnx
importmatplotlib.pyplotasplt
G=nx.DiGraph()
road_nodes={'a':
{'id':
1},'b':
{'id':
1},'c':
{'id':
3}}
road_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']
print
"b'spred"
G.pred[
'b']
print
"c'spred"
G.pred[
'c']
print
"d'spred"
G.pred[
'd']
print
"a'ssucc"
G.succ[
'a']
print
"b'ssucc"
G.succ[
'b'
]
print
"c'ssucc"
G.succ[
'c'
]
print
"d'ssucc"
G.succ[
'd'
]
nx.draw(G)
plt.savefig("wuxiangtu.png")plt.draw()
结果
1
['a',
'c','b','d']
2
[('a',
'c'),('a','b'),('c',
'd')]
3
4
a'spred
{}
5
b'spred
{'a':
{}}
6
c'spred
{'a':
{}}
7
d'spred
{'c':
{}}
8
9
a'ssucc
{'c':
{},'b':
{}}
10
b'ssucc
{}
11
c'ssucc
{'d':
{}}
12
d'ssucc
{}