Shortest Path in Undirected Graph with Unit Weights

Given vertexCount vertices numbered 0 through vertexCount - 1 and undirected unit-weight edges, return shortest distances from vertex 0 in vertex order.…

Given vertexCount vertices numbered 0 through vertexCount - 1 and undirected unit-weight edges, return shortest distances from vertex 0 in vertex order. Return -1 for unreachable vertices. Each edge is [left, right]. Self-loops, repeated edges and disconnected graphs are allowed.

- 1 <= vertexCount <= 10000 - 1 <= edges.length <= 10000 - Endpoints are integers from 0 through vertexCount - 1 - Each undirected edge has unit weight - Source is always vertex 0 - Visualizer inputs are limited to 24 vertices and 48 edge occurrences; Run Code and Submit support the full coding bounds.

Example 1: input vertexCount = 4, edges = [[3,2],[2,1],[1,0]]; output [0,1,2,3]. The reversed edge rows still describe an undirected chain from source 0.

Example 2: input vertexCount = 4, edges = [[1,2],[2,3]]; output [0,-1,-1,-1]. The component containing vertices 1, 2 and 3 cannot be reached from source 0.

Category: Graphs. Topic: graph. Difficulty: Medium. Patterns: graph-traversal, bfs.