-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdijkstra.py
More file actions
63 lines (46 loc) · 1.14 KB
/
Copy pathdijkstra.py
File metadata and controls
63 lines (46 loc) · 1.14 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
import sys
def read_dijkstra_graph(filename):
graph = {}
weights = {}
with open(filename, 'r') as f:
for line in f:
line = line.strip('\n')
line = line.strip('\r')
line = line.split('\t')
v = int(line[0])
graph[v] = []
weights[v] = []
for i in range(1, len(line) - 1):
node = line[i].split(',')
graph[v].append(int(node[0]))
weights[v].append(int(node[1]))
return graph, weights
def shortest_path(G, weights, source):
Q = []
dist = {}
for vertex in G:
dist[vertex] = float('Inf')
Q.append(vertex)
dist[source] = 0
while Q:
distances = []
vertices = []
for v in Q:
distances.append(dist[v])
vertices.append(v)
min_element = min(distances)
index_of_min_element = distances.index(min_element)
u = vertices[index_of_min_element]
Q.remove(u)
for i in range(len(G[u])):
neighbor = G[u][i]
alt = dist[u] + weights[u][i]
if alt < dist[neighbor]:
dist[neighbor] = alt
return dist
filename = 'dijkstraData.txt'
G, weights = read_dijkstra_graph(filename)
dist = shortest_path(G, weights, 1)
nodes = [7,37,59,82,99,115,133,165,188,197]
for node in nodes:
print(dist[node])