Chu-Liu Edmond关于有向图最小生成树的算法 [英] Chu-Liu Edmond's algorithm for Minimum Spanning Tree on Directed Graphs
问题描述
我想在加权有向图上找到最小生成树(MST)。我一直在尝试使用我在Python中实现的 Chu-Liu / Edmond算法 (代码如下)。可以在此处找到简单明了的算法说明。我有两个问题。
-
Edmond算法是否可以保证收敛于一个解决方案?
我担心删除一个循环会增加另一个循环。如果发生这种情况,算法将继续尝试永久删除循环。
我似乎找到了一个发生这种情况的例子。输入图如下所示(在代码中)。该算法永远不会完成,因为它在循环[1,2]和[1,3]以及[5,4]和[5,6]之间切换。添加到图表中以解决循环的边缘[5,4]创建循环[5,6],反之亦然,对于[1,2]和[1,3]也是如此。
我应该注意,我不确定我的实现是否正确。
为了解决这个问题,我引入了一个临时补丁。当一个边被移除以去除一个循环时,我会从我们正在搜索MST的底层图G中永久删除该边。因此,该边缘不能再次添加,这应该防止算法卡住。有了这个改变,我保证会找到一个MST吗?
我怀疑可以找到一个病态的情况,这个步骤会导致一个不是MST的结果,但我一直没有想到。它似乎适用于我尝试过的所有简单测试用例。
代码:
import sys
#--------------------- -------------------------------------------------- ----------#
def _reverse(graph):
r = {}
用于图中的src:
for(dst,c )in graph [src] .items():
if dst in r:
r [dst] [src] = c
else:
r [dst] = {src:c }
return r
#使用Tarjan算法查找图中的所有循环
def strong_connected_components(graph):
Tarjan算法它的发现者Robert Tarjan)是一个图论算法
,用于查找图的强连通分量
基于:http://en.wikipedia.org/wiki/Tarjan% 27s_strongly_connected_components_algorithm
index_counter = [0]
stack = []
lowlinks = { }
index = {}
result = []
def strongconnect(节点):
#将此节点的深度索引设置为最小的未使用索引
index [node] = index_counter [0]
lowlinks [node] = index_counter [0]
index_counter [0] + = 1
stack.append(node)
#考虑`node`的后继者
try:
后继者= graph [节点]
除了:
后继者= []
继任者:
如果继任者不在低链接:
#继任者尚未被访问;对它进行递推
strongconnect(后继)
lowlinks [节点] = min(低链接[节点],低链接[后继])
elif后继栈:
#后继者在堆栈,因此在当前强连通组件(SCC)
lowlinks [node] = min(lowlinks [node],index [后继])
#如果`node`是一个根节点,弹出堆栈并生成一个SCC
if lowlinks [node] == index [node]:
connected_component = []
而真:
后继者= stack.pop()
connected_component.append(后继)
if后继==节点:break
component = tuple(connected_component)
#存储结果
结果。附加(组件)
用于图中的节点:$ b $ b如果节点不在低链接中:
strongconnect(节点)
返回结果
def _mergeCycles(cycle,G,RG,g,rg):
allInEdges = []#从外部循环进入循环的所有边界
minInternal = None
minInternalWeight = sys.maxint
#寻找最小内部边缘权重
for循环中:
for e中RG [n]:
如果循环中有e:
如果minInternal是None或RG [n] [e]< minInternalWeight:
minInternal =(n,e)
minInternalWeight = RG [n] [e]
continue
else:
allInEdges.append((n,e) )#边缘进入循环
#以最小修改成本查找输入边
#修改成本c(i,k)= c(i,j) - (c(x_j,j) - min {j}(c(x_j,j)))
minExternal = None
minModifiedWeight = 0
在allInEdges中为j,i:#j为循环中的顶点,i为候选顶点外循环
xj,weight_xj_j = rg [j] .popitem()#xj是循环中的顶点,当前转到j
rg [j] [xj] = weight_xj_j#将项目放回字典$ b (c(x_j,j))= c(x_j,j)-min {j} = c(i,j) ))
如果minExternal为None或w <= minModifiedWeight:
minExternal =(j,i)
minModifiedWeight = w
w = RG [minExternal [0] ] [minExtern al [1]]#进入循环的边的权重
xj,_ = rg [minExternal [0]]。popitem()#xj是当前转到j
rem =(minExternal [ 0],xj)#边缘删除
rg [minExternal [0]]。clear()#popitem()应该删除一条边到j,但我们确保
#删除违反边缘从RG
#RG [minExternal [0]]。pop(xj,None)#高度实验。扔掉违规边缘,所以我们永远不会再获得它
如果rem [1]在g:
如果rem [0]在g [rem [1]]中:
如果minExternal [1]在g:
g [minExternal [1]] [minExternal [0]] = w
中,则删除[rem [1]] [rem [0]]
else:
g [minExternal [1]] = {minExternal [0]:w}
rg = _reverse(g)
#-------- -------------------------------------------------- -----------------------#
def mst(root,G):
Chu- Liu / Edmond算法
参数:
root - MST的根源
G - MST所在的图形
返回:MST
的图形表示形式与以下示例中的图形表示形式相同:
http://code.activestate.com/recipes/119466/
解释在这里被逐字复制:
假设输入图G具有以下
表示:一个顶点可以是任何可以为
的对象被用作字典的索引。 G是一个
字典,由顶点索引。对于任何顶点v,
G [v]本身就是一个字典,由v的邻居
索引。对于任何边v-> w,G [v] [w]是长度
边缘。
RG = _reverse(G)
g = {}
在RG中为n:
如果len(RG [ n])== 0:
继续
最小值= sys.maxint
s,d = None,None
用于RG中的e [n]:
如果RG [n] [e] <最小值:
最小值= RG [n] [e]
s,d = n,e
如果是g:
g [d] [s] = RG [s] [d]
else:
g [d] = {s:RG [s] [d]}
周期= [列表(c)for c in strong_connected_components(g)]
cycles_exist = True
while cycles_exist:
cycles_exist = False
cycles =如果循环中有root,则
$ [$ c
继续
如果len(循环)== 1:
continue
_mergeCycles(cycle,G, RG,g,rg)
cycles_exist = True
返回g
#------------------ -------------------------------------------------- -------------#
if __name__ ==__main__:
#作用于
的输入示例root = 0
g = {0:{1:23,2:22,3:22},1:{2:1,3:1},3:{1:1,2:0}}
#导致无限循环的输入示例
root = 0
g = {0:{1:17,2:16,3:19,4:16,5 :16:6:18},1:{2:3,3:3,4:11,5:10,6:12},2:{1:3,3:4,4:8,5:8 ,6:11},3:{1:3,2:4,4:12,5:11,6:14},4:{1:11,2:8,3:12,5:6,6 :10},5:{1:10,2:8,3:11,4:6,6:4},6:{1:12,2:11,3:14,4:10,5:4 }}
h = mst(int(root),g)
print h
for s in h:
for t in h [s]:
print%d-%d%(s,t)
不要做临时补丁。我承认实施收缩/不收敛逻辑并不直观,在某些情况下递归是不可取的,所以这里有一个合适的Python实现,可以提高生产质量。我们将其推迟到最后并使用深度优先搜索,而不是在每个递归级别执行不收敛步骤,从而避免递归。 (这种修改的正确性最终来自互补松弛,这是线性规划理论的一部分。)
下面的命名约定是 _rep
表示一个超级节点(即一个或多个签约节点的块)。
#!/ usr / bin / env python3
from collections import defaultdict,namedtuple
Arc = namedtuple('Arc',('tail','weight','head'))
def min_spanning_arborescence(弧线,汇点):
good_arcs = []
quotient_map = {arc.tail:arc.tail弧线弧线}
quotient_map [sink] = sink
,而True:
min_arc_by_tail_rep = {}
successor_rep = {}
圆弧弧:
如果arc.tail == sink:
continue
tail_rep = quotient_map [arc.tail]
head_rep = quotient_map [arc.head]
if tail_rep == head_rep:
conti如果tail_rep不在min_arc_by_tail_rep或min_arc_by_tail_rep [tail_rep] .weight>中,则为
; arc.weight:
如果cycle_reps为None,则为
:min_arc_by_tail_rep [tail_rep] = arc
successor_rep [tail_rep] = head_rep
cycle_reps = find_cycle(successor_rep,sink)
good_arcs
返回spanning_arborescence(good_arcs,汇)
good_arcs.extend(min_arc_by_tail_rep [cycle_rep]为cycle_rep中的cycle_rep)
cycle_rep_set = set(cycle_reps)
cycle_rep = cycle_rep_set.pop()
quotient_map = {node:cycle_rep if node_rep in cycle_rep_set else node_rep for node,node_rep in quotient_map.items()}
def find_cycle(后继节点,接收节点):
visited = {sink}
for后继节点:$ b $ b cycle = []
不在拜访节点:$ b $ b visited.add(节点)
cycle.append(node)
node = successor [node]
如果节点在循环中:
return cycl e [cycle.index(节点):]
返回无
def spanning_arborescence(弧,汇):
arcs_by_head = defaultdict(列表)
对于弧中的弧线:
if arc.tail == sink:
continue
arcs_by_head [arc.head] .append(arc)
solution_arc_by_tail = {}
stack = arcs_by_head [sink]
stack:
arc = stack.pop()
如果在solution_arc_by_tail中为arc.tail:
continue
solution_arc_by_tail [arc.tail] = (弧(1,17,0),圆弧(1),圆弧(1),圆弧(1),圆弧(1)), (2),16,0),弧(3,19,0),弧(4,16,0),弧(5,16,0),弧(6,18,0),弧(2,3,1) ),弧(3,3,1),弧(4,11,1),弧(5,10,1),弧(6,12,1),弧(1,3,2),弧(3,1) ,4,2),弧(4,8,2),弧(5,8,2),弧(6,11,2),弧(1,3,3),弧(2,4,3) ,弧(4,12,3),弧(5,11,3),弧(6,14,3),弧(1,11,4),弧(2,8,4),Ar (3,12,4),弧(5,6,4),弧(6,10,4),弧(1,10,5),弧(2,8,5),弧(3,11 ,5),弧(4,6,5),弧(6,4,5),弧(1,12,6),弧(2,11,6),弧(3,14,6),弧(4,10,6),弧(5,4,6)],0))
I would like to find a minimum spanning tree (MST) on a weighted directed graph. I have been trying to use Chu-Liu/Edmond's algorithm, which I have implemented in Python (code below). A simple, clear description of the algorithm can be found here. I have two questions.
Is Edmond's algorithm guaranteed to converge on a solution?
I am concerned that removing a cycle will add another cycle. If this happens, the algorithm will continue trying to remove cycles forever.
I seem to have found an example where this happens. The input graph is shown below (in the code). The algorithm never finishes because it switches between cycles [1,2] and [1,3], and [5,4] and [5,6]. The edge added to the graph to resolve the cycle [5,4] creates cycle [5,6] and vice versa, and similarly for [1,2] and [1,3].
I should note that I am not certain that my implementation is correct.
To resolve this issue, I introduced an ad hoc patch. When an edge is removed to remove a cycle, I permanently remove that edge from the underlying graph G on which we are searching for an MST. Consequently, that edge cannot be added again and this should prevent the algorithm from getting stuck. With this change, am I guaranteed to find an MST?
I suspect that one can find a pathological case where this step will lead to a result that is not an MST, but I have not been able to think of one. It seems to work on all the simple test cases that I have tried.
Code:
import sys
# --------------------------------------------------------------------------------- #
def _reverse(graph):
r = {}
for src in graph:
for (dst,c) in graph[src].items():
if dst in r:
r[dst][src] = c
else:
r[dst] = { src : c }
return r
# Finds all cycles in graph using Tarjan's algorithm
def strongly_connected_components(graph):
"""
Tarjan's Algorithm (named for its discoverer, Robert Tarjan) is a graph theory algorithm
for finding the strongly connected components of a graph.
Based on: http://en.wikipedia.org/wiki/Tarjan%27s_strongly_connected_components_algorithm
"""
index_counter = [0]
stack = []
lowlinks = {}
index = {}
result = []
def strongconnect(node):
# set the depth index for this node to the smallest unused index
index[node] = index_counter[0]
lowlinks[node] = index_counter[0]
index_counter[0] += 1
stack.append(node)
# Consider successors of `node`
try:
successors = graph[node]
except:
successors = []
for successor in successors:
if successor not in lowlinks:
# Successor has not yet been visited; recurse on it
strongconnect(successor)
lowlinks[node] = min(lowlinks[node],lowlinks[successor])
elif successor in stack:
# the successor is in the stack and hence in the current strongly connected component (SCC)
lowlinks[node] = min(lowlinks[node],index[successor])
# If `node` is a root node, pop the stack and generate an SCC
if lowlinks[node] == index[node]:
connected_component = []
while True:
successor = stack.pop()
connected_component.append(successor)
if successor == node: break
component = tuple(connected_component)
# storing the result
result.append(component)
for node in graph:
if node not in lowlinks:
strongconnect(node)
return result
def _mergeCycles(cycle,G,RG,g,rg):
allInEdges = [] # all edges entering cycle from outside cycle
minInternal = None
minInternalWeight = sys.maxint
# Find minimal internal edge weight
for n in cycle:
for e in RG[n]:
if e in cycle:
if minInternal is None or RG[n][e] < minInternalWeight:
minInternal = (n,e)
minInternalWeight = RG[n][e]
continue
else:
allInEdges.append((n,e)) # edge enters cycle
# Find the incoming edge with minimum modified cost
# modified cost c(i,k) = c(i,j) - (c(x_j, j) - min{j}(c(x_j, j)))
minExternal = None
minModifiedWeight = 0
for j,i in allInEdges: # j is vertex in cycle, i is candidate vertex outside cycle
xj, weight_xj_j = rg[j].popitem() # xj is vertex in cycle that currently goes to j
rg[j][xj] = weight_xj_j # put item back in dictionary
w = RG[j][i] - (weight_xj_j - minInternalWeight) # c(i,k) = c(i,j) - (c(x_j, j) - min{j}(c(x_j, j)))
if minExternal is None or w <= minModifiedWeight:
minExternal = (j,i)
minModifiedWeight = w
w = RG[minExternal[0]][minExternal[1]] # weight of edge entering cycle
xj,_ = rg[minExternal[0]].popitem() # xj is vertex in cycle that currently goes to j
rem = (minExternal[0], xj) # edge to remove
rg[minExternal[0]].clear() # popitem() should delete the one edge into j, but we ensure that
# Remove offending edge from RG
# RG[minExternal[0]].pop(xj, None) #highly experimental. throw away the offending edge, so we never get it again
if rem[1] in g:
if rem[0] in g[rem[1]]:
del g[rem[1]][rem[0]]
if minExternal[1] in g:
g[minExternal[1]][minExternal[0]] = w
else:
g[minExternal[1]] = { minExternal[0] : w }
rg = _reverse(g)
# --------------------------------------------------------------------------------- #
def mst(root,G):
""" The Chu-Liu/Edmond's algorithm
arguments:
root - the root of the MST
G - the graph in which the MST lies
returns: a graph representation of the MST
Graph representation is the same as the one found at:
http://code.activestate.com/recipes/119466/
Explanation is copied verbatim here:
The input graph G is assumed to have the following
representation: A vertex can be any object that can
be used as an index into a dictionary. G is a
dictionary, indexed by vertices. For any vertex v,
G[v] is itself a dictionary, indexed by the neighbors
of v. For any edge v->w, G[v][w] is the length of
the edge.
"""
RG = _reverse(G)
g = {}
for n in RG:
if len(RG[n]) == 0:
continue
minimum = sys.maxint
s,d = None,None
for e in RG[n]:
if RG[n][e] < minimum:
minimum = RG[n][e]
s,d = n,e
if d in g:
g[d][s] = RG[s][d]
else:
g[d] = { s : RG[s][d] }
cycles = [list(c) for c in strongly_connected_components(g)]
cycles_exist = True
while cycles_exist:
cycles_exist = False
cycles = [list(c) for c in strongly_connected_components(g)]
rg = _reverse(g)
for cycle in cycles:
if root in cycle:
continue
if len(cycle) == 1:
continue
_mergeCycles(cycle, G, RG, g, rg)
cycles_exist = True
return g
# --------------------------------------------------------------------------------- #
if __name__ == "__main__":
# an example of an input that works
root = 0
g = {0: {1: 23, 2: 22, 3: 22}, 1: {2: 1, 3: 1}, 3: {1: 1, 2: 0}}
# an example of an input that causes infinite cycle
root = 0
g = {0: {1: 17, 2: 16, 3: 19, 4: 16, 5: 16, 6: 18}, 1: {2: 3, 3: 3, 4: 11, 5: 10, 6: 12}, 2: {1: 3, 3: 4, 4: 8, 5: 8, 6: 11}, 3: {1: 3, 2: 4, 4: 12, 5: 11, 6: 14}, 4: {1: 11, 2: 8, 3: 12, 5: 6, 6: 10}, 5: {1: 10, 2: 8, 3: 11, 4: 6, 6: 4}, 6: {1: 12, 2: 11, 3: 14, 4: 10, 5: 4}}
h = mst(int(root),g)
print h
for s in h:
for t in h[s]:
print "%d-%d" % (s,t)
Don't do ad hoc patches. I concede that implementing the contraction/uncontraction logic is not intuitive, and recursion is undesirable in some contexts, so here's a proper Python implementation that could be made production quality. Rather than perform the uncontraction step at each recursive level, we defer it to the end and use depth-first search, thereby avoiding recursion. (The correctness of this modification follows ultimately from complementary slackness, part of the theory of linear programming.)
The naming convention below is that _rep
signifies a supernode (i.e., a block of one or more contracted nodes).
#!/usr/bin/env python3
from collections import defaultdict, namedtuple
Arc = namedtuple('Arc', ('tail', 'weight', 'head'))
def min_spanning_arborescence(arcs, sink):
good_arcs = []
quotient_map = {arc.tail: arc.tail for arc in arcs}
quotient_map[sink] = sink
while True:
min_arc_by_tail_rep = {}
successor_rep = {}
for arc in arcs:
if arc.tail == sink:
continue
tail_rep = quotient_map[arc.tail]
head_rep = quotient_map[arc.head]
if tail_rep == head_rep:
continue
if tail_rep not in min_arc_by_tail_rep or min_arc_by_tail_rep[tail_rep].weight > arc.weight:
min_arc_by_tail_rep[tail_rep] = arc
successor_rep[tail_rep] = head_rep
cycle_reps = find_cycle(successor_rep, sink)
if cycle_reps is None:
good_arcs.extend(min_arc_by_tail_rep.values())
return spanning_arborescence(good_arcs, sink)
good_arcs.extend(min_arc_by_tail_rep[cycle_rep] for cycle_rep in cycle_reps)
cycle_rep_set = set(cycle_reps)
cycle_rep = cycle_rep_set.pop()
quotient_map = {node: cycle_rep if node_rep in cycle_rep_set else node_rep for node, node_rep in quotient_map.items()}
def find_cycle(successor, sink):
visited = {sink}
for node in successor:
cycle = []
while node not in visited:
visited.add(node)
cycle.append(node)
node = successor[node]
if node in cycle:
return cycle[cycle.index(node):]
return None
def spanning_arborescence(arcs, sink):
arcs_by_head = defaultdict(list)
for arc in arcs:
if arc.tail == sink:
continue
arcs_by_head[arc.head].append(arc)
solution_arc_by_tail = {}
stack = arcs_by_head[sink]
while stack:
arc = stack.pop()
if arc.tail in solution_arc_by_tail:
continue
solution_arc_by_tail[arc.tail] = arc
stack.extend(arcs_by_head[arc.tail])
return solution_arc_by_tail
print(min_spanning_arborescence([Arc(1, 17, 0), Arc(2, 16, 0), Arc(3, 19, 0), Arc(4, 16, 0), Arc(5, 16, 0), Arc(6, 18, 0), Arc(2, 3, 1), Arc(3, 3, 1), Arc(4, 11, 1), Arc(5, 10, 1), Arc(6, 12, 1), Arc(1, 3, 2), Arc(3, 4, 2), Arc(4, 8, 2), Arc(5, 8, 2), Arc(6, 11, 2), Arc(1, 3, 3), Arc(2, 4, 3), Arc(4, 12, 3), Arc(5, 11, 3), Arc(6, 14, 3), Arc(1, 11, 4), Arc(2, 8, 4), Arc(3, 12, 4), Arc(5, 6, 4), Arc(6, 10, 4), Arc(1, 10, 5), Arc(2, 8, 5), Arc(3, 11, 5), Arc(4, 6, 5), Arc(6, 4, 5), Arc(1, 12, 6), Arc(2, 11, 6), Arc(3, 14, 6), Arc(4, 10, 6), Arc(5, 4, 6)], 0))
这篇关于Chu-Liu Edmond关于有向图最小生成树的算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!