Skip to content

Update simple_paths.py: consistent behaviour for is_simple_path when path contains nodes not in the graph.#6272

Merged
dschult merged 9 commits intonetworkx:mainfrom
SultanOrazbayev:patch-paths
Dec 20, 2022
Merged

Update simple_paths.py: consistent behaviour for is_simple_path when path contains nodes not in the graph.#6272
dschult merged 9 commits intonetworkx:mainfrom
SultanOrazbayev:patch-paths

Conversation

@SultanOrazbayev
Copy link
Copy Markdown
Contributor

Current version of is_simple_path fails with KeyError if the first element of the node list is not in the graph.

Resolves #6271, by returning False if any node in the provided path is outside the graph.

Current version of `is_simple_path` fails with `KeyError` if the first element of the node list is not in the graph.
Simplify the test condition. Checking for a single node in the list can be combined with checking for duplicates in the list without meaningful efficiency loss.
Add the case when the start of the path is a node not in the graph.
@SultanOrazbayev SultanOrazbayev changed the title Update simple_paths.py Update simple_paths.py: consistent behaviour for is_simple_path when path contains nodes not in the graph. Dec 10, 2022
Copy link
Copy Markdown
Contributor

@rossbar rossbar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 on the test, but it seems like there are more proposed changes than necessary - isn't all that's needed is to add the if not all(n in G for n in nodes) check? It could just be that I'm reading the diff wrong!

@SultanOrazbayev
Copy link
Copy Markdown
Contributor Author

You're right, @rossbar , the simplified code is more readable also.

@SultanOrazbayev
Copy link
Copy Markdown
Contributor Author

OK, tests are failing due to this:

import networkx as nx

G = nx.trivial_graph()
print(all(n in G for n in []))  # True

Still need to check the special case of a list with 1 item.
Copy link
Copy Markdown
Member

@MridulS MridulS left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @SultanOrazbayev !

@dschult dschult merged commit b15b516 into networkx:main Dec 20, 2022
@jarrodmillman jarrodmillman added this to the networkx-3.0 milestone Jan 4, 2023
MridulS pushed a commit to MridulS/networkx that referenced this pull request Feb 4, 2023
…n path contains nodes not in the graph. (networkx#6272)

* Update simple_paths.py

Current version of `is_simple_path` fails with `KeyError` if the first element of the node list is not in the graph.

* Update simple_paths.py

* Update simple_paths.py

Simplify the test condition. Checking for a single node in the list can be combined with checking for duplicates in the list without meaningful efficiency loss.

* Update test_simple_paths.py

Add the case when the start of the path is a node not in the graph.

* Update simple_paths.py

* Update simple_paths.py

* Update simple_paths.py

* Update simple_paths.py

* Update simple_paths.py

Still need to check the special case of a list with 1 item.
cvanelteren pushed a commit to cvanelteren/networkx that referenced this pull request Apr 22, 2024
…n path contains nodes not in the graph. (networkx#6272)

* Update simple_paths.py

Current version of `is_simple_path` fails with `KeyError` if the first element of the node list is not in the graph.

* Update simple_paths.py

* Update simple_paths.py

Simplify the test condition. Checking for a single node in the list can be combined with checking for duplicates in the list without meaningful efficiency loss.

* Update test_simple_paths.py

Add the case when the start of the path is a node not in the graph.

* Update simple_paths.py

* Update simple_paths.py

* Update simple_paths.py

* Update simple_paths.py

* Update simple_paths.py

Still need to check the special case of a list with 1 item.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Current version of is_simple_path fails with KeyError if the first element of the node list is not in the graph.

5 participants