Model bus routes as BFS nodes to find the fewest buses between two stops. Learn how a stop-to-routes index and one-time transfer expansion give linear incidence complexity, then reconstruct route and transfer choices.
Given `routes`, `source`, and `destination`, return the minimum number of buses needed to travel from the source stop to the destination stop. Each `routes[i]` lists the stops served by one route. Two routes permit a transfer when they share a stop. Once aboard a route, its listed stops are mutually reachable for this exercise; the task has no timetable or directional-segment restriction.
Return zero if the endpoints are equal, even if the stop appears in no route. Return `-1` if the destination cannot be reached. Stop identifiers are integers; route indices are zero-based. No numerical size bounds were reported.
### Example
For source `10`, destination `70`, and routes
```
[[10,20,30], [25,30,40,50], [50,60,70], [20,80]]
```
the answer is `3`: board route 0, transfer at stop 30 to route 1, and transfer at stop 50 to route 2. The objective counts buses boarded, not stops traveled and not just transfers.
### Clarifications and Edge Cases
Handle a direct route, disconnected groups of routes, and a source absent from every route. For practice robustness, an empty route serves no stops and repeated stops within one route do not create extra buses. Clarify that physical travel time is outside the task.
### What a Strong Answer Covers
Model routes as BFS nodes, build a stop-to-routes index, and show how to avoid repeatedly expanding the same transfer stop. Prove that the first reached destination uses the fewest buses. Define complexity using the total number of route-stop entries, and distinguish an implicit incidence representation from explicitly materializing all route-pair edges.
### Follow-up Questions
How would you record the chosen route indices and transfer stops to reconstruct an itinerary? Why does BFS guarantee minimum buses? What exactly can be reconstructed when the input does not specify an ordered or directional path within each route?
Overview: Model bus routes as BFS nodes to find the fewest buses between two stops. Learn how a stop-to-routes index and one-time transfer expansion give linear incidence complexity, then reconstruct route and transfer choices.
Given routes, source, and destination, return the minimum number of buses needed to travel from the source stop to the destination stop. Each routes[i] lists the stops served by one route. Two routes permit a transfer when they share a stop. Once aboard a route, its listed stops are mutually reachable for this exercise; the task has no timetable or directional-segment restriction.
Return zero if the endpoints are equal, even if the stop appears in no route. Return -1 if the destination cannot be reached. Stop identifiers are integers; route indices are zero-based. No numerical size bounds were reported.
Example
For source 10, destination 70, and routes
[[10,20,30], [25,30,40,50], [50,60,70], [20,80]]
the answer is 3: board route 0, transfer at stop 30 to route 1, and transfer at stop 50 to route 2. The objective counts buses boarded, not stops traveled and not just transfers.
Clarifications and Edge Cases
Handle a direct route, disconnected groups of routes, and a source absent from every route. For practice robustness, an empty route serves no stops and repeated stops within one route do not create extra buses. Clarify that physical travel time is outside the task.
What a Strong Answer Covers Guidance
Model routes as BFS nodes, build a stop-to-routes index, and show how to avoid repeatedly expanding the same transfer stop. Prove that the first reached destination uses the fewest buses. Define complexity using the total number of route-stop entries, and distinguish an implicit incidence representation from explicitly materializing all route-pair edges.
Follow-up Questions Guidance
How would you record the chosen route indices and transfer stops to reconstruct an itinerary? Why does BFS guarantee minimum buses? What exactly can be reconstructed when the input does not specify an ordered or directional path within each route?