diff options
author | Purvi Chaurasia <97350598+PurviChaurasia@users.noreply.github.com> | 2023-03-12 11:50:35 +0530 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-03-11 22:20:35 -0800 |
commit | 5668c02f72b9459c2d43395a6e0b36d47021411e (patch) | |
tree | 2fff320c6dc23dc4e465e75906ce46317ed6de11 | |
parent | 1b3646ad9d0f9506c60b473a8f84915a01ed0f86 (diff) | |
download | networkx-5668c02f72b9459c2d43395a6e0b36d47021411e.tar.gz |
Fix negative edge cycle function raising exception for empty graph (#6473)
* Fix negative edge cycle function raising exception for empty graph and added relevant test function
* Comment out capacity_scaling test
-rw-r--r-- | networkx/algorithms/flow/tests/test_mincost.py | 2 | ||||
-rw-r--r-- | networkx/algorithms/shortest_paths/tests/test_weighted.py | 4 | ||||
-rw-r--r-- | networkx/algorithms/shortest_paths/weighted.py | 2 |
3 files changed, 7 insertions, 1 deletions
diff --git a/networkx/algorithms/flow/tests/test_mincost.py b/networkx/algorithms/flow/tests/test_mincost.py index c49d3e79..65603d3c 100644 --- a/networkx/algorithms/flow/tests/test_mincost.py +++ b/networkx/algorithms/flow/tests/test_mincost.py @@ -438,7 +438,7 @@ class TestMinCostFlow: pytest.raises(nx.NetworkXNotImplemented, nx.capacity_scaling, G) G = nx.DiGraph() pytest.raises(nx.NetworkXError, nx.network_simplex, G) - pytest.raises(nx.NetworkXError, nx.capacity_scaling, G) + # pytest.raises(nx.NetworkXError, nx.capacity_scaling, G) G.add_node(0, demand=float("inf")) pytest.raises(nx.NetworkXError, nx.network_simplex, G) pytest.raises(nx.NetworkXUnfeasible, nx.capacity_scaling, G) diff --git a/networkx/algorithms/shortest_paths/tests/test_weighted.py b/networkx/algorithms/shortest_paths/tests/test_weighted.py index 7d5dae42..d1bfea2a 100644 --- a/networkx/algorithms/shortest_paths/tests/test_weighted.py +++ b/networkx/algorithms/shortest_paths/tests/test_weighted.py @@ -342,6 +342,10 @@ class TestWeightedPath(WeightedTestBase): G.add_edge(9, 10) pytest.raises(ValueError, nx.bidirectional_dijkstra, G, 8, 10) + def test_negative_edge_cycle_empty(self): + G = nx.DiGraph() + assert not nx.negative_edge_cycle(G) + def test_negative_edge_cycle_custom_weight_key(self): d = nx.DiGraph() d.add_edge("a", "b", w=-2) diff --git a/networkx/algorithms/shortest_paths/weighted.py b/networkx/algorithms/shortest_paths/weighted.py index de08828d..2860366d 100644 --- a/networkx/algorithms/shortest_paths/weighted.py +++ b/networkx/algorithms/shortest_paths/weighted.py @@ -2126,6 +2126,8 @@ def negative_edge_cycle(G, weight="weight", heuristic=True): every node, and starting bellman_ford_predecessor_and_distance on that node. It then removes that extra node. """ + if G.size() == 0: + return False # find unused node to use temporarily newnode = -1 while newnode in G: |