有谁能帮助我在不使用python库的情况下计算图中每个节点的集群吗?一般公式为2.0 * E / (V *(V - 1))
。
代码(工作不正常):
def clustering():
clust = []
print(vertexDegree)
E = len(list1)
for i in vertexDegree:
if i <= 1:
clust.append(0)
else:
clust.append(2.0 * E / (i *(i - 1)))
vertex = 1
for i in clust:
print("Vertex ", vertex, "have clustering: ", i)
vertex += 1
print(clust)
list1是连接节点的列表-[[1, 2], [3, 5], [2, 4]]
E是所有连接(边)的数量
V是邻居(节点)之间可能的连接(边)数
图形由一个字典表示-{1: [2], 2: [1, 4], 3: [5], 4: [2], 5: [3]}
,计算顶点度并保存在列表中-[1, 2, 1, 1, 1]
将networkx的代码转换为使用基于字典的图形
Networkx代码:How to calculate clustering coefficient of each node in the graph in Python using Networkx
代码
用法
使用字典
与边缘一起工作
解释
所有系数均为零的原因是:
测试Source
输出
测试图
相关问题 更多 >
编程相关推荐