Algorithm Search⌘K

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 TT be a tree on n≥1n \ge 1 vertices with edge lengths w≥0w \ge 0. For two vertices u,vu, v, d(u,v)d(u, v) is the total length of the unique path between them, ecc(v)=max⁡ud(v,u)\mathrm{ecc}(v) = \max_u d(v, u), and D=max⁡vecc(v)D = \max_v \mathrm{ecc}(v).

Lemma 1 (DISTANCES). DISTANCES(T,s)(T, s) terminates, marks every vertex, and returns d[v]=d(s,v)d[v] = d(s, v) for each vv, with parpar links that lead from each v≠sv \ne s back to ss along the path from ss to vv.

Proof. A vertex is pushed only when it is marked, and it is marked only once (line L13 skips marked vertices), so at most nn pushes and nn 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 TT is connected, some edge would join a marked vertex xx to an unmarked vertex yy. When xx was popped, line L12 would have found yy unmarked and marked it, a contradiction. So all vertices are marked.

Order the vertices by the time they are marked. For v≠sv \ne s, par[v]par[v] was marked before vv. So the chain v,par[v],par[par[v]],…v, par[v], par[par[v]], \ldots has strictly decreasing mark times, has no repeated vertex, and ends at ss, the only vertex with par=−1par = -1. Consecutive vertices of the chain are adjacent. So the chain, read backwards, is a simple path from ss to vv, and it is the only one in a tree. By induction on the mark time, d[v]=d[par[v]]+w(par[v],v)d[v] = d[par[v]] + w(par[v], v) is the length of this path, which is d(s,v)d(s, v). □\square

Projection. Fix a diametral path PP with ends x,yx, y, so d(x,y)=Dd(x, y) = D. For each vertex vv, let π(v)\pi(v) be the vertex of PP with the smallest number of edges to vv, and h(v)=d(v,π(v))h(v) = d(v, \pi(v)).

Lemma 2. For each vertex vv and each vertex zz of PP, the path from vv to zz is the path from vv to π(v)\pi(v) followed by the part of PP from π(v)\pi(v) to zz. So d(v,z)=h(v)+d(π(v),z)d(v, z) = h(v) + d(\pi(v), z). Moreover, if a vertex uu lies on the path from vv to π(v)\pi(v), then π(u)=π(v)\pi(u) = \pi(v).

Proof. The path QQ from vv to π(v)\pi(v) meets PP only at π(v)\pi(v): an earlier vertex of QQ on PP would be closer to vv. So QQ followed by the part of PP from π(v)\pi(v) to zz has no repeated vertex, and it is a path from vv to zz, the unique one. Its length is h(v)+d(π(v),z)h(v) + d(\pi(v), z). For uu on QQ, the part of QQ from uu to π(v)\pi(v) meets PP only at its end, and by the first part (applied to uu) the path from uu to each vertex of PP starts with the path to π(u)\pi(u). The path from uu to π(v)\pi(v) meets PP first at π(v)\pi(v), so π(u)=π(v)\pi(u) = \pi(v). □\square

Lemma 3 (the farthest vertex is an end). Let ss be any vertex and aa a vertex with d(s,a)=ecc(s)d(s, a) = \mathrm{ecc}(s). Then ecc(a)=D\mathrm{ecc}(a) = D.

Proof. Let t=π(s)t = \pi(s) and ta=π(a)t_a = \pi(a). Since aa is farthest from ss, Lemma 2 gives

d(s,a)≥d(s,x)=h(s)+d(t,x),d(s,a)≥d(s,y)=h(s)+d(t,y).(1)d(s, a) \ge d(s, x) = h(s) + d(t, x), \qquad d(s, a) \ge d(s, y) = h(s) + d(t, y). \tag{1}

Case 1: ta≠tt_a \ne t. The path from ss to tt and the path from tat_a to aa have no common vertex: a common vertex uu would have π(u)=t\pi(u) = t and π(u)=ta\pi(u) = t_a by Lemma 2. So the path from ss to tt, then the part of PP from tt to tat_a, then the path from tat_a to aa has no repeated vertex, and it is the path from ss to aa. Hence d(s,a)=h(s)+d(t,ta)+h(a)d(s, a) = h(s) + d(t, t_a) + h(a). The vertex tat_a lies on PP on one side of tt. Say that it lies between tt and yy (the other case is the same with xx and yy swapped). Then d(t,y)=d(t,ta)+d(ta,y)d(t, y) = d(t, t_a) + d(t_a, y), and the second inequality of (1) gives h(a)≥d(ta,y)h(a) \ge d(t_a, y). By Lemma 2, d(a,x)=h(a)+d(ta,x)≥d(ta,y)+d(ta,x)=Dd(a, x) = h(a) + d(t_a, x) \ge d(t_a, y) + d(t_a, x) = D.

Case 2: ta=tt_a = t. The walk from ss to tt and then from tt to aa has length h(s)+h(a)h(s) + h(a). In a tree, the path from ss to aa is obtained from this walk by deleting closed sub-walks, which have non-negative length because w≥0w \ge 0. So d(s,a)≤h(s)+h(a)d(s, a) \le h(s) + h(a). With the first inequality of (1), h(a)≥d(t,x)h(a) \ge d(t, x). By Lemma 2, d(a,y)=h(a)+d(t,y)≥d(t,x)+d(t,y)=Dd(a, y) = h(a) + d(t, y) \ge d(t, x) + d(t, y) = D.

In both cases ecc(a)≥D\mathrm{ecc}(a) \ge D, and ecc(a)≤D\mathrm{ecc}(a) \le D by the definition of DD. □\square

Theorem. DOUBLE-SWEEP(T)(T) terminates and returns DD and a path of length DD.

Proof. By Lemma 1, line L1 computes d(0,⋅)d(0, \cdot), so line L2 picks a vertex aa with d(0,a)=ecc(0)d(0, a) = \mathrm{ecc}(0). By Lemma 3 with s=0s = 0, ecc(a)=D\mathrm{ecc}(a) = D. By Lemma 1, line L3 computes d(a,⋅)d(a, \cdot), so line L4 picks bb with d(a,b)=ecc(a)=Dd(a, b) = \mathrm{ecc}(a) = D. Lines L5 to L7 follow the parent links from bb, which by Lemma 1 reach aa after at most n−1n - 1 steps along the path from aa to bb, so the loop ends. Line L8 returns DD and this path, which has length d(a,b)=Dd(a, b) = D. ■\blacksquare

Where w≥0w \ge 0 is used. Only in Case 2 of Lemma 3. On the path 0,1,20, 1, 2 with w(01)=−2w(01) = -2 and w(12)=1w(12) = 1, the vertex a=0a = 0 is farthest from s=0s = 0, both are projected to the same vertex of the diametral path 1,21, 2, and ecc(0)=0<D=1\mathrm{ecc}(0) = 0 < D = 1.

Corollary (eccentricities). For w≥0w \ge 0 and each vertex vv, ecc(v)=max⁡(d(v,a),d(v,b))\mathrm{ecc}(v) = \max(d(v, a), d(v, b)), where a,ba, b are the ends found by the algorithm.

Proof. Take PP to be the path from aa to bb, which is diametral, and define π\pi and hh for it. First, h(z)≤min⁡(d(π(z),a),d(π(z),b))h(z) \le \min(d(\pi(z), a), d(\pi(z), b)) for each vertex zz: if h(z)>d(π(z),a)h(z) > d(\pi(z), a), then by Lemma 2 d(z,b)=h(z)+d(π(z),b)>d(a,π(z))+d(π(z),b)=Dd(z, b) = h(z) + d(\pi(z), b) > d(a, \pi(z)) + d(\pi(z), b) = D, which is impossible, and the same holds with aa and bb swapped. Now let zz be any vertex and t=π(v)t = \pi(v). The walk from vv to tt, along PP to π(z)\pi(z), then to zz has length h(v)+d(t,π(z))+h(z)h(v) + d(t, \pi(z)) + h(z), and since w≥0w \ge 0 the path from vv to zz is not longer. If π(z)\pi(z) lies between tt and aa, then h(z)≤d(π(z),a)h(z) \le d(\pi(z), a) gives d(v,z)≤h(v)+d(t,a)=d(v,a)d(v, z) \le h(v) + d(t, a) = d(v, a). Otherwise π(z)\pi(z) lies between tt and bb, and in the same way d(v,z)≤d(v,b)d(v, z) \le d(v, b). So ecc(v)≤max⁡(d(v,a),d(v,b))\mathrm{ecc}(v) \le \max(d(v, a), d(v, b)), and the other inequality holds by the definition of ecc\mathrm{ecc}. □\square

Back to Tree diameter by double sweep