-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdijkstra.py
More file actions
55 lines (49 loc) · 1.31 KB
/
dijkstra.py
File metadata and controls
55 lines (49 loc) · 1.31 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
import sys
from collections import deque
file_name = sys.argv[1]
name1 = sys.argv[2]
graph = dict()
f = open(file_name, "r", encoding="utf-8")
while True:
s = f.readline().strip("\n")
if s == "":
break
else:
city1, city2, dist = [x for x in s.split("\t")]
dist = int(dist)
if city1 not in graph:
graph[city1] = dict()
graph[city1][city2] = dist
else:
graph[city1][city2] = dist
if city2 not in graph:
graph[city2] = dict()
graph[city2][city1] = dist
else:
graph[city2][city1] = dist
f.close()
from math import inf
dists = {x : inf for x in graph}
dists[name1] = 0
visited = dict()
vertex = name1
while dists:
for neighbour, dist in graph[vertex].items():
if neighbour not in visited:
calc_dist = dists[vertex] + dist
if dists[neighbour] > calc_dist:
dists[neighbour] = calc_dist
visited[vertex] = dists[vertex]
dists.pop(vertex)
if dists:
next_vertex = [dist for dist in dists.items()]
next_vertex = sorted(next_vertex, key = lambda x: x[1])
vertex = next_vertex[0][0]
tmp= list(visited)
print("Top 5 nearest cities:")
for i in range(1, 6):
print(f"{tmp[i], visited[tmp[i]]}")
print("Top 5 distant cities:")
for i in range(5, 0, -1):
print(f"{tmp[-i], visited[tmp[-i]]}")
#python3 dijkstra.py "belarus.tsv" "Минск"