
网络全局效率、最大连通子图、网络抗毁性、蓄意攻击、随意攻击python代码
import networkx as nxfrom matplotlib import pyplot as plt# 创建空的网格G=nx.MultiGraph()#创建空的多图#G = nx.Graph()# 计算网络的直径=6print( nx.diameter(G) )#(2)效率# 计算节点1和5之间的效率#print( nx.efficiency(G,1,5) )#(3)最短路径# 计算
·
import networkx as nx
from matplotlib import pyplot as plt
# 创建空的网格
G=nx.MultiGraph()#创建空的多图
#G = nx.Graph()
# 计算网络的直径=6
print( nx.diameter(G) )
#(2)效率
# 计算节点1和5之间的效率
#print( nx.efficiency(G,1,5) )
#(3)最短路径
# 计算节点1和5之间最短路径长度=2
#print( nx.shortest_path_length(G,1,5) )
#(4)局部效率
print( nx.local_efficiency(G) )
#(5)全局效率
print( nx.global_efficiency(G) )
#(6)求整个网络的平均距离
print( nx.average_shortest_path_length(G) )
nx.draw_networkx(G)
plt.show()
G.remove_edge('1','3')#移除边
最大连通子图
def max_connect_num(G2):
largest_components = max(nx.connected_components(G2), key=len) # 高效找出最大的联通成分
h=len(largest_components)
print( h)
max_connect_num(G)
nx.draw_networkx(G)
plt.show()
更多推荐
所有评论(0)