Skip to content
C

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.

Input (stdin)

Output

Run your code to see output here...