-
Notifications
You must be signed in to change notification settings - Fork 0
/
deg_centrality.py
54 lines (41 loc) · 1.36 KB
/
deg_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
import json
import sys
from pprint import pprint
import heapq
def run(NUM_ROUNDS = 1):
file = sys.argv[1]
info = file.split('.')
num_players = int(info[0])
num_seeds = int(info[1])
graph_id = int(info[2])
with open(file) as data_file:
data = json.load(data_file)
# do degree centrality for now
top = []
myfile = open("deg_centrality.txt", "w")
# This is each node/neighbor pair.
for key, val in data.iteritems():
centrality = len(val)
if len(top) < num_seeds:
heapq.heappush(top, (centrality, key))
# If we find something larger than the minimum, we pop that and push
# the new value.
elif centrality > top[0][0]:
heapq.heappop(top)
heapq.heappush(top, (centrality, key))
# Selecting the right nodes.
for i in range(len(top)):
top[i] = [top[i][0], int(top[i][1])]
top.sort(reverse = True)
for i in range(NUM_ROUNDS):
for centrality, node in top:
myfile.write("%d,%d\n" %(int(node),int(centrality)))
if __name__ == '__main__':
if len(sys.argv) < 2:
print "Usage: python", sys.argv[0], "graph_filename.json \n" \
"Example: python", sys.argv[0], "2.5.1.json"
sys.exit()
rounds = 50
if len(sys.argv) == 3:
rounds = int(sys.argv[2])
run(NUM_ROUNDS = rounds)