计算度数k的平均聚类

时间:2020-01-29 17:43:43

标签: python python-3.x networkx

如何使用Python中的NetworkX库计算图的度数为k的节点的平均聚类c(k)? 我能找到的唯一功能是针对节点(部分或全部),以及针对整个图的另一功能。

1 个答案:

答案 0 :(得分:1)

您可以获取所有度为k的节点,然后获得平均聚类度

示例:

>>> G=nx.complete_graph(5)
>>> k=4
>>> nodes_degree_k = [u for u in G.nodes() if G.degree(u) == k]
>>> clustering_coeff = nx.clustering(G, nodes_degree_k)
>>> sum(clustering_coeff.values())/len(clustering_coeff)
1.0

仅通过对节点进行分组就可以扩展到各个程度。

from collections import defaultdict

d = defaultdict(list)

for u in G.nodes():
   d[G.degree(u)].append(u)

for degree in d:
   clustering_coeff = nx.clustering(G, d[degree])
   print(degree, sum(clustering_coeff.values())/len(clustering_coeff))