-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdijkstra.py
More file actions
51 lines (51 loc) · 1.02 KB
/
dijkstra.py
File metadata and controls
51 lines (51 loc) · 1.02 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
from collections import deque
import math
graph = {}
graph[1] = {}
graph[2] = {}
graph[3] = {}
graph[4] = {}
graph[5] = {}
graph[6] = {}
graph[1][2] = 50
graph[1][3] = 45
graph[1][4] = 10
graph[2][4] = 15
graph[2][3] = 10
graph[3][5] = 30
graph[5][3] = 35
graph[4][1] = 10
graph[4][5] = 15
graph[5][2] = 20
graph[6][5] = 3
graph[3][6] = 10
print(graph)
visited = []
costs = {x:math.inf for x in range(1,7) }
costs[1]=0
parents = {1:None}
q = 1
def find_lowest(costs,visited):
m = math.inf
res = None
for item in costs:
if costs[item] < m and item not in visited:
m = costs[item]
res = item
print(res)
return res
while q:
if q == 6:
item = 6
while item:
print(item,end='->')
item = parents[item]
break
visited.append(q)
for child in graph[q]:
if costs[child] > costs[q]+graph[q][child]:
costs[child] = costs[q]+graph[q][child]
print(costs)
low = find_lowest(costs,visited)
parents[low] = q
q = low