dfs_tree#

dfs_tree(G, source=None, depth_limit=None, *, sort_neighbors=None)[源代码]#

返回从源节点开始的深度优先搜索构建的有向树。

参数:
GNetworkX 图
source节点, 可选

指定深度优先搜索的起始节点。

depth_limit整数, 可选 (默认为 len(G))

指定最大搜索深度。

sort_neighbors函数 (默认为 None)

一个函数,以节点的迭代器作为输入,并返回具有自定义顺序的相同节点的迭代器。例如,sorted 将按升序对节点进行排序。

返回:
TNetworkX 有向图 (DiGraph)

一棵有向树

示例

>>> G = nx.path_graph(5)
>>> T = nx.dfs_tree(G, source=0, depth_limit=2)
>>> list(T.edges())
[(0, 1), (1, 2)]
>>> T = nx.dfs_tree(G, source=0)
>>> list(T.edges())
[(0, 1), (1, 2), (2, 3), (3, 4)]