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. How can I find end node for this?
G.add_edges_from([(2,1),(3,1)] 

and G.in_degree(x)==1from the endnode list comp?G.out_degree(x)==0sufficient to define and end node?