networkx 中的最低共同祖先

Lowest common ancestor in networkx

我怎样才能得到这个生成器的输出? out.next()next(out) 不起作用:

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

nx.tree_all_pairs_lowest_common_ancestor 仅针对某些图形结构,如文档中所述。在没有指定 root 的情况下,如您的情况,函数将执行以下操作:

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.

所以您的函数很可能有多个 root 节点,或者有 none,即您的图不是树。因此,您可以使用 Breadth-first search 在本地搜索,或者在 nx.tree_all_pairs_lowest_common_ancestor.

中指定要操作的子树的根节点