bfs_tree#
- bfs_tree(G, source, reverse=False, depth_limit=None, sort_neighbors=None)[源代码]#
返回从源位置开始的宽度优先搜索构造的定向树。
- 参数
- G网络X图表
- source结点
指定广度优先搜索的起始节点
- reverse布尔值,可选
如果为True,则以相反方向遍历有向图
- depth_limit整型,可选(默认值=len(G))
指定最大搜索深度
- sort_neighbors功能
接受给定节点的邻居列表作为输入的函数,并返回 迭代器 超过这些邻居,但有定制的订单。
- 返回
- T:网络X有向图
有方向的树
参见
dfs_tree
bfs_edges
edge_bfs
笔记
根据D.eppstein于2004年7月发布的http://www.ics.uci.edu/~eppstein/pads/bfs.py。根据维基百科的文章“深度限制搜索”修改允许深度限制。
实例
>>> G = nx.path_graph(3) >>> print(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]) >>> print(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)]