generatornetworkxlowest-common-ancestor

Lowest common ancestor in networkx


How can I get the output of this generator? out.next() or next(out) doesn't work:

out=nx.tree_all_pairs_lowest_common_ancestor(G)
print(out)
<generator object tree_all_pairs_lowest_common_ancestor at 0x000002BE4EF90D48>

Solution

  • nx.tree_all_pairs_lowest_common_ancestor is only aimed at working on certain graph structures, as mentioned in the docs. In the case no root is specified, as in your case, the function will do as follows:

    If root is not specified, find the exactly one node with in degree 0 and use it. Raise an error if none are found, or more than one is. Also check for any nodes with in degree larger than 1, which would imply G is not a tree.

    So it is likely that your function either has multiple root nodes, or there are none, i.e your graph is not a tree. So you can either search locally using a Breadth-first search, or specify the root node of the subtree to operate on in nx.tree_all_pairs_lowest_common_ancestor.