-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshortest-path.py
42 lines (33 loc) · 1.22 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
# calculates shortest paths within graph passed in as adjacency list
from collections import defaultdict
def import_file():
res = {}
with open('/home/tom/Documents/lisp/dijkstraData.txt','r') as f:
for line in f.readlines():
split_line = [x for x in line.strip(' \t\n\r').split()]
res[int(split_line[0])] = [x.split(',') for x in split_line[1:]]
return res
def dijkstra(graph, start=1):
processed = set()
unvisited = {x for x in range(1, 201)} # 201
distance = {}
for zxy in range(1, 201):
distance[zxy] = 1000000
distance[start] = 0
previous = {}
current = start
while len(unvisited) > 0:
neighbours = graph[current]
for n in neighbours:
alt = distance[current] + int(n[1])
if alt < distance[int(n[0])]:
distance[int(n[0])] = alt
previous[int(n[0])] = current
unvisited.remove(current)
foodict = {k: v for k, v in distance.items() if k in unvisited}
if foodict:
current = min(foodict, key=foodict.get)
return distance, previous
output = dijkstra(import_file())[0]
for x12 in [7, 37, 59, 82, 99, 115, 133, 165, 188, 197]:
print output[x12]