Dsa
Graphs Bfs
Medium
Communication Network
You are tasked with managing a network of computers represented as an undirected graph. Each computer might either have a connection or not. Given an initial computer and a target computer, determine the minimum number of connections required to reach the target computer or return -1 if it's unreachable.
Input:
- The first line contains two integers, N (number of computers) and M (number of connections).
- The next M lines contain two integers each, representing a connection between two computers (0-based index).
- The last line contains two integers representing the initial computer and the target computer.
Output:
- An integer representing the minimum number of connections to reach the target computer or -1 if unreachable.
Example:
Input:
5 4
0 1
0 2
1 3
2 4
0 3
Output:
2
Key concepts
graphsbfsnetworkconnectivity
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