code: def _kou_steiner_tree(G, terminal_nodes, weight):
      # H is the subgraph induced by terminal_nodes in the metric closure M of G.
      M = metric_closure(G, weight=weight)
      H = M.subgraph(terminal_nodes)
  
      # Use the 'distance' attribute of each edge provided by M.
      mst_edges = nx.minimum_spanning_edges(
          H, weight="distance", data=True
      )
  
      # Create an iterator over each edge in each shortest path; repeats are okay
      mst_all_edges = chain.from_iterable(
          pairwise(d["path"]) for u, v, d in mst_edges
      )
      if G.is_multigraph():
          mst_all_edges = (
              (u, v, min(G[u][v], key=lambda k: G[u][v][k][weight]))
              for u, v in mst_all_edges
          )
  
      # Find the MST again, over this new set of edges
      G_S = G.edge_subgraph(mst_all_edges)
      T_S = nx.minimum_spanning_edges(G_S, weight="weight", data=False)
  
      # Leaf nodes that are not terminal might still remain; remove them here
      T_H = G.edge_subgraph(T_S).copy()
      _remove_nonterminal_leaves(T_H, terminal_nodes)
  
      return T_H.edges()
  
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 3 for calling a global function with example usage: _kou_steiner_tree(G, terminal_nodes, weight) and returns: T_H