Networkx graph searches: dfs_successors vs. dfs_predecessors

前端 未结 1 1046
借酒劲吻你
借酒劲吻你 2021-01-13 18:17

Consider the following graph structure (borrowed from this question):

G = networkx.DiGraph()
G.add_edges_from([(\'n\', \'n1\'), (\'n\', \'n2\'), (\'n\', \'n3         


        
相关标签:
1条回答
  • 2021-01-13 18:23

    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']
    
    0 讨论(0)
提交回复
热议问题