Dsa
Graphs Bfs
Easy
Network Connection Path
You are working with a network of computers, where each computer is represented as a node in a graph. You are given a list of connections (edges) between computers and want to find the shortest path of connections between a source computer and a target computer, both represented as integers. If no path exists, return -1.
Input:
The first line contains two integers n and e (1 <= n <= 1000, 0 <= e <= 10000), the number of computers (vertices) and connections (edges).
The next e lines contain two integers u and v (0-indexed), representing a bi-directional connection between computers u and v.
The last line contains two integers source and target (0-indexed).
Output:
An integer representing the length of the shortest path, or -1 if no path exists.
Example:
Input:
5 4
0 1
0 2
1 3
2 3
0 3
3
Output:
2
Key concepts
graphsbfspathfinding
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