Tree Center
Definition
In a tree, if node \(x\) as the root yields the shortest longest chain starting from \(x\), then \(x\) is called the center of the tree.
Properties
- The center of a tree is not necessarily unique, but there are at most 2 centers, and these two centers are adjacent.
- The center of a tree must lie on the tree's diameter.
- The paths from all nodes in the tree to their farthest points must intersect at the tree's center.
- When the tree's center is taken as the root, the two chains reaching the endpoints of the diameter are the longest chain and the second longest chain, respectively.
- When merging two trees into one by adding an edge between them, connecting the centers of the two trees minimizes the diameter of the new tree.
- The distance from the tree's center to any other node does not exceed half of the tree's diameter.
Finding the Center
Find a node \(x\) such that when \(x\) is taken as the root, the length of the longest chain is minimized.
Detailed Steps
- Maintain \(len1_x\), representing the longest chain within the subtree of node \(x\).
- Maintain \(len2_x\), representing the longest chain that does not overlap with \(len1_x\).
- Maintain \(up_x\), representing the longest chain outside the subtree of node \(x\), which must pass through the parent of \(x\).
- Find the node \(x\) that minimizes \(\max(len1_x, up_x)\). Then \(x\) is the center of the tree.
Reference Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | |
Example
Suppose we have a tree as follows:
1 2 3 4 5 | |
- The diameter of the tree is \(D \rightarrow B \rightarrow A \rightarrow C \rightarrow F\). The diameter length is \(4\).
- The center of the tree is node \(A\), because the longest chain starting from \(A\) (to \(D\) or \(F\)) is \(2\).
- If \(B\) or \(C\) is taken as the root, the longest chain starting from these nodes would be longer, so they are not the tree's center.
Time Complexity
The time complexity of the above algorithm is \(O(n)\), where \(n\) is the number of nodes in the tree.
References
- TutorialsPoint: Centers of a Tree
- ProofWiki: Definition of Center of Tree
- Wikipedia: Tree (graph theory)
本页面最近更新:,更新历史
发现错误?想一起完善? 在 GitHub 上编辑此页!
本页面贡献者:OI-wiki
本页面的全部内容在 CC BY-SA 4.0 和 SATA 协议之条款下提供,附加条款亦可能应用