本文介绍了如何将C编码编写为C以实现中心性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
尝试将python代码转换为c以实现中介中心性
我尝试过:
trying to convert the python code to c for betweenness centrality
What I have tried:
# Betweenness Centrality", by Ulrik Brandes.
from collections import deque
def brandes(V, A):
"Compute betweenness centrality in an unweighted graph."
# Brandes algorithm
# see http://www.cs.ucc.ie/~rb4/resources/Brandes.pdf
C = dict((v,0) for v in V)
for s in V:
S = []
P = dict((w,[]) for w in V)
g = dict((t, 0) for t in V); g[s] = 1
d = dict((t,-1) for t in V); d[s] = 0
Q = deque([])
Q.append(s)
while Q:
v = Q.popleft()
S.append(v)
for w in A[v]:
if d[w] < 0:
Q.append(w)
d[w] = d[v] + 1
if d[w] == d[v] + 1:
g[w] = g[w] + g[v]
P[w].append(v)
e = dict((v, 0) for v in V)
while S:
w = S.pop()
for v in P[w]:
e[v] = e[v] + (g[v]/g[w]) * (1 + e[w])
if w != s:
C[w] = C[w] + e[w]
return C
推荐答案
这篇关于如何将C编码编写为C以实现中心性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!