I am trying to find nodes with no outgoing edges in a digraph in networkx.
Is there a way to do this? I found isolates but that finds edges without incoming or outgoing edges, and I don't want that.
nbunch. An nbunch is a single node, container of nodes or None (representing all nodes). It can be a list, set, graph, etc.. To filter an nbunch so that only nodes actually in G appear, use G.
To check if the graph is directed you can use nx. is_directed(G) , you can find the documentation here. 'weight' in G[1][2] # Returns true if an attribute called weight exists in the edge connecting nodes 1 and 2.
A DiGraph stores nodes and edges with optional data, or attributes. DiGraphs hold directed edges. Self loops are allowed but multiple (parallel) edges are not. Nodes can be arbitrary (hashable) Python objects with optional key/value attributes.
If G
is your DiGraph
, you can get an iterator of sinks through
(node for node, out_degree in G.out_degree_iter() if out_degree == 0)
"methods that returned an iterator have been removed"
in NetworkX 2.x, so @fuglede's answer needs a minor update:
(node for node, out_degree in G.out_degree() if out_degree == 0)
The view/reporting API provided by .out_degree in 2.x provides an OutDegreeView over (node, out_degree)
pairs, making this approach slightly simpler than @Fony Lew's.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With