bfs_successors#

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

从源返回广度优先搜索的后续项的迭代器。

参数
G网络X图表
source结点

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

depth_limit整型,可选(默认值=len(G))

指定最大搜索深度

sort_neighbors功能

接受给定节点的邻居列表作为输入的函数,并返回 迭代器 超过这些邻居,但有定制的订单。

返回
成功:迭代器

(节点、后继器)迭代器 successors 的继任者的非空列表。 node 在广泛的第一次搜索中 source 。出现在迭代器中, node 必须有继任者。

参见

bfs_tree
bfs_edges
edge_bfs

笔记

基于D.eppstein于2004年7月发布的http://www.ics.uci.edu/~eppstein/pads/bfs.py。根据维基百科文章“深度限制搜索”修改允许深度限制。

实例

>>> G = nx.path_graph(3)
>>> print(dict(nx.bfs_successors(G, 0)))
{0: [1], 1: [2]}
>>> H = nx.Graph()
>>> H.add_edges_from([(0, 1), (0, 2), (1, 3), (1, 4), (2, 5), (2, 6)])
>>> print(dict(nx.bfs_successors(H, 0)))
{0: [1, 2], 1: [3, 4], 2: [5, 6]}
>>> G = nx.Graph()
>>> nx.add_path(G, [0, 1, 2, 3, 4, 5, 6])
>>> nx.add_path(G, [2, 7, 8, 9, 10])
>>> print(dict(nx.bfs_successors(G, source=1, depth_limit=3)))
{1: [0, 2], 2: [3, 7], 3: [4], 7: [8]}
>>> G = nx.DiGraph()
>>> nx.add_path(G, [0, 1, 2, 3, 4, 5])
>>> print(dict(nx.bfs_successors(G, source=3)))
{3: [4], 4: [5]}