code: def strongly_connected_components(G):
      """Generate nodes in strongly connected components of graph.
  
      Parameters
      ----------
      G : NetworkX Graph
          A directed graph.
  
      Returns
      -------
      comp : generator of sets
          A generator of sets of nodes, one for each strongly connected
          component of G.
  
      Raises
      ------
      NetworkXNotImplemented
          If G is undirected.
  
      Examples
      --------
      Generate a sorted list of strongly connected components, largest first.
  
      >>> G = nx.cycle_graph(4, create_using=nx.DiGraph())
      >>> nx.add_cycle(G, [10, 11, 12])
      >>> [
      ...     len(c)
      ...     for c in sorted(nx.strongly_connected_components(G), key=len, reverse=True)
      ... ]
      [4, 3]
  
      If you only want the largest component, it's more efficient to
      use max instead of sort.
  
      >>> largest = max(nx.strongly_connected_components(G), key=len)
  
      See Also
      --------
      connected_components
      weakly_connected_components
      kosaraju_strongly_connected_components
  
      Notes
      -----
      Uses Tarjan's algorithm[1]_ with Nuutila's modifications[2]_.
      Nonrecursive version of algorithm.
  
      References
      ----------
      .. [1] Depth-first search and linear graph algorithms, R. Tarjan
         SIAM Journal of Computing 1(2):146-160, (1972).
  
      .. [2] On finding the strongly connected components in a directed graph.
         E. Nuutila and E. Soisalon-Soinen
         Information Processing Letters 49(1): 9-14, (1994)..
  
      """
      preorder = {}
      lowlink = {}
      scc_found = set()
      scc_queue = []
      i = 0  # Preorder counter
      neighbors = {v: iter(G[v]) for v in G}
      for source in G:
          if source not in scc_found:
              queue = [source]
              while queue:
                  v = queue[-1]
                  if v not in preorder:
                      i = i + 1
                      preorder[v] = i
                  done = True
                  for w in neighbors[v]:
                      if w not in preorder:
                          queue.append(w)
                          done = False
                          break
                  if done:
                      lowlink[v] = preorder[v]
                      for w in G[v]:
                          if w not in scc_found:
                              if preorder[w] > preorder[v]:
                                  lowlink[v] = min(
                                      [lowlink[v], lowlink[w]]
                                  )
                              else:
                                  lowlink[v] = min(
                                      [lowlink[v], preorder[w]]
                                  )
                      queue.pop()
                      if lowlink[v] == preorder[v]:
                          scc = {v}
                          while (
                              scc_queue
                              and preorder[scc_queue[-1]] > preorder[v]
                          ):
                              k = scc_queue.pop()
                              scc.add(k)
                          scc_found.update(scc)
                          yield scc
                      else:
                          scc_queue.append(v)
  
file_path: networkx/networkx/algorithms/components/strongly_connected.py
imports: import networkx
  from networkx.utils.decorators import not_implemented_for
label_desc: Use image node_id 1 for calling a global function with example usage: strongly_connected_components(G) without return types