[英]igraph (maximum) spanning tree is disconnected
I have a graph and want to obtain the maximum spanning tree, therefore I obtain the minimum spanning tree of the graph with inverse weights. 我有一个图,并且想要获得最大的生成树,因此我获得了具有反向权重的图的最小生成树。 However the results gives a disconnected graph.
但是结果给出了一个断开的图。
Below an example of my problem: 下面是我的问题的一个例子:
import igraph
import numpy as np
AM = ([[0, 2, 1], [1, 0, 1], [2, 1, 0]])
g = igraph.Graph.Weighted_Adjacency(AM)
print g.is_connected()
inv_weight = [1./w for w in g.es["weight"]]
print g.spanning_tree(weights=inv_weight).is_connected()
the results is: 结果是:
True
False
how is this possible? 这怎么可能?
Turns out the spanning tree is directed and only weakly connected. 事实证明,生成树是定向的并且仅是弱连接的。 Therefore
因此
g.spanning_tree(weights=inv_weight).is_connected(mode="weak")
returns: 收益:
True
To get a strongly connected tree either of the following lines would work: 要获得强连接的树,可以使用以下任意一种方法:
g = igraph.Graph.Weighted_Adjacency(AM, mode="undirected")
or 要么
T = g.spanning_tree(weights=inv_weight)
T = T.to_undirected()
print T.is_connected()
result is: 结果是:
True
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.