简体   繁体   English

在有向无环网络的组件中获得有序节点x DiGraph

[英]get ordered nodes in components of directed acyclic networkx DiGraph

I have a directed graph with subgraphs where the order of nodes is important. 我有一个带有子图的有向图,其中节点的顺序很重要。

Example my graph would have two subgraphs, all linear 1-->2-->3 & 9-->8-->7-->6 示例我的图将有两个子图,所有线性1-->2-->3 & 9-->8-->7-->6

note: the node names will be random and unique, no cycles in the graphs 注意:节点名称将是随机且唯一的,图中没有循环

NG = nx.DiGraph()
NG.add_edges_from([(1,2),(2,3)])
NG.add_edges_from([(9,8),(8,7),(7,6)])

I need to get the subgraphs or a list of nodes in the subgraphs with the nodes ordered according to their connection. 我需要获取子图中的子图或节点列表,其中节点根据其连接进行排序。

I've tried 我试过了

[i.nodes() for i in list(nx.weakly_connected_component_subgraphs(NG))]

resulting in list of nodelists, almost right, but not ordered according to their connections: 导致节点列表的列表,几乎是正确的,但没有根据它们的连接排序:

[[1, 2, 3], [8, 9, 6, 7]]

How would I need to proceed to get the list of ordered nodelists. 我如何才能获得有序节点列表的列表。 ie: 即:

[[1, 2, 3], [9, 8, 7, 6]]

I'm assuming that you are sure these are "Directed Acyclic Graphs". 我假设你确定这些是“有向无环图”。

Then nx.topological_sort(G) sorts the nodes in a graph according to the order you want: that is, if u appears before v , that means there is no path from v to u (but possibly a path from u to v ). 然后nx.topological_sort(G)根据您想要的顺序对图中的节点进行排序:也就是说,如果u出现在v之前,则表示没有从vu的路径(但可能是从uv的路径)。 If you use nx.weakly_connected_component_subgraphs(G) you get a generator for the subgraphs you're interested in. So the approach I recommend is to first find the subgraphs and then sort the nodes. 如果使用nx.weakly_connected_component_subgraphs(G)你会得到一个你感兴趣的子图的生成器。所以我建议的方法是首先找到子图然后对节点进行排序。

[nx.topological_sort(H) for H in nx.weakly_connected_component_subgraphs(NG)]

Should get what you want. 应该得到你想要的。


An alternate more efficient method: 另一种更有效的方法:

The previous code will not be the most efficient code possible, but it is simple. 以前的代码不是最有效的代码,但很简单。 Creating each subgraph H takes time that could be avoided. 创建每个子图H需要时间,可以避免。 A more efficient method would do weakly_connected_components(NG) (which generates lists of the nodes in each subgraph rather than the subgraph themselves). 一个更有效的方法是做weakly_connected_components(NG) (它生成每个子图中的节点列表而不是子图本身)。 This would create lists like you have already. 这将创建像您一样的列表。 Then you do the topological sort: 然后你做拓扑排序:

[nx.topological_sort(NG, nbunch=subgraph_nodes) for subgraph_nodes in nx.weakly_connected_components(NG)]

This will be more efficient. 这样会更有效率。 What it does is first it generates the list of nodes in each component. 它的作用首先是它生成每个组件中的节点列表。 Then it goes through sorting the nodes in each component. 然后,它将对每个组件中的节点进行排序。 If you do this, you should take a look at the source for topological_sort . 如果你这样做,你应该看一下topological_sort来源 It will return a sorted list of all nodes that are reachable from any node it nbunch . 它将返回所有节点的排序列表,这些节点可以从它nbunch任何节点nbunch In your case this is just nbunch , but more generally it does not just have to return nbunch . 在你的情况下,这只是nbunch ,但更普遍的是它不仅仅需要返回nbunch


Note: in your code there is no need for list(...) . 注意:在您的代码中不需要list(...) You would get the same result without it. 没有它你会得到相同的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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