Dijkstra guided by a heuristic toward the target.
def astar(graph, start, target):
pq = [(h(start), start)]
g_score = {n: inf for n in graph}g_score[start] = 0
while pq:
f, u = heappop(pq) # Visit
if u == target: break # Reached
for v, w in graph[u]:
tentative = g_score[u] + w
if tentative < g_score[v]:
g_score[v] = tentative
heappush(pq, (tentative + h(v), v))
Dynamically typed and interpreted — every comparison and swap is dispatched by the interpreter at run time, so tight loops run roughly 10–100× slower than compiled C/C++. Unbeatable for learning the idea with the least code; not what you reach for when the inner loop is the bottleneck.