I have to give an algorithm as follows:
Given an undirected connected graph G, give an algorithm that finds two nodes x,y such that their distance is at least half the diameter of the Graph. Prove any claim.
I'm assuming I have to run a BFS from any arbitrary node and find its furthest node to find the diameter. Then find two of the explored nodes whose distance is bigger than half the diameter. But I doubt this is the optimal and asked for solution. Is there any other way that when running the BFS to find the diameter, to simultaneously find these two required nodes? So that the complexity remains polynomial. Any guidance or hint would be appreciated!
The diameter (lets call it
D
) of a graph is the largest distance (= minimal number of hops) between any of its nodes.Choose any node and perform BFS, while retaining, for each node, the number of hops from your initial node. This takes O(V), since you will visit all nodes exactly once. Note that this
number of hops
is also theshortest distance to v from the root
- which I will refer to asd(root, v)
.Now, take the leaf
z
that has the largest number of hops from your root. Congratulations,d(root, z) >= D/2
, becauseLemma: for any node
x
in a connected graph of diameterD
, there must exist a nodey
that is at leastD/2
far away.Proof: If this were not so, then there would be some node
x
so that, for ally
,d(x,y) = D/2 - k <= D/2
(withk>=1
). But then, by passing throughx
, we could find paths from any node to all others in at most2 * (D/2 - k) = D - 2k
- and therefore, the graph's diameter could not beD
, butD - 2k
.