祖先#
- ancestors(G, source)[source]#
返回在图
G
中到source
有路径的所有节点。- 参数:
- GNetworkX 图
- source图
G
中的节点
- 返回:
- set()
在图
G
中source
的祖先节点
- 抛出异常:
- NetworkXError
如果节点
source
不在图G
中。
另请参阅
示例
>>> 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] ----