summaryrefslogtreecommitdiff
path: root/networkx
diff options
context:
space:
mode:
authorRoss Barnowski <rossbar@berkeley.edu>2022-09-08 10:49:36 -0700
committerGitHub <noreply@github.com>2022-09-08 10:49:36 -0700
commitd8b07498dfcd9fb2ce1c83ccf190ab24209234e8 (patch)
tree79b71de90e2f549e9f069b098107681b255d5cc2 /networkx
parentbffcd74649fb95a57fb834846eb3c7d9693c55b8 (diff)
downloadnetworkx-d8b07498dfcd9fb2ce1c83ccf190ab24209234e8.tar.gz
Minor docstring touchups and test refactor for `is_path` (#5967)
* Touch up docstring. * Condense conditional. * Minor refactor of ispath test - parametrize and rm redundant. * Add release note. * Update networkx/classes/function.py Co-authored-by: Dan Schult <dschult@colgate.edu> Co-authored-by: Dan Schult <dschult@colgate.edu>
Diffstat (limited to 'networkx')
-rw-r--r--networkx/classes/function.py12
-rw-r--r--networkx/classes/tests/test_function.py22
2 files changed, 15 insertions, 19 deletions
diff --git a/networkx/classes/function.py b/networkx/classes/function.py
index d1277cf6..8e949a36 100644
--- a/networkx/classes/function.py
+++ b/networkx/classes/function.py
@@ -1243,19 +1243,17 @@ def is_path(G, path):
G : graph
A NetworkX graph.
- path: list
- A list of node labels which defines the path to traverse
+ path : list
+ A list of nodes which defines the path to traverse
Returns
-------
- isPath: bool
- A boolean representing whether or not the path exists
+ bool
+ True if `path` is a valid path in `G`
"""
for node, nbr in nx.utils.pairwise(path):
- if node not in G:
- return False
- if nbr not in G[node]:
+ if (node not in G) or (nbr not in G[node]):
return False
return True
diff --git a/networkx/classes/tests/test_function.py b/networkx/classes/tests/test_function.py
index bb961abd..704253f7 100644
--- a/networkx/classes/tests/test_function.py
+++ b/networkx/classes/tests/test_function.py
@@ -736,19 +736,17 @@ def test_pathweight():
pytest.raises(nx.NetworkXNoPath, nx.path_weight, graph, invalid_path, "cost")
-def test_ispath():
+@pytest.mark.parametrize(
+ "G", (nx.Graph(), nx.DiGraph(), nx.MultiGraph(), nx.MultiDiGraph())
+)
+def test_ispath(G):
+ G.add_edges_from([(1, 2), (2, 3), (1, 2), (3, 4)])
valid_path = [1, 2, 3, 4]
- invalid_path = [1, 2, 4, 3]
- another_invalid_path = [1, 2, 3, 4, 5]
- yet_another_invalid_path = [1, 2, 5, 3, 4]
- graphs = [nx.Graph(), nx.DiGraph(), nx.MultiGraph(), nx.MultiDiGraph()]
- edges = [(1, 2), (2, 3), (1, 2), (3, 4)]
- for graph in graphs:
- graph.add_edges_from(edges)
- assert nx.is_path(graph, valid_path)
- assert not nx.is_path(graph, invalid_path)
- assert not nx.is_path(graph, another_invalid_path)
- assert not nx.is_path(graph, yet_another_invalid_path)
+ invalid_path = [1, 2, 4, 3] # wrong node order
+ another_invalid_path = [1, 2, 3, 4, 5] # contains node not in G
+ assert nx.is_path(G, valid_path)
+ assert not nx.is_path(G, invalid_path)
+ assert not nx.is_path(G, another_invalid_path)
@pytest.mark.parametrize("G", (nx.Graph(), nx.DiGraph()))