-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Description
Current Behavior
Due to this conditional:
networkx/networkx/algorithms/simple_paths.py
Lines 253 to 254 in 5f2f88f
| if source in targets: | |
| return _empty_generator() |
whenever the source node appears as one of the possible targets, all_simple_paths always generates an empty list of paths.
For example:
import networkx as nx
graph = nx.Graph([(1, 2), (1, 3)])>>> list(nx.all_simple_paths(graph, source=1, target={1, 2, 3}))
[]Expected Behavior
Apologies if I have misunderstood what the function is meant to do. Here's what I understand.
The function generates a list of simple paths from the given source to (any of the) given target(s). The docstring says:
Pass an iterable of nodes as target to generate all paths ending in any of several nodes
So the fact that the source node is in the set of target nodes does not necessarily exclude all paths, there could be some other target that works. For instance, in the example above the function missed out on two perfectly reasonable paths: [1, 2] and [1, 3]. As far as I understand they both satisfy the description above: they're simple paths from the source (1) to any of the targets (1, 2, 3).
So why does it return an empty generator? It should return the same as list(nx.all_simple_paths(graph, source=1, target={2, 3})), that is, [[1, 2], [1, 3]].
I understand that any path from source to itself wouldn't be simple, but again this doesn't automatically exclude paths to any of the other specified targets.
Steps to Reproduce
import networkx as nx
graph = nx.Graph([(1, 2), (1, 3)])
list(nx.all_simple_paths(graph, source=1, target={1, 2, 3}))Environment
Python version: 3.11
NetworkX version: 5f2f88f (3.1)