Build adjacency list from the edges.
Run BFS from node , track distances, find node with maximum distance.
Run BFS from , track distances, find node with maximum distance.
Return dist[v] as the diameter. Two BFS traversals, each . Total time: . Space: for adjacency list and distance array. This beats the naive approach decisively.