【发布时间】:2012-05-01 21:35:46
【问题描述】:
这是消费税
假设给定图 G 的最小生成树 T (有 n 个顶点和 m 个边)和一个权重为 w 的新边 e = (u, v) 我们将添加到 G 中。给出一个有效的算法来找到最小值 图 G + e 的生成树。 您的算法应该在 O(n) 中运行 是时候获得全额学分了。
我有这个想法:
In the MST, just find out the path between u and v. Then find the edge (along the path) with maximum weight; if the maximum weight is bigger than w, then remove that edge from the MST and add the new edge to the MST.
棘手的部分是如何在 O(n) 时间内做到这一点,而且我也被卡住了。
问题是如何存储 MST。在普通 Prim 算法中,MST 存储为父数组,即每个元素都是相应顶点的父节点。
那么假设消费税给了我一个指示 MST 的父数组,我怎样才能在 O(n) 中释放上述算法?
首先,如何从父数组中识别 u 和 v 之间的路径?我可以为 u 和 v 设置两个祖先数组,然后检查共同祖先,然后我可以获得路径,尽管是向后的。我觉得这部分,要找到共同的祖先,至少得在O(n^2)内完成吧?
然后,我们有了路径。但是我们仍然需要找到沿路径的每条边的权重。由于我认为该图将使用 Prim 算法的邻接列表,因此我们必须执行 O(m)(m 是边数)来定位边的每个权重。
...
所以我认为不可能在 O(n) 中执行该算法。我错了吗?
【问题讨论】:
标签: algorithm data-structures graph minimum-spanning-tree