1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| import numpy as np import networkx as nx
L = [(1,2), (1,3), (2,3), (2,4), (2,5), (3,5), (4,5), (4,6)] G = nx.Graph() G.add_nodes_from(range(1,7)) G.add_edges_from(L)
D = nx.diameter(G) LH = nx.average_shortest_path_length(G) Ci = nx.clustering(G) C = nx.average_clustering(G)
print('网络直径为:', D, '\n平均路径长度为:', LH) print('各顶点聚类系数为:') for index, value in enumerate(Ci.values()): print('(顶点v{:d}:{:.4f});'.format(index+1, value), end='') print('\n整个网络的平均聚类系数为:{:.4f}'.format(C))
|