forked from lucasosouza/graph-competition
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlgorithm-2017-09-06.py
More file actions
158 lines (119 loc) · 4.04 KB
/
Algorithm-2017-09-06.py
File metadata and controls
158 lines (119 loc) · 4.04 KB
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
# coding: utf-8
# In[ ]:
import pandas as pd
import numpy as np
from pprint import pprint
import math
from utils import import_net, export_net
from time import time
# In[ ]:
def sort_nodes(t):
v = t[1]
return -v[0], -v[1]
def BFS(graph, start):
""" André, please clarify:
this code is adding +1 to the second counter
if the node connected by an outgoing edge
also has an outgoing edge going back to the parent node
is this what we were looking for?
my understanding is that we wanted to count the number of incoming nodes,
regardless of its origin
"""
queue = list()
output = dict()
visited = set()
queue.append(start)
while queue:
node = queue.pop(0)
visited.add(node)
if node in output:
output[node][0] = len(graph[node])
else:
output[node] = [len(graph[node]), 0]
for adjacent in graph[node]:
# check logic of next two lines
if adjacent in output:
output[adjacent][1] += 1;
else:
output[adjacent] = [0, 1];
if adjacent not in visited:
visited.add(adjacent)
if adjacent in graph:
queue.append(adjacent)
return sorted(output.items(), key=sort_nodes), visited
# In[ ]:
def sort_forests(f):
return -len(f)
def get_full_bfs(network):
""" Run BFS for the entire network """
visited_nodes = set()
output = list()
for node in all_nodes:
if node not in visited_nodes:
forest, visited = BFS(network, node)
output.append(forest)
visited_nodes.update(visited)
return output
def score(full_bfs, num_nodes):
""" Normalize and calculate score for full bfs"""
output = {}
for forest in full_bfs:
forest_size = len(forest)
for node in forest:
node_id = node[0]
num_incoming_nodes = node[1][0]
num_outgoing_nodes = node[1][1]
score = (num_incoming_nodes+num_outgoing_nodes) * forest_size / num_nodes / 10e6
if node_id not in output:
output[node_id] = score
else:
output[node_id] += score
forest_size -= 1
return sorted(output.items(), key=lambda x:-x[1])
# In[ ]:
### ALTERNATIVE: RUN THIS TO TEST
# network = { 'A': ['B', 'C'],
# 'B': ['A', 'C'],
# 'C': ['B', 'D', 'E' ,'F'],
# 'D': ['E'],
# 'E': ['C'],
# 'F': ['B', 'D'],
# 'G': ['H', 'I','C'],
# 'H': ['G'],
# 'I': ['G', 'J']}
# all_nodes = ['A', 'B', 'C', 'D', 'E',
# 'F', 'G', 'H', 'I', 'J']
# full_bfs = get_full_bfs(network)
# pprint(full_bfs)
# results = score(full_bfs, len(all_nodes))
# pprint(results)
# In[ ]:
### ON REAL DATA
# first = True
# output = []
# for ftype in ['real','model']:
# for i in range(1,5):
# t0 = time()
# network_name = ftype+str(i)
# network, all_nodes = import_net(network_name)
# full_bfs = get_full_bfs(network)
# results = score(full_bfs, len(all_nodes))
# output.append((network_name, results))
# print('Nodes in results: ' + str(len(results)))
# print("Run-time (in minutes): " + str(int((time()-t0)/60)))
# if first:
# export_net(results, network_name, 'results-full-v2.csv', first=True)
# first = False
# else:
# export_net(results, network_name, 'results-full-v2.csv', first=False)
# t0 = time()
# network_name = "model4"
# network, all_nodes = import_net(network_name)
# full_bfs = get_full_bfs(network)
# results = score(full_bfs, len(all_nodes))
# #output.append((network_name, results))
# print('Nodes in results: ' + str(len(results)))
# print("Run-time (in minutes): " + str(int((time()-t0)/60)))
# export_net(results, network_name, 'results-full-v2.csv', first=False)
# In[ ]:
# In[ ]: