summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShichu Zhu <shichuzhu@users.noreply.github.com>2021-03-07 12:53:19 -0500
committerGitHub <noreply@github.com>2021-03-07 09:53:19 -0800
commit1de57a8ca852cf27820512720968c6cffb51a74b (patch)
tree7c434106e89122dad3cc07df2727572235753afa
parent7a1b50e5b4c348aa8b83b0a852aa25439c7dc83a (diff)
downloadnetworkx-1de57a8ca852cf27820512720968c6cffb51a74b.tar.gz
fix for negative_edge_cycle weight kwarg to bellman_ford (#4658)
Use keyword argument instead of positional argument to avoid argument mismatch. Affected function: `negative_edge_cycle`
-rw-r--r--networkx/algorithms/shortest_paths/tests/test_weighted.py8
-rw-r--r--networkx/algorithms/shortest_paths/weighted.py4
2 files changed, 11 insertions, 1 deletions
diff --git a/networkx/algorithms/shortest_paths/tests/test_weighted.py b/networkx/algorithms/shortest_paths/tests/test_weighted.py
index a4ba92c0..57638253 100644
--- a/networkx/algorithms/shortest_paths/tests/test_weighted.py
+++ b/networkx/algorithms/shortest_paths/tests/test_weighted.py
@@ -338,6 +338,12 @@ class TestWeightedPath(WeightedTestBase):
G.add_edge(9, 10)
pytest.raises(ValueError, nx.bidirectional_dijkstra, G, 8, 10)
+ def test_negative_edge_cycle_custom_weight_key(self):
+ d = nx.DiGraph()
+ d.add_edge("a", "b", w=-2)
+ d.add_edge("b", "a", w=-1)
+ assert nx.negative_edge_cycle(d, weight="w")
+
def test_weight_function(self):
"""Tests that a callable weight is interpreted as a weight
function instead of an edge attribute.
@@ -349,6 +355,7 @@ class TestWeightedPath(WeightedTestBase):
G.adj[0][2]["weight"] = 10
G.adj[0][1]["weight"] = 1
G.adj[1][2]["weight"] = 1
+
# The weight function will take the multiplicative inverse of
# the weights on the edges. This way, weights that were large
# before now become small and vice versa.
@@ -414,6 +421,7 @@ class TestDijkstraPathLength:
G.adj[0][2]["weight"] = 10
G.adj[0][1]["weight"] = 1
G.adj[1][2]["weight"] = 1
+
# The weight function will take the multiplicative inverse of
# the weights on the edges. This way, weights that were large
# before now become small and vice versa.
diff --git a/networkx/algorithms/shortest_paths/weighted.py b/networkx/algorithms/shortest_paths/weighted.py
index 0f2e189b..f721a1be 100644
--- a/networkx/algorithms/shortest_paths/weighted.py
+++ b/networkx/algorithms/shortest_paths/weighted.py
@@ -1965,7 +1965,9 @@ def negative_edge_cycle(G, weight="weight", heuristic=True):
G.add_edges_from([(newnode, n) for n in G])
try:
- bellman_ford_predecessor_and_distance(G, newnode, weight, heuristic=heuristic)
+ bellman_ford_predecessor_and_distance(
+ G, newnode, weight=weight, heuristic=heuristic
+ )
except nx.NetworkXUnbounded:
return True
finally: