寻找树的中心

发布于 2024-09-29 01:10:33 字数 159 浏览 0 评论 0原文

我有一个问题,这是我的计划的一部分。

对于树 T=(V,E),我们需要找到树中的节点 v,使从 v 到任何其他节点的最长路径的长度最小化。

那么我们如何找到树的中心呢?中心只能有一个还是多个?

如果有人能给我一个好的算法,这样我就可以了解如何适应我的程序。

I have a question which is part of my program.

For a tree T=(V,E) we need to find the node v in the tree that minimize the length of the longest path from v to any other node.

so how we find the center of the tree? Is there can be only one center or more?

If anyone can give me good algorithm for this so i can get the idea on how i can fit into my program.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

听闻余生 2024-10-06 01:10:33

有两种方法可以做到这一点(两者同时运行):


选择树上的任意顶点 v1。从此顶点运行 BFS。您要处理的最后一个顶点 (v2) 将是距 v1 最远的顶点。现在运行另一个 BFS,这次从顶点 v2 开始,并获取最后一个顶点 v3

v2v3 的路径是树的直径,您的中心位于树上的某个位置。更准确地说,它位于它的中间。如果路径有 2n + 1 个点,则只有 1 个中心(在位置 n + 1)。如果路径有 2n 个点,则在位置 nn + 1 处将有两个中心。

您仅使用 2 个 BFS 调用,其运行时间为 2 * O(V) 。

There are two approaches to do this (both runs in the same time):

  • using BFS (which I will describe here)
  • using FIFO queue.

Select any vertex v1 on your tree. Run BFS from this vertex. The last vertex (v2) you will proceed will be the furthest vertex from v1. Now run another BFS, this time from vertex v2 and get the last vertex v3.

The path from v2 to v3 is the diameter of the tree and your center lies somewhere on it. More precisely it lies in the middle of it. If the path has 2n + 1 points, there will be only 1 center (in the position n + 1). If the path has 2n points, there will be two centers at the positions n and n + 1.

You only use 2 BFS calls which runs in 2 * O(V) time.

只怪假的太真实 2024-10-06 01:10:33

考虑一棵有两个节点的树?哪个是中心?任何一个就足够了,因此一棵树可以有多个中心。

现在,想想成为中心意味着什么。如果所有分支的高度相同,则中心为根(所有路径都经过根)。如果分支具有不同的高度,则中心必须是根或具有最大高度的分支,否则最大路径大于最高分支的高度,并且根将是更好的选择。现在,我们需要从最高的树枝往下看多远?最高的分支(从根开始)和下一个最高的分支之间的高度差的一半(如果差异最多为 1,则根就足够了)。为什么,因为当我们将最高的分支向下一层时,我们会将到下一个最高分支的最深节点的路径延长一级,并将到当前分支中最深节点的距离减少一级。最终,当您穿过深度差的一半时,它们将相等。现在,当我们沿着最高的分支走下去时,我们只需在每一层选择最高的分支即可。如果多个高度相同,我们只需任意选择一个即可。

本质上,您要做的就是找到图中的最长路径,即树的最高分支和下一个最高分支之间的距离,然后找到该分支上的中间节点。因为可能有多条路径与最长路径的长度相同,并且最长路径的长度可能是偶数,所以有多个可能的中心。

Consider a tree with two nodes? Which is the center? Either one will suffice, ergo a tree can have more than one center.

Now, think about what it means to be the center. If all of the branches are the same height, the center is the root (all paths go through the root). If the branches are of different heights then the center must be either the root or in the branch with the greatest height otherwise the maximum path is greater than the height of the tallest branch and the root would be a better choice. Now, how far down the tallest branch do we need to look? Half the difference in height between the tallest branch (from the root) and the next tallest branch (if the difference is at most 1 the root will suffice). Why, because as we go down the tallest branch by one level we are lengthening the path to the deepest node of the next tallest branch by one and reducing the distance to the deepest node in the current branch by one. Eventually, they will be equal when you've traversed half the difference in the depth. Now as we go down the tallest branch, we simply need to choose at each level the tallest sub-branch. If more than one has the same height, we simply choose one arbitrarily.

Essentially, what you are doing is finding the longest path in the graph, which is the distance between the tallest branch of the tree and the next tallest branch, then finding the middle node on that branch. Because there may be multiple paths of the same length as the longest path and the length of the longest path may be even, you have multiple possible centers.

深居我梦 2024-10-06 01:10:33

我不会为您做这个作业问题,而是会通过思考过程来问您以获得答案...

1) 您会如何处理图 abc(三个顶点,两条边,并且肯定是非循环的)?想象一下,您必须在某些顶点上放置一些标签,您知道您将获得“中心”顶点上最长路径的最小值。 (b,最终标签为“1”)但是一步完成这一任务需要心灵力量。所以问问自己 b 离什么远 1 步。如果到 b 的最长路径是 1,并且我们刚刚沿着该路径向后退了一步,那么到目前为止我们的路径长度是多少? (最长路径 = 1,-1 表示后退一步。啊哈:0)。所以这一定是叶子的标签。

2)作为算法的第一步,这意味着什么?将叶子标记为“0”,将其上游标记为“1”,将其上游标记为“2”,依此类推。从树叶处行进,边走边计算距离...

3) 嗯...我们在图 abcd 方面遇到了问题。 (从现在开始,标记的顶点将被其标签替换。)将叶子标记为“0”给出 0-bc-0...我们不能得到两个中心...哎呀,我们在更简单的情况下做什么条件0-b-1?我们想用“1”和“2”来标记 b...以相反的顺序处理...

在 0-b-1 中,如果我们将 b 左边的路径延伸 1,我们会得到长度为 1 的路径如果我们从 b 的右侧延伸路径,我们会得到 2。我们想要跟踪“从 v 到任何其他节点的最长路径的长度”,因此我们想要跟踪到 b 的最长路径。这意味着我们用“2”标记 b。

 0-b-1  ->  0-2-1 

在 0-bc-0 中,计算机实际上并没有同时更新 b 和 c。它更新其中之一,给出 0-1-c-0 或 0-b-1-0,下一次更新给出 0-1-2-0 或 0-2-1-0。 b 和 c 都是该图的“中心”,因为它们都满足“树中的节点 v 最小化从 v 到任何其他节点的最长路径的长度”的需求。 (长度为 2。)

这导致了另一个观察结果:计算的结果不是标记图,而是找到我们标记的最后一个顶点和/或最终具有最大标签的顶点。 (我们可能找不到排序标签的好方法,所以我们最终需要在最后找到最大值。或者也许我们会。谁知道呢。)

4)所以现在我们有这样的东西:图表的两份副本——带标签的副本和燃尽副本。第一个将存储标签,并且其中将包含最终答案。当我们从中删除未标记的顶点(以查找新的可标记顶点)时,燃尽副本将变得越来越小。 (还有其他方法可以组织这个,以便只使用图表的一个副本。当您最终理解这个答案时,您应该找到一种方法来减少这种浪费。)大纲:

    label = 0
    while the burndown graph is nonempty
        collect all the leaves in the burndown-graph into the set X
        for each leaf in the set X
            if the leaf does not have a label
                set the leaf's label (to the current value of label)
            delete the leaf from the burn-down graph (these leafs are two copies of the same leaf in the input graph)
        label = label+1
    find the vertex with the largest label and return it

5)如果您真的观看了这个运行,你会发现有几个走捷径的机会。包括用更快的方法来识别答案,从而替换大纲最后一行的搜索。

现在介绍算法问题的一般策略技巧:
* 手工做一些小例子。如果你不明白如何处理小案件,你就无法直接跳到告诉计算机如何处理大案件。
* 如果上述任何步骤看起来没有动力或完全不透明,那么您将需要更加努力地学习才能在计算机科学方面取得好成绩。可能计算机科学不适合你......

Rather than do this homework problem for you, I'm going to ask you through the thought process that gets the answer...

1) What would you do with the graph a-b-c (three vertices, two edges, and definitely acyclic)? Imagine for a moment that you have to put some labels on some of the vertices, you know you're going to get the minimum of the longest path on the "center" vertex. (b, with eventual label "1") But doing that in one step requires psychic powers. So ask yourself what b is 1 step away from. If the longest path to b is 1, and we've just stepped one step backwards along that path, what's the length of our path so far? (longest path = 1, -1 for the back one step. Aha: 0). So that must be the label for the leaves.

2) What does this suggest as a first cut for an algorithm? Mark the leaves "0", mark their upstreams "1", mark their upstreams "2" and so on. Marching in from the leaves and counting the distance as we go...

3) Umm... We have a problem with the graph a-b-c-d. (From now on, a labelled vertex will be replaced with its label.) Labeling the leaves "0" gives 0-b-c-0... We can't get two centers... Heck, what do we do in the simpler condition 0-b-1? We want to label b with both "1" and "2"... Handle those in reverse order...

In 0-b-1, if we extend the path from b's left by one, we get a path of length 1. If we extend the path from b's right, we get 2. We want to track "the length of the longest path from v to any other node", so we want to keep track of the longest path to b. That means we mark b with a "2".

 0-b-1  ->  0-2-1 

In 0-b-c-0, the computer doesn't actually simultaneously update b and c. It updates one of them, giving either 0-1-c-0 or 0-b-1-0, and the next update gives 0-1-2-0 or 0-2-1-0. Both b and c are "center"s of this graph since each of them meets the demand "the node v in the tree that minimize the length of the longest path from v to any other node." (That length is 2.)

This leads to another observation: The result of the computation is not to label a graph, it's to find the last vertex we label and/or the vertex that ends up with the largest label. (It's possible that we won't find a good way to order labels, so we'll end up needing to find the max at the end. Or maybe we will. Who knows.)

4) So now we have something like: Make two copies of the graph -- the labelled copy and the burn-down copy. The first one will store the labels and it's the one that will have the final answer in it. The burn-down copy will get smaller and smaller as we delete unlabeled vertices from it (to find new labelable vertices). (There are other ways to organize this so that only one copy of the graph is used. When you get to the end of understanding this answer, you should find a way to reduce this waste.) Outline:

    label = 0
    while the burndown graph is nonempty
        collect all the leaves in the burndown-graph into the set X
        for each leaf in the set X
            if the leaf does not have a label
                set the leaf's label (to the current value of label)
            delete the leaf from the burn-down graph (these leafs are two copies of the same leaf in the input graph)
        label = label+1
    find the vertex with the largest label and return it

5) If you actually watch this run, you'll notice several opportunities for short-cutting. Including replacing the search on the last line of the outline with a much quicker method for recognizing the answer.

And now for general strategy tips for algorithm problems:
* Do a few small examples by hand. If you don't understand how to do the small cases, there's no way you can jump straight in and tell the computer how to do the large cases.
* If any of the above steps seemed unmotivated or totally opaque, you will need to study much, much harder to do well in Computer Science. It may be the Computer Science is not for you...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文