-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbetweenness_centrality.py
156 lines (134 loc) · 4.52 KB
/
betweenness_centrality.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import ast
import datetime
import queue
import random
from collections import defaultdict
import numpy as np
def Brandes_Betweenness(graph, start,Cb):
Q = queue.Queue() # FIFO
S = [] # list as stack : LIFO
Pred = defaultdict(lambda: [])
distance = defaultdict(lambda:-1)
distance[start] = 0
sigma = defaultdict(lambda: 0)
sigma[start] = 1
Q.put(start)
while not Q.empty():
v = Q.get()
if v in S: # prevent a loop back to the visited node and go into circles
continue
S.append(v)
for w in graph.get(v, []):
if distance[w] < 0:
Q.put(w)
distance[w] = distance[v] + 1
if distance[w] == distance[v] + 1:
sigma[w] += sigma[v]
Pred[w].append(v)
delta = defaultdict(lambda: 0)
# Cb = defaultdict(lambda: 0) # betweenness centrality for each node
while S:
w = S.pop()
for v in Pred[w]:
delta[v] += sigma[v] / sigma[w] * (1 + delta[w])
if w != start:
Cb[w] += delta[w]
return Cb
def Matteo_Betweenness(graph,source,target,r,Cb):
Q = queue.Queue() # FIFO
S = [] # list as stack : LIFO
Pred = defaultdict(lambda: [])
distance = defaultdict(lambda:-1)
distance[source] = 0
sigma = defaultdict(lambda: 0) # number of paths through this node
sigma[source] = 1
Q.put(source)
while not Q.empty():
v = Q.get()
if v in S: # stop back to the visited node
continue
if target == v:
break
S.append(v)
for w in graph.get(v, []):
if distance[w] < 0:
Q.put(w)
distance[w] = distance[v] + 1
if distance[w] == distance[v] + 1:
sigma[w] += sigma[v]
Pred[w].append(v)
if len(Pred[target])>0:
trace_back = queue.Queue()
trace_back.put(np.random.choice(Pred[target]))
while not trace_back.empty():
v = trace_back.get()
if source == v:
break
Cb[v] += 1/r
if len(Pred[v])>0:
trace_back.put(np.random.choice(Pred[v]))
# print(Cb.items())
def find_r(graph,source):
Q = queue.Queue() # FIFO
S = [] # list as stack : LIFO
distance = defaultdict(lambda:-1)
distance[source] = 0
sigma = defaultdict(lambda: 0) # number of paths through this node
sigma[source] = 1
Q.put(source)
while not Q.empty():
v = Q.get()
if v in S: # stop back to the visited node
continue
S.append(v)
for w in graph.get(v, []):
if distance[w] < 0:
Q.put(w)
distance[w] = distance[v] + 1
if distance[w] == distance[v] + 1:
sigma[w] += sigma[v]
return sorted(list(sigma.values()))[-2:]
if __name__ == "__main__":
degree_file = "out_degree.csv"
degree_dict = {}
with open(degree_file, 'r')as file:
for line in file:
degree_dict[int(line.split(":")[0])] = ast.literal_eval(line.split(":")[1])
source_list = list(degree_dict.keys())
# fast algorithm
Cb = defaultdict(lambda: 0)
for start in source_list:
begin = datetime.datetime.now()
Cb = Brandes_Betweenness(degree_dict, start,Cb)
print("start from node :", start,"start:", begin, "end:", datetime.datetime.now())
# break
# faster algorithm
# find r
start = np.random.choice(source_list)
print("start to find r")
max_1, max_2 = find_r(degree_dict, start)
VD_G=max_1+max_2
print("VD_G is",VD_G)
epsilon = 0.05
c = 0.5
delta = 0.1
r = (c / np.math.pow(epsilon, 2)) * (np.math.floor(np.math.log(VD_G - 2, 2)) + np.math.log(1 / delta))
r = np.math.ceil(r)
print("r is ",r)
# BETWEENNESS Sampling
degree_file = "in_degree.csv"
target_list = []
with open(degree_file, 'r')as file:
for line in file:
target_list.append(int(line.split(":")[0]))
Cb = defaultdict(lambda:0)
begin = datetime.datetime.now()
for i in range(1,r):
start = np.random.choice(source_list, replace=False)
stop = np.random.choice(target_list, replace=False)
print("start node is:", start, "stop node is ", stop)
Matteo_Betweenness(degree_dict,start,stop,r,Cb)
# break
print("start time :", begin, "end time:", datetime.datetime.now())
top_k = 10
print("the top", top_k, "nodes are:", sorted(Cb.items(), key=lambda x: -x[1])[:top_k])