PythiloLesson 24 of 32 · progress kept in this browser

Find cheapest routes with Dijkstra's algorithm

When edges have costs, the fewest edges is not the cheapest route. Dijkstra's algorithm always settles the closest unsettled node next, using a heap of tentative distances.

import heapq
frontier = [(0, "a")]
cost, node = heapq.heappop(frontier)
Your task

Write distances(graph, start) where graph maps a node to a list of (neighbour, cost) pairs with non-negative costs. Return a dict of the cheapest cost to every reachable node.