Skip to content

single_source_all_shortest_paths: don't loop over all nodes - #7762

Merged
rossbar merged 1 commit into
networkx:mainfrom
Celelibi:fix_GH7762_faster_single_source_all_shortest_paths
Dec 19, 2024
Merged

rossbar merged 1 commit into
networkx:mainfrom
Celelibi:fix_GH7762_faster_single_source_all_shortest_paths

Conversation

@Celelibi

@Celelibi Celelibi commented Dec 12, 2024 •

Copy link
Copy Markdown
Contributor

Current Behavior

Currently, the function single_source_all_shortest_paths contains this line:

for n in G:

Which might be a significant waste of time if most nodes aren't reachable from the given source.

Expected Behavior

Merely looping on the keys of pred could lead to a significant improvement for large very sparse graphs.

for n in pred:

And I guess the try-except in the loop is then no longer necessary.

Steps to Reproduce

Here's an example that shows the performance issue. The more nodes in the graph, the slower, even if they are unreachable. Even though it might not be an issue for a single call to that function, when calling it on several sources, it quickly adds up. (See Additional context below.)

>>> import networkx as nx
>>> g = nx.DiGraph()
>>> g.add_edges_from((n, n+1) for n in range(1000000))
>>> nx.add_path(g, "ABZ")
>>> nx.add_path(g, "ACZ")
>>> nx.add_path(g, "ADZ")
>>> dict(nx.single_source_all_shortest_paths(g, "A"))
{'A': [['A']], 'B': [['A', 'B']], 'Z': [['A', 'B', 'Z'], ['A', 'C', 'Z'], ['A', 'D', 'Z']], 'C': [['A', 'C']], 'D': [['A', 'D']]}

Environment

Python version: 3.12.8
NetworkX version: 3.2.1

Additional context

Day 10 part 2 of Advent of Code has a graph with many small connected components and asks to find the number of paths from all the nodes marked 0 to all the nodes marked 9.
https://adventofcode.com/2024/day/10
As it turns out, calling single_source_all_shortest_paths for all source nodes is pretty slow and takes 1.5 seconds for me. With the proposed change, it only takes 50ms.

@dschult

dschult commented Dec 10, 2024

Copy link
Copy Markdown
Member

Yes -- this is a nice suggestion. The helper function checks if n in pred in its first lines and rasies precisely the exception the main function then looks for. We know which nodes will cause the exception because we know pred at the time of the loop. So change the lopp to be over pred instead of G and remove the try/except. Thanks very much!!

And I think it is really cool that adventofcode led you to this improvement! :) What a nice project.

@dschult dschult left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for the fix!!

But it looks like the fix changes the order of the iterated dict entries which makes a doctest fail. I've made a suggestion below which shouldn't slow things much but keeps the node order the same as in G. Thoughts?

We already know which nodes are reachable. No need to iterate the
whole graph.

Signed-off-by: Celelibi <[email protected]>

@dschult dschult left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yup! Sorry for the wrong line number.
Thanks for the suggestion and help and getting it all ready!

@rossbar rossbar left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM, thanks @Celelibi and @dschult for the improvements + review!

@rossbar
rossbar merged commit 3b8f470 into networkx:main Dec 19, 2024
@jarrodmillman jarrodmillman added this to the 3.5 milestone Dec 19, 2024
@Celelibi
Celelibi deleted the fix_GH7762_faster_single_source_all_shortest_paths branch December 30, 2024 14:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

4 participants