code: def steiner_tree(G, terminal_nodes, weight="weight", method=None):
      r"""Return an approximation to the minimum Steiner tree of a graph.
  
      The minimum Steiner tree of `G` w.r.t a set of `terminal_nodes` (also *S*)
      is a tree within `G` that spans those nodes and has minimum size (sum of
      edge weights) among all such trees.
  
      The approximation algorithm is specified with the `method` keyword
      argument. All three available algorithms produce a tree whose weight is
      within a ``(2 - (2 / l))`` factor of the weight of the optimal Steiner tree,
      where ``l`` is the minimum number of leaf nodes across all possible Steiner
      trees.
  
      * ``"kou"`` [2]_ (runtime $O(|S| |V|^2)$) computes the minimum spanning tree of
        the subgraph of the metric closure of *G* induced by the terminal nodes,
        where the metric closure of *G* is the complete graph in which each edge is
        weighted by the shortest path distance between the nodes in *G*.
  
      * ``"mehlhorn"`` [3]_ (runtime $O(|E|+|V|\log|V|)$) modifies Kou et al.'s
        algorithm, beginning by finding the closest terminal node for each
        non-terminal. This data is used to create a complete graph containing only
        the terminal nodes, in which edge is weighted with the shortest path
        distance between them. The algorithm then proceeds in the same way as Kou
        et al..
  
      Parameters
      ----------
      G : NetworkX graph
  
      terminal_nodes : list
           A list of terminal nodes for which minimum steiner tree is
           to be found.
  
      weight : string (default = 'weight')
          Use the edge attribute specified by this string as the edge weight.
          Any edge attribute not present defaults to 1.
  
      method : string, optional (default = 'kou')
          The algorithm to use to approximate the Steiner tree.
          Supported options: 'kou', 'mehlhorn'.
          Other inputs produce a ValueError.
  
      Returns
      -------
      NetworkX graph
          Approximation to the minimum steiner tree of `G` induced by
          `terminal_nodes` .
  
      Notes
      -----
      For multigraphs, the edge between two nodes with minimum weight is the
      edge put into the Steiner tree.
  
  
      References
      ----------
      .. [1] Steiner_tree_problem on Wikipedia.
             https://en.wikipedia.org/wiki/Steiner_tree_problem
      .. [2] Kou, L., G. Markowsky, and L. Berman. 1981.
             ‘A Fast Algorithm for Steiner Trees’.
             Acta Informatica 15 (2): 141–45.
             https://doi.org/10.1007/BF00288961.
      .. [3] Mehlhorn, Kurt. 1988.
             ‘A Faster Approximation Algorithm for the Steiner Problem in Graphs’.
             Information Processing Letters 27 (3): 125–28.
             https://doi.org/10.1016/0020-0190(88)90066-X.
      """
      if method is None:
          import warnings
  
          msg = (
              "steiner_tree will change default method from 'kou' to 'mehlhorn' "
              "in version 3.2.\nSet the `method` kwarg to remove this warning."
          )
          warnings.warn(msg, FutureWarning, stacklevel=4)
          method = "kou"
  
      try:
          algo = ALGORITHMS[method]
      except KeyError as e:
          msg = f"{method} is not a valid choice for an algorithm."
          raise ValueError(msg) from e
  
      edges = algo(G, terminal_nodes, weight)
      # For multigraph we should add the minimal weight edge keys
      if G.is_multigraph():
          edges = (
              (u, v, min(G[u][v], key=lambda k: G[u][v][k][weight]))
              for u, v in edges
          )
      T = G.edge_subgraph(edges)
      return T
  
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 5 for calling a global function with example usage: steiner_tree(G, terminal_nodes, weight, method) and returns: T