How do you find the diameter of a tree?.
June 25, 2022
Trees
Trees are connected undirected graphs with:
- no cycles,
- or, only unique paths between two vertices,
- or, vertices and edges,
- or, all edges are bridges.
All of these can be proved using one another — they are equivalent.
Diameter
Graph
The diameter is the longest of all shortest paths in a graph.
How to find it?
- Use Floyd-Warshall to find all-pairs shortest paths, then take the maximum.
- Time complexity is .
Tree
Since all paths are unique, the answer here is just the longest path.
How to find it?
- BFS from all nodes, and find the largest.
- Time complexity is , since there are edges and BFS costs .
- Find , the height of node , for all nodes.
- Use the heights of all nodes to find the length of the diameter, assuming the diameter goes through the current node.
- Take the maximum of all these answers.
- Greedy approach:
- Pick any node,
s. - Run BFS from
s. - Find the furthest node from
s:t = furthest(s). - Run BFS from
t. - Find the furthest node from
t:r = furthest(t). - The diameter is the path from
rtot.
- Pick any node,
Q. Why does the greedy method work? [PROOF]
Case 1: picked node s is on the diameter of the tree.
- First BFS gives one endpoint of the diameter.
- Second BFS gives the other endpoint of the diameter.
Case 2: picked node is not on the diameter.
- Then, to get an endpoint of the diameter, the diameter must be in the deepest subtree if the tree is rooted at
s. - Assume the deepest subtree does not contain the diameter.

.
Let the diameter be in a subtree , with . We know that .
Thus, in subtree , the diameter can be , whereas in , . So — a contradiction.
QED
Tree = edges of diameter + forest

In other words, the height of each component with root in the left half of the diameter (i.e., ) is at most the distance of the component’s root from the left end of the diameter.
You can prove the same statement for the right half of the diameter (i.e., ), using that is the farthest node from .
Farthest node for each node — remoteness
For each node , find a node such that is maximum.
- Claim: or always works, where and are the diameter endpoints.
Center of a tree
- The remoteness of a node is its distance from the furthest node.
- The center is the node with minimum remoteness.

Theorem
All diameters in T must go through the center c.
Proof
Assume the diameter does not go through c.
- Root the tree at
c. - Any subtree of
chas or () edges. Thus : and . - So
cmust be on the diameter.
QED
Theorem
There are at most centers in a tree.
Count number of diameters in a tree

For a graph of this kind, there are diameters, which is of order .
Case 1: tree has 1 center.
- = number of nodes at depth in subtree , with the tree rooted at the center.

We have to select 2 nodes — one from a given subtree, the other from any other subtree.
Number of diameters = .
The inner summation can be computed with a running sum, so this is fast.
Case 2: tree has 2 centers.

- Number of nodes at maximum depth in × number of nodes at maximum depth in .