[英]Find end node in directed graph
Im a beginner at networkx. 我是networkx的初学者。 I have many subgraphs like this one: 我有很多像这样的子图:
import networkx as nx
G = nx.DiGraph()
G.add_edges_from([(2,1),(3,1),(1,4)])
nx.draw(G)
I want to find all start and endnodes. 我想找到所有的开始和结束节点。 So I use: 所以我使用:
startnodes = [x for x in G.nodes() if G.out_degree(x)==1 and G.in_degree(x)==0]
endnode = [x for x in G.nodes() if G.out_degree(x)==0 and G.in_degree(x)==1][0]
print(startnodes, endnode)
[2, 3] 4
But some of the subgraphs look like below, with 2 in degrees for end node. 但是一些子图看起来如下,端节点有2度。 How can I find end node for this? 我怎样才能找到终端节点?
G.add_edges_from([(2,1),(3,1)]
There is only one consideration for each: 每个只有一个考虑因素:
That's all. 就这样。 If you need to identify your sub-graphs as well, then simply perform any of the standard closure operations from each start node. 如果您还需要识别子图,则只需从每个起始节点执行任何标准闭包操作。 Where those sub-graphs have common nodes, merge the sub-graphs. 在这些子图具有公共节点的情况下,合并子图。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.