[英]Spanning tree of directed graph with networkx
I have a directed graph G in networkx and I want to get the minimum spanning tree of it. 我在networkx中有一个有向图G,我想得到它的最小生成树。 I do:
我做:
T = nx.algorithms.minimum_spanning_tree( G.to_undirected() )
This is undirected and I would like to restore the directions but I don't know how to do it. 这是无向的,我想恢复方向,但我不知道该怎么做。 I tried:
我试过了:
G[T.edges()]
This last line looks very pythonic but this is not the way networkx works, apparently... Does anyone knows how to do it? 这最后一行看起来非常pythonic但这不是networkx的工作方式,显然......有谁知道怎么做?
In other words: How can I get the subgraph of a directed tree given the (undirected) edges ? 换句话说:如何给出(无向)边缘的有向树的子图?
You can get the edges in G
that appear in the MST T
with a simple comprehension: 您可以通过简单的理解获得出现在MST
T
中的G
边缘:
E = set(T.edges()) # optimization
[e for e in G.edges() if e in E or reversed(e) in E]
You can then build a new graph from this. 然后,您可以从中构建新图表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.