Proof of correctness: Algorithm for diameter of a tree in graph theory

前端 未结 4 673
南笙
南笙 2020-12-12 17:13

In order to find the diameter of a tree I can take any node from the tree, perform BFS to find a node which is farthest away from it and then perform BFS on that node. The g

4条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-12 18:00

    1:procedureTreeDiameter(T)

    2:pick an arbitrary vertex v where v∈V

    3:u = BFS ( T, v )

    4:t = BFS ( T, u )

    5:return distance ( u, t )

    Result: Complexity = O(|V|)

提交回复
热议问题