code: def metric_closure(G, weight="weight"):
      """Return the metric closure of a graph.
  
      The metric closure of a graph *G* is the complete graph in which each edge
      is weighted by the shortest path distance between the nodes in *G* .
  
      Parameters
      ----------
      G : NetworkX graph
  
      Returns
      -------
      NetworkX graph
          Metric closure of the graph `G`.
  
      """
      M = nx.Graph()
  
      Gnodes = set(G)
  
      # check for connected graph while processing first node
      all_paths_iter = nx.all_pairs_dijkstra(G, weight=weight)
      u, (distance, path) = next(all_paths_iter)
      if Gnodes - set(distance):
          msg = "G is not a connected graph. metric_closure is not defined."
          raise nx.NetworkXError(msg)
      Gnodes.remove(u)
      for v in Gnodes:
          M.add_edge(u, v, distance=distance[v], path=path[v])
  
      # first node done -- now process the rest
      for u, (distance, path) in all_paths_iter:
          Gnodes.remove(u)
          for v in Gnodes:
              M.add_edge(u, v, distance=distance[v], path=path[v])
  
      return M
  
file_path: networkx/networkx/algorithms/approximation/steinertree.py
imports: from itertools import chain
  import networkx
  from networkx.utils import not_implemented_for, pairwise
label_desc: Use image node_id 1 for calling a global function with example usage: metric_closure(G, weight) and returns: M