bfs_tree#

bfs_tree(G, source, reverse=False, depth_limit=None, sort_neighbors=None)[source]#

返回从 source 开始的广度优先搜索构建的有向树。

参数:
GNetworkX 图
source节点

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

reverse布尔值, 可选

如果为 True,则沿反向遍历有向图

depth_limitint, 可选 (默认为 len(G))

指定最大搜索深度

sort_neighbors函数 (默认为 None)

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

返回:
T: NetworkX DiGraph

一个有向树

另请参阅

dfs_tree
bfs_edges
edge_bfs

注意

基于 D. Eppstein 于 2004 年 7 月编写的 http://www.ics.uci.edu/~eppstein/PADS/BFS.py。允许深度限制的修改基于维基百科文章“深度限制搜索”。

示例

>>> G = nx.path_graph(3)
>>> list(nx.bfs_tree(G, 1).edges())
[(1, 0), (1, 2)]
>>> H = nx.Graph()
>>> nx.add_path(H, [0, 1, 2, 3, 4, 5, 6])
>>> nx.add_path(H, [2, 7, 8, 9, 10])
>>> sorted(list(nx.bfs_tree(H, source=3, depth_limit=3).edges()))
[(1, 0), (2, 1), (2, 7), (3, 2), (3, 4), (4, 5), (5, 6), (7, 8)]
----

其他后端实现了此函数

cugraphGPU 加速后端。

sort_neighbors 参数尚未支持。