-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDijkstra.py
More file actions
57 lines (48 loc) · 1.5 KB
/
Dijkstra.py
File metadata and controls
57 lines (48 loc) · 1.5 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
from collections import deque
from util import PriorityQueue
class Dijkstra:
start = None
start_cost = 0
def __init__(self):
self.came_from = {}
self.end = None
self.cost = self.start_cost
def is_goal(self, state) -> bool:
return False
def neighbors(self, state):
pass
def reconstruct_path(self, state):
path = deque([state])
while state in self.came_from:
state = self.came_from[state]
path.appendleft(state)
return path
def reset(self):
self.came_from.clear()
self.end = None
self.cost = None
def run(self):
self.reset()
q = PriorityQueue()
q.append((self.start_cost, self.start))
seen = set()
g_score = { self.start: self.start_cost }
while q:
cost, state = q.pop()
if state in seen:
continue
seen.add(state)
if self.is_goal(state):
self.end = state
self.cost = cost
return cost
for d_cost, adj_state in self.neighbors(state):
new_cost = cost + d_cost
best = g_score.get(adj_state, None)
if best is None or new_cost < best:
self.came_from[adj_state] = state
g_score[adj_state] = new_cost
q.append((new_cost, adj_state))
@property
def path(self):
return self.reconstruct_path(self.end)