Algorithm Search⌘K

Proofs: Tree center by leaf peeling

These are the human proofs of Tree center by leaf peeling, at full length.

Leaf peeling returns the centre of an unweighted tree full

This proof shows: correctness, termination.

Let TT be a free tree with nn vertices and unit edge lengths. Write dTd_T for the distance, eccT\mathrm{ecc}_T for the eccentricity and Z(T)Z(T) for the centre. A leaf is a vertex of degree 1.

Lemma 1. Let n≥3n \ge 3, and let T′T' be TT with all leaves removed. Then T′T' is a tree with at least one vertex, dT′(u,v)=dT(u,v)d_{T'}(u, v) = d_T(u, v) for all u,vu, v in T′T', and eccT′(v)=eccT(v)−1\mathrm{ecc}_{T'}(v) = \mathrm{ecc}_T(v) - 1 for each vv in T′T'.

Proof. Not empty. Since TT is connected with n≥3n \ge 3 vertices, it has a path with 2 edges, and its middle vertex has degree at least 2, so it is not a leaf.

Distances. Let u,vu, v be in T′T', and let QQ be the path from uu to vv in TT. Each inner vertex of QQ has two neighbours on QQ, so it has degree at least 2 and is not a leaf. The ends uu, vv are in T′T'. So QQ lies in T′T'. Hence T′T' is connected and dT′=dTd_{T'} = d_T on T′T'. It is acyclic as a subgraph of TT, so it is a tree.

Eccentricities. Let vv be in T′T' and e=eccT(v)e = \mathrm{ecc}_T(v). Since n≥2n \ge 2, e≥1e \ge 1. Let zz be a vertex with dT(v,z)=ed_T(v, z) = e. If zz had a neighbour yy that is not on the path from vv to zz, then the path from vv to zz followed by yy would be a path of length e+1e + 1, which is impossible. So the only neighbour of zz is its neighbour z′z' on the path to vv, and zz is a leaf. If z′=vz' = v, then e=1e = 1, and vv is adjacent to every other vertex (each vertex is at distance at most 1); then T′T' is the single vertex vv, and eccT′(v)=0=e−1\mathrm{ecc}_{T'}(v) = 0 = e - 1. Otherwise z′z' is an inner vertex of a path, so z′∈T′z' \in T', and dT′(v,z′)=e−1d_{T'}(v, z') = e - 1. So eccT′(v)≥e−1\mathrm{ecc}_{T'}(v) \ge e - 1. Conversely, let u≠vu \ne v be in T′T'. Then uu is not a leaf, so it has a neighbour yy other than its neighbour on the path to vv, and dT(v,y)=dT(v,u)+1≤ed_T(v, y) = d_T(v, u) + 1 \le e. So dT′(v,u)≤e−1d_{T'}(v, u) \le e - 1, and eccT′(v)≤e−1\mathrm{ecc}_{T'}(v) \le e - 1. □\square

Lemma 2. If n≥3n \ge 3, then Z(T′)=Z(T)Z(T') = Z(T), where T′T' is as in Lemma 1.

Proof. A leaf zz of TT is not in Z(T)Z(T): its neighbour z′z' has d(z′,u)≤d(z,u)−1d(z', u) \le d(z, u) - 1 for each u≠zu \ne z (the path from zz to uu goes through z′z'), and d(z′,z)=1≤ecc(z)−1d(z', z) = 1 \le \mathrm{ecc}(z) - 1 because ecc(z)≥2\mathrm{ecc}(z) \ge 2 when n≥3n \ge 3. So ecc(z′)≤ecc(z)−1\mathrm{ecc}(z') \le \mathrm{ecc}(z) - 1. So Z(T)Z(T) lies in T′T'. By Lemma 1, eccT′=eccT−1\mathrm{ecc}_{T'} = \mathrm{ecc}_T - 1 on T′T', so the vertices of T′T' with the smallest eccentricity are the same in both trees. □\square

Invariant of the loop. At the test of line L4, let RR be the set of vertices not yet removed. Then (a) the subgraph T[R]T[R] is a tree with ∣R∣=left|R| = \mathit{left}, (b) deg[v]\mathrm{deg}[v] is the degree of vv in T[R]T[R] for v∈Rv \in R, and deg[v]=0\mathrm{deg}[v] = 0 for v∉Rv \notin R, (c) LL lists each leaf of T[R]T[R] exactly once, and (d) Z(T[R])=Z(T)Z(T[R]) = Z(T).

Initialization. Line L1 handled n≤2n \le 2, so n≥3n \ge 3. After lines L2 and L3, R=VR = V, the counters are the degrees, LL lists the leaves, and (d) is trivial.

Maintenance. Suppose the invariant holds and left>2\mathit{left} > 2. In a tree with at least 3 vertices, no two leaves are adjacent (two adjacent leaves would form a component of 2 vertices). So when line L8 removes a leaf uu of LL, its only neighbour in RR is not in LL, and line L9 finds it as the only neighbour with deg>0\mathrm{deg} > 0; removed vertices have counter 0, and the other leaves of LL are not neighbours of uu. Line L10 lowers its counter by 1. After the loop of line L7, the set R′R' of vertices left is RR minus the leaves of T[R]T[R], so T[R′]=(T[R])′T[R'] = (T[R])', which by Lemma 1 is a tree; line L5 set left=∣R′∣\mathit{left} = |R'|. Each counter of a vertex of R′R' went down by its number of leaf neighbours, so (b) holds for R′R'. A vertex v∈R′v \in R' is a leaf of T[R′]T[R'] exactly when its counter is now 1. It had a counter of at least 2 before the round, since a vertex with counter 1 was a leaf of T[R]T[R] and is removed. So its counter passed through 1 during this round, and line L11 appended it to L′L' exactly once, at the moment it reached 1. A vertex of R′R' whose counter went from 2 or more down to 1 and further down to 0 is the single vertex of T[R′]T[R'], since a tree in which a vertex has degree 0 has one vertex; it is in L′L' too, and it is the only element of L′L', which is then the vertex set of T[R′]T[R']. So after line L12, (c) holds in the form "L lists the leaves of T[R′]T[R'], or its single vertex". By Lemma 2, Z(T[R′])=Z(T[R])=Z(T)Z(T[R']) = Z(T[R]) = Z(T).

Termination. Each round removes all leaves of a tree with at least 3 vertices, at least 2 of them, so left\mathit{left} drops by at least 2 per round and the loop ends after at most n/2n/2 rounds.

Theorem. LEAF-PEEL returns Z(T)Z(T).

Proof. For n≤2n \le 2, every vertex has the same eccentricity (00 or 11), so line L1 is correct. Otherwise, at the end of the loop, left∈{1,2}\mathit{left} \in \{1, 2\}. If ∣R∣=2|R| = 2, T[R]T[R] is one edge, both ends are leaves, L=RL = R, and both have eccentricity 1 in T[R]T[R], so Z(T[R])=RZ(T[R]) = R. If ∣R∣=1|R| = 1, then L=RL = R as shown in the maintenance step, and Z(T[R])=RZ(T[R]) = R. By (d), Z(T)=Z(T[R])=LZ(T) = Z(T[R]) = L, which line L13 returns. ■\blacksquare

Rounds. By Lemma 1, each round lowers the eccentricity of the remaining vertices by 1. If the loop ran kk rounds and c=∣R∣c = |R| vertices are left, the centre has eccentricity c−1c - 1 in T[R]T[R], so rad(T)=k+c−1\mathrm{rad}(T) = k + c - 1. A diametral path of TT has its two ends removed in round 1 (they are leaves), and its rest is a diametral path of T′T', so the diameter drops by 2 per round and D(T)=2k+(c−1)D(T) = 2k + (c - 1).

Back to Tree center by leaf peeling