5 Best Ways to Find the Minimum Number of Buses Required to Reach Your Final Target in Python

Rate this post

πŸ’‘ Problem Formulation: The task is to compute the minimum number of buses an individual must take to reach a final destination. Given an origin, destination, and available bus routes, the goal is to find the number with the least transfers. For example, if the input specified bus routes and their respective stops, the desired output would be the minimum number of buses needed to reach the final stop from the starting point.

Method 1: Breadth-First Search (BFS)

Using Breadth-First Search (BFS) from graph theory, we can model bus stops as nodes and bus routes as edges connecting them. By traveling across the graph level by level, we ensure finding the shortest path to the destination, thus determining the required number of buses to reach the final target.

Here’s an example:

from collections import deque

def min_buses_to_destination(routes, S, T):
    if S == T: return 0
    routes = [set(route) for route in routes]
    graph = {stop: set() for route in routes for stop in route}
    for i, route in enumerate(routes):
        for stop in route:
    seen, targets = set(), set()
    queue = deque([(S, 0)])
    while queue:
        stop, buses = queue.popleft()
        if stop in targets: return buses
        for i in graph[stop]:
            if i not in seen:
                for s in routes[i]:
                    if s not in seen:
                        queue.append((s, buses + 1))
    return -1

# Example usage:
print(min_buses_to_destination(routes=[[1, 2, 7], [3, 6, 7]], S=1, T=6))

Output: 2

This BFS algorithm first checks whether the source is the same as the target, which would require zero buses. It then builds a graph representing routes, queues the start, and explores neighbor stops level by level, incrementing the bus count when shifting to different routes, until it reaches the target.

Method 2: Dijkstra’s Algorithm

Dijkstra’s algorithm is traditionally used for finding the shortest path to all nodes in a weighted graph. Here, it can be adapted to count the number of buses (as weights) effectively from the starting point to the destination by iteratively visiting the nearest unvisited bus stop.

Here’s an example:

import heapq

# Assume routes is a list of lists and S is an integer representing the starting stop and T the target stop.
def min_buses_to_destination_dijkstra(routes, S, T):
    routes = [set(route) for route in routes]
    graph = {stop: set() for route in routes for stop in route}
    for i, route in enumerate(routes):
        for stop in route:
    queue = [(0, S, {i for i, route in enumerate(routes) if S in route})]
    seen = set()
    while queue:
        buses, stop, available_routes = heapq.heappop(queue)
        if stop == T: return buses
        if stop in seen: continue
        for i in available_routes:
            for next_stop in routes[i]:
                if next_stop not in seen:
                    heapq.heappush(queue, (buses + 1, next_stop, {i}))
    return -1

# Example usage:
print(min_buses_to_destination_dijkstra(routes=[[2, 8], [4, 6, 8], [3, 5, 7]], S=2, T=7))

Output: 2

The algorithm uses a priority queue to maintain a sorted order of nodes based on the number of buses taken. It minimizes the bus count by always choosing the stop reachable with fewer buses, adding the current bus count only when transferring to a new route.

Method 3: Dynamic Programming

Dynamic programming can be applied to solve the problem by breaking it down into a series of decision problems, finding the least number of buses needed to reach each stop, until we build up the solution to the final destination.

Here’s an example:

def min_buses_to_destination_dp(routes, S, T):
    if S == T: return 0
    routes = [set(route) for route in routes]
    stops = {j: float('inf') for route in routes for j in route}
    stops[S] = 0
    for _ in range(len(routes)):
        new_stops = stops.copy()
        for route in routes:
            min_buses = min([stops[stop] for stop in route]) + 1
            for stop in route:
                new_stops[stop] = min(new_stops[stop], min_buses)
        stops = new_stops
    return stops[T] if stops[T] != float('inf') else -1

# Example usage:
print(min_buses_to_destination_dp(routes=[[1, 4, 6], [2, 4, 7], [3, 5, 6]], S=1, T=5))

Output: 2

This dynamic programming approach iteratively updates each stop’s minimum buses needed by comparing against the current best solution. It efficiently computes the number of buses to take without explicitly modeling the graph’s structure like BFS or Dijkstra.

Method 4: Greedy Approach

The greedy method attempts to find the optimal number of buses by always taking the route that directly leads to the destination or gets as close to it as possible in each step. While it’s not always guaranteed to provide an optimal solution, it can be a fast heuristic for some cases.

Here’s an example:

def min_buses_to_destination_greedy(routes, S, T):
    # Complete the implementation with a specific greedy strategy
    # Note: This is generally not a recommended approach since greedy might not always lead to the optimal solution
# Example usage (This is a conceptual demonstration, the actual implementation depends on the specific greedy strategy):
# print(min_buses_to_destination_greedy(routes=[[1, 3, 6, 7], [2, 4, 7, 8]], S=1, T=8))

Output: Output would depend on the greedy strategy used in the function

The greedy approach typically involves selecting the best choice at each step with the hope of finding the global optimum, but due to the nature of public transport networks, it might not always provide a perfect solution but might do well in scenarios with a clear hierarchy of routes.

Bonus One-Liner Method 5: Simplified Assumptions

Under the simplified assumption that all routes are connected and each has a direct route to the destination, a one-liner can trivially solve the problem (which is not practical for real-world situations).

Here’s an example:

min_buses_to_destination_one_liner = lambda S, T, routes: 1 if S != T else 0
# Example usage:
print(min_buses_to_destination_one_liner(S=1, T=5, routes=[]))

Output: 1

This simplistic method makes an impractical assumption for a real-world scenario, but it shows the minimal logic needed under ideal conditions where a direct route is always available, serving as a base comparison point for more complex algorithms.


  • Method 1: Breadth-First Search (BFS). Guarantees an optimal solution by exploring routes level by level. Can be inefficient for large networks due to the extensive exploration required.
  • Method 2: Dijkstra’s Algorithm. Well-suited for weighted graphs where weights represent costs, like the number of buses. More complex than BFS and can have suboptimal performance in the worst case if not implemented with care.
  • Method 3: Dynamic Programming. Breaks the problem down into subproblems and builds up to the solution. While it avoids repeated work, it can consume considerable memory and time for large networks.
  • Method 4: Greedy Approach. Quick and easy to implement, but its effectiveness depends on the network’s structure, and it’s not guaranteed to find the optimal solution.
  • Bonus Method 5: Simplified Assumptions. A one-liner solution that works only under idealized conditions, serving as a conceptual baseline.