Guest
Sign in to keep your graphs and progress.
Select an algorithm and generate steps to begin visualization
Two searches find the longest path in a tree
Finds the longest path in a tree with two BFS passes and the center in its middle, then confirms the center by peeling leaves
Select an algorithm and generate steps to begin visualization
The diameter of a tree is the length of its longest path, the largest distance between two vertices. Its center is the vertex, or the two adjacent vertices, whose farthest vertex is as close as possible, and that distance is the radius. Camille Jordan proved in 1869 that the center of a tree is always one vertex or two adjacent ones, and it sits in the middle of every longest path. In a general graph the diameter needs a search from every vertex, but in a tree two are enough.
The first BFS starts anywhere and finds the farthest vertex u. That vertex is always an end of some longest path: if it were not, the path to it could be extended past a longest path, a contradiction. The second BFS starts at u, and its farthest vertex v is the other end, so the distance from u to v is the diameter and the BFS parents give the path. The center is the middle vertex of that path, or the two middle vertices when its length is odd. As a check, leaf peeling removes all leaves round after round until one or two vertices remain, and those are exactly the center. Everything takes O(n). Edges count as length one, weights are ignored. The visualizer shows each BFS distance, draws the longest path in green, marks the round in which each leaf was peeled and colours the center orange.
In a tree-shaped network the diameter is the worst-case delay between two nodes, and the center is the best place for a server, a hub or a facility that everyone must reach. "Minimum Height Trees" asks for the roots that make a tree as shallow as possible, which are the center, and the two-BFS trick is a classic interview question. On weighted trees with non-negative weights the same two searches work with DFS distances. In graphs with cycles, two searches only give a lower bound on the diameter.
Two BFS passes for the diameter, and leaf peeling as an independent check of the center.
TreeDiameter(T, s):
dist = BFS(T, s), u = vertex with the largest dist
dist, parent = BFS(T, u), v = vertex with the largest dist
diameter = dist[v]
path = v, parent[v], parent[parent[v]], ..., u
center = middle vertex of path (two if diameter is odd)
LeafPeeling(T): // the check
leaves = vertices of degree 1
while more than 2 vertices remain:
remove all leaves, lowering their neighbours' degrees
leaves = neighbours that now have degree 1
return the 1 or 2 vertices left // the centerEverything rests on one fact: in a tree, the vertex farthest from any start is an end of some longest path. Its proof uses the unique path between two vertices of a tree, which is why the trick fails on graphs with cycles.
The eleven-vertex tree on this page, with the first BFS from G.
Example graph: Edges A–B, A–C, B–D, B–E, C–F, C–G, D–H, E–I, F–J and J–K. The deepest branch under B ends in H, and the deepest under C ends in K.
Diameter 7 from H to K, center A and C, radius 4. Rooting the tree at A or at C gives height 4, the smallest possible, which is the answer to "Minimum Height Trees" for this tree.
Time: O(n) · Space: O(n)
Each BFS visits every vertex and every edge once, and a tree has n − 1 edges, so each pass is O(n). Following the parents back gives the path in O(n). Leaf peeling removes each vertex once and lowers each degree once, O(n) as well.
Other ways to get diameters and centers.
| Alternative | Prefer it when | Cost |
|---|---|---|
| BFS from every vertex | General unweighted graphs, where two BFS passes only give a lower bound. It also gives every eccentricity. | O(n·m) |
| Tree DP over the two deepest branches | One DFS is run over the tree anyway, for example with weights. At each vertex, add the two deepest child branches, and the best sum is the diameter. | O(n) |
| Floyd–Warshall | Small weighted graphs where all pairwise distances are needed anyway. | O(n³) |
Related algorithms: Breadth-First Search, Tree Traversal (Pre, In, Post, Level), Lowest Common Ancestor (Binary Lifting), Prüfer Code, Floyd-Warshall Algorithm