PythiloMedium · 20 min · Task 13 of 20 · progress kept in this browser

Fewest hops

A graph is given as a dict from each node to the nodes it links to. Find the smallest number of edges from one node to another.

g = {"a": ["b", "c"], "b": ["d"],
     "c": ["d"], "d": []}
fewest_hops(g, "a", "d")  # 2
Your task

Write fewest_hops(graph, start, goal) returning the smallest number of edges from start to goal, or -1 if goal cannot be reached. If start equals goal, the answer is 0.