43

This link provides an algorithm for finding the diameter of an undirected tree using BFS/DFS. Summarizing:

Run BFS on any node s in the graph, remembering the node u discovered last. Run BFS from u remembering the node v discovered last. d(u,v) is the diameter of the tree.

Why does it work ?

Page 2 of this provides a reasoning, but it is confusing. I am quoting the initial portion of the proof:

Run BFS on any node s in the graph, remembering the node u discovered last. Run BFS from u remembering the node v discovered last. d(u,v) is the diameter of the tree.

Correctness: Let a and b be any two nodes such that d(a,b) is the diameter of the tree. There is a unique path from a to b. Let t be the first node on that path discovered by BFS. If the paths $p_1$ from s to u and $p_2$ from a to b do not share edges, then the path from t to u includes s. So

$d(t,u) \ge d(s,u)$

$d(t,u) \ge d(s,a)$

....(more inequalities follow ..)

The inequalities do not make sense to me.

curryage
  • 541
  • 2
  • 7
  • 8

9 Answers9

14

All parts of proving the claim hinge on 2 crucial properties of trees with undirected edges:

  • 1-connectedness (ie. between any 2 nodes in a tree there is exactly one path)
  • any node can serve as the root of the tree.

Choose an arbitrary tree node $s$. Assume $u, v \in V(G)$ are nodes with $d(u,v) = diam(G)$. Assume further that the algorithm finds a node $x$ starting at $s$ first, some node $y$ starting at $x$ next. wlog $d(s,u) \geq d(s,v)$. note that $d(s,x) \geq d(s,y)$ must hold, unless the algorithm's first stage wouldn't end up at $x$. We will see that $d(x,y) = d(u,v)$.

The most general configuration of all nodes involved can be seen in the following pseudo-graphics ( possibly $s = z_{uv}$ or $s = z_{xy}$ or both ):

(u)                                            (x)
  \                                            /
   \                                          /
    \                                        /
     ( z_uv )---------( s )----------( z_xy )
    /                                        \
   /                                          \
  /                                            \
(v)                                            (y)

we know that:

  1. $d(z_{uv},y) \leq d(z_{uv},v)$. otherwise $d(u,v) < diam(G)$ contradicting the assumption.
  2. $d(z_{uv},x) \leq d(z_{uv},u)$. otherwise $d(u,v) < diam(G)$ contradicting the assumption.
  3. $d(s,z_{xy}) + d(z_{xy},x) \geq d(s,z_{uv}) + d(z_{uv},u)$, otherwise stage 1 of the algorithm wouldn't have stopped at $x$.
  4. $d(z_{xy},y) \geq d(v,z_{uv}) + d(z_{uv},z_{xy})$, otherwise stage 2 of the algorithm wouldn't have stopped at $y$.

1) and 2) imply $\, \\ d(u,v) = d(z_{uv},v) + d(z_{uv},u) \\ \qquad\geq d(z_{uv},x) + d(z_{uv},y) = d(x,y) + 2\, d(z_{uv}, z_{xy}) \\ \qquad\qquad\geq d(x,y)$.

3) and 4) imply $\, \\ d(z_{xy},y) + d(s,z_{xy}) + d(z_{xy},x) \\ \qquad\geq d(s,z_{uv}) + d(z_{uv},u) + d(v,z_{uv}) + d(z_{uv},z_{xy}) \qquad\qquad\qquad\qquad \\ \, $ equivalent to $\, \\ d(x,y) = d(z_{xy},y) + d(z_{xy},x) \\ \qquad\geq 2*\,d(s,z_{uv}) + d(v,z_{uv}) + d(u,z_{uv}) \\ \qquad\qquad\geq d(u,v)$.

therefore $d(u,v) = d(x,y)$.

analogue proofs hold for the alternative configurations

                 (u)                          (x)
                   \                          /
                    \                        /
                     \                      /
     ( s )---------( z_uv )----------( z_xy )
                     /                      \
                    /                        \
                   /                          \
                 (v)                          (y)

and

                          (x)        (u)  
                          /            \  
                         /              \ 
                        /                \
     ( s )---------( z_xy )----------( z_uv )
                        \                /          
                         \              /           
                          \            /            
                          (y)        (v)            

these are all possible configurations. in particular, $x \not\in path(s,u), x \not\in path(s,v)$ due to the result of stage 1 of the algorithm and $y \not\in path(x,u), y \not\in path(x,v)$ due to stage 2.

collapsar
  • 932
  • 6
  • 12
14

The intuition behind is very easy to understand. Suppose I have to find longest path that exists between any two nodes in the given tree.

After drawing some diagrams we can observe that the longest path will always occur between two leaf nodes( nodes having only one edge linked). This can also be proved by contradiction that if longest path is between two nodes and either or both of two nodes is not a leaf node then we can extend the path to get a longer path.

So one way is to first check what nodes are leaf nodes, then start BFS from one of the leaf node to get the node farthest from it.

Instead of first finding which nodes are leaf nodes , we start BFS from a random node and then see which node is farthest from it. Let the node farthest be x. It is clear that x is a leaf node. Now if we start BFS from x and check farthest node from it, we will get our answer.

But what is the guarantee that x will be a end point of a maximum path?

Let's see by an example :-

       1   
    / /\ \
   6 2  4 8
         \ \
          5 9
           \
            7

Suppose I started my BFS from 6. The node at maximum distance from 6 is node 7. Using BFS we can get this node. Now we star BFS from node 7 to get node 9 at maximum distance. Path from node 7 to node 9 is clearly the longest path.

What if BFS that started from node 6 gave 2 as the node at maximum distance. Then when we will BFS from 2 we will get 7 as node at maximum distance and longest path will be then 2->1->4->5->7 with length 4. But the actual longest path length is 5. This cannot happen because BFS from node 6 will never give node 2 as node at maximum distance.

Hope that helps.

MayankPratap
  • 315
  • 3
  • 4
7

Update 3 and corrected answer

There's an error in the linked solution set (see update 2 below), but it can be easily corrected with @Yuval Filmus's suggestion in the question's comment, which further allows us to rule out one of the possibilities mentioned in the solution set.

This proof hinges on two facts: one, that there is exactly one path between any two nodes in a binary tree; and two, $d(x,y)=d(y,x)$.

Let $s$ be the root of the first BFS and let $u$ be the final vertex discovered (which means that $u$ must be the farthest point from $s$). Also, let $d(a,b)$ be a diameter. Clearly, $a$, $b$, and $u$ must all be leaves, since otherwise $d(a,b)$ wouldn't be a diameter, and $u$ wouldn't be discovered last (if any of the nodes had children, they would no longer be endpoints of paths of "maximal length", as you could just extend the path by exploring their children). Let $t$ be the lowest common ancestor of $a$ and $b$.

We claim that $t$ must be an ancestor of $u$. Suppose that it were not, then let $r$ be the lowest common ancestor of $u$ and $t$.

$d(u,s)=d(u,r)+d(r,s)$, so we have $d(u,s) \geq d(u,r)$.

Since $u$ was discovered last, $d(u,s) \geq d(a,s)$. Since $d(a,s)=d(a,r)+d(r,s)$, and $d(u,s)=d(u,r)+d(r,s)$, therefore, $d(u,r) \geq d(a,r)$.

Since $r$ is an ancestor of $t$, and $r \neq t$ (since $t$ is not an ancestor of $u$, yet $r$ is), $d(t,r) \geq 1$. Since $d(a,r)=d(a,t)+d(t,r)$, $d(a,r) > d(a,t)$.

Thus, since $d(u,r) > d(a,t)$, $d(u,r) + d(r,t) + d(t,b) > d(a,t) + d(t,b)$ $\to d(u,b) > d(a,b)$, contradicting the fact that $d(a,b)$ is a diameter (since diameters are supposed to be paths of maximal length).

Hence, $t$ is an ancestor of $u$.

Since $u$ was discovered last, $d(s,u) \geq d(s,a)$. Since $d(s,u)=d(s,t)+d(t,u)$, and $d(s,a)=d(s,t)+d(t,a)$, $d(t,u) \geq d(t,a)$. Since $d(t,u) \geq d(t,a)$, $d(b,t)+d(t,u) \geq d(b,t)+d(t,a) \to d(b,u) \geq d(b,a)$.

Since $d(a,b)$ is a diameter, $d(b,a) \geq d(b,u)$.

Therefore, since $d(b,u) \geq d(b,a)$ and $d(b,u) \leq d(b,a)$, $d(u,b) = d(a,b)$.

Therefore, $u$ is the endpoint of some diameter, and thus the second BFS works (the longest path from an endpoint of a diameter must be a diameter).

Update 2

Note that the linked solution set contains an error:

If the paths $p_1$ from $s$ to $u$ and $p_2$ from $a$ to $b$ do not share edges, then the path from $t$ to $u$ includes $s$.

Consider the following counterexample: suppose $s$ is the root of the first BFS tree, and $t$ is the lowest common ancestor of leaves $a$, $u$, and $b$. No edges are shared; only the vertex $t$ is shared. Let $d(a,t) = d(b,t) = d(u,t) = 2$, and let $d(s,t)=1$. Finally, suppose $u$ is discovered last in the BFS. Then $d(a,b)$ is a diameter, $t$ is the first node discovered on that path, and the path from $t$ to $u$ does not include $s$.

To fix this, it's probably necessary to change "do not share edges" to "do not share vertices, as suggested by @Yuval Filmus.

Update

As @j_random_hacker points out in the comments, Lemma 1 below is not sufficient to show that $u$ is an endpoint of some diameter. Hence, the below proof is incomplete.

Original incorrect answer

Suppose we have $d(a, b)$ being a diameter, but we don't know which vertices $a$ and $b$ are, so we start a BFS from some vertex $s$.

If $s = a$, then the first BFS would yield either $b$ or some node $b'$ equally distant from $a$, and the second would either go back to $a$ or to some equally distant node $a'$, and the scheme obviously works. Similarly, if $s = b$, then two BFS would work for the same reason.

Otherwise, we have $s \neq a, b$.

Lemma 0: Both $a$ and $b$ are leaf nodes in the tree rooted at $s$.

Proof 0: If they weren't leaf nodes, we could increase $d(a,b)$ by extending the endpoints to leaf nodes, contradicting $d(a, b)$ being a diameter.

Lemma 1: At least one of $d(s,a)$ and $d(s, b)$ is the largest possible value of $d(s,u)$ for all $u$.

Proof 1: Suppose we have a $u$ that violates the lemma. $u$ cannot be a descendent of $a$ or $b$ since they are leaves from Lemma 0, and $u$ cannot be an ancestor, since that would make it closer to $s$, and it would no longer be able to violate the lemma. Let $t$ be the lowest common ancestor of $a$ and $b$. If $t = s$, then both $d(a, u)$ and $d(b, u)$ would be greater than $d(a, b)$, a contradiction. Otherwise, $t \neq s$ and either $u$ is a descendant of $t$, or not.

If $u$ is a descendant of $t$, then since $d(s,u)$ is greater than both $d(s, a)$ and $d(s, b)$, we have $d(t,u)$ is greater than both $d(t, a)$ and $d(t, b)$ and thus $d(a, u)$ and $d(b,u)$ are both greater than $d(a,b)$, a contradiction since $d(a,b)$ is a diameter.

If $u$ is not a descendant of $t$, then let $w$ be the unique ancestor of $u$ such that $d(s,t) = d(s,w)$. We know $w$ exists because $d(s,t) < d(s,u)$. Then, we have $d(t, b) < d(w,u) < d(t,w) + d(w,u) = d(t,u)$ and thus $d(a, u) = d(a, t) + d(t, u) > d(a, t) + d(t, b) = d(a, b)$, again contradicting $d(a, b)$ being a diameter.

Main result: Starting from any root $s$ and performing a BFS will result in some $a$ being discovered last. Using that $a$ as the root of a second $BFS$ will result in $b$ being discovered last, with $d(a,b)$ guaranteed to be a diameter.

proof: From lemma 1, the first BFS will find an $a$ that is furthest from $s$, which is guaranteed to be one of the endpoints of a diameter. The second BFS will find that diameter, since in a tree there's exactly one simple path between any two nodes.

xdavidliu
  • 888
  • 7
  • 17
3

It works because-

  1. For any randomly chosen vertex $u$, the farthest vertex from it lies on one end of the diameter.

  2. If vertex $a$ is one end of the diameter, the farthest vertex from it is the other end.

The first BFS takes you to one end of the diameter and the second BFS from that end takes you to the other end.

Proof

#2 is easy to see why it's true. We will see why #1 is true by contradiction. Let's suppose, the path from vertex $a$ to $b$ is the diameter of the tree and $v$, which is not on the diameter, is the farthest vertex from $u$. Also, let's assume paths $(a,b)$ and $(u,v)$ cross over at $t$ ($t$ can more generally be a path with multiple vertices). Because we don't have any cycles, all paths between vertices $a$, $b$, $u$ and $v$ pass through $t$.

enter image description here

  1. By our assumptions $d(a,b)$ is the diameter and $d(u,v)$ is larger than both $d(u,a)$ and $d(u,b)$.

  2. Path $(u,t)$ is the common piece in all paths originating from $u$ and hence we can deduce that $d(v, t) > d(a, t)$ ($v$ is farther away from $t$ as compared to $a$).

  3. This implies that $v$ is also farther away from $b$ as compared to $a$ and $d(v,b) > d(a,b)$. (add $d(t,b)$ to both sides in previous step)

  4. This contradicts our initial assumption that $(a,b)$ is a diameter because we have found a path $(v,b)$ which is longer. Symmetrically, we can also see why $d(v,a) > d(a,b)$.

  5. Hence, the farthest vertex from $u$ must either be $a$ or $b$ if $(a,b)$ is indeed the diameter.

Shubham
  • 39
  • 2
2

The claim is equivalent to claiming that every deepest leaf in a tree rooted at $s$ is an endpoint to a diameter.

Suppose $p$ is an arbitrary path with end points $(a,b)$, and that $d$ is some leaf that is one of the deepest. Let $x$ be the deepest common ancestor of $a$ and $b$.

Here is how to construct a path ending in $d$ that it at least as long as path $p$, assuming WLOG $\text{depth}(a) \ge \text{depth}(b)$:

   x 
  / \       (b,d) is at least as long
 / \ \
a   d b

x / \ (a,d) is at least as long / /
a d b

/\ x \ (a,d) is at least as long / \
a b d

Therefore a diameter can be constructed for every vertex $d$ from any other diameter.

(Edited: the previous proof only established that every diameter ended in a deepest vertex, not that every deepest vertex was part of a diameter, embarrassing mistake.)

DanielV
  • 516
  • 3
  • 12
0

One key thing to know is that a tree is always planar, which means it can be laid out on a plane, so often ordinary 2-dimensional thinking works. In this case, the algorithm says starting anywhere, go as far away as you can. The distance from that point to as far as you can get away from that point is the longest distance in the tree, and therefore the diameter.

This method would also work to find the diameter of a real, physical island if we defined that as the diameter of the smallest circle that would fully enclose the island.

Old Pro
  • 131
  • 4
0

@op, the way the cases are defined in the PDF may be a bit off.

I think that the two cases should be:

  1. $p_1$ does not intersect with $p_2$, i.e. there are no common vertices between paths $p_1$ and $p_2$. In this case, define $t$ as the first node on $p_2$ discovered by the first BFS starting at $s$.

  2. $p_1$ and $p_2$ have at least one common vertex. In this case, define $t$ to be the first node on $p_2$ discovered by the first BFS that is also on $p_1$.

The rest of the proof in the PDF should follow.

With this definition, the figure shown by OP falls into Case 2.

0

By the definition of BFS, the distance (from the starting node) of each node explored is either equal to the distance of the previous node explored or greater by 1. Thus, the last node explored by BFS will be among those farthest from the starting node.

Thus, the algorithm of using BFS twice amounts to "Pick an arbitrary node $x$. Find the node $a$ farthest from $x$ (last node found by BFS starting from $x$). Find the node $b$ farthest from $a$ (last node found by BFS starting from $a$).", which thus finds two nodes of maximum distance from eachother.

Extrarius
  • 771
  • 4
  • 9
-1

First run a DFS from a random node then the diameter of a tree is the path between the deepest leaves of a node in its DFS subtree: enter image description here

Yuval Filmus
  • 280,205
  • 27
  • 317
  • 514