← 返回 bytedance 的题目列表Shortest Path in Graph
类型:online_judge
Question
Given an undirected graph, compute the shortest path length between any two nodes.
Input
graph: A 2D array representing edges of the graph, where each element is a connection between two nodes like [node1, node2].
node1, node2: The two nodes for which to compute the shortest path.
Output
Return the shortest path length from node1 to node2.
Example
Input:
graph = [[1, 2], [2, 3], [3, 4]],
node1 = 1,
node2 = 4
Output:
3
Constraints
Number of nodes does not exceed 1000, number of edges does not exceed 2000.
Example
Input
3
[[1, 2], [2, 3], [1, 3]]
1
3