简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM