-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathastar.py
More file actions
40 lines (29 loc) · 910 Bytes
/
astar.py
File metadata and controls
40 lines (29 loc) · 910 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
31
32
33
34
35
36
37
38
39
40
"""The A* algorithm for shortest-path search with a heuristic."""
import math
import heapq
from collections import namedtuple
Node = namedtuple("Node", "name x y")
def euclid(p, q):
return math.hypot(p.x - q.x, p.y - q.y)
def create_path(parent, t):
path = [t]
while t in parent:
t = parent[t]
path.append(t)
return tuple(reversed(path))
def astar(G, source, target, h):
dist = {v: float("inf") for v in G.nodes}
parent = {}
dist[source] = 0
pq = [(h(source, target), source)]
while pq:
d, v = heapq.heappop(pq)
if v == target:
return create_path(parent, target), d
for u in G[v]:
this = dist[v] + G[v][u]["weight"]
if this < dist[u]:
dist[u] = this
parent[u] = v
heapq.heappush(pq, (this + h(u, target), u))
return None, float("inf")