Dsa
Graphs Bfs
Hard
Reachable Nodes in a Network
You have a network of nodes represented as an undirected graph where each node is connected by edges. Your goal is to find all nodes that can be reached from a given starting node using a BFS traversal. The nodes are numbered from 1 to N. Return the sorted list of reachable nodes, excluding the start node itself.
Input Format:
- The first line contains two integers N (number of nodes) and M (number of edges).
- The next M lines each contain two integers u and v representing an edge between nodes u and v.
- The last line contains one integer s representing the starting node.
Output Format:
- Print a sorted list of reachable nodes.
Example:
Input:
5 4
1 2
2 3
3 4
1 4
2
Output:
[3, 4]
Key concepts
graphsbfsreachability
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