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()

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐