← 返回 amazon 的题目列表Find the Diameter of a Graph
类型:online_judge
amazon
Given an undirected graph, the diameter of the graph is defined as the length of the longest shortest path. Write a function to calculate the diameter of the graph. The input provides a list of edges, with each edge represented as a pair of nodes. Ensure that the function can handle large-scale data and return results in a reasonable time.
Input
A list edges where each element is a pair tuple representing an undirected edge in the graph.
Output
An integer representing the diameter of the graph.
Example
Example 1:
Input: [(1, 2), (2, 3), (3, 4)] Output: 3
Example 2:
Input: [(1, 2), (2, 3), (3, 4), (4, 1)] Output: 2
Constraints
The graph may consist of multiple disconnected components.
The number of edges in the graph does not exceed 10^5.
Node identifiers are positive integers and are unique.
Example
Input
[(1, 2), (2, 3), (3, 4)]