prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  Closeness centrality assumes symmetric distance when "distance" parameter is used
  At the beginning of `nx.closeness_centrality` code there is this logic:
  
  ```python
      if distance is not None:
          # use Dijkstra's algorithm with specified attribute as edge weight
          path_length = functools.partial(nx.single_source_dijkstra_path_length,
                                          weight=distance)
      else:  # handle either directed or undirected
          if G.is_directed() and not reverse:
              path_length = nx.single_target_shortest_path_length
          else:
              path_length = nx.single_source_shortest_path_length
  ```
  
  This means that if `distance` parameter is passed then the direction of the edges is ignored, and the `reverse` parameter has no effect.
  
  Is this a design decision? I have directed networks with non-symmetric distances and am interested in both inbound and outbound centralities. It looks to me this would be a simple change (checking `reverse` and using `nx.single_target_dijkstra_path_length` if needed). Am willing to make a PR if this is deemed an appropriate change.
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `networkx/algorithms/centrality/closeness.py`
  Content:
  ```
  1 #    Copyright (C) 2004-2018 by
  2 #    Aric Hagberg <hagberg@lanl.gov>
  3 #    Dan Schult <dschult@colgate.edu>
  4 #    Pieter Swart <swart@lanl.gov>
  5 #    All rights reserved.
  6 #    BSD license.
  7 #
  8 # Authors:  Aric Hagberg <aric.hagberg@gmail.com>
  9 #           Pieter Swart <swart@lanl.gov>
  10 #           Sasha Gutfraind <ag362@cornell.edu>
  11 #           Dan Schult <dschult@colgate.edu>
  12 """
  13 Closeness centrality measures.
  14 """
  15 import functools
  16 import networkx as nx
  17 
  18 __all__ = ['closeness_centrality']
  19 
  20 
  21 def closeness_centrality(G, u=None, distance=None,
  22                          wf_improved=True, reverse=False):
  23     r"""Compute closeness centrality for nodes.
  24 
  25     Closeness centrality [1]_ of a node `u` is the reciprocal of the
  26     average shortest path distance to `u` over all `n-1` reachable nodes.
  27 
  28     .. math::
  29 
  30         C(u) = \frac{n - 1}{\sum_{v=1}^{n-1} d(v, u)},
  31 
  32     where `d(v, u)` is the shortest-path distance between `v` and `u`,
  33     and `n` is the number of nodes that can reach `u`.
  34 
  35     Notice that higher values of closeness indicate higher centrality.
  36 
  37     Wasserman and Faust propose an improved formula for graphs with
  38     more than one connected component. The result is "a ratio of the
  39     fraction of actors in the group who are reachable, to the average
  40     distance" from the reachable actors [2]_. You might think this
  41     scale factor is inverted but it is not. As is, nodes from small
  42     components receive a smaller closeness value. Letting `N` denote
  43     the number of nodes in the graph,
  44 
  45     .. math::
  46 
  47         C_{WF}(u) = \frac{n-1}{N-1} \frac{n - 1}{\sum_{v=1}^{n-1} d(v, u)},
  48 
  49     Parameters
  50     ----------
  51     G : graph
  52       A NetworkX graph
  53 
  54     u : node, optional
  55       Return only the value for node u
  56 
  57     distance : edge attribute key, optional (default=None)
  58       Use the specified edge attribute as the edge distance in shortest
  59       path calculations
  60 
  61     wf_improved : bool, optional (default=True)
  62       If True, scale by the fraction of nodes reachable. This gives the
  63       Wasserman and Faust improved formula. For single component graphs
  64       it is the same as the original formula. 
  65 
  66     reverse : bool, optional (default=False)
  67       If True and G is a digraph, reverse the edges of G, using successors
  68       instead of predecessors.
  69 
  70     Returns
  71     -------
  72     nodes : dictionary
  73       Dictionary of nodes with closeness centrality as the value.
  74 
  75     See Also
  76     --------
  77     betweenness_centrality, load_centrality, eigenvector_centrality,
  78     degree_centrality
  79 
  80     Notes
  81     -----
  82     The closeness centrality is normalized to `(n-1)/(|G|-1)` where
  83     `n` is the number of nodes in the connected part of graph
  84     containing the node.  If the graph is not completely connected,
  85     this algorithm computes the closeness centrality for each
  86     connected part separately scaled by that parts size.
  87 
  88     If the 'distance' keyword is set to an edge attribute key then the
  89     shortest-path length will be computed using Dijkstra's algorithm with
  90     that edge attribute as the edge weight.
  91 
  92     References
  93     ----------
  94     .. [1] Linton C. Freeman: Centrality in networks: I.
  95        Conceptual clarification. Social Networks 1:215-239, 1979.
  96        http://leonidzhukov.ru/hse/2013/socialnetworks/papers/freeman79-centrality.pdf
  97     .. [2] pg. 201 of Wasserman, S. and Faust, K.,
  98        Social Network Analysis: Methods and Applications, 1994,
  99        Cambridge University Press.
  100     """
  101     if distance is not None:
  102         # use Dijkstra's algorithm with specified attribute as edge weight
  103         path_length = functools.partial(nx.single_source_dijkstra_path_length,
  104                                         weight=distance)
  105     else:  # handle either directed or undirected
  106         if G.is_directed() and not reverse:
  107             path_length = nx.single_target_shortest_path_length
  108         else:
  109             path_length = nx.single_source_shortest_path_length
  110 
  111     if u is None:
  112         nodes = G.nodes()
  113     else:
  114         nodes = [u]
  115     closeness_centrality = {}
  116     for n in nodes:
  117         sp = dict(path_length(G, n))
  118         totsp = sum(sp.values())
  119         if totsp > 0.0 and len(G) > 1:
  120             closeness_centrality[n] = (len(sp) - 1.0) / totsp
  121             # normalize to number of nodes-1 in connected part
  122             if wf_improved:
  123                 s = (len(sp) - 1.0) / (len(G) - 1)
  124                 closeness_centrality[n] *= s
  125         else:
  126             closeness_centrality[n] = 0.0
  127     if u is not None:
  128         return closeness_centrality[u]
  129     else:
  130         return closeness_centrality
  131
  ```
  --- END FILES ---
  
  Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.
  
  Here is an example:
  
  ```diff
  diff --git a/examples/server_async.py b/examples/server_async.py
  --- a/examples/server_async.py
  +++ b/examples/server_async.py
  @@ -313,4 +313,4 @@
   
   
   if __name__ == "__main__":
  -    asyncio.run(run_async_server("."), debug=True)
  +    asyncio.run(run_async_server(), debug=True)
  diff --git a/examples/server_sync.py b/examples/server_sync.py
  --- a/examples/server_sync.py
  +++ b/examples/server_sync.py
  @@ -313,5 +313,5 @@
   
   
   if __name__ == "__main__":
  -    server = run_sync_server(".")
  +    server = run_sync_server()
       server.shutdown()
  
  ```
  
golden_diff: diff --git a/networkx/algorithms/centrality/closeness.py b/networkx/algorithms/centrality/closeness.py
  --- a/networkx/algorithms/centrality/closeness.py
  +++ b/networkx/algorithms/centrality/closeness.py
  @@ -18,8 +18,7 @@
   __all__ = ['closeness_centrality']
   
   
  -def closeness_centrality(G, u=None, distance=None,
  -                         wf_improved=True, reverse=False):
  +def closeness_centrality(G, u=None, distance=None, wf_improved=True):
       r"""Compute closeness centrality for nodes.
   
       Closeness centrality [1]_ of a node `u` is the reciprocal of the
  @@ -30,7 +29,9 @@
           C(u) = \frac{n - 1}{\sum_{v=1}^{n-1} d(v, u)},
   
       where `d(v, u)` is the shortest-path distance between `v` and `u`,
  -    and `n` is the number of nodes that can reach `u`.
  +    and `n` is the number of nodes that can reach `u`. Notice that the
  +    closeness distance function computes the incoming distance to `u`
  +    for directed graphs. To use outward distance, act on `G.reverse()`.
   
       Notice that higher values of closeness indicate higher centrality.
   
  @@ -63,10 +64,6 @@
         Wasserman and Faust improved formula. For single component graphs
         it is the same as the original formula. 
   
  -    reverse : bool, optional (default=False)
  -      If True and G is a digraph, reverse the edges of G, using successors
  -      instead of predecessors.
  -
       Returns
       -------
       nodes : dictionary
  @@ -89,6 +86,10 @@
       shortest-path length will be computed using Dijkstra's algorithm with
       that edge attribute as the edge weight.
   
  +    In NetworkX 2.2 and earlier a bug caused Dijkstra's algorithm to use the
  +    outward distance rather than the inward distance. If you use a 'distance'
  +    keyword and a DiGraph, your results will change between v2.2 and v2.3.
  +
       References
       ----------
       .. [1] Linton C. Freeman: Centrality in networks: I.
  @@ -98,18 +99,18 @@
          Social Network Analysis: Methods and Applications, 1994,
          Cambridge University Press.
       """
  +    if G.is_directed():
  +        G = G.reverse()  # create a reversed graph view
  +
       if distance is not None:
           # use Dijkstra's algorithm with specified attribute as edge weight
           path_length = functools.partial(nx.single_source_dijkstra_path_length,
                                           weight=distance)
  -    else:  # handle either directed or undirected
  -        if G.is_directed() and not reverse:
  -            path_length = nx.single_target_shortest_path_length
  -        else:
  -            path_length = nx.single_source_shortest_path_length
  +    else:
  +        path_length = nx.single_source_shortest_path_length
   
       if u is None:
  -        nodes = G.nodes()
  +        nodes = G.nodes
       else:
           nodes = [u]
       closeness_centrality = {}
  
verification_info: {"golden_diff": "diff --git a/networkx/algorithms/centrality/closeness.py b/networkx/algorithms/centrality/closeness.py\n--- a/networkx/algorithms/centrality/closeness.py\n+++ b/networkx/algorithms/centrality/closeness.py\n@@ -18,8 +18,7 @@\n __all__ = ['closeness_centrality']\n \n \n-def closeness_centrality(G, u=None, distance=None,\n-                         wf_improved=True, reverse=False):\n+def closeness_centrality(G, u=None, distance=None, wf_improved=True):\n     r\"\"\"Compute closeness centrality for nodes.\n \n     Closeness centrality [1]_ of a node `u` is the reciprocal of the\n@@ -30,7 +29,9 @@\n         C(u) = \\frac{n - 1}{\\sum_{v=1}^{n-1} d(v, u)},\n \n     where `d(v, u)` is the shortest-path distance between `v` and `u`,\n-    and `n` is the number of nodes that can reach `u`.\n+    and `n` is the number of nodes that can reach `u`. Notice that the\n+    closeness distance function computes the incoming distance to `u`\n+    for directed graphs. To use outward distance, act on `G.reverse()`.\n \n     Notice that higher values of closeness indicate higher centrality.\n \n@@ -63,10 +64,6 @@\n       Wasserman and Faust improved formula. For single component graphs\n       it is the same as the original formula. \n \n-    reverse : bool, optional (default=False)\n-      If True and G is a digraph, reverse the edges of G, using successors\n-      instead of predecessors.\n-\n     Returns\n     -------\n     nodes : dictionary\n@@ -89,6 +86,10 @@\n     shortest-path length will be computed using Dijkstra's algorithm with\n     that edge attribute as the edge weight.\n \n+    In NetworkX 2.2 and earlier a bug caused Dijkstra's algorithm to use the\n+    outward distance rather than the inward distance. If you use a 'distance'\n+    keyword and a DiGraph, your results will change between v2.2 and v2.3.\n+\n     References\n     ----------\n     .. [1] Linton C. Freeman: Centrality in networks: I.\n@@ -98,18 +99,18 @@\n        Social Network Analysis: Methods and Applications, 1994,\n        Cambridge University Press.\n     \"\"\"\n+    if G.is_directed():\n+        G = G.reverse()  # create a reversed graph view\n+\n     if distance is not None:\n         # use Dijkstra's algorithm with specified attribute as edge weight\n         path_length = functools.partial(nx.single_source_dijkstra_path_length,\n                                         weight=distance)\n-    else:  # handle either directed or undirected\n-        if G.is_directed() and not reverse:\n-            path_length = nx.single_target_shortest_path_length\n-        else:\n-            path_length = nx.single_source_shortest_path_length\n+    else:\n+        path_length = nx.single_source_shortest_path_length\n \n     if u is None:\n-        nodes = G.nodes()\n+        nodes = G.nodes\n     else:\n         nodes = [u]\n     closeness_centrality = {}\n", "issue": "Closeness centrality assumes symmetric distance when \"distance\" parameter is used\nAt the beginning of `nx.closeness_centrality` code there is this logic:\r\n\r\n```python\r\n    if distance is not None:\r\n        # use Dijkstra's algorithm with specified attribute as edge weight\r\n        path_length = functools.partial(nx.single_source_dijkstra_path_length,\r\n                                        weight=distance)\r\n    else:  # handle either directed or undirected\r\n        if G.is_directed() and not reverse:\r\n            path_length = nx.single_target_shortest_path_length\r\n        else:\r\n            path_length = nx.single_source_shortest_path_length\r\n```\r\n\r\nThis means that if `distance` parameter is passed then the direction of the edges is ignored, and the `reverse` parameter has no effect.\r\n\r\nIs this a design decision? I have directed networks with non-symmetric distances and am interested in both inbound and outbound centralities. It looks to me this would be a simple change (checking `reverse` and using `nx.single_target_dijkstra_path_length` if needed). Am willing to make a PR if this is deemed an appropriate change.\n", "before_files": [{"content": "#    Copyright (C) 2004-2018 by\n#    Aric Hagberg <hagberg@lanl.gov>\n#    Dan Schult <dschult@colgate.edu>\n#    Pieter Swart <swart@lanl.gov>\n#    All rights reserved.\n#    BSD license.\n#\n# Authors:  Aric Hagberg <aric.hagberg@gmail.com>\n#           Pieter Swart <swart@lanl.gov>\n#           Sasha Gutfraind <ag362@cornell.edu>\n#           Dan Schult <dschult@colgate.edu>\n\"\"\"\nCloseness centrality measures.\n\"\"\"\nimport functools\nimport networkx as nx\n\n__all__ = ['closeness_centrality']\n\n\ndef closeness_centrality(G, u=None, distance=None,\n                         wf_improved=True, reverse=False):\n    r\"\"\"Compute closeness centrality for nodes.\n\n    Closeness centrality [1]_ of a node `u` is the reciprocal of the\n    average shortest path distance to `u` over all `n-1` reachable nodes.\n\n    .. math::\n\n        C(u) = \\frac{n - 1}{\\sum_{v=1}^{n-1} d(v, u)},\n\n    where `d(v, u)` is the shortest-path distance between `v` and `u`,\n    and `n` is the number of nodes that can reach `u`.\n\n    Notice that higher values of closeness indicate higher centrality.\n\n    Wasserman and Faust propose an improved formula for graphs with\n    more than one connected component. The result is \"a ratio of the\n    fraction of actors in the group who are reachable, to the average\n    distance\" from the reachable actors [2]_. You might think this\n    scale factor is inverted but it is not. As is, nodes from small\n    components receive a smaller closeness value. Letting `N` denote\n    the number of nodes in the graph,\n\n    .. math::\n\n        C_{WF}(u) = \\frac{n-1}{N-1} \\frac{n - 1}{\\sum_{v=1}^{n-1} d(v, u)},\n\n    Parameters\n    ----------\n    G : graph\n      A NetworkX graph\n\n    u : node, optional\n      Return only the value for node u\n\n    distance : edge attribute key, optional (default=None)\n      Use the specified edge attribute as the edge distance in shortest\n      path calculations\n\n    wf_improved : bool, optional (default=True)\n      If True, scale by the fraction of nodes reachable. This gives the\n      Wasserman and Faust improved formula. For single component graphs\n      it is the same as the original formula. \n\n    reverse : bool, optional (default=False)\n      If True and G is a digraph, reverse the edges of G, using successors\n      instead of predecessors.\n\n    Returns\n    -------\n    nodes : dictionary\n      Dictionary of nodes with closeness centrality as the value.\n\n    See Also\n    --------\n    betweenness_centrality, load_centrality, eigenvector_centrality,\n    degree_centrality\n\n    Notes\n    -----\n    The closeness centrality is normalized to `(n-1)/(|G|-1)` where\n    `n` is the number of nodes in the connected part of graph\n    containing the node.  If the graph is not completely connected,\n    this algorithm computes the closeness centrality for each\n    connected part separately scaled by that parts size.\n\n    If the 'distance' keyword is set to an edge attribute key then the\n    shortest-path length will be computed using Dijkstra's algorithm with\n    that edge attribute as the edge weight.\n\n    References\n    ----------\n    .. [1] Linton C. Freeman: Centrality in networks: I.\n       Conceptual clarification. Social Networks 1:215-239, 1979.\n       http://leonidzhukov.ru/hse/2013/socialnetworks/papers/freeman79-centrality.pdf\n    .. [2] pg. 201 of Wasserman, S. and Faust, K.,\n       Social Network Analysis: Methods and Applications, 1994,\n       Cambridge University Press.\n    \"\"\"\n    if distance is not None:\n        # use Dijkstra's algorithm with specified attribute as edge weight\n        path_length = functools.partial(nx.single_source_dijkstra_path_length,\n                                        weight=distance)\n    else:  # handle either directed or undirected\n        if G.is_directed() and not reverse:\n            path_length = nx.single_target_shortest_path_length\n        else:\n            path_length = nx.single_source_shortest_path_length\n\n    if u is None:\n        nodes = G.nodes()\n    else:\n        nodes = [u]\n    closeness_centrality = {}\n    for n in nodes:\n        sp = dict(path_length(G, n))\n        totsp = sum(sp.values())\n        if totsp > 0.0 and len(G) > 1:\n            closeness_centrality[n] = (len(sp) - 1.0) / totsp\n            # normalize to number of nodes-1 in connected part\n            if wf_improved:\n                s = (len(sp) - 1.0) / (len(G) - 1)\n                closeness_centrality[n] *= s\n        else:\n            closeness_centrality[n] = 0.0\n    if u is not None:\n        return closeness_centrality[u]\n    else:\n        return closeness_centrality\n", "path": "networkx/algorithms/centrality/closeness.py"}], "after_files": [{"content": "#    Copyright (C) 2004-2018 by\n#    Aric Hagberg <hagberg@lanl.gov>\n#    Dan Schult <dschult@colgate.edu>\n#    Pieter Swart <swart@lanl.gov>\n#    All rights reserved.\n#    BSD license.\n#\n# Authors:  Aric Hagberg <aric.hagberg@gmail.com>\n#           Pieter Swart <swart@lanl.gov>\n#           Sasha Gutfraind <ag362@cornell.edu>\n#           Dan Schult <dschult@colgate.edu>\n\"\"\"\nCloseness centrality measures.\n\"\"\"\nimport functools\nimport networkx as nx\n\n__all__ = ['closeness_centrality']\n\n\ndef closeness_centrality(G, u=None, distance=None, wf_improved=True):\n    r\"\"\"Compute closeness centrality for nodes.\n\n    Closeness centrality [1]_ of a node `u` is the reciprocal of the\n    average shortest path distance to `u` over all `n-1` reachable nodes.\n\n    .. math::\n\n        C(u) = \\frac{n - 1}{\\sum_{v=1}^{n-1} d(v, u)},\n\n    where `d(v, u)` is the shortest-path distance between `v` and `u`,\n    and `n` is the number of nodes that can reach `u`. Notice that the\n    closeness distance function computes the incoming distance to `u`\n    for directed graphs. To use outward distance, act on `G.reverse()`.\n\n    Notice that higher values of closeness indicate higher centrality.\n\n    Wasserman and Faust propose an improved formula for graphs with\n    more than one connected component. The result is \"a ratio of the\n    fraction of actors in the group who are reachable, to the average\n    distance\" from the reachable actors [2]_. You might think this\n    scale factor is inverted but it is not. As is, nodes from small\n    components receive a smaller closeness value. Letting `N` denote\n    the number of nodes in the graph,\n\n    .. math::\n\n        C_{WF}(u) = \\frac{n-1}{N-1} \\frac{n - 1}{\\sum_{v=1}^{n-1} d(v, u)},\n\n    Parameters\n    ----------\n    G : graph\n      A NetworkX graph\n\n    u : node, optional\n      Return only the value for node u\n\n    distance : edge attribute key, optional (default=None)\n      Use the specified edge attribute as the edge distance in shortest\n      path calculations\n\n    wf_improved : bool, optional (default=True)\n      If True, scale by the fraction of nodes reachable. This gives the\n      Wasserman and Faust improved formula. For single component graphs\n      it is the same as the original formula. \n\n    Returns\n    -------\n    nodes : dictionary\n      Dictionary of nodes with closeness centrality as the value.\n\n    See Also\n    --------\n    betweenness_centrality, load_centrality, eigenvector_centrality,\n    degree_centrality\n\n    Notes\n    -----\n    The closeness centrality is normalized to `(n-1)/(|G|-1)` where\n    `n` is the number of nodes in the connected part of graph\n    containing the node.  If the graph is not completely connected,\n    this algorithm computes the closeness centrality for each\n    connected part separately scaled by that parts size.\n\n    If the 'distance' keyword is set to an edge attribute key then the\n    shortest-path length will be computed using Dijkstra's algorithm with\n    that edge attribute as the edge weight.\n\n    In NetworkX 2.2 and earlier a bug caused Dijkstra's algorithm to use the\n    outward distance rather than the inward distance. If you use a 'distance'\n    keyword and a DiGraph, your results will change between v2.2 and v2.3.\n\n    References\n    ----------\n    .. [1] Linton C. Freeman: Centrality in networks: I.\n       Conceptual clarification. Social Networks 1:215-239, 1979.\n       http://leonidzhukov.ru/hse/2013/socialnetworks/papers/freeman79-centrality.pdf\n    .. [2] pg. 201 of Wasserman, S. and Faust, K.,\n       Social Network Analysis: Methods and Applications, 1994,\n       Cambridge University Press.\n    \"\"\"\n    if G.is_directed():\n        G = G.reverse()  # create a reversed graph view\n\n    if distance is not None:\n        # use Dijkstra's algorithm with specified attribute as edge weight\n        path_length = functools.partial(nx.single_source_dijkstra_path_length,\n                                        weight=distance)\n    else:\n        path_length = nx.single_source_shortest_path_length\n\n    if u is None:\n        nodes = G.nodes\n    else:\n        nodes = [u]\n    closeness_centrality = {}\n    for n in nodes:\n        sp = dict(path_length(G, n))\n        totsp = sum(sp.values())\n        if totsp > 0.0 and len(G) > 1:\n            closeness_centrality[n] = (len(sp) - 1.0) / totsp\n            # normalize to number of nodes-1 in connected part\n            if wf_improved:\n                s = (len(sp) - 1.0) / (len(G) - 1)\n                closeness_centrality[n] *= s\n        else:\n            closeness_centrality[n] = 0.0\n    if u is not None:\n        return closeness_centrality[u]\n    else:\n        return closeness_centrality\n", "path": "networkx/algorithms/centrality/closeness.py"}]}