in Algorithms
925 views
0 votes
0 votes

The diameter of a tree $T= (V, E)$ is defined as $max_{u,v\ \epsilon\ V}\ \delta(u,v)$, that is, the largest of all shortest-path distances in the tree. Give an efficient algorithm to compute the diameter of a tree, and analyze the running time of your algorithm.

in Algorithms

1 comment

U can do bfs and compute the distance of the leaf which is at a maximum distance from the root node

Now consider that leaf node as root node and do breadth first search to find a leaf node which is at a maximum distance from the this leaf node the distance will be the answer

so time complexity of this is o(v+e) and space complexity is also o(v) for maintaining distance of each leaf node
0
0

1 Answer

0 votes
0 votes
we can use Floyd-Warshall Algortihm to calculate the shortest distance between every pair of vertices and then find the maximum.

so, the time complexity would be O(n^3)

Space Complexity is O(n^2).
edited by

Related questions