Bus Routes
10 Prerequisite LeetCode Problems
“Bus Routes” involves Graphs and BreadthFirst Search (BFS). Here are some problems to build up to this one:
“Number of Islands” (LeetCode 200): This problem will help you understand the basic concept of BreadthFirst Search (BFS) in a grid, which can then be generalized to other graphs.
“Open the Lock” (LeetCode 752): This is a direct application of BFS in a problem which is not presented as a traditional graph problem.
“Perfect Squares” (LeetCode 279): Another problem which can be solved using BFS. Here, the nodes of the graph will be the numbers and edges will be if one number can be reached from another by adding a perfect square.
“Word Ladder” (LeetCode 127): This problem is another good practice for BFS, with a little more complexity.
“Rotting Oranges” (LeetCode 994): This problem introduces the concept of multisource BFS which is useful in a variety of contexts.
“01 Matrix” (LeetCode 542): This problem further extends the multisource BFS concept in a grid.
“Shortest Path in Binary Matrix” (LeetCode 1091): Here you will apply BFS in a grid where you can move in all 8 directions.
“Flood Fill” (LeetCode 733): A simpler application of BFS on a grid.
“Clone Graph” (LeetCode 133): This problem helps you understand how to manipulate graph structures and perform BFS on them.
“Shortest Path Visiting All Nodes” (LeetCode 847): This problem is a more complex application of BFS in a graph.
You will get familiar with the concepts of BFS and Graphs to solve the “Bus Routes” problem.


Problem Classification
This problem falls under the Graphs domain. It represents a reallife scenario where you have multiple bus routes represented as routes[i] which is a list of bus stops that bus i visits in a circular route. Your goal is to determine the minimum number of buses you need to take to get from a source bus stop to a target bus stop.
What Components:
 You have an array of bus routes, with each route represented as an array of bus stops.
 Each bus route is represented as a list of unique integers.
 You have a source bus stop and a target bus stop, both are integers.
 You are required to calculate the minimum number of buses you must take to travel from the source bus stop to the target bus stop.
 You can only travel between bus stops by buses.
This problem can be classified as a shortest path problem in Graph Theory. The buses represent the vertices, and the routes represent the edges. Each bus stop is a vertex, and there is an edge between two bus stops if they are served by the same bus. The aim is to find the minimum number of edges that must be traversed to get from the source vertex to the target vertex. The graph is unweighted and undirected, as there is no cost associated with changing buses and the buses can be taken in any direction.
The approach to solving this problem could involve using BreadthFirst Search (BFS) or Dijkstra’s algorithm, both common algorithms for finding shortest paths in graphs. The difficulty lies in constructing the graph from the given routes and handling the case where no route from source to target exists.
Clarification Questions
What are the clarification questions we can ask about this problem?
Identifying Problem Isomorphism
The problem “Bus Routes” can be mapped to “Number of Islands”.
The “Bus Routes” problem can be seen as a graph problem where each bus route is a node, and two nodes are connected if their bus routes share a common stop. The task is to find the shortest path in this graph.
Similarly, “Number of Islands” is a graph problem. The map can be represented as a grid where each land piece is a node, and adjacent land pieces (north, south, east, west) are connected. The task is to count all connected components (islands).
While both problems are about exploring connected components in a graph, the actual tasks differ: in “Bus Routes”, you’re asked to find a shortest path; in “Number of Islands”, you’re asked to count connected components. Therefore, this is an approximate mapping. The “Bus Routes” problem is more complex because it involves not only finding connected components but also determining the shortest path between two particular nodes.
Constraints
Based on the problem statement and constraints, here are some specific characteristics or conditions that could be exploited to find an efficient solution:
Bounded Number of Routes: The constraint that the number of routes is at most 500, and each route has at most 10^5 bus stops allows us to exploit graphbased algorithms for solving the problem. These algorithms usually have a time complexity proportional to the number of nodes (vertices) and edges, which is manageable given the provided constraints.
Unique Stops in Each Route: The problem specifies that all the values of routes[i] are unique. This means that a particular bus route does not visit the same bus stop more than once. This reduces the complexity of the graph that we would construct from the problem’s inputs, and this is helpful in simplifying the problem.
Circular Routes: The problem specifies that each bus route is a circular route (i.e., it repeats forever). This means that we can consider the routes as cyclic graphs. As a result, we can traverse the graph in any direction, simplifying our task of finding a path from the source to the target.
Source and Target are Bus Stops: The problem specifies that the source and target are bus stops that exist in the bus routes. This can help us to eliminate any routes that do not contain either the source or target, as they would not be needed for finding the shortest path.
Remember, however, that there is no guarantee that a path between the source and target exists. In this case, the problem asks us to return 1, so we need to incorporate a check for this in our solution.
By understanding these characteristics of the problem, we can make informed decisions on the appropriate data structures and algorithms to use for an efficient solution.
Thought Process
The problem statement cues are that we want the “least number of buses” we must take to go from “source” to “target”, and if it is impossible, we return 1. This suggests we need to find the shortest path in a graph, and if no path is found, return 1.
The problem involves taking buses, each of which is a route of bus stops. We can take a different bus only when two routes have a common bus stop. This gives us a clue that we can model this problem as a graph problem where each bus route is a node and there is an edge between two nodes if the bus routes have a common bus stop.
The direction to the solution is to perform a BreadthFirst Search (BFS) from the bus route that includes the source bus stop, and keep traversing until we reach a bus route that includes the target bus stop.
Here is a Python solution:


Here we first build a graph where an edge exists between two bus stops if they belong to the same bus route. Then we do a BFS from the source to the target. If we are unable to reach the target, we return 1.
By using BFS we can ensure that the first time we reach the target, we have used the minimum number of buses, since BFS traverses a graph in layers and thus guarantees the shortest path in terms of the number of edges (or buses in this context).
Solution Approach and Analysis
The best approach is to use a BreadthFirst Search (BFS) to traverse the graph of bus routes. This algorithm will allow us to find the shortest path (least number of buses) from the source bus stop to the target bus stop.
Step 1: Create the Graph
First, represent each bus route as a node in the graph. Create edges between two nodes if the corresponding bus routes share a common bus stop. This shared bus stop signifies that it’s possible to switch from one bus route to another at that stop.
Step 2: Implement the BreadthFirst Search (BFS)
Starting from the node that contains the source bus stop, perform a BFS to find the shortest path to the node that contains the target bus stop. Use a queue to keep track of the nodes to visit next and a set to record visited nodes (to avoid cycles).
Step 3: Check for No Solution
If after performing the BFS, we do not find the target, return 1 to signify that it’s not possible to reach the target bus stop from the source bus stop.
A useful analogy would be a transportation map of a city, where each bus route is a station, and the buses are trains. We’re trying to find the fewest number of trains we need to take to go from one station to another. We can switch trains at stations that have more than one train stopping there.
Example:
Suppose we have routes = [[1,2,7],[3,6,7]], source = 1, and target = 6. We start at bus stop 1 (node [1,2,7]) and want to get to bus stop 6 (node [3,6,7]).
 The BFS starts at the node [1,2,7] and checks its neighbors.
 It finds that it can switch to the bus route [3,6,7] at bus stop 7.
 It then hops onto the new bus route and reaches the target bus stop 6.
 The BFS found a path with 2 bus changes, which is the shortest path. So the function returns 2.
This approach would work for any number of bus routes and bus stops, as long as the total number of stops is not too large. If the number of bus stops increases, the BFS could become slow as it might have to traverse a large number of nodes. Therefore, it’s always crucial to consider the problem’s constraints when selecting and implementing an algorithm.
Language Agnostic Coding Drills
 Dissect the code and identify each distinct concept it contains:
Concept 1  Data Structures: The code uses several key data structures including lists, sets, and dictionaries (in the form of defaultdict). Each of these data structures is used for a different purpose, and understanding when to use each one is key to the solution.
Concept 2  Looping: There are several loops in the solution which iterate over lists and sets. Understanding how to construct and control these loops is a core part of the solution.
Concept 3  Conditional Statements: The code uses conditional statements to control the flow of the program based on the state of the data.
Concept 4  Enumeration: The code uses the enumerate function to get both the index and value of elements in a list. This is important when you need to reference the position of an item in a list, not just its value.
Concept 5  BreadthFirst Search (BFS): The solution uses the BFS algorithm, a fundamental graph traversal algorithm. Understanding BFS and how to implement it is crucial to this problem.
 List out coding concepts in order of increasing difficulty:
Level 1  Data Structures: Understanding how and when to use basic data structures like lists, sets, and dictionaries is a fundamental coding skill.
Level 2  Looping: Looping constructs are a bit more complex because they involve understanding control flow, but they are still a fundamental coding concept.
Level 3  Conditional Statements: Conditional statements are slightly more complex because they require understanding of boolean logic.
Level 4  Enumeration: Enumeration is a more advanced concept because it involves working with multiple pieces of data simultaneously (the index and the value).
Level 5  BFS: Implementing BFS is the most advanced concept here. It requires a solid understanding of graph theory and traversal algorithms.
 Problemsolving approach from problem statement to final solution:
Step 1: Recognize the problem as a graph problem. In this case, each bus is a node and an edge exists between two buses if they share a stop.
Step 2: Create a mapping between each stop and the routes that use that stop. This can be done using a dictionary where the keys are stops and the values are sets of routes.
Step 3: Initialize a BFS queue with the starting stop and set the number of buses taken to 0. Also, initialize a set of visited stops.
Step 4: Perform BFS. For each stop currently in the queue, if it’s the target stop, return the number of buses taken. Otherwise, for each route that uses this stop, add all its stops to the BFS queue (if they haven’t been visited yet), increment the number of buses taken, and mark those stops as visited.
Step 5: If the BFS finishes without finding the target stop, return 1, indicating that it’s impossible to reach the target stop.
Targeted Drills in Python
 Python code that encapsulates each identified concept:
Concept 1  Data Structures:


Concept 2  Looping:


Concept 3  Conditional Statements:


Concept 4  Enumeration:


Concept 5  BFS (BreadthFirst Search):


 Problemspecific concepts:
Concept 6  Mapping stops to routes:


 Assembling the drills to solve the problem:
Step 1: Use the “Mapping stops to routes” concept to create a dictionary that maps each stop to the routes that pass through it.
Step 2: Initialize a BFS queue and a set to keep track of visited stops. The BFS queue initially contains a tuple with the starting stop and 0 buses.
Step 3: Use the “Looping” concept to iterate through the BFS queue. For each stop, if it’s the target stop, return the number of buses taken. If not, add all unvisited stops from routes that pass through the current stop to the BFS queue and mark them as visited.
Step 4: Use the “Conditional Statements” concept to check whether the BFS found the target stop. If the BFS finished without finding the target stop, return 1.
Q&A
Hint: I first wrote a BFS using bus stops as nodes in a graph, but got TLE in 44/47 test case. So the hint is to use bus as graph nodes and think how to determine if two buses are “connected”, ie. they share at least 1 common bus stops.
After that, find all the nodes you can start from (ie. all buses whose stops include source) and all the nodes you want to reach (ie. all buses whose stops include target). Then you can see a BFS coming into shape, remember to record the distance along the way.
Hopefully this makes the problem more approachable.
Based on your hint, the key is to treat each bus as a node in the graph. Two nodes (buses) are connected if their routes share at least one bus stop. The BFS will then be performed over the buses instead of the bus stops. This could likely solve the time limit exceeded (TLE) problem you were facing.
First, we need to determine which buses are “connected”, i.e., share at least one common bus stop. Then, we find all buses that have the source in their route and all buses that have the target in their route.
Finally, we perform a BFS over this graph of buses, starting from the buses that include the source, with the aim to reach a bus that includes the target in its route.
Another Hint: One solution is to think about this problem as a graph problem, where buses and bus stops are both vertices in the same graph. Then try using BFS to find the shortest path from source to target.
You’re correct. To reiterate, the crux of this problem lies in modeling it as a graph problem, where each bus route is a node, and there exists an edge between two nodes if their corresponding bus routes share at least one bus stop.
Once we establish this framework, we can utilize BreadthFirst Search (BFS) to find the shortest path (minimum number of buses) from source to target. The BFS starts from all buses that pass through the source stop and spreads to neighboring buses that share at least one stop with the current bus. This process continues until the BFS reaches a bus that passes through the target stop.
This solution is efficient because BFS is guaranteed to find the shortest path in an unweighted graph, which applies to this scenario because switching from one bus to another costs the same, regardless of the buses or stops involved. If no such path exists (i.e., we’ve exhausted all possible buses and haven’t found the target), we return 1 to signify that the target stop is unreachable from the source stop.
Here’s the Python code implementing this approach:


In the example:
Input: routes = [[1,2,7],[3,6,7]], source = 1, target = 6 Output: 2 Explanation: The best strategy is take the first bus to the bus stop 7, then take the second bus to the bus stop 6. How does that make any sense?
For example, if routes[0] = [1, 5, 7], this means that the 0th bus travels in the sequence 1 > 5 > 7 > 1 > 5 > 7 > 1 > … forever. The problem says it travels in that order looping which means 0th bus should travel 1 > 2 > 7 > 1 > 2 > 7 so it’s not even possible for us to go directly from bus stop 1 to bus stop 7 nor is it possible to go from bus stop 7 directly to 6? Unless the problem statement is wrong and you can go in reverse order?
My algo outputted 5 and thats what I get by following the rules logically as well.
I see where the confusion comes from. The problem statement could be clearer. When it says “if routes[0] = [1, 5, 7], this means that the 0th bus travels in the sequence 1 > 5 > 7 > 1 > 5 > 7 > 1 > … forever”, it doesn’t mean that you cannot change to a different bus at stops 5 or 7.
What it means is that this specific bus will keep following this route in this order. It does not imply that you, as a passenger, cannot switch buses at any given stop.
So in your example, you start at stop 1 and take the first bus (bus 0), then you can get off at stop 7 and switch to the second bus (bus 1), which also stops at 7, and then continue to stop 6. That’s why the answer is 2  you need to take two buses to reach your destination.
It’s like if you were in a city with a public transport system, the buses have their routes that they follow in a specific order, but you as a passenger can get on and off and switch buses at the different stops. That’s the logic followed in this problem.
Would this problem be solvable using DSU? Not sure but it reminds me of it.
While I can see why you might consider Disjoint Set Union (DSU) due to the problem’s nature of connections and relationships, DSU may not be the optimal or most straightforward approach for this problem. DSU is typically used to handle queries about whether two elements are in the same set or union, and efficiently merge two sets.
In this problem, we need to determine the minimal number of bus routes needed to get from one stop to another. This is essentially a shortest path problem in an undirected graph where bus stops are nodes, and bus routes act as edges. The BreadthFirst Search (BFS) algorithm is typically more suitable for this kind of problem, as it can search the graph level by level from the source node, making it easy to track the shortest path.
In contrast, DSU doesn’t inherently keep track of path lengths or allow for the calculation of the shortest path, making it less suitable for this problem. A conversion of the problem into a form where DSU could be applied might be possible, but it’s likely to be more complicated and less efficient than a BFSbased approach.
What if you ride the same bus twice; does that count as one or two buses?
The problem statement doesn’t explicitly clarify this point, but based on typical understanding and standard conventions, each time you board a bus would count as taking a bus. Thus, if you ride the same bus twice (meaning you get off and then later get back on), it would typically be counted as taking two buses.
In the context of the problem, the goal is to minimize the number of buses you must take, or equivalently, the number of times you have to board a bus. So riding the same bus twice would contribute 2 to the total count.
It’s always a good idea to clarify such points if you’re unsure and have the opportunity to ask.