[Math] Proof that this algorithm calculates the “degrees of separation” in a graph

algorithmsgraph theory

I was doing a computer programming contest today and came across essentially the following problem:
"Given a graph G find the degree of separation of the graph"
Where the degree of separation is defined as the longest distance between 2 points in the graph. For example, below, the degree of separation of the graph is 3, the longest path is 6 to 1 or 6 to 2.

My team had the idea to solve the problem by choosing a point on the graph and finding what the farthest point from it is, then finding the farthest point from that point which will then give the degree of separation. Intuitively this feels correct but why does this work? What is the proof that this 2 step algorithm works every time?

Note: If on the first step the distance to farthest points is equal do the second step to both and take the maximum.

enter image description here

Best Answer

This algorithm does always not work. Here is a counter-example:

counterexample

If you start from $F$ and go to the farthest vertex, you end up at $A$. The farthest vertex from $A$ is any of $\{E,F,G\}$, all of which are at distance $3$ from $A$.

However, the actual longest distance between two vertices in this graph is $4$: $E$ and $G$ are $4$ steps apart.

Related Question