Complete the code to initialize the distance to the source node as zero.
distances = {node: float('inf') for node in graph}
distances[source] = [1]The distance from the source node to itself is always zero.
Complete the code to select the unvisited node with the smallest distance.
current_node = min( (node for node in unvisited), key=lambda node: distances[node] ) if unvisited else [1]
If there are no unvisited nodes left, current_node should be None to stop the algorithm.
Fix the error in updating the distance for neighbor nodes.
for neighbor, weight in graph[current_node].items(): if neighbor in unvisited: new_distance = distances[current_node] + weight if new_distance [1] distances[neighbor]: distances[neighbor] = new_distance
We update the neighbor's distance only if the new distance is smaller than the current known distance.
Fill both blanks to correctly remove the current node from unvisited and add it to visited.
unvisited.[1](current_node) visited.[2](current_node)
We remove the current node from unvisited and add it to visited sets.
Fill all three blanks to complete the dictionary comprehension that builds the graph from edges.
graph = {}
for u, v, w in edges:
graph.setdefault([1], [2])[[3]] = wWe use u as the key, setdefault with an empty dictionary, and v as the neighbor key to assign weight w.
