We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Next Nearest Node

We also need a function that finds the "next nearest node," i.e. the next node to visit; this is a critical part of Dijkstra's algorithm.

Assignment

Complete the next_nearest_node function. It returns the closest unvisited node.

This function accepts a distances dictionary, which is a mapping of:

node label -> distance

And it returns the "node label" (a string) with the smallest "distance" value that exists in the unvisited set. If there are no eligible nodes, it returns None.

For example, given:

  • distances = {'Hartford': 7, 'New Haven': 2, 'Greenwich': 3}
  • unvisited = {'Hartford', 'Greenwich'}

The function should return 'Greenwich', since it has the smallest distance (3) among the unvisited nodes.