Tree Diameter & Center

What is the longest path in a tree, and where is its middle? Two BFS runs find the first, peeling the leaves finds the second, both in linear time.

★ Lecture: trees_Intro1 · Zaza Gamezardashvili▶ video intermediate⏱ 12 min read
01

Learn

The idea, the mechanics and the cost.

01The diameter: the longest distance

The diameter of a tree is the longest distance between any two of its vertices. In the lecture’s tree it runs from 13 to 10 through 12, 8, 5, 2, 1, 3 and 6: 8 edges. A tree can have more than one diameter (a Y shape with three equal arms has three), but they all have the same length.

The lecture’s first observation is a lemma: both ends of a diameter are always leaves. Proof by contradiction: suppose an end V of a longest path were not a leaf. Then V has a neighbour off the path, and one more step gives a path longer than the longest one. Contradiction.

Brute force would run BFS from every vertex and keep the largest distance seen: O(V²). The next two sections bring it down to O(V).

12345678910111213diameter = 8longest distancebetween two vertices13 … 10both ends are leaves
The tree from the lecture’s slides 14 and 18 (the slides leave the vertices unnumbered, so the numbers are ours); its diameter runs from 13 to 10.

02The theorem: the farthest vertex is a diameter end

Theorem: let a diameter run from U to V. Then from any vertex X of the tree, the farthest vertex is U or V.

The lecture assumes the opposite: from X the farthest vertex is some leaf Y that is neither U nor V. Two cases:

  • a) X lies on the diameter. If Y were farther from X than V is, replacing the X…V part of the diameter by X…Y would give a path U…X…Y longer than the diameter. Contradiction.
  • b) X lies off the diameter. Let Z be the diameter vertex closest to X. If Y is farther from X than V, then Y is also farther from Z than V, so U…Z…Y is longer than the diameter. Contradiction.

Both cases are impossible, so the farthest vertex from any X is an end of a diameter. ∎ This is the whole reason the algorithm in the next section works.

UVZXYdiameter U … VZ: the diameter vertex closest to Xif |XY| > |XV| …… then U → Z → Y beats the diameter ✗
Case b of the lecture’s proof: X lies off the diameter, Z is its closest diameter vertex.

03The algorithm: two BFS runs

The theorem turns straight into an algorithm:

  • run BFS (or DFS) from any vertex and take the farthest vertex V: by the theorem it is one end of a diameter;
  • run BFS again from V; the farthest vertex U is the other end, and d[U] is the diameter.

On this tree, starting from 7: the first BFS reaches 13 at distance 7, the farthest, so V = 13. The second BFS from 13 reaches 10 at distance 8: U = 10, diameter 8. Keep the parent array of the second run and you can print the path itself: 13, 12, 8, 5, 2, 1, 3, 6, 10.

Careful: the first distance (7) is not the diameter; it only tells you where to start the second run. Each BFS is O(V) on a tree, since E = V − 1, so the whole algorithm is O(V). With non-negative edge weights, replace BFS by a DFS that sums the weights; the theorem still holds.

BFS #1 from vertex 7123456789101112132314420553667farthest: 13 (d = 7)BFS #2 from vertex 13123456789101112135465377248310farthest: 10 (d = 8) = diameter
Small numbers: BFS distances. From 7 the farthest is 13; from 13 the farthest is 10, at distance 8.

04Eccentricity, center and radius

The eccentricity of a vertex is the distance to its farthest vertex. The vertex with the smallest eccentricity is the center of the tree, and that smallest value is the radius. The largest eccentricity is the diameter.

A tree has one or two centers; the lecture calls them central and bicentric trees. The 13-vertex tree has center 2, radius 4 and diameter 8. Add one more leaf so that the diameter grows to 9 and you get the bicentric tree on the right, with centers 1 and 2 and radius 5.

Why care? The center is the best spot for a server, a warehouse or a meeting point when you want to minimize the worst-case distance. It is also the canonical root when you compare trees for isomorphism. And it always lies on the diameter, right in its middle: radius = ⌈diameter / 2⌉.

central tree12345678910111213center 2 · radius 4 · diameter 8bicentric tree1234567891011121314centers 1, 2 · radius 5 · diameter 9
Slide 18: a central tree and a bicentric tree, diameters in pink.

05Finding the center: peel the leaves

Leaves always have a larger eccentricity than the inner vertices, so they can never be the center (unless the tree has at most two vertices). The lecture’s algorithm: remove all leaves at once, together with their edges. What remains is again a tree; repeat until one or two vertices are left. Those are the centers.

Implementation: keep the degree of every vertex, put all leaves (degree 1) in a queue, and process round by round. Removing a leaf decrements its neighbour’s degree, and a neighbour whose degree drops to 1 becomes a leaf of the next round. O(V).

On this tree: round 1 removes the six leaves 4, 7, 9, 10, 11, 13; round 2 removes 6 and 12; round 3 removes 3 and 8; round 4 removes 1 and 5; vertex 2 remains. The diameter, the longest path, survives the longest, which is why the center lies on it: an odd number of vertices on the diameter gives one center, an even number gives two.

12345678910111213centerround 16round 22round 32round 42
Colours show the round in which each vertex is peeled; 2 survives.

Cost at a glance

Diameter (two BFS runs)O(V)
Center (leaf peeling)O(V)
Brute force: all eccentricitiesO(V²)
Radius from diameter D⌈D / 2⌉

Remember

  1. Both ends of a diameter are leaves, and the farthest vertex from any vertex is an end of a diameter.
  2. Two BFS runs find the diameter in O(V): any vertex → farthest V → farthest U, and d[U] is the answer.
  3. Peeling all leaves round by round leaves the one or two centers, which sit in the middle of the diameter.
02

Watch

The lecture as a narrated explainer video.

▶

Watch the lecture, animated

The video follows the lecture slide by slide: the same example, the same notation. Use it before or after playing with the visualizer.

03

Play

Step through the algorithm, then try it on your own input.

👀 What to watch: The Play tab uses a smaller 10-vertex tree (diameter 6, center 3). Start the first BFS from different vertices: the far end may change, the diameter never does.

Interactive visualizerfocus here, then space ← →
✎ Your inputThe theorem says ANY start works: try a few and watch the diameter stay the same.

Tree diameter & center · BFS #1 (from v1)

12345678910
A tree’s diameter is its longest path. A theorem (from the trees lecture) gives a trick with two BFS runs to find it in O(V).

Pseudocode

 1 diameter = longest path between any two vertices 2 BFS from ANY vertex → its farthest vertex u is a diameter end 3 note u 4 BFS from u → its farthest vertex v; dist(u,v) = diameter 5 (diameter path highlighted) 6 center: peel leaves layer by layer until 1–2 vertices remain
1 / 1
04

Check

Three questions. Pick an answer to see why.

Q1

BFS from some vertex X finds its farthest vertex Y at distance 5. What do you know?

Q2

A path graph (a chain) with 7 vertices. How many centers, and what radius?

Q3

Leaf peeling on a tree ends with two vertices. What does that say about the diameter?

05

Practice

Real problems to lock it in, easiest first.