Dsa
Graphs Dfs
Easy
City Distance Tracker
You are given a network of cities represented as an undirected graph. Each city has a unique identifier and is connected by roads. Given a starting city and a destination city, write a program to find the shortest distance (in terms of the number of roads) between the two cities. If the destination is unreachable, print 'Unreachable'.
Input Format:
- The first line contains an integer n (number of cities).
- The second line contains an integer m (number of roads).
- The next m lines contain two integers u and v, which indicates a road between city u and city v.
- The last line contains two integers, start and end, representing the cities for which you want to find the distance.
Output Format:
- Print the shortest distance between the start and end city or 'Unreachable'.
Example:
Input:
5
6
0 1
1 2
2 3
3 4
0 4
2 4
0 4
Output:
2
Key concepts
graphsdfspathfinding
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