-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdjikstra.py
More file actions
54 lines (45 loc) · 1.42 KB
/
djikstra.py
File metadata and controls
54 lines (45 loc) · 1.42 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
costs = [
[1, 1, 1, 10],
[10, 10, 1, 10],
[10, 10, 1, 10],
[10, 10, 1, 1],
]
def get_neighbors(node, n):
x, y = node
neighbors = []
if x < n - 1:
neighbors += [(x + 1, y)]
if y < n - 1:
neighbors += [(x, y + 1)]
# if x < n - 1 and y < n - 1:
# neighbors += [(x + 1, y + 1)]
return neighbors
def get_travels(costs):
n = len(costs)
# init
travels = [[(-1, []) for i in range(n)] for i in range(n)]
travels[0][0] = costs[0][0], [(0, 0)]
edges = [(0, 0)]
while edges:
new_edges = []
for edge in edges:
xe, ye = edge
current_cost, current_path = travels[xe][ye]
neighbors = get_neighbors(edge, n)
for neighbor in neighbors:
xn, yn = neighbor
c = costs[xn][yn]
c_tot, path = travels[xn][yn]
if not(path) or current_cost + c < c_tot:
new_cost = current_cost + c
new_path = current_path + [(xn, yn)]
travels[xn][yn] = new_cost, new_path
new_edges.append(neighbor)
edges = new_edges
return travels
if __name__ == '__main__':
for cost in costs:
print(cost)
travels = get_travels(costs)
for travel in travels:
print(travel)