Skip to content
CampusEduX

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.

Related Problems

  • Detect Cycle in Undirected Graph
    Medium · java, python, c, cpp, javascript
    Solve Problem
  • Detect Cycle in Directed Graph
    Medium · java, python, c, cpp, javascript
    Solve Problem
  • Topological Sort
    Medium · java, python, c, cpp, javascript
    Solve Problem

Input (stdin)

Output

Run your code to see output here...