简体   繁体   中英

NetworkX recursive child nodes

Using the latest NetworkX ( version 1.11 at the time of writing), what is the most efficient way of retrieving all (recursive) child nodes for a given node?
The successors() function retrieves the immediate children and in previous versions there was dfs_preorder_nodes solution .

there is descendants method which will find all children nodes, including children of every child:

 1-2 \\ 3 \\ 4
import networkx as nx

G = nx.DiGraph()
G.add_edge(1, 2)
G.add_edge(1, 3)
G.add_edge(2, 4)
res = nx.descendants(G, 1)
print (res) #{2,3,4}

dfs_preorder_nodes is still valid in the latest version as pointed out by Joel. The documentation is available here .

The following achieves the desired, ie all recursive children for a node of interest:

import networkx as nx 
children = [node for node in nx.dfs_preorder_nodes(network, queryID)]`

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM