图中循环中边的最大权重

时间:2015-01-02 10:06:12

标签: algorithm graph minimum-spanning-tree

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

1 个答案:

答案 0 :(得分:1)

让新边缘添加到节点 i j 之间。将只有一个周期包含节点 i 和<之间的所有节点strong> j ,包括他们。与之前一样,树只有一条路径从节点 i j 。因此,您可以使用DFS / BFS遍历图形并计算从 i j 的路径中出现的任何边的最大权重。如果最大权重小于新的边缘权重,不添加新的。删除前一个并添加这个。复杂性 O(V)。 以下是伪代码,此处 ans [k] [0],ans [k] [1] 存储节点,使得如果源节点 i 和目的地 k ans [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)