code: def bfs_layers(G, sources):
      """Returns an iterator of all the layers in breadth-first search traversal.
  
      Parameters
      ----------
      G : NetworkX graph
          A graph over which to find the layers using breadth-first search.
  
      sources : node 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]}
      """
      if sources in G:
          sources = [sources]
  
      current_layer = list(sources)
      visited = set(sources)
  
      for source in current_layer:
          if source not in G:
              raise nx.NetworkXError(
                  f"The node {source} is not in the graph."
              )
  
      # this is basically BFS, except that the current layer only stores the nodes at
      # same distance from sources at each iteration
      while current_layer:
          yield current_layer
          next_layer = []
          for node in current_layer:
              for child in G[node]:
                  if child not in visited:
                      visited.add(child)
                      next_layer.append(child)
          current_layer = next_layer
  
file_path: networkx/networkx/algorithms/traversal/breadth_first_search.py
imports: import math
  from collections import deque
  import networkx
label_desc: Use image node_id 6 for calling a global function with example usage: bfs_layers(G, sources) without return types