Skip to content

all_simple_paths returns empty generator when the source is one of the possible targets #6690

@plammens

Description

@plammens

Current Behavior

Due to this conditional:

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions