Proofs: Tree diameter by double sweep
These are the human proofs of Tree diameter by double sweep, at full length.
The double sweep returns the diameter of a tree with non-negative lengths full
This proof shows: correctness, termination.
Let be a tree on vertices with edge lengths . For two vertices , is the total length of the unique path between them, , and .
Lemma 1 (DISTANCES). DISTANCES terminates, marks every vertex, and returns for each , with links that lead from each back to along the path from to .
Proof. A vertex is pushed only when it is marked, and it is marked only once (line L13 skips marked vertices), so at most pushes and pops happen, and the loop of line L10 ends. Every pushed vertex is popped before the loop ends. If some vertex were not marked, then, since is connected, some edge would join a marked vertex to an unmarked vertex . When was popped, line L12 would have found unmarked and marked it, a contradiction. So all vertices are marked.
Order the vertices by the time they are marked. For , was marked before . So the chain has strictly decreasing mark times, has no repeated vertex, and ends at , the only vertex with . Consecutive vertices of the chain are adjacent. So the chain, read backwards, is a simple path from to , and it is the only one in a tree. By induction on the mark time, is the length of this path, which is .
Projection. Fix a diametral path with ends , so . For each vertex , let be the vertex of with the smallest number of edges to , and .
Lemma 2. For each vertex and each vertex of , the path from to is the path from to followed by the part of from to . So . Moreover, if a vertex lies on the path from to , then .
Proof. The path from to meets only at : an earlier vertex of on would be closer to . So followed by the part of from to has no repeated vertex, and it is a path from to , the unique one. Its length is . For on , the part of from to meets only at its end, and by the first part (applied to ) the path from to each vertex of starts with the path to . The path from to meets first at , so .
Lemma 3 (the farthest vertex is an end). Let be any vertex and a vertex with . Then .
Proof. Let and . Since is farthest from , Lemma 2 gives
Case 1: . The path from to and the path from to have no common vertex: a common vertex would have and by Lemma 2. So the path from to , then the part of from to , then the path from to has no repeated vertex, and it is the path from to . Hence . The vertex lies on on one side of . Say that it lies between and (the other case is the same with and swapped). Then , and the second inequality of (1) gives . By Lemma 2, .
Case 2: . The walk from to and then from to has length . In a tree, the path from to is obtained from this walk by deleting closed sub-walks, which have non-negative length because . So . With the first inequality of (1), . By Lemma 2, .
In both cases , and by the definition of .
Theorem. DOUBLE-SWEEP terminates and returns and a path of length .
Proof. By Lemma 1, line L1 computes , so line L2 picks a vertex with . By Lemma 3 with , . By Lemma 1, line L3 computes , so line L4 picks with . Lines L5 to L7 follow the parent links from , which by Lemma 1 reach after at most steps along the path from to , so the loop ends. Line L8 returns and this path, which has length .
Where is used. Only in Case 2 of Lemma 3. On the path with and , the vertex is farthest from , both are projected to the same vertex of the diametral path , and .
Corollary (eccentricities). For and each vertex , , where are the ends found by the algorithm.
Proof. Take to be the path from to , which is diametral, and define and for it. First, for each vertex : if , then by Lemma 2 , which is impossible, and the same holds with and swapped. Now let be any vertex and . The walk from to , along to , then to has length , and since the path from to is not longer. If lies between and , then gives . Otherwise lies between and , and in the same way . So , and the other inequality holds by the definition of .