-
Notifications
You must be signed in to change notification settings - Fork 0
/
ShapleyBetweenness.py
56 lines (51 loc) · 1.93 KB
/
ShapleyBetweenness.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import networkx as nx
import matplotlib.pyplot as plt
import colorsys
import numpy as np
import queue
#Input - G: networkX graph
#Output - cSh: array of ShapelyBetweeness of each node
def ShapelyBetweeness(self, G):
#Distance between nodes
d = np.zeros((G.number_of_nodes(), G.number_of_nodes()))
#list of predecessors on all node pairs
Pred_s = [[] for i in range(G.number_of_nodes()) ]
#Length of shortest path on each pair
sigma = np.zeros((G.number_of_nodes(), G.number_of_nodes()))
#One-side dependency of source node on target node
delta = np.zeros((G.number_of_nodes(), G.number_of_nodes()))
cSh = np.zeros(G.number_of_nodes())
#Sructs
Q = queue.Queue()
S = []
#Create node list
nodes = []
for n in G.nodes.data():
nodes.append(n[0])
for s in range(0, G.number_of_nodes()):
for v in range(0, G.number_of_nodes()):
Pred_s[v] = []; d[s,v] = float("inf") ;sigma[s,v] = 0
d[s,s] = 1; sigma[s,s] = 1;
Q.put(s)
while Q.empty() == False:
v = Q.get()
S.append(v)
w = list(G.edges(nodes[v], data=True))
for i in range(0,len(w)):
if d[s, nodes.index(w[i][1])] == float("inf"):
d[s, nodes.index(w[i][1])] = d[s, v] + 1
Q.put(nodes.index(w[i][1]))
if d[s, nodes.index(w[i][1])] == d[s, v] + 1:
sigma[s,nodes.index(w[i][1])] += sigma[s,v]
Pred_s[nodes.index(w[i][1])].append(v)
for v in range(0, G.number_of_nodes()-1):
delta[s,v] = 0
while len(S) > 0:
w = S.pop()
for v in Pred_s[w]:
delta[s,v] += (sigma[s,v]/sigma[s,w])*(1/d[s,w] + delta[s,w])
if w != s:
cSh[w] += delta[s,w] + (2-d[s,w])/d[s,w]
for v in range(0, G.number_of_nodes()):
cSh[v] = cSh[v]/2
return cSh