Dsa
Graphs Bfs
Easy
Device Connectivity
In a network of devices, some devices are directly connected while others are not. Given a list of connections between devices, your task is to determine how many devices can be reached from a given starting device through direct connections.
Input format:
- The first line contains two integers n (1 <= n <= 1000) and m (0 <= m <= 2000), where n is the number of devices and m is the number of connections.
- The next m lines each contain two integers a and b (1 <= a, b <= n), indicating that device a is connected to device b.
- The last line contains a single integer d (1 <= d <= n), the starting device.
Output format:
- Output a single integer representing how many devices can be reached from device d.
Example:
Input:
5 4
1 2
2 3
1 4
4 5
1
Output:
5
Key concepts
graphsbfsnetwork
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