Center of a Tree

Definition

In a tree, if the longest path starting from vertex xx, when xx is chosen as the root, is as short as possible, then xx is called the center of the tree.

Properties

  • The center of a tree is not necessarily unique, but there are at most 22 centers, and if there are two, they are adjacent.
  • The center of a tree always lies on the tree's diameter.
  • The paths from every vertex of a tree to its farthest vertex always meet at the tree's center.
  • When the center of a tree is the root, the two paths from it to the endpoints of the diameter are the longest and second-longest paths, respectively.
  • When two trees are merged by adding an edge between them, connecting their centers minimizes the diameter of the resulting tree.
  • The distance from the center of a tree to any other vertex is at most half the tree's diameter.

Finding the center

Find a vertex xx for which the longest path starting from it, when it is the root, is as short as possible.

Steps

  1. Maintain len1xlen1_x, the longest path within the subtree of vertex xx.
  2. Maintain len2xlen2_x, the longest path that does not overlap with len1xlen1_x.
  3. Maintain upxup_x, the longest path outside the subtree of vertex xx; this path must pass through the parent of xx.
  4. Find the vertex xx that minimizes max⁡(len1x,upx)\max(len1_x, up_x); this xx is the center of the tree.
Reference implementation
// Vertices are numbered from 1, so i ∈ [1,n]; the graph uses vector storage.
int d1[N], d2[N], up[N], x, y, mini = 1e9;  // d1 and d2 correspond to len1 and len2 above

struct node {
  int to, val;  // to is the endpoint and val is the edge weight
};

vector<node> nbr[N];

void dfsd(int cur, int fa) {  // compute len1 and len2
  for (node nxtn : nbr[cur]) {
    int nxt = nxtn.to, w = nxtn.val;  // nxt is the endpoint of this edge and val is its weight
    if (nxt == fa) {
      continue;
    }
    dfsd(nxt, cur);
    if (d1[nxt] + w > d1[cur]) {  // can update the longest path
      d2[cur] = d1[cur];
      d1[cur] = d1[nxt] + w;
    } else if (d1[nxt] + w > d2[cur]) {  // cannot update the longest path, but can update the second-longest path
      d2[cur] = d1[nxt] + w;
    }
  }
}

void dfsu(int cur, int fa) {
  for (node nxtn : nbr[cur]) {
    int nxt = nxtn.to, w = nxtn.val;
    if (nxt == fa) {
      continue;
    }
    up[nxt] = up[cur] + w;
    if (d1[nxt] + w != d1[cur]) {  // if the longest path in this subtree is not in the nxt subtree
      up[nxt] = max(up[nxt], d1[cur] + w);
    } else {  // if the longest path in this subtree is in the nxt subtree, use the second-longest path
      up[nxt] = max(up[nxt], d2[cur] + w);
    }
    dfsu(nxt, cur);
  }
}

void GetTreeCenter() {  // find the tree's centers, denoted by x and y if they exist
  dfsd(1, 0);
  dfsu(1, 0);
  for (int i = 1; i <= n; i++) {
    if (max(d1[i], up[i]) < mini) {  // found a vertex minimizing the current max(len1[x],up[x])
      mini = max(d1[i], up[i]);
      x = i;
      y = 0;
    } else if (max(d1[i], up[i]) == mini) {  // the other center
      y = i;
    }
  }
}

Example

Suppose we have the following tree:

           A
          / \
         B   C
        / \   \
       D   E   F
  • The tree's diameter is D→B→A→C→FD \rightarrow B \rightarrow A \rightarrow C \rightarrow F, with length 44.
  • The center of the tree is vertex AA, because the longest paths from AA (to DD or FF) both have length 22.
  • If BB or CC is chosen as the root, the longest path from that vertex becomes longer, so neither is the center of the tree.

Time complexity

The time complexity of the algorithm above is O(n)O(n), where nn is the number of vertices in the tree.

References

This content comes from OI Wiki: Center of a Tree. See the original for authors and revision history. AIOJ has adapted the formatting, in-site links, and resource paths.

Content is shared under CC BY-SA 4.0 and SATA license; additional terms are governed by the original notice. Thanks to the OI Wiki authors and contributors; per the SATA terms, please visit the OI Wiki repository and star it to show your support.