-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeg_centrality2.py
84 lines (64 loc) · 2.23 KB
/
deg_centrality2.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
import json
import sys
from pprint import pprint
import heapq
'''
strategy designed to beat max degree ta
Take top 1 to n-1 nodes in terms of degree and
one node adjacent to highest degree node
Should result in pick clashes on 1 to n-1 highest degree nodes
so TA will get the nth degree node, and we will get the 1st highest
degree node.
'''
def run(NUM_ROUNDS = 1):
# This is the JSON file that will be parsed.
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 = []
adj_list = []
myfile = open("deg_centrality2.txt", "w")
# This is each node/neighbor pair.
for key, val in data.iteritems():
centrality = len(val)
if len(top) < num_seeds:
if len(val) > len(adj_list):
# Trying to get the adjacency list of the highest
# degree.
adj_list = val
# This heap is used to sort quickly and to store minimum easily.
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]:
if len(val) > len(adj_list):
adj_list = val
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)
top[-1] = [-1, adj_list[0]]
for i in range(NUM_ROUNDS):
for centrality, node in top:
myfile.write("%d,%d\n" %(int(node),int(centrality)))
# # add first neighbor of the largest
# top[-1] = [-1, int(top[-1][2][0])]
# for i in range(len(top)):
# print top[i][1]
# myfile.write("%d\n" %(int(top[i][1])))
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)