I needed an algorithm to find the diameter of a tree, and found the following: Pick an arbitrary vertex y and run breadth-first search to find the farthest vertex from y, call it x, then run BFS on x to find the farthest vertex from x, call it z, this distance d(x,z) is the diameter.
However, I do not understand why x must necessarily be in a vertex-pair that has the largest diameter. I have found Proof of correctness: Algorithm for diameter of a tree in graph theory where proof by contradiction is suggested, assuming some d(u,v) which is bigger than d(x,z). A case distinction can then be made between these cases:
- The path from u to v intersects the path from the root to x
- The path from u to v does not intersect the path from the root to x
Knowing that there is some vertex h on the path from u to v which is the 'highest' (closest to the root), it should be possible to prove for both these cases that d(u,v) cannot be bigger than d(x,z).
But I don't understand what the 'root' is (is this y?) and despite the hints I cannot see why it is true. I don't need a formal proof, but I would like to understand why the algorithm is correct.