[Math] Finding the longest path in a tree

algorithmsdynamic programminggraph theory

Give a linear time algorithm that, given an undirected tree T = (V,E) with edges weights
w, returns the weight of the heaviest path in T.

This is basically asking to do the exact opposite of what the Floyd-Warshall algorithm does. Any suggestions (or even solution) as to how to approach/solve this question?

Best Answer

You should use Dynamic Programming to solve this problem. Make the tree rooted from arbitrary vertex. Then for each subtree find the maximum weighted path from root of that subtree to other nodes.