prompt: You will be provided with a partial code base and an issue statement explaining a problem to resolve.
  <issue>
  metric_closure will throw KeyError with unconnected graph
  Suggest checking connectedness with `nx.is_connected()` on entry to `metric_closure()` and throwing a more informative error if not.
  
  </issue>
  <code>
  [start of networkx/algorithms/approximation/steinertree.py]
  1 from itertools import combinations, chain
  2 
  3 from networkx.utils import pairwise, not_implemented_for
  4 import networkx as nx
  5 
  6 __all__ = ['metric_closure', 'steiner_tree']
  7 
  8 
  9 @not_implemented_for('directed')
  10 def metric_closure(G, weight='weight'):
  11     """  Return the metric closure of a graph.
  12 
  13     The metric closure of a graph *G* is the complete graph in which each edge
  14     is weighted by the shortest path distance between the nodes in *G* .
  15 
  16     Parameters
  17     ----------
  18     G : NetworkX graph
  19 
  20     Returns
  21     -------
  22     NetworkX graph
  23         Metric closure of the graph `G`.
  24 
  25     """
  26     M = nx.Graph()
  27 
  28     seen = set()
  29     Gnodes = set(G)
  30     for u, (distance, path) in nx.all_pairs_dijkstra(G, weight=weight):
  31         seen.add(u)
  32         for v in Gnodes - seen:
  33             M.add_edge(u, v, distance=distance[v], path=path[v])
  34 
  35     return M
  36 
  37 
  38 @not_implemented_for('directed')
  39 def steiner_tree(G, terminal_nodes, weight='weight'):
  40     """ Return an approximation to the minimum Steiner tree of a graph.
  41 
  42     Parameters
  43     ----------
  44     G : NetworkX graph
  45 
  46     terminal_nodes : list
  47          A list of terminal nodes for which minimum steiner tree is
  48          to be found.
  49 
  50     Returns
  51     -------
  52     NetworkX graph
  53         Approximation to the minimum steiner tree of `G` induced by
  54         `terminal_nodes` .
  55 
  56     Notes
  57     -----
  58     Steiner tree can be approximated by computing the minimum spanning
  59     tree of the subgraph of the metric closure of the graph induced by the
  60     terminal nodes, where the metric closure of *G* is the complete graph in
  61     which each edge is weighted by the shortest path distance between the
  62     nodes in *G* .
  63     This algorithm produces a tree whose weight is within a (2 - (2 / t))
  64     factor of the weight of the optimal Steiner tree where *t* is number of
  65     terminal nodes.
  66 
  67     """
  68     # M is the subgraph of the metric closure induced by the terminal nodes of
  69     # G.
  70     M = metric_closure(G, weight=weight)
  71     # Use the 'distance' attribute of each edge provided by the metric closure
  72     # graph.
  73     H = M.subgraph(terminal_nodes)
  74     mst_edges = nx.minimum_spanning_edges(H, weight='distance', data=True)
  75     # Create an iterator over each edge in each shortest path; repeats are okay
  76     edges = chain.from_iterable(pairwise(d['path']) for u, v, d in mst_edges)
  77     T = G.edge_subgraph(edges)
  78     return T
  79
  [end of networkx/algorithms/approximation/steinertree.py]
  </code>
  
  I need you to solve this issue by generating a single patch file that I can apply directly to this repository using git apply. Please respond with a single patch file in the following format.
  <patch>
  diff --git a/file.py b/file.py
  --- a/file.py
  +++ b/file.py
  @@ -1,27 +1,35 @@
   def euclidean(a, b):
  -    while b:
  -        a, b = b, a % b
  -    return a
  +    if b == 0:
  +        return a
  +    return euclidean(b, a % b)
   
   
   def bresenham(x0, y0, x1, y1):
       points = []
       dx = abs(x1 - x0)
       dy = abs(y1 - y0)
  -    sx = 1 if x0 < x1 else -1
  -    sy = 1 if y0 < y1 else -1
  -    err = dx - dy
  +    x, y = x0, y0
  +    sx = -1 if x0 > x1 else 1
  +    sy = -1 if y0 > y1 else 1
   
  -    while True:
  -        points.append((x0, y0))
  -        if x0 == x1 and y0 == y1:
  -            break
  -        e2 = 2 * err
  -        if e2 > -dy:
  -            err -= dy
  -            x0 += sx
  -        if e2 < dx:
  -            err += dx
  -            y0 += sy
  +    if dx > dy:
  +        err = dx / 2.0
  +        while x != x1:
  +            points.append((x, y))
  +            err -= dy
  +            if err < 0:
  +                y += sy
  +                err += dx
  +            x += sx
  +    else:
  +        err = dy / 2.0
  +        while y != y1:
  +            points.append((x, y))
  +            err -= dx
  +            if err < 0:
  +                x += sx
  +                err += dy
  +            y += sy
  +
  +    points.append((x, y))
       return points
  
  </patch>
golden_diff: diff --git a/networkx/algorithms/approximation/steinertree.py b/networkx/algorithms/approximation/steinertree.py
  --- a/networkx/algorithms/approximation/steinertree.py
  +++ b/networkx/algorithms/approximation/steinertree.py
  @@ -25,11 +25,22 @@
       """
       M = nx.Graph()
   
  -    seen = set()
       Gnodes = set(G)
  -    for u, (distance, path) in nx.all_pairs_dijkstra(G, weight=weight):
  -        seen.add(u)
  -        for v in Gnodes - seen:
  +
  +    # 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
  
verification_info: {"golden_diff": "diff --git a/networkx/algorithms/approximation/steinertree.py b/networkx/algorithms/approximation/steinertree.py\n--- a/networkx/algorithms/approximation/steinertree.py\n+++ b/networkx/algorithms/approximation/steinertree.py\n@@ -25,11 +25,22 @@\n     \"\"\"\n     M = nx.Graph()\n \n-    seen = set()\n     Gnodes = set(G)\n-    for u, (distance, path) in nx.all_pairs_dijkstra(G, weight=weight):\n-        seen.add(u)\n-        for v in Gnodes - seen:\n+\n+    # check for connected graph while processing first node\n+    all_paths_iter = nx.all_pairs_dijkstra(G, weight=weight)\n+    u, (distance, path) = next(all_paths_iter)\n+    if Gnodes - set(distance):\n+        msg = \"G is not a connected graph. metric_closure is not defined.\"\n+        raise nx.NetworkXError(msg)\n+    Gnodes.remove(u)\n+    for v in Gnodes:\n+        M.add_edge(u, v, distance=distance[v], path=path[v])\n+\n+    # first node done -- now process the rest\n+    for u, (distance, path) in all_paths_iter:\n+        Gnodes.remove(u)\n+        for v in Gnodes:\n             M.add_edge(u, v, distance=distance[v], path=path[v])\n \n     return M\n", "issue": "metric_closure will throw KeyError with unconnected graph\nSuggest checking connectedness with `nx.is_connected()` on entry to `metric_closure()` and throwing a more informative error if not.\n", "before_files": [{"content": "from itertools import combinations, chain\n\nfrom networkx.utils import pairwise, not_implemented_for\nimport networkx as nx\n\n__all__ = ['metric_closure', 'steiner_tree']\n\n\n@not_implemented_for('directed')\ndef metric_closure(G, weight='weight'):\n    \"\"\"  Return the metric closure of a graph.\n\n    The metric closure of a graph *G* is the complete graph in which each edge\n    is weighted by the shortest path distance between the nodes in *G* .\n\n    Parameters\n    ----------\n    G : NetworkX graph\n\n    Returns\n    -------\n    NetworkX graph\n        Metric closure of the graph `G`.\n\n    \"\"\"\n    M = nx.Graph()\n\n    seen = set()\n    Gnodes = set(G)\n    for u, (distance, path) in nx.all_pairs_dijkstra(G, weight=weight):\n        seen.add(u)\n        for v in Gnodes - seen:\n            M.add_edge(u, v, distance=distance[v], path=path[v])\n\n    return M\n\n\n@not_implemented_for('directed')\ndef steiner_tree(G, terminal_nodes, weight='weight'):\n    \"\"\" Return an approximation to the minimum Steiner tree of a graph.\n\n    Parameters\n    ----------\n    G : NetworkX graph\n\n    terminal_nodes : list\n         A list of terminal nodes for which minimum steiner tree is\n         to be found.\n\n    Returns\n    -------\n    NetworkX graph\n        Approximation to the minimum steiner tree of `G` induced by\n        `terminal_nodes` .\n\n    Notes\n    -----\n    Steiner tree can be approximated by computing the minimum spanning\n    tree of the subgraph of the metric closure of the graph induced by the\n    terminal nodes, where the metric closure of *G* is the complete graph in\n    which each edge is weighted by the shortest path distance between the\n    nodes in *G* .\n    This algorithm produces a tree whose weight is within a (2 - (2 / t))\n    factor of the weight of the optimal Steiner tree where *t* is number of\n    terminal nodes.\n\n    \"\"\"\n    # M is the subgraph of the metric closure induced by the terminal nodes of\n    # G.\n    M = metric_closure(G, weight=weight)\n    # Use the 'distance' attribute of each edge provided by the metric closure\n    # graph.\n    H = M.subgraph(terminal_nodes)\n    mst_edges = nx.minimum_spanning_edges(H, weight='distance', data=True)\n    # Create an iterator over each edge in each shortest path; repeats are okay\n    edges = chain.from_iterable(pairwise(d['path']) for u, v, d in mst_edges)\n    T = G.edge_subgraph(edges)\n    return T\n", "path": "networkx/algorithms/approximation/steinertree.py"}]}