bfs_layers#

bfs_layers(G, sources)[source]#

Returns an iterator of all the layers in breadth-first search traversal.

Parameters:
GNetworkX graph

A graph over which to find the layers using breadth-first search.

sourcesnode in G or list of nodes in G

Specify starting nodes for single source or multiple sources breadth-first search

Yields:
layer: list of nodes

Yields list of nodes at the same distance from sources

Examples

>>> G = nx.path_graph(5)
>>> dict(enumerate(nx.bfs_layers(G, [0, 4])))
{0: [0, 4], 1: [1, 3], 2: [2]}
>>> H = nx.Graph()
>>> H.add_edges_from([(0, 1), (0, 2), (1, 3), (1, 4), (2, 5), (2, 6)])
>>> dict(enumerate(nx.bfs_layers(H, [1])))
{0: [1], 1: [0, 3, 4], 2: [2], 3: [5, 6]}
>>> dict(enumerate(nx.bfs_layers(H, [1, 6])))
{0: [1, 6], 1: [0, 3, 4, 2], 2: [5]}