Shortest Path in Unweighted Graph
Mediumjavapythonccppjavascript
Given an unweighted undirected graph, find the minimum number of edges on a path from a source vertex S to a destination vertex D.
Input Format
The first line contains two integers V and E. Each of the next E lines contains an undirected edge u v. The last line contains two integers S and D.
Output Format
Print the shortest distance in edges, or -1 if D is not reachable from S.
Example 1
Input
5 5 0 1 0 2 1 3 2 3 3 4 0 4
Output
3
Explanation: One shortest route is 0 -> 1 -> 3 -> 4, using three edges.
- 1 <= V <= 100000
- 0 <= E <= 200000
- 0 <= u, v, S, D < V