Algorithm to find any two nodes with distance of at least half the (undirected) graph's diameter

506 views Asked by At

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!

2

There are 2 answers

1
tucuxi On BEST ANSWER

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 the shortest distance to v from the root - which I will refer to as d(root, v).

Now, take the leaf z that has the largest number of hops from your root. Congratulations, d(root, z) >= D/2, because

Lemma: for any node x in a connected graph of diameter D, there must exist a node y that is at least D/2 far away.

Proof: If this were not so, then there would be some node x so that, for all y, d(x,y) = D/2 - k <= D/2 (with k>=1). But then, by passing through x, we could find paths from any node to all others in at most 2 * (D/2 - k) = D - 2k - and therefore, the graph's diameter could not be D, but D - 2k.

2
libik On

Thats actually the tricky one, but I think I got it. Interesting thing is that your partially wrong solution put me on the right way.

Lets just copy here few definitions:

  • Distance between two vertices in a graph is the number of edges in a shortest path
  • The eccentricity of a vertex v is the greatest distance between v and any other vertex
  • The diameter d of a graph is the maximum eccentricity of any vertex in the graph. That is, d is the greatest distance between any pair of vertices

The real issue would be to actually find the diameter, its not an easy task. To find diameter you cannot just choose any node and run BFS - in such case you just find node that has highest distance from that node (the eccentricity), but it is not diameter. To actually find diameter you would have to run BFS (=find eccentricity) from every single node and the highest distance you got is diameter (there are some better alghoritms, but as I said - its not simple task).

However! You dont have to know the diameter at all. If you actually run BFS from random node and you find the node with highest distance (eccentricity) - thats the solution to your alghorithm. x would be your starting node and y would be the node with highest distance.

Why? If you imagine super simple graph like this

graph01

You can see that the diameter is between nodes 1 and nodes 4. So no matter from which point you run the BFS, that point has to be either in a middle (which means it will have half the diameter) or not in the middle and then the node with highest distance must have even higher distance than half the diameter.

Even more complex graphs do not change the fact graph02

If you choose 6 or 7, its not exactly in diameter path (because the highest distance is between 1-2-3-4-5), but it means that you get even higher distance, which is fine for your task.


Result: Run the BFS from random node, when it ends, take node with highest distance from the starting node (=find eccentricity and remember the furthest node) and the starting and "ending" nodes are (x,y)