dsa · easy
Unweighted Shortest Path
GreyOrangeGraphQueueFoundation
Undirected unweighted graph with n nodes labeled 1..n, edge list edges, start src, end dst. Return the fewest edges, or -1 if unreachable.
Arguments
n— number of nodes, labeled1..nedges— the edge listsrc— the source nodedst— the destination node
**Example:** n=4, edges=[[1,2],[2,3],[3,4]], src=1, dst=4 → 3.
Constraints
1 <= n <= 200, 0 <= edges.length <= n*(n-1)/2 Hidden tests include near-max size for this bound; a slower-than-intended solution TLEs.
Examples
Example 1
Input: 4 [[1,2],[2,3],[3,4]] 1 4 Expected: 3
Example 2
Input: 3 [[1,2]] 1 3 Expected: -1