Settle the nearest unvisited node, relax its edges.
def dijkstra(graph, start):
pq = [(0, start)]
dist = {n: inf for n in graph}dist[start] = 0
while pq:
d, u = heappop(pq) # Settle node
for v, w in graph[u]:
if dist[u] + w < dist[v]: # Relax
dist[v] = dist[u] + w
heappush(pq, (dist[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.