-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNRA.py
178 lines (137 loc) · 3.97 KB
/
NRA.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
#Vasileios Gewrgoulas
#AM 2954
import sys
import heapq
import ast
import itertools
minDist = pow(2, 30)
counter = itertools.count()
graph = ''
struct = []
pqs = []
dicts = []
spds = []
paths = []
bounds = []
visited = []
LBq = []
bests = {}
found = False
best = None
idIndex = {}
def addNode(nId, dist, s):
global pqs, dicts, counter
if dicts[s].get(nId):
removeNode(nId, s)
count = next(counter)
e = [dist, count, nId]
dicts[s][nId] = e
heapq.heappush(pqs[s], e)
def removeNode(nId, s):
global dicts
e = dicts[s].pop(nId)
e[-1] = 'removed'
def popNode(s):
global pqs, dicts
while pqs[s]:
e = heapq.heappop(pqs[s])
if e[-1] != 'removed':
del dicts[s][e[-1]]
return e
return 'done'
#####################################################################################################33
def checkBest():
global found, best, bests, LBq, bounds
if best is None:
return
v = LBq[0]
while bests.get(v[1]) and LBq:
heapq.heappop(LBq)
if LBq:
v = LBq[0]
if not LBq:
return
if v[0] > bounds[best][1]:
print('stoped by condition\n')
found = True
def updateBest(nId):
global best, bounds
bests[nId] = True
if best is None:
best = nId
elif bounds[best][1] > bounds[nId][1]:
best = nId
def Dijkstra(ni): #continue dijkstra from ni
global spds, paths, idIndex, visited
i = idIndex[ni]
v = popNode(i)
if v == 'done':
return 'done'
visited[i][v[2]] = 1
for u in struct[v[2]][2]:
if not visited[i][u[0]]:
if spds[i][u[0]] > spds[i][v[2]] + u[1]:
spds[i][u[0]] = spds[i][v[2]] + u[1]
paths[i][u[0]] = str(paths[i][v[2]]) + '->' + str(u[0])
addNode(u[0], spds[i][u[0]], i)
return v
def NRA(ids):
global pqs, bounds, spds, paths, dicts, idIndex, found, LBq, visited
j = 0
for i in range(0, len(ids)):
if idIndex.get(ids[i]) is None:
idIndex[ids[i]] = j
j += 1
t = len(idIndex)
bounds = [[minDist, 0] for i in range(0, len(struct))]
spds = [[minDist for i in range(0, len(struct))] for j in range(0, t)]
paths = [[None for i in range(0, len(struct))] for j in range(0, t)]
dicts = [{} for i in range(0, t)]
pqs = [[] for j in range(0, t)]
visited = [[0 for i in range(0, len(struct))] for j in range(0, t)]
j = 0
for key in idIndex:
spds[j][key] = 0
paths[j][key] = key
addNode(key, 0, j)
j += 1
if t == 1:
updateBest(pqs[0][0][-1])
return
while not found:
for key in idIndex:
v = Dijkstra(key)
if v == 'done':
return
if v[0] < bounds[v[2]][0]:
bounds[v[2]][0] = v[0]
heapq.heappush(LBq, (bounds[v[2]][0], v[2]))
maxD = max([spds[k][v[2]] for k in range(0, t)])
bounds[v[2]][1] = maxD
if maxD < minDist: #found another candidate
updateBest(v[2])
checkBest()
#############3#main#################
ids = []
try:
graph = open('out.txt', 'r')
ids = [int(sys.argv[i]) for i in range(1, len(sys.argv))]
except:
graph.close()
exit(1)
e = graph.readline().rstrip("\n")
while e:
struct.append(ast.literal_eval(e))
e = graph.readline().rstrip("\n")
graph.close()
NRA(ids)
if best is None:
print('meeting point doesnt exist')
else:
print('The optimal meeting point is: ', best)
print('Shortest path distance: ', bounds[best][1])
print('paths')
for i in range(0 , len(idIndex)):
result = [spds[i][best], paths[i][best]]
print(result)
print()