Finding all descendants of node

1,039 views
Skip to first unread message

Ram Rachum

unread,
Mar 23, 2014, 6:49:52 AM3/23/14
to networkx...@googlegroups.com
Hi guys,

I'm a newb at graph theory and networkx, so please excuse my ignorance.

Assume I have a tree in networkx. How do I find all its descendants recursively? (i.e., all its children, and its children's children, etc.)?

Followup question: How do I find all its children recursively that are leaves (i.e. have no children.)


Thanks,
Ram.

Patrick Dolan

unread,
Mar 23, 2014, 1:10:54 PM3/23/14
to networkx...@googlegroups.com
Ram, 

I think you this set of functions. Depth first search and breadth first search: http://networkx.lanl.gov/reference/algorithms.traversal.html

Patrick Dolan
--
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,
Mar 26, 2014, 6:28:23 AM3/26/14
to networkx...@googlegroups.com
Thanks! I'll give it a try. 

Ram Rachum

unread,
Mar 29, 2014, 7:35:18 PM3/29/14
to networkx...@googlegroups.com
I checked it out, and I couldn't figure out which of these functions finds all children, recursively. (Including children of children, and children of children of children, etc.) Which is it? 


Thanks,
Ram.

Ram Rachum

unread,
Mar 29, 2014, 7:38:09 PM3/29/14
to networkx...@googlegroups.com
My mistake, I see that dfs_preorder_nodes provides this. Thanks!


--
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/SukXT4ZHrRk/unsubscribe.
To unsubscribe from this group and all its topics, send an email to networkx-discu...@googlegroups.com.

Ram Rachum

unread,
Mar 29, 2014, 7:52:17 PM3/29/14
to networkx-discuss
Though I wonder, why aren't there bfs versions of these two functions?

Aric Hagberg

unread,
Mar 29, 2014, 8:47:30 PM3/29/14
to networkx...@googlegroups.com
I suppose nobody ever wrote a bfs_nodes() function since it is one
line of python:
In [1]: import networkx as nx

In [2]: G = nx.path_graph(4)

In [3]: [v for u,v in nx.bfs_edges(G,0)]
Out[3]: [1, 2, 3]

But we could consider adding that if that is a commonly used expression.

Aric
Reply all
Reply to author
Forward
0 new messages