word完整版python复杂网络分析库NetworkXWord格式文档下载.docx
- 文档编号:6763154
- 上传时间:2023-05-07
- 格式:DOCX
- 页数:14
- 大小:138.15KB
word完整版python复杂网络分析库NetworkXWord格式文档下载.docx
《word完整版python复杂网络分析库NetworkXWord格式文档下载.docx》由会员分享,可在线阅读,更多相关《word完整版python复杂网络分析库NetworkXWord格式文档下载.docx(14页珍藏版)》请在冰点文库上搜索。
utf8—*-
importmatplotlib.pyplotasplt
G=nx.Graph()#建立一个空的无向图G
G。
add_node
(1)#添加一个节点1
add_edge(2,3)#添加一条边2—3(隐含着添加了两个节点2、3)
G.add_edge(3,2)#对于无向图,边3—2与边2—3被认为是一条边
print”nodes:
"
G.nodes()#输出全部的节点:
[1,2,3]
print”edges:
G。
edges()#输出全部的边:
[(2,3)]
print”numberofedges:
”,G。
number_of_edges()#输出边的数量:
1
nx。
draw(G)
plt.savefig("
wuxiangtu。
png"
)
plt。
show()
输出
2
3
nodes:
[1,2,3]
edges:
[(2,3)]
numberofedges:
1
例2:
#—*—coding:
utf8—*—
G=nx.DiGraph()
add_node
(1)
add_node
(2)#加点
add_nodes_from([3,4,5,6])#加点集合
add_cycle([1,2,3,4])#加环
add_edge(1,3)
G.add_edges_from([(3,5),(3,6),(6,7)])#加边集合
plt.savefig(”youxiangtu.png"
plt.show()
有向图
#!
—*-coding:
utf8-*-
importmatplotlib。
pyplotasplt
G=nx。
DiGraph()
G.add_node
(2)
add_nodes_from([3,4,5,6])
add_cycle([1,2,3,4])
add_edge(1,3)
G.add_edges_from([(3,5),(3,6),(6,7)])
nx.draw(G)
youxiangtu。
png”)
注:
有向图和无向图可以互相转换,使用函数:
∙Graph.to_undirected()
∙Graph.to_directed()
例2,例子中把有向图转化为无向图:
#!
—*—coding:
utf8—*-
add_nodes_from([3,4,5,6])
add_cycle([1,2,3,4])
add_edge(1,3)
G.add_edges_from([(3,5),(3,6),(6,7)])
G=G.to_undirected()
savefig("
注意区分以下2例
例3-1
#-*-coding:
utf8-*-
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'
)]
add_nodes_from(road_nodes。
iteritems())
G.add_edges_from(road_edges)
savefig(”youxiangtu。
例3—2
#-*—coding:
#road_nodes={’a’:
1,’b’:
2,’c’:
a’:
1},'
{2:
2},'
:
3}}
road_edges=[('
a’,’b’),(’b’,’c’)]
add_nodes_from(road_nodes.iteritems())
plt.savefig(”youxiangtu。
加权图
有向图和无向图都可以给边赋予权重,用到的方法是add_weighted_edges_from,它接受1个或多个三元组[u,v,w]作为参数,其中u是起点,v是终点,w是权重。
Graph()#建立一个空的无向图G
G.add_edge(2,3)#添加一条边2—3(隐含着添加了两个节点2、3)
add_weighted_edges_from([(3,4,3。
5),(3,5,7。
0)])#对于无向图,边3—2与边2—3被认为是一条边
printG。
get_edge_data(2,3)
get_edge_data(3,4)
printG.get_edge_data(3,5)
{}
{'
weight’:
3。
5}
{’weight'
7.0}
经典图论算法计算
计算1:
求无向图的任意两点间的最短路径
#—*—coding:
cp936-*—
#计算1:
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:
找图中两个点的最短路径
G=nx。
G.add_nodes_from([1,2,3,4])
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的路径.
∙弱联通:
将有向图的所有的有向边替换为无向边,所得到的图称为原图的基图.如果一个有向图的基图是连通图,则有向图是弱连通图.
距离
弱连通
#—*—coding:
#G=nx。
path_graph(4,create_using=nx。
Graph())
#0123
G=nx.path_graph(4,create_using=nx。
DiGraph())#默认生成节点0123,生成有向变0-〉1,1->
2,2—〉3
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)]
执行结果
set([0,1,2,3,7,8])
[6]
例2:
强连通
#-*-coding:
#G=nx。
path_graph(4,create_using=nx.Graph())
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)
<
generatorobjectstrongly_connected_componentsat0x0000000008AA1D80>
〈type’generator’〉
[set([8,1,2,3]),set([0])]
子图
#-*—coding:
add_path([5,6,7,8])
sub_graph=G.subgraph([5,6,8])
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- word 完整版 python 复杂 网络分析 NetworkX
![提示](https://static.bingdoc.com/images/bang_tan.gif)