summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoryusuf-csdev <62771956+yusuf-csdev@users.noreply.github.com>2022-03-25 00:29:40 +0300
committerGitHub <noreply@github.com>2022-03-24 17:29:40 -0400
commit1ca4a78556859dfc2d1bb8c2e730043234d71ace (patch)
treed3e94ff78d11930c3fdf331f17099ab82f224037
parente221641790f5f6d924acb96e81f89ec7abd9cf76 (diff)
downloadnetworkx-1ca4a78556859dfc2d1bb8c2e730043234d71ace.tar.gz
Update extrema bounding method for compute="eccentricities" parameter (#5409)
Fixes #5402 * update extrema bounding method * update style * update style 2 * update style 3 * update style 4 * update documentation and add a test * Rm test encapsulation. Co-authored-by: Ross Barnowski <rossbar@berkeley.edu>
-rw-r--r--networkx/algorithms/distance_measures.py20
-rw-r--r--networkx/algorithms/tests/test_distance_measures.py6
2 files changed, 17 insertions, 9 deletions
diff --git a/networkx/algorithms/distance_measures.py b/networkx/algorithms/distance_measures.py
index ef665dc7..7a1ce3d6 100644
--- a/networkx/algorithms/distance_measures.py
+++ b/networkx/algorithms/distance_measures.py
@@ -30,20 +30,23 @@ def extrema_bounding(G, compute="diameter"):
compute : string denoting the requesting metric
"diameter" for the maximal eccentricity value,
"radius" for the minimal eccentricity value,
- "periphery" for the set of nodes with eccentricity equal to the diameter
- "center" for the set of nodes with eccentricity equal to the radius
+ "periphery" for the set of nodes with eccentricity equal to the diameter,
+ "center" for the set of nodes with eccentricity equal to the radius,
+ "eccentricities" for the maximum distance from each node to all other nodes in G
Returns
-------
value : value of the requested metric
int for "diameter" and "radius" or
- list of nodes for "center" and "periphery"
+ list of nodes for "center" and "periphery" or
+ dictionary of eccentricity values keyed by node for "eccentricities"
Raises
------
NetworkXError
If the graph consists of multiple components
-
+ ValueError
+ If `compute` is not one of "diameter", "radius", "periphery", "center", or "eccentricities".
Notes
-----
This algorithm was proposed in the following papers:
@@ -125,14 +128,12 @@ def extrema_bounding(G, compute="diameter"):
for i in candidates
if ecc_upper[i] <= maxlower and 2 * ecc_lower[i] >= maxupper
}
-
elif compute == "radius":
ruled_out = {
i
for i in candidates
if ecc_lower[i] >= minupper and ecc_upper[i] + 1 <= 2 * minlower
}
-
elif compute == "periphery":
ruled_out = {
i
@@ -140,7 +141,6 @@ def extrema_bounding(G, compute="diameter"):
if ecc_upper[i] < maxlower
and (maxlower == maxupper or ecc_lower[i] > maxupper)
}
-
elif compute == "center":
ruled_out = {
i
@@ -148,9 +148,11 @@ def extrema_bounding(G, compute="diameter"):
if ecc_lower[i] > minupper
and (minlower == minupper or ecc_upper[i] + 1 < 2 * minlower)
}
-
elif compute == "eccentricities":
- ruled_out = {}
+ ruled_out = set()
+ else:
+ msg = "compute must be one of 'diameter', 'radius', 'periphery', 'center', 'eccentricities'"
+ raise ValueError(msg)
ruled_out.update(i for i in candidates if ecc_lower[i] == ecc_upper[i])
candidates -= ruled_out
diff --git a/networkx/algorithms/tests/test_distance_measures.py b/networkx/algorithms/tests/test_distance_measures.py
index f36d58a3..e3c825e5 100644
--- a/networkx/algorithms/tests/test_distance_measures.py
+++ b/networkx/algorithms/tests/test_distance_measures.py
@@ -7,6 +7,12 @@ import networkx as nx
from networkx import convert_node_labels_to_integers as cnlti
+def test_extrema_bounding_invalid_compute_kwarg():
+ G = nx.path_graph(3)
+ with pytest.raises(ValueError, match="compute must be one of"):
+ nx.extrema_bounding(G, compute="spam")
+
+
class TestDistance:
def setup_method(self):
G = cnlti(nx.grid_2d_graph(4, 4), first_label=1, ordering="sorted")