Tutorials › Graph Theory (Undergraduate) › Computing and Comparing Tree Centers and Centroids

Rooted Trees, Distance and Centers · Tutorial 158 of 799

Computing and Comparing Tree Centers and Centroids

Use distances to find tree centers and component sizes to find centroids, then compare what each measure identifies.

Intermediate 10 min read

What You'll Learn

  • Find a tree’s center by locating the middle vertex or edge of a diameter path.
  • Compute eccentricities along a diameter path and verify the center.
  • Test whether a vertex is a centroid by checking every component after its deletion.
  • Compute component sizes efficiently using subtree sizes from a rooted tree.
  • Compare centers and centroids on trees where they agree or differ.
  • Explain why distance balance and vertex-count balance need not select the same vertices.

Two Different Ways to Balance a Tree

A tree can be balanced in more than one sense. Its center consists of the vertex or vertices with the smallest possible maximum distance to all other vertices. Its centroid consists of the vertex or vertices whose deletion leaves components as small as possible in the sense that no component contains more than half the tree’s vertices. The center concerns distances; the centroid concerns component sizes. These are different measurements, so their answers need not be the same.

All graphs in this tutorial are finite, simple, undirected trees. For vertices \(u,v\), distance \(d(u,v)\) is the number of edges in a shortest path. The eccentricity of \(v\) is \(\operatorname{ecc}(v)=\max_{u\in V}d(u,v)\). The radius is the minimum eccentricity, and a center is a vertex attaining that minimum. A diameter is a path of maximum length, and its length is the diameter of the tree. A centroid is a vertex \(v\) such that every component of \(T-v\) has at most \(|V|/2\) vertices.

Definition: If \(T\) has \(n\) vertices, a vertex \(v\) is a centroid when every component of \(T-v\) has size at most \(n/2\). A center is a vertex of minimum eccentricity. A tree may have one or two centers and one or two centroids; these sets are not defined by the same condition.

Deleting a vertex of a tree separates it into one component for each neighbor of that vertex. To test whether a vertex is a centroid, count the vertices in every such component and compare the largest count with \(n/2\). To find a center, first find a diameter path; its middle vertex, or the two vertices at the ends of its middle edge, give the center.

Why the Middle of a Diameter Gives the Center

Theorem: Let \(T\) be a tree and let \(P\) be any diameter path of length \(D\). The center of \(T\) is exactly the middle vertex of \(P\) if \(D\) is even, and exactly the two vertices at the ends of the middle edge of \(P\) if \(D\) is odd.

Proof. Write the diameter path as \(p_0,p_1,\ldots,p_D\), so its endpoints are \(p_0\) and \(p_D\). For a vertex \(p_i\) on the path, its distances to these endpoints are \(i\) and \(D-i\). Therefore \(\operatorname{ecc}(p_i)\geq \max(i,D-i)\).

We show that this lower bound is also an upper bound. Take any vertex \(z\) of the tree. The unique path from \(z\) to \(P\) first meets \(P\) at some \(p_j\); let \(h=d(z,p_j)\). Since \(p_0,\ldots,p_D\) is a diameter path, the paths from \(z\) to either endpoint cannot have length greater than \(D\). Thus \(h+j\leq D\) and \(h+D-j\leq D\), so \(h\leq \min(j,D-j)\). The path from \(p_i\) to \(z\) has length \(|i-j|+h\). If \(j\leq i\), then this is at most \(i-j+j=i\). If \(j\geq i\), it is at most \(j-i+D-j=D-i\). In either case it is at most \(\max(i,D-i)\). This holds for every \(z\), so \(\operatorname{ecc}(p_i)=\max(i,D-i)\).

Now let \(z\) be off \(P\), and let \(p_j\) be where its path first meets \(P\). Put \(h=d(z,p_j)\), so \(h\geq1\). Its distances to \(p_0\) and \(p_D\) are \(h+j\) and \(h+D-j\); hence \(\operatorname{ecc}(z)\geq h+\max(j,D-j)\). This is strictly greater than \(\max(j,D-j)\), which is at least the minimum of \(\max(i,D-i)\) over \(0\leq i\leq D\). So no vertex off \(P\) is a center. On \(P\), the quantity \(\max(i,D-i)\) is minimized at the middle index if \(D\) is even, and at the two middle indices if \(D\) is odd. Those are therefore exactly the centers. \(\square\)

This result gives a direct procedure: choose a diameter path and count in from both ends. It also verifies the result without computing every vertex’s eccentricity. The proof explains why branches off the diameter cannot produce a better center: their vertices are farther from both diameter endpoints than their attachment points are.

Worked Example: A Path Where Center and Centroid Agree

Let \(T\) have \(V=\{1,2,3,4,5,6,7\}\) and \(E=\{12,23,34,45,56,67\}\). This is a path on seven vertices. Its diameter path is the whole path \(1,2,3,4,5,6,7\), of length \(6\). The middle index is \(3\), so the theorem gives center \(4\).

For a direct eccentricity check, the distances from \(4\) to \(1,2,3,4,5,6,7\) are \(3,2,1,0,1,2,3\), so \(\operatorname{ecc}(4)=3\). For vertex \(3\), the distance to \(7\) is \(4\), and for vertex \(5\), the distance to \(1\) is \(4\). Vertices farther toward either endpoint have eccentricity at least \(4\) as well. Thus no other vertex has eccentricity \(3\).

To test the centroid, \(n=7\), so every component after deletion must have size at most \(7/2=3.5\), which for integer sizes means at most \(3\). Deleting \(4\) leaves components \(\{1,2,3\}\) and \(\{5,6,7\}\), each of size \(3\). Deleting \(3\) leaves a component of size \(4\), namely \(\{4,5,6,7\}\); deleting \(5\) likewise leaves a component of size \(4\). Deleting any other vertex leaves a component of size at least \(5\). So \(4\) is the unique centroid as well as the unique center.

Check: the largest component after deleting \(4\) has size \(3\leq 3.5\), and the maximum distance from \(4\) is \(3\). The vertex \(4\) meets both tests.

Computing Centroids from Component Sizes

The centroid test uses the actual sizes of the components, not how far they extend. In a tree, those sizes can be computed efficiently by rooting the tree at any vertex. The subtree of a vertex \(v\) consists of \(v\) and all its descendants in the chosen rooting; write its size as \(s(v)\). After deleting \(v\), each child \(w\) of \(v\) contributes a component of size \(s(w)\). If \(v\) has a parent, the component in the parent’s direction has size \(n-s(v)\). These counts cover all components of \(T-v\).

Proposition: In a tree with \(n\) vertices, rooted at any vertex, \(v\) is a centroid exactly when each child \(w\) of \(v\) has \(s(w)\leq n/2\), and, if \(v\) has a parent, \(n-s(v)\leq n/2\).

Proof. Removing \(v\) separates the tree into the components reached through its neighbors. For each child \(w\), its descendants and \(w\) itself form exactly the component reached through \(w\), of size \(s(w)\). If \(v\) has a parent, all vertices outside \(v\)’s subtree lie in the component reached through the parent, of size \(n-s(v)\). There are no other components because every neighbor is either the parent or a child. The definition of centroid requires every one of these component sizes to be at most \(n/2\), which is precisely the stated test. If the root is \(v\), there is no parent component, so only the child conditions apply. \(\square\)

Each vertex needs only its child-subtree sizes and, except at the root, the size on its parent side. Once the subtree sizes have been counted, the centroid test does not require a fresh search of the entire tree for each candidate vertex.

A Tree Whose Center and Centroid Differ

Worked Example: A Long Arm and Four Leaves

Let \(T\) have \(V=\{x,a_1,a_2,a_3,a_4,b,c,d,e\}\) and \(E=\{xa_1,a_1a_2,a_2a_3,a_3a_4,xb,xc,xd,xe\}\). This is a path of length \(4\) from \(x\) to \(a_4\), with four additional leaves adjacent to \(x\). There are \(n=9\) vertices.

A longest path runs from any one of \(b,c,d,e\) to \(a_4\), for example \(b,x,a_1,a_2,a_3,a_4\). Its length is \(5\). No path is longer: a path can use at most one of the leaves at \(x\), and its longest possible route from that leaf is the four-edge arm from \(x\) to \(a_4\), giving \(1+4=5\). The two middle vertices of the displayed diameter path are \(a_1\) and \(a_2\), so the centers are \(a_1,a_2\).

For verification, \(a_1\) is at distance \(3\) from \(a_4\) and distance \(2\) from each of \(b,c,d,e\), so its eccentricity is \(3\). Vertex \(a_2\) is at distance \(2\) from \(a_4\) and \(3\) from each of the four leaves, also giving eccentricity \(3\). The theorem shows these are the centers.

For the centroid test, \(n/2=4.5\). Deleting \(x\) leaves one component \(\{a_1,a_2,a_3,a_4\}\) of size \(4\), and four singleton components, each of size \(1\). The largest size is \(4\leq4.5\), so \(x\) is a centroid. Deleting \(a_1\) leaves a component \(\{x,b,c,d,e\}\) of size \(5\), which is greater than \(4.5\); therefore \(a_1\) is not a centroid. Deleting \(a_2\) leaves a component containing \(x,b,c,d,e,a_1\) of size \(6\), so \(a_2\) is not a centroid. For \(a_3\) or \(a_4\), the component toward \(x\) has size at least \(7\). Deleting any of \(b,c,d,e\) leaves a component of size \(8\). Thus \(x\) is the unique centroid.

Check: the center set is \(\{a_1,a_2\}\), while the centroid set is \(\{x\}\). The centers minimize the farthest distance, which is \(3\); the centroid balances the vertex counts, with largest component size \(4\). The two criteria select no common vertex here.

When Both Sets Have Two Vertices

Proposition: A tree has at most two centroids. If it has two, they are adjacent.

Proof. For any edge \(uv\), deleting the edge separates the tree into two sides. If \(u\) is a centroid, the side containing \(v\) has size at most \(n/2\), since it is one of the components of \(T-u\). If \(v\) is also a centroid, the side containing \(u\) has size at most \(n/2\). The two side sizes sum to \(n\), so both must equal \(n/2\). In particular, \(n\) is even.

Now suppose two distinct centroids \(u,v\) were not adjacent, and consider the first edge \(ux\) on the path from \(u\) to \(v\). The side of this edge containing \(x\) has size at most \(n/2\), because it is a component of \(T-u\). Since \(v\) is not \(x\), deleting \(v\) leaves a component containing \(u\) that includes all vertices on the \(u\)-side of the edge \(ux\) and also \(x\). If the \(x\)-side has size less than \(n/2\), the \(u\)-side already has more than \(n/2\) vertices, contradicting that \(v\) is a centroid. If the \(x\)-side has size \(n/2\), the \(u\)-side has size \(n/2\), and the component containing \(u\) after deleting \(v\) includes \(x\) as well, so has more than \(n/2\) vertices, again a contradiction. Thus distinct centroids must be adjacent. Since a tree has no three pairwise adjacent vertices, it cannot have three centroids. \(\square\)

Worked Example: A Double-Star Tree

Let \(T\) have \(V=\{u,v,a_1,a_2,a_3,a_4,b_1,b_2,b_3,b_4\}\) and \(E=\{uv,ua_1,ua_2,ua_3,ua_4,vb_1,vb_2,vb_3,vb_4\}\). There are \(10\) vertices and \(9\) edges; the graph is connected, and each of its eight leaves attaches directly to \(u\) or \(v\).

Every path has length at most \(3\): a longest one goes from some \(a_i\) through \(u,v\) to some \(b_j\), for example \(a_1,u,v,b_1\). Its two middle vertices are \(u,v\), so the centers are \(u,v\). Each has eccentricity \(2\): from \(u\), the farthest vertices are the \(b_j\); from \(v\), the farthest vertices are the \(a_i\).

Here \(n/2=5\). Deleting \(u\) leaves four singleton components and the component \(\{v,b_1,b_2,b_3,b_4\}\) of size \(5\). Deleting \(v\) leaves four singleton components and the component \(\{u,a_1,a_2,a_3,a_4\}\) of size \(5\). So both \(u\) and \(v\) are centroids. Deleting any leaf leaves a component of size \(9\), which is greater than \(5\). The centroid set is therefore exactly \(\{u,v\}\).

Check: the diameter length is \(3\), with middle edge \(uv\), and each of its sides contains \(5\) vertices. Thus the same adjacent pair is selected by the diameter test and the component-size test.

Common Pitfall: Mixing Up the Tests

A frequent error is to use the diameter midpoint rule to identify a centroid, or the component-size rule to identify a center. In the long-arm example, \(x\) is the centroid because the largest component after deleting it has size \(4\), no more than \(9/2\). But \(x\) is not a center: its distance to \(a_4\) is \(4\), whereas the centers \(a_1,a_2\) have eccentricity \(3\). Conversely, the diameter rule identifies \(a_1,a_2\) as centers, but deleting either leaves a component larger than \(9/2\), so neither is a centroid.

When computing, write down the quantity being tested. For a center, compare maximum distances to all vertices. For a centroid, list every component size after deletion and compare each with \(n/2\), including the strict failure case of a size greater than \(n/2\). Neither definition asks for the same kind of balance: one limits travel distance, the other limits how many vertices lie in a component.

Check Your Understanding

For each question, keep the center test and centroid test separate.

  1. For a path on \(9\) vertices, identify its center and centroid using the diameter and component sizes.
  2. In a tree on \(12\) vertices, a vertex has component sizes \(6,3,2\) after deletion. Is it a centroid? Explain using the exact threshold.
  3. Why does a diameter of odd length give two centers, while a diameter of even length gives one?
  4. In the long-arm example, compute the eccentricity of \(x\) and the largest component size after deleting \(x\). Which notion does each value test?
  5. Give the component sizes on the two sides of an edge joining two centroids in a tree, and explain why they must be equal.