AlgoFrames

Spot the bug: wrong distances

Standard · Find the bug · about 8 min

This function should return the fewest edges from source to target, but it returns a longer distance for some graphs. Click the line with the bug, then fix it.

Example 1

Input:

[{"n":5,"edges":[[0,1],[0,2],[2,3],[3,4],[1,4]],"directed":false},0,4]

Output:

2

The buggy code returns 3 here.

Constraints

  • 1 ≤ n ≤ 200 nodes, numbered 0 to n - 1
  • Undirected or directed graphs; every edge counts as 1

Learn the idea first: Shortest paths in unweighted graphs; path reconstruction, a visual lesson.

More Shortest paths in unweighted graphs; path reconstruction problems