Finding all leaves in a DiGraph

3,322 views
Skip to first unread message

Ram Rachum

unread,
Jun 22, 2014, 8:17:29 PM6/22/14
to networkx...@googlegroups.com
How can I find all the leaves in a DiGraph? (i.e. nodes that have edges pointing at them but that point at no other node.)

Kechao Cai

unread,
Jun 22, 2014, 10:58:04 PM6/22/14
to networkx...@googlegroups.com
Maybe I can give you a simple example.

import networkx as nx
G = nx.DiGraph()
G.add_path([0,1,2,3])
leaf_nodes = [node for node in G.nodes() if G.in_degree(node)!=0 and G.out_degree(node)==0]

# According to your definition for leaf node, the leaf_nodes should be node 3.


On Mon, Jun 23, 2014 at 8:17 AM, Ram Rachum <ram.r...@gmail.com> wrote:
How can I find all the leaves in a DiGraph? (i.e. nodes that have edges pointing at them but that point at no other node.)

--
You received this message because you are subscribed to the Google Groups "networkx-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to networkx-discu...@googlegroups.com.
To post to this group, send email to networkx...@googlegroups.com.
Visit this group at http://groups.google.com/group/networkx-discuss.
For more options, visit https://groups.google.com/d/optout.

Ram Rachum

unread,
Jun 23, 2014, 7:12:38 AM6/23/14
to networkx-discuss
So this is the simplest way? I was hoping for something like digraph.leaves.


--
You received this message because you are subscribed to a topic in the Google Groups "networkx-discuss" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/networkx-discuss/odh8Jhni08g/unsubscribe.
To unsubscribe from this group and all its topics, send an email to networkx-discu...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages