Shortest path with BFS

Find a route from start to finish with the fewest steps. On a map where each point is a station, this gives a route with the fewest stops along the way.

Search in layers. The first time we reach a vertex, remember who led us there. Follow those links back from the finish and read the route in the other direction.

With adjacency lists, traversal takes O(V + E) time for V vertices and E edges.