【问题标题】:Maximum weight of an edge in a cycle in graph图中一个循环中边的最大权重
【发布时间】:2015-01-02 10:06:11
【问题描述】:

如果图中不属于 MST 的边的权重减少,我正在尝试修改最小生成树。我在 stackoverflow 上读到,首先将边连接到 MST 现在MST中只有一个循环,并且通过循环属性可以从MST中删除循环中权重最大的边?如何找到该循环中的最大权重边缘?

【问题讨论】:

  • 你只需要这样做一次吗?
  • 我只需要减少一次边的权重..
  • 您使用哪种算法来查找 MST。还有你使用了哪种数据结构。我需要知道这一点,因为如果您使用了 union-find 数据结构,那么我认为我有更好的解决方案
  • 我还没有实现它,但我会使用 kruskal 和 union find 数据结构。
  • 好的,一旦你将 MST 保存在联合查找数据结构中。假设连接节点 I 和 J 的边的权重减小。现在从节点开始,我向 MST 的根移动,并检查边的权重是否大于节点(I,J)的权重。如果是,那么你就完成了。否则对 Node J 做同样的事情。我认为这是一个很好的解决方案

标签: algorithm graph minimum-spanning-tree


【解决方案1】:

让新添加的边在节点ij之间。节点ij之间正好有一个包含所有节点的循环strong>j,包括他们。同样和以前一样,它是一棵树,从节点 ij 只有一条路径。因此,您可以使用 DFS/BFS 遍历图并计算从 ij 的路径中出现的任何边的最大权重。如果最大权重小于该值新的边权重,不要添加新的。否则删除前一个并添加这个。复杂性将 O(V)。 以下是伪代码,此处 ans[k][0],ans[k][1] 存储节点,如果源节点为 ,则这些节点之间的边具有最大权重>i 和目的地 kans[k][2] 作为该边的权重。

   for all nodes
       mark them unvisited
       mark ans[node][2] as -1
   /*i is the node which is one of the nodes of two of the new edge (i--j) */
   Push node i in queue Q
   mark node i visited
   while Q is not empty
       assign current_node as front element of Q
       pop Q
       for all neighbors of current_node
           if neighbor is unvisited
               mark neighbor visited
               assign w to be maximum of weight of edge (current_node---neighbor) and ans[current_node]
               if w is greater than ans[neighbor]
                  ans[neighbor][2] = w
                  ##Depending on which was max in the the if condition
                  ans[neighbor][0] = current_node/ans[current_node][0]
                  ans[neighbor][1] = neighbor/ans[current_node][1]

               push neighbor in Q  
if weight of edge (i--j) is greater than ans[j][2] 
     don't add the new edge
else 
     remove edge (ans[j][0]---ans[j][1]) and add edge (i--j)

【讨论】:

  • 通过使用 bfs,我必须跟踪每个顶点的父节点,而不是只有我才能获得从一个顶点到另一个顶点的路径。我正确吗?
  • 我的意思是说,一旦我使用 bfs 从 i 到达顶点 j 以获得该路径中边缘的最大权重,我是否需要回溯?
  • 不,你只需要知道当时的最大边缘是多少
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-06
相关资源
最近更新 更多