Checkpoint

Checkpoint: Bridge Routes

The Guild's bridge-keepers send in a list of bridges. Each bridge is a tuple (from_island, to_island, minutes). Bridges are one-way. Before couriers can use them, someone has to turn that list into a graph and find routes. That someone is you!

bridges = [("Depot", "Reef", 1), ("Reef", "Mill", 2)]
build_network(bridges)
# {"Depot": [("Reef", 1)], "Reef": [("Mill", 2)]}

Remembering the way back

BFS can tell you the route, not just the number of bridges. Each time it discovers an island, store where it came from in a prev dict. Then start at the goal and follow prev back to the start:

path = [goal]
while path[-1] != start:
    path.append(prev[path[-1]])
path.reverse()

Assignment

  1. build_network(bridges) returns a dict mapping each island to a list of (neighbor, minutes) tuples, in the order the bridges are listed. Only islands with an outgoing bridge need a key.
  2. fewest_bridges(network, start, goal) uses BFS to return the list of islands on a route with the fewest bridges (ignore the minutes). Return [start] if start == goal, and [] if the goal can't be reached.
  3. Some islands have no outgoing bridges, so they have no key. Use network.get(island, []) to get their (empty) list.