祖先#

ancestors(G, source)[source]#

返回在图 G 中到 source 有路径的所有节点。

参数:
GNetworkX 图
sourceG 中的节点
返回:
set()

在图 Gsource 的祖先节点

抛出异常:
NetworkXError

如果节点 source 不在图 G 中。

另请参阅

descendants

示例

>>> DG = nx.path_graph(5, create_using=nx.DiGraph)
>>> sorted(nx.ancestors(DG, 2))
[0, 1]

节点 source 不是自身的祖先,但可以手动包含

>>> sorted(nx.ancestors(DG, 2) | {2})
[0, 1, 2]
----

其他后端实现了此函数

cugraph : GPU加速后端。

graphblas : 启用OpenMP的稀疏线性代数后端。