Shortest number of steps
graph is an adjacency dictionary for a two-way graph; every node is a
key. Write shortest_steps(graph, start, goal) that returns the fewest
edges on any route from start to goal, or -1 if there is no route.
start and goal may be the same node, in which case the answer is 0.
graph = {"A": ["B", "C"], "B": ["A", "D"], "C": ["A", "D"], "D": ["B", "C", "E"], "E": ["D"], "F": []}
shortest_steps(graph, "A", "E") -> 3
shortest_steps(graph, "A", "F") -> -1
shortest_steps(graph, "A", "A") -> 0
The point is to practise breadth-first search with a distance dictionary.
def shortest_steps(graph, start, goal):
...