Back to problems

Shortest Path in Unweighted Graph

Algorithm · Pinterest · Easy

You are given an undirected graph with n nodes whose labels run from 0 through n - 1 and a list of edges edges, where each entry [u, v] represents a bidirectional edge connecting node u and node v. Compute the length of the shortest path from the source node to the target node. If the target is not reachable from the source, return -1. Constraints 1 ≤ n ≤ 10⁵ Total number of undirected edges ≤ 10⁵ Examples Example 1 Explanation: The shortest distance from the source node to…

Checking your access…