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 be a free tree with vertices and unit edge lengths. Write for the distance, for the eccentricity and for the centre. A leaf is a vertex of degree 1.
Lemma 1. Let , and let be with all leaves removed. Then is a tree with at least one vertex, for all in , and for each in .
Proof. Not empty. Since is connected with 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 be in , and let be the path from to in . Each inner vertex of has two neighbours on , so it has degree at least 2 and is not a leaf. The ends , are in . So lies in . Hence is connected and on . It is acyclic as a subgraph of , so it is a tree.
Eccentricities. Let be in and . Since , . Let be a vertex with . If had a neighbour that is not on the path from to , then the path from to followed by would be a path of length , which is impossible. So the only neighbour of is its neighbour on the path to , and is a leaf. If , then , and is adjacent to every other vertex (each vertex is at distance at most 1); then is the single vertex , and . Otherwise is an inner vertex of a path, so , and . So . Conversely, let be in . Then is not a leaf, so it has a neighbour other than its neighbour on the path to , and . So , and .
Lemma 2. If , then , where is as in Lemma 1.
Proof. A leaf of is not in : its neighbour has for each (the path from to goes through ), and because when . So . So lies in . By Lemma 1, on , so the vertices of with the smallest eccentricity are the same in both trees.
Invariant of the loop. At the test of line L4, let be the set of vertices not yet removed. Then (a) the subgraph is a tree with , (b) is the degree of in for , and for , (c) lists each leaf of exactly once, and (d) .
Initialization. Line L1 handled , so . After lines L2 and L3, , the counters are the degrees, lists the leaves, and (d) is trivial.
Maintenance. Suppose the invariant holds and . 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 of , its only neighbour in is not in , and line L9 finds it as the only neighbour with ; removed vertices have counter 0, and the other leaves of are not neighbours of . Line L10 lowers its counter by 1. After the loop of line L7, the set of vertices left is minus the leaves of , so , which by Lemma 1 is a tree; line L5 set . Each counter of a vertex of went down by its number of leaf neighbours, so (b) holds for . A vertex is a leaf of 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 and is removed. So its counter passed through 1 during this round, and line L11 appended it to exactly once, at the moment it reached 1. A vertex of whose counter went from 2 or more down to 1 and further down to 0 is the single vertex of , since a tree in which a vertex has degree 0 has one vertex; it is in too, and it is the only element of , which is then the vertex set of . So after line L12, (c) holds in the form "L lists the leaves of , or its single vertex". By Lemma 2, .
Termination. Each round removes all leaves of a tree with at least 3 vertices, at least 2 of them, so drops by at least 2 per round and the loop ends after at most rounds.
Theorem. LEAF-PEEL returns .
Proof. For , every vertex has the same eccentricity ( or ), so line L1 is correct. Otherwise, at the end of the loop, . If , is one edge, both ends are leaves, , and both have eccentricity 1 in , so . If , then as shown in the maintenance step, and . By (d), , which line L13 returns.
Rounds. By Lemma 1, each round lowers the eccentricity of the remaining vertices by 1. If the loop ran rounds and vertices are left, the centre has eccentricity in , so . A diametral path of has its two ends removed in round 1 (they are leaves), and its rest is a diametral path of , so the diameter drops by 2 per round and .