Please suggest some algorithm for finding a node in a tree whose distance to the farthest node is the smallest among all nodes

Please suggest some algorithm for finding a node in a tree whose distance to the farthest node is the smallest among all nodes.

This is not a schedule, and it is not weighted.

+5
source share
5 answers

Select an arbitrary node v in the tree T. Start BFS by creating v as the root of T. BFS displays the distances from v to all other nodes of T.

Now select node u, which is further from v. Run BFS again, creating u as the root. At the output of the new distance, find node w, which is further from u.

u w. T. node T.

, . , .

: O (n), n - T.

: (u), node v, .

, , u, , , DFS, .

: retucto ad absurdum. , u --- r - ; node v v --- u, v --- r - v. v --- k. :

a) u --- r v - k node o. v - o - u v - o - r u --- o --- k. o --- r o --- k. u --- o --- r , u --- o --- k . .

b) u --- r v - k . , o1 o2, o1 - o2 . , ), 1 - 2 ( , 1 = 2).

, , .

( , , ).

+10

. , . node ( 2 ) node, .

:

node (s) P . node ( ). node P, , P, node (s). node n P node ( n node P, c) + ( c P), , node (s), .

+3

(http://en.wikipedia.org/wiki/Dijkstra%27s_algorithm) node , node node; , node. Dijkstra'd node, .

Dijkstra O(v^2), v - ; node, O(v^3) . , Dijkstra .

+1

, - , .

, node node, .

+1

: - - , - . "" ( ).

0

All Articles