Dsa
Graphs Bfs
Easy
Transportation Network Analysis
In a metropolitan area, there is a network of bus lines connecting various bus stops. Each stop can be connected to multiple other stops, and any stop can be reached from any other stop using the bus lines. Given a representation of this network (as a graph) and a starting stop, write a function to return the number of bus stops that can be reached from the starting stop. Input:
The first line contains two integers N (the number of bus stops) and M (the number of connections).
The next M lines contain two integers A and B representing a direct connection between bus stops A and B.
The last line contains a single integer representing the starting bus stop.
Output:
An integer representing the number of reachable bus stops, excluding the starting stop.
Example:
Input:
4 3
1 2
1 3
2 4
1
Output:
3
Key concepts
graphsbfstransportation
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now