summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSebastiano Vigna <sebastiano.vigna@gmail.com>2022-10-09 21:18:53 +0200
committerGitHub <noreply@github.com>2022-10-09 12:18:53 -0700
commitea311319282e01e8740869471bc73a3442ddf209 (patch)
tree955e2e15533a9ed170660ab4a5a63d7dc13e7776
parentdf2b8fbea846234be3342d7cb0fc1a6dee885d36 (diff)
downloadnetworkx-ea311319282e01e8740869471bc73a3442ddf209.tar.gz
Fixed test for average shortest path in the case of directed graphs (#6003)
* Fixed test for average shortest path in the case of directed graphs * Fixed order * Update networkx/algorithms/shortest_paths/tests/test_generic.py Co-authored-by: Ross Barnowski <rossbar@berkeley.edu> * Release bullet; added versionchanged * Add brief description to versionchanged directive. * Fixed definition * Using \substack Co-authored-by: Ross Barnowski <rossbar@berkeley.edu>
-rw-r--r--doc/release/release_dev.rst4
-rw-r--r--networkx/algorithms/shortest_paths/generic.py15
-rw-r--r--networkx/algorithms/shortest_paths/tests/test_generic.py9
3 files changed, 20 insertions, 8 deletions
diff --git a/doc/release/release_dev.rst b/doc/release/release_dev.rst
index faad122b..4c72240e 100644
--- a/doc/release/release_dev.rst
+++ b/doc/release/release_dev.rst
@@ -61,6 +61,10 @@ Improvements
``is_path`` used to raise a `KeyError` when the ``path`` argument contained
a node that was not in the Graph. The behavior has been updated so that
``is_path`` returns `False` in this case rather than raising the exception.
+- [`#6003 <https://github.com/networkx/networkx/pull/6003>`_]
+ ``avg_shortest_path_length`` now raises an exception if the provided
+ graph is directed but not strongly connected. The previous test (weak
+ connecting) was wrong; in that case, the returned value was nonsensical.
API Changes
-----------
diff --git a/networkx/algorithms/shortest_paths/generic.py b/networkx/algorithms/shortest_paths/generic.py
index 129f7417..fdced25d 100644
--- a/networkx/algorithms/shortest_paths/generic.py
+++ b/networkx/algorithms/shortest_paths/generic.py
@@ -320,12 +320,16 @@ def average_shortest_path_length(G, weight=None, method=None):
.. math::
- a =\sum_{s,t \in V} \frac{d(s, t)}{n(n-1)}
+ a =\sum_{\substack{s,t \in V \\ s\neq t}} \frac{d(s, t)}{n(n-1)}
where `V` is the set of nodes in `G`,
`d(s, t)` is the shortest path from `s` to `t`,
and `n` is the number of nodes in `G`.
+ .. versionchanged:: 3.0
+ An exception is raised for directed graphs that are not strongly
+ connected.
+
Parameters
----------
G : NetworkX graph
@@ -354,7 +358,7 @@ def average_shortest_path_length(G, weight=None, method=None):
If `G` is the null graph (that is, the graph on zero nodes).
NetworkXError
- If `G` is not connected (or not weakly connected, in the case
+ If `G` is not connected (or not strongly connected, in the case
of a directed graph).
ValueError
@@ -397,9 +401,10 @@ def average_shortest_path_length(G, weight=None, method=None):
# For the special case of the trivial graph, return zero immediately.
if n == 1:
return 0
- # Shortest path length is undefined if the graph is disconnected.
- if G.is_directed() and not nx.is_weakly_connected(G):
- raise nx.NetworkXError("Graph is not weakly connected.")
+ # Shortest path length is undefined if the graph is not strongly connected.
+ if G.is_directed() and not nx.is_strongly_connected(G):
+ raise nx.NetworkXError("Graph is not strongly connected.")
+ # Shortest path length is undefined if the graph is not connected.
if not G.is_directed() and not nx.is_connected(G):
raise nx.NetworkXError("Graph is not connected.")
diff --git a/networkx/algorithms/shortest_paths/tests/test_generic.py b/networkx/algorithms/shortest_paths/tests/test_generic.py
index 093fd9c7..91b0e309 100644
--- a/networkx/algorithms/shortest_paths/tests/test_generic.py
+++ b/networkx/algorithms/shortest_paths/tests/test_generic.py
@@ -324,13 +324,16 @@ class TestAverageShortestPathLength:
)
assert ans == pytest.approx(4, abs=1e-7)
- def test_disconnected(self):
+ def test_directed_not_strongly_connected(self):
+ G = nx.DiGraph([(0, 1)])
+ with pytest.raises(nx.NetworkXError, match="Graph is not strongly connected"):
+ nx.average_shortest_path_length(G)
+
+ def test_undirected_not_connected(self):
g = nx.Graph()
g.add_nodes_from(range(3))
g.add_edge(0, 1)
pytest.raises(nx.NetworkXError, nx.average_shortest_path_length, g)
- g = g.to_directed()
- pytest.raises(nx.NetworkXError, nx.average_shortest_path_length, g)
def test_trivial_graph(self):
"""Tests that the trivial graph has average path length zero,