-
Notifications
You must be signed in to change notification settings - Fork 0
/
shortest_path.py
307 lines (265 loc) · 7.82 KB
/
shortest_path.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
'''
Created on March 20, 2013
@author: Boris Jurosevic
Trip through Germany
CS 2430
'''
from __future__ import print_function
import heapq
from data_manager import Data_Manager
from collections import defaultdict
import math
import random
# our main algorithm for testing
def algo_2(pocetak, kraj):
ad = Data_Manager()
z = ad.dajMiGradove()
D = {}
P = {}
lock = {}
open = {}
for gradovi in z:
D[gradovi] = float('inf')
P[gradovi] = ""
D[pocetak] = 0
r = z
while len(r) > 0:
Q = None
GRAD = ''
for SVEU in r:
if Q == None:
l = D[SVEU]
Q = l
GRAD = SVEU
elif D[SVEU] < Q:
p = D[SVEU]
Q = p
GRAD = SVEU
r.remove(GRAD)
removITALL = GRAD
W = ad.dajMiSpoj(removITALL)
for allIN in W:
if allIN[1] == GRAD:
intthFirstSpot = allIN[2]
inttheLastSpot = allIN[0]
xy = GRAD
all = D[xy] + inttheLastSpot
if D[intthFirstSpot] == float('inf'):
D[intthFirstSpot] = all
P[intthFirstSpot] = xy
elif D[intthFirstSpot] > all:
D[intthFirstSpot] = all
P[intthFirstSpot] = xy
to = []
m = kraj
while m != pocetak:
if to.count(m) == 0:
veliko = m
to.insert(0, veliko)
node = P[veliko]
else:
break
veratiTOALL = pocetak
to.insert(0, pocetak)
return (D[kraj], to)
# our starter as a path
def pocniOdArray( puteljak ):
infromacija = Data_Manager()
papir = list
a = defaultdict( papir )
value = 0
abc = infromacija.pocetakSav()
for point in abc:
myArray = 0
presliSve = []
putic = []
thisPint = point
presliSve.append(thisPint)
alpha = point
spoj = infromacija.dajMiSpoj(point)
heapq.heapify(spoj)
while spoj:
alphaAll = 0
getInfo = spoj
kolikoJeSkupo, PartOne, PartTwo = heapq.heappop(getInfo)
if PartTwo not in presliSve:
getAllofInfo = PartTwo
presliSve.append(getAllofInfo)
all_Together = (PartOne, PartTwo, kolikoJeSkupo)
putic.append(all_Together)
putItBackIn = PartTwo
for all in infromacija.dajMiSpoj(putItBackIn):
abc = spoj
heapq.heappush(abc, all)
alphaTo = putic
puteljak.append(alphaTo)
put_1 = []
put_2 = []
put_3 = []
for all in puteljak:
put_1.append(nacrtajSvePuteve(all))
return put_1
# make them appear
# come back to this
def nacrtajSvePuteve(poruka):
prije = (-1,-1,-1)
sviBrojevi = 0
PUT = []
PUT.append([])
for xyz in poruka:
Part_1 = xyz[0]
Part_2 = prije[1]
if Part_1 != Part_2 and Part_2 != -1:
sviBrojevi = sviBrojevi + 1
PUT.append([])
prije = xyz
connect = sviBrojevi
protocol = xyz
PUT[connect].append(protocol)
last = PUT
return spojiSvePuteve(last)
# this is where we have them all together
#test it one more time
def spojiSvePuteve(nastavi):
begin = None
pocni = 0
kadSeSveZavrsi = nastavi.pop(pocni)
while nastavi:
begin = len(kadSeSveZavrsi)
end = kadSeSveZavrsi[begin -1][1]
dobarPocetak = end
k = None
aTob = None
for later in nastavi:
if k != None:
thisTime = dobarPocetak
thisLast = later[0][0]
y = algo(thisTime, thisLast)
if y[0] < k[0]:
k = y
aTob = later
else:
together = dobarPocetak
allLater = later[0][0]
k = algo(together, allLater)
aTob = later
if k[0] > 0:
pointA = k[1][0]
pintB = k[1][len(k[1])-1]
pintC = k[0]
kadSeSveZavrsi.append((pointA, pintB, pintC))
kadSeSveZavrsi = kadSeSveZavrsi + aTob
pointToIt = aTob
nastavi.remove(pointToIt)
afterAll = len(kadSeSveZavrsi)
pointerr = kadSeSveZavrsi[afterAll - 1][1]
end = krajSviPuteva(pointerr)
kadSeSveZavrsi.append(end)
return kadSeSveZavrsi
# this is gonna be the last route
# looks ok for now
def krajSviPuteva(x):
a = (float('inf'), 0)
b = Data_Manager()
end = b.pocetakSav()
for allOfIt in end:
putvi = x
putvi_2 = allOfIt
STAZA = algo(putvi, putvi_2)
if STAZA[0] < a[0]:
a = STAZA
mesto = a[0]
STAZA = a[1]
prosto_A = STAZA[0]
prosto_B = len(STAZA) - 1
allToged = mesto
return (prosto_A, STAZA[prosto_B], allToged)
# out main method for ways
def algo(pocetak, kraj):
ad = Data_Manager()
z = ad.dajMiGradove()
D = {}
P = {}
lock = {}
open = {}
for gradovi in z:
D[gradovi] = float('inf')
P[gradovi] = ""
D[pocetak] = 0
r = z
while len(r) > 0:
Q = None
GRAD = ''
for SVEU in r:
if Q == None:
l = D[SVEU]
Q = l
GRAD = SVEU
elif D[SVEU] < Q:
p = D[SVEU]
Q = p
GRAD = SVEU
r.remove(GRAD)
removITALL = GRAD
W = ad.dajMiSpoj(removITALL)
for allIN in W:
if allIN[1] == GRAD:
intthFirstSpot = allIN[2]
inttheLastSpot = allIN[0]
xy = GRAD
all = D[xy] + inttheLastSpot
if D[intthFirstSpot] == float('inf'):
D[intthFirstSpot] = all
P[intthFirstSpot] = xy
elif D[intthFirstSpot] > all:
D[intthFirstSpot] = all
P[intthFirstSpot] = xy
to = []
m = kraj
while m != pocetak:
if to.count(m) == 0:
veliko = m
to.insert(0, veliko)
node = P[veliko]
else:
break
veratiTOALL = pocetak
to.insert(0, pocetak)
return (D[kraj], to)
# this is our tester method
def spojiSvePuteve_2(nastavi):
begin = None
pocni = 0
kadSeSveZavrsi = nastavi.pop(pocni)
while nastavi:
begin = len(kadSeSveZavrsi)
end = kadSeSveZavrsi[begin -1][1]
dobarPocetak = end
k = None
aTob = None
for later in nastavi:
if k != None:
thisTime = dobarPocetak
thisLast = later[0][0]
y = algo(thisTime, thisLast)
if y[0] < k[0]:
k = y
aTob = later
else:
together = dobarPocetak
allLater = later[0][0]
k = algo(together, allLater)
aTob = later
if k[0] > 0:
pointA = k[1][0]
pintB = k[1][len(k[1])-1]
pintC = k[0]
kadSeSveZavrsi.append((pointA, pintB, pintC))
kadSeSveZavrsi = kadSeSveZavrsi + aTob
pointToIt = aTob
nastavi.remove(pointToIt)
afterAll = len(kadSeSveZavrsi)
pointerr = kadSeSveZavrsi[afterAll - 1][1]
end = krajSviPuteva(pointerr)
kadSeSveZavrsi.append(end)
return kadSeSveZavrsi