generic_bfs_edges#

generic_bfs_edges(G, source, neighbors=None, depth_limit=None, sort_neighbors=None)[source]#

Iterate over edges in a breadth-first search.

The breadth-first search begins at source and enqueues the neighbors of newly visited nodes specified by the neighbors function.

Parameters:
GNetworkX graph
sourcenode

Starting node for the breadth-first search; this function iterates over only those edges in the component reachable from this node.

neighborsfunction

A function that takes a newly visited node of the graph as input and returns an iterator (not just a list) of nodes that are neighbors of that node with custom ordering. If not specified, this is just the``G.neighbors`` method, but in general it can be any function that returns an iterator over some or all of the neighbors of a given node, in any order.

depth_limitint, optional(default=len(G))

Specify the maximum search depth.

sort_neighborsCallable

Deprecated since version 3.2: The sort_neighbors parameter is deprecated and will be removed in version 3.4. A custom (e.g. sorted) ordering of neighbors can be specified with the neighbors parameter.

A function that takes the list of neighbors of a given node as input, and returns an iterator over these neighbors but with a custom ordering.

Yields:
edge

Edges in the breadth-first search starting from source.

Notes

This implementation is from PADS, which was in the public domain when it was first accessed in July, 2004. The modifications to allow depth limits are based on the Wikipedia article “Depth-limited-search”.

Examples

>>> G = nx.path_graph(3)
>>> list(nx.bfs_edges(G, 0))
[(0, 1), (1, 2)]
>>> list(nx.bfs_edges(G, source=0, depth_limit=1))
[(0, 1)]