Find the cheapest route to every node in a weighted graph, always expanding the closest node next with a heap.
Python · intermediate · 10 min · 58 steps · free
import heapq
graph = {'A': {'B': 4, 'C': 1}, 'B': {'D': 1}, 'C': {'B': 2, 'D': 5}, 'D': {}}
def dijkstra(start):
dist = {start: 0}
heap = [(0, start)]
while heap:
d, node = heapq.heappop(heap)
if d > dist[node]:
continue
for neighbor, weight in graph[node].items():
new = d + weight
if new < dist.get(neighbor, float('inf')):
dist[neighbor] = new
heapq.heappush(heap, (new, neighbor))
return dist
print(dijkstra('A'))heapq module, so the program can use the functions it provides.dijkstra(). Its body does not run yet: it runs each time the function is called.dijkstra(), with start = 'A'. It gets its own names.dist is created and points to a new dict: {A: 0}.heap is created and points to a new list: [a tuple].heap is true, so the loop body runs.d is created with the value 0. node is created with the value 'A'. a tuple is removed from the end of the list that heap points to.d > dist[node] is false, so the block under it is skipped.neighbor is created with the value 'B'. weight is created with the value 4.new is created with the value 4.new < dist.get(neighbor, float('inf')) is true, so the block under it runs.B with the value 4 is added to the dict that dist points to.heap points to.neighbor changes from 'B' to 'C'. weight changes from 4 to 1.new changes from 4 to 1.new < dist.get(neighbor, float('inf')) is true, so the block under it runs.C with the value 1 is added to the dict that dist points to.heap points to, index 0 changes from a tuple to a tuple and index 1 changes from null to a tuple.for neighbor, weight in graph[node].items().heap is true, so the loop body runs.d > dist[node] is false, so the block under it is skipped.neighbor changes from 'C' to 'B'. weight changes from 1 to 2.new changes from 1 to 3.new < dist.get(neighbor, float('inf')) is true, so the block under it runs.heap points to, index 0 changes from a tuple to a tuple and index 1 changes from null to a tuple.neighbor changes from 'B' to 'D'. weight changes from 2 to 5.new changes from 3 to 6.new < dist.get(neighbor, float('inf')) is true, so the block under it runs.D with the value 6 is added to the dict that dist points to.heap points to.for neighbor, weight in graph[node].items().heap is true, so the loop body runs.d changes from 1 to 3. node changes from 'C' to 'B'. a tuple is removed from the start of the list that heap points to.d > dist[node] is false, so the block under it is skipped.weight changes from 5 to 1.new changes from 6 to 4.new < dist.get(neighbor, float('inf')) is true, so the block under it runs.D in the dict that dist points to changes from 6 to 4.heap points to.for neighbor, weight in graph[node].items().heap is true, so the loop body runs.d changes from 3 to 4. In the list that heap points to, index 0 changes from a tuple to a tuple.d > dist[node] is true, so the block under it runs.heap is true, so the loop body runs.node changes from 'B' to 'D'. a tuple is removed from the start of the list that heap points to.d > dist[node] is false, so the block under it is skipped.for neighbor, weight in graph[node].items().heap is true, so the loop body runs.d changes from 4 to 6. a tuple is removed from the end of the list that heap points to.d > dist[node] is true, so the block under it runs.continue.heap is false, so the loop ends.dijkstra() has finished, so its names are gone. It prints {'A': 0, 'B': 3, 'C': 1, 'D': 4}.Answer: That entry is outdated
Answer: Use Bellman-Ford instead
← Depth-First Search · Backtracking: All Subsets →