generic_bfs_edges#
- generic_bfs_edges(G, source, neighbors=None, depth_limit=None)[source]#
迭代广度优先搜索中的边。
广度优先搜索从
source
开始,并将由neighbors
函数指定的新访问节点的邻居加入队列。- 参数:
- GNetworkX 图
- source节点
广度优先搜索的起始节点;此函数仅迭代从此节点可达的组件中的边。
- neighbors函数
一个函数,接受图中的一个新访问节点作为输入,并返回一个该节点的邻居节点的迭代器(不仅仅是列表),具有自定义排序。如果未指定,则默认为
G.neighbors
方法,但通常它可以是任何函数,以任意顺序返回给定节点的部分或全部邻居的迭代器。- depth_limitint, 可选(默认为 len(G))
指定最大搜索深度。
- 返回:
- 边
从
source
开始的广度优先搜索中的边。
注意
此实现来自 PADS,该项目在2004年7月首次访问时属于公共领域。允许设置深度限制的修改基于维基百科文章“深度限制搜索”。
示例
>>> G = nx.path_graph(7) >>> list(nx.generic_bfs_edges(G, source=0)) [(0, 1), (1, 2), (2, 3), (3, 4), (4, 5), (5, 6)] >>> list(nx.generic_bfs_edges(G, source=2)) [(2, 1), (2, 3), (1, 0), (3, 4), (4, 5), (5, 6)] >>> list(nx.generic_bfs_edges(G, source=2, depth_limit=2)) [(2, 1), (2, 3), (1, 0), (3, 4)]
参数
neighbors
可用于通用地指定每个节点邻居的访问顺序。在以下示例中,我们修改默认邻居函数以首先返回奇数节点>>> def odd_first(n): ... return sorted(G.neighbors(n), key=lambda x: x % 2, reverse=True)
>>> G = nx.star_graph(5) >>> list(nx.generic_bfs_edges(G, source=0)) # Default neighbor ordering [(0, 1), (0, 2), (0, 3), (0, 4), (0, 5)] >>> list(nx.generic_bfs_edges(G, source=0, neighbors=odd_first)) [(0, 1), (0, 3), (0, 5), (0, 2), (0, 4)] ----
其他后端实现了此函数
- cugraphGPU 加速后端。
暂不支持
neighbors
参数。