Search⌘ K

Solution: Find the Shortest Path between Two Vertices

Understand how to use Breadth-First Search to find the shortest path between two nodes in a directed graph. Explore queue-based traversal with distance tracking, and learn to handle cases with no available path. This lesson prepares you to implement efficient graph algorithms in C++.

We'll cover the following...

Statement

Given a directed graph of n nodes and two vertices, src and dest, return the length of the shortest path between src and dest. The shortest path will contain the minimum number of edges.

If no path exists between src and dest, return -1.

Constraints:

  • 00 \leq n 100\leq 100
  • 00 \leq Node.data \leq n
  • Node.data is unique for each node.

Solution

The solution ...