code: def _custom_assign_layers(graph):
      # Each node gets a depth assigned, based on the distance to the closest root node.
      layers = {}
  
      if not isinstance(graph, nx.DiGraph):
          sub_graphs = [
              graph.subgraph(c) for c in nx.connected_components(graph)
          ]
          # In case of undirected graphs, we just take any node as root node.
          root_nodes = [
              list(sub_graph.nodes)[0] for sub_graph in sub_graphs
          ]
      else:
          sub_graphs = [graph]
          root_nodes = [n for n, d in graph.in_degree() if d == 0]
  
      for sub_graph in sub_graphs:
          nodes_in_subgraph = list(sub_graph.nodes)
  
          for node in nodes_in_subgraph:
              min_distance = float("inf")
  
              for root_node in root_nodes:
                  try:
                      distance = nx.shortest_path_length(
                          graph, root_node, node
                      )
                      min_distance = min(min_distance, distance)
                  except nx.NetworkXNoPath:
                      # No path to root node, ignore this connection then.
                      continue
  
              layers[node] = min_distance
  
      return layers
  
file_path: dowhy/dowhy/utils/networkx_plotting.py
imports: from copy import deepcopy
  from typing import Any, Dict, List, Optional, Tuple, Union
  import matplotlib.pyplot
  import networkx
  import numpy
  import pandas
  from networkx.drawing import nx_pydot
label_desc: Use image node_id 3 for calling a global function with example usage: _custom_assign_layers(graph) and returns: layers