-
Notifications
You must be signed in to change notification settings - Fork 0
/
analyze_centrality.py
183 lines (90 loc) · 4.42 KB
/
analyze_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
import sys
import snap
import time
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 calculate_degree_centrality(G,GName,Nodes,nodes,edges):
print "Initialting Calculations of Degree using inbuilt function"
start_time = time.time()
closeness_centralities = []
for NI in G.Nodes():
CloseCentr = snap.GetClosenessCentr(G, NI.GetId())
closeness_centralities.append([CloseCentr,NI.GetId()])
closeness_centralities.sort(reverse=True)
time_taken = time.time() - start_time
print "Execution for Cetweeness Centrality completed in ",time_taken//60," mins and ",(time_taken//1)%60, "seconds"
return closeness_centralities
def calculate_closeness_centrality(G,GName,Nodes,nodes,edges):
print "Initialting Calculations of Closeness Centrality using inbuilt function"
start_time = time.time()
closeness_centralities = []
for NI in G.Nodes():
CloseCentr = snap.GetClosenessCentr(G, NI.GetId())
closeness_centralities.append([CloseCentr,NI.GetId()])
closeness_centralities.sort(reverse=True)
time_taken = time.time() - start_time
print "Execution for Closeness Centrality completed in ",time_taken//60," mins and ",(time_taken//1)%60, "seconds"
return closeness_centralities
def calculate_betweeness_centrality(G,GName,Nodes,nodes,edges):
print "Initialting Calculations of Betweeness Centrality using inbuilt function"
start_time = time.time()
Nodes = snap.TIntFltH()
Edges = snap.TIntPrFltH()
snap.GetBetweennessCentr(G, Nodes, Edges, 0.8)
# for node in Nodes:
# print "node: %d centrality: %f" % (node, Nodes[node])
betweeness_centralities = []
for Node in Nodes:
betweeness_centralities.append([Nodes[Node],Node])
betweeness_centralities.sort(reverse=True)
time_taken = time.time() - start_time
print "Execution for Betweeness Centrality completed in ",time_taken//60," mins and ",(time_taken//1)%60, "seconds"
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])
def get_values_to_be_compared_with():
closeness_centralities = [195,444,273,78,516,423,639,588,1077,378]
betweeness_centralities = [273,588,195,543,516,1824,4746,444,213,5022]
return closeness_centralities,betweeness_centralities
def compare_top_10(centrality_type,calculated_values,generated_values_with_inbuilt):
number_of_overlaps = 0
generated_values = []
for X in generated_values_with_inbuilt:
generated_values.append(X[1])
for NI in calculated_values:
if NI in generated_values:
number_of_overlaps = number_of_overlaps + 1
print "Number of overlaps for {0}: {1}".format(centrality_type,number_of_overlaps)
if __name__ == "__main__":
#Setting Snap Randomized Seed Value to 42
Rnd = snap.TRnd(42)
Rnd.Randomize()
G,GName,Nodes,nodes,edges = fetch_graph()
closeness_centralities = calculate_closeness_centrality(G,GName,Nodes,nodes,edges)
betweeness_centralities = calculate_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)
closeness_calculated_values,betweeness_calculated_values = get_values_to_be_compared_with()
compare_top_10("Closeness Centrality",closeness_calculated_values,closeness_top10)
compare_top_10("Betweeness Centrality",betweeness_calculated_values,betweeness_top10)