Networkx graph searches: dfs_successors vs. dfs_predecessors

流过昼夜 提交于 2019-12-01 06:23:58

The dfs_predecessors() function only gives the immediate predecessor. So if you say this (DFS of G from node 'n' and looking back one link from 'n22')

>>> print(networkx.dfs_predecessors(G, 'n')['n221'])
n22

you get part of what you want.

To get the path in the DFS tree from n221 back to the root:

>>> T = networkx.dfs_tree(G,'n')

>>> print(networkx.shortest_path(G.reverse(),'n221','n'))
['n221', 'n22', 'n2', 'n']
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!