-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdijkstras.py
More file actions
30 lines (26 loc) · 928 Bytes
/
dijkstras.py
File metadata and controls
30 lines (26 loc) · 928 Bytes
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
nodes = ('A', 'B', 'C', 'D')
distances = {
'B': {'A': 1, 'D': 2},
'A': {'B': 1, 'D': 9, 'C' :1},
'D': {'B': 2, 'C': 1, 'A': 9},
'C': {'A': 1, 'D': 1}}
unvisited = {node: None for node in nodes} #using None as +inf
print (unvisited)
visited = {}
current = 'A'
currentDistance = 0
unvisited[current] = currentDistance
while True:
for neighbour, distance in distances[current].items():
print(neighbour)
print(unvisited)
if neighbour not in unvisited: continue
newDistance = currentDistance + distance
if unvisited[neighbour] is None or unvisited[neighbour] > newDistance:
unvisited[neighbour] = newDistance
visited[current] = currentDistance
del unvisited[current]
if not unvisited: break
candidates = [node for node in unvisited.items() if node[1]]
current, currentDistance = sorted(candidates, key = lambda x: x[1])[0]
print(visited)