简体   繁体   English

在有向图中查找结束节点

[英]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: 每个只有一个考虑因素:

  • An end-node is one without any "out" edges. 端节点是没有任何“外”边缘的节点。
  • A start-node is one without any "in" edges. 起始节点是没有任何“入”边缘的节点。

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.

相关问题 在有向图中查找所有节点前驱 - Find all node predecessors in a directed graph NetworkX 在有向图中查找特定节点的 root_node - NetworkX find root_node for a particular node in a directed graph Python NetworkX从节点作为根在有向图中找到子图 - Python NetworkX find a subgraph in a Directed Graph from a node as root "如何在有向无环图中找到每个节点的所有父节点?" - How to find all the parent nodes of each node in a directed acyclic graph? 在定向加权图中查找最短路径,该路径访问每个节点,对重新访问节点和边缘没有限制 - Find shortest path in directed, weighted graph that visits every node with no restrictions on revisiting nodes and edges 在有向加权图中,有效地找到两个节点 A 和 B 之间穿过节点 X 的最短路径的成本 - In a directed, weighted graph, efficiently find the cost of the shortest path between two nodes A and B that traverses through a node X Networkx - 在有向图上获取命运节点 - Networkx - Get destiny node on a directed graph 在有向图上使用后继者计数节点属性 - Counting node attributes using succesors on a directed graph 将句子作为节点的定向图usnig Python - Directed Graph with sentences as node usnig Python 有向动态图中单个节点的增长影响 - Growing affect of a single node in a directed dynamic graph
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM