-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen_centrality.py
221 lines (114 loc) · 4.73 KB
/
gen_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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import sys
import snap
import time
from collections import deque
def fetch_graph():
if len(sys.argv) != 2 :
print "[ERROR: INCORRECT ARGUMENTS] You need to pass exactly one argument which is the name of the edge list graph file stored in subgraphs folder."
exit()
GName=sys.argv[1]
path="subgraphs/"+GName
try:
G=snap.LoadEdgeList(snap.PUNGraph,path,0,1)
except:
print "[ERROR: GRAPH NOT FOUND] Please check the graph file name and try again. Ensure the edge list is stored in subgraphs folder"
exit()
nodes = G.GetNodes()
edges = G.GetEdges()
Nodes = []
for NI in G.Nodes():
Nodes.append(NI.GetId())
return G,GName,Nodes,nodes,edges
def store_in_file(arg_list,file_name,GName):
path = GName[:-10]+"_"+file_name+".txt"
file = open(path,'w')
for X in arg_list:
file.write(str(X[1])+" "+str(X[0])+"\n")
print "File created for storing " + GName[:-10] + " with the name " + path
file.close()
def compute_degree_centrality(G,GName):
degree_centrality = []
for NI in G.Nodes():
degree_centrality.append([NI.GetOutDeg(),NI.GetId()])
degree_centrality.sort()
store_in_file(degree_centrality,"degree_centrality",GName)
def compute_closeness_centrality(G,GName,Nodes,nodes,edges):
counter =0
closeness_centralities = []
start_time = time.time()
for NI in G.Nodes():
NIdToDistH = snap.TIntH()
sum_of_shortest_paths = 0
shortestPath = snap.GetShortPath(G, NI.GetId(), NIdToDistH)
for paths in NIdToDistH:
sum_of_shortest_paths = sum_of_shortest_paths + NIdToDistH[paths]
sum_of_shortest_paths = sum_of_shortest_paths + nodes*(nodes - len(NIdToDistH)) #incorporating unreachable nodes
current_centrality=float(nodes)/sum_of_shortest_paths
closeness_centralities.append([current_centrality,NI.GetId()])
time_taken = time.time() - start_time
print "Execution for Closeness Centrality completed in ",time_taken//60," mins and ",(time_taken//1)%60, "seconds"
closeness_centralities.sort()
store_in_file(closeness_centralities,"closeness_centrality",GName)
closeness_centralities.sort(reverse=True)
return closeness_centralities
def compute_betweeness_centrality(G,GName,Nodes,nodes,edges):
Adj = dict((v,[]) for v in Nodes)
for EI in G.Edges():
u = EI.GetSrcNId()
v = EI.GetDstNId()
Adj[u].append(v)
Adj[v].append(u)
C = dict((v,0.00) for v in Nodes)
start_time = time.time()
for s in Nodes:
S = []
P = dict((w,[]) for w in Nodes)
g = dict((t, 0) for t in Nodes); g[s] = 1
d = dict((t,-1) for t in Nodes); d[s] = 0
Q = deque([])
Q.append(s)
while Q:
v = Q.popleft()
S.append(v)
for w in Adj[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 Nodes)
while S:
w = S.pop()
for v in P[w]:
e[v] = e[v] + float(g[v]/g[w]) * (1 + e[w])
if w != s:
C[w] = C[w] + e[w]
time_taken = time.time() - start_time
print "Execution for Betweeness Centrality completed in ",time_taken//60," mins and ",(time_taken//1)%60, "seconds"
betweeness_centralities = []
for Node in Nodes:
betweeness_centralities.append([C[Node],Node])
betweeness_centralities.sort()
store_in_file(betweeness_centralities,"betweeness_centrality",GName)
betweeness_centralities.sort(reverse=True)
time_taken = time.time() - start_time
return betweeness_centralities
def get_top_10(arg_list):
return arg_list[:10]
def print_values(method_name, arg_list):
print "The top ",len(arg_list)," nodes of ",method_name," are:"
for i in range(len(arg_list)):
print "{0} \t node = {1} \t centrality = {2}".format(i+1,arg_list[i][1],arg_list[i][0])
if __name__ == "__main__":
#Setting Snap Randomized Seed Value to 42
Rnd = snap.TRnd(42)
Rnd.Randomize()
G,GName,Nodes,nodes,edges = fetch_graph()
compute_degree_centrality(G,GName)
closeness_centralities = compute_closeness_centrality(G,GName,Nodes,nodes,edges)
betweeness_centralities = compute_betweeness_centrality(G,GName,Nodes,nodes,edges)
closeness_top10 = get_top_10(closeness_centralities)
betweeness_top10 = get_top_10(betweeness_centralities)
print_values("Closeness Centraliry" , closeness_top10)
print_values("Betweeness Centrality" , betweeness_top10)