Skip to content

Adding shortest-paths documentation - #8187

Merged
dschult merged 10 commits into
networkx:mainfrom
amcandio:shortest-paths-docs
Aug 31, 2025
Merged

dschult merged 10 commits into
networkx:mainfrom
amcandio:shortest-paths-docs

Conversation

@amcandio

@amcandio amcandio commented Aug 8, 2025 •

Copy link
Copy Markdown
Contributor

After working on #8169 I realized that the whole Shortest Path section lacked some introduction and context.

In this PR I extend the docs with some definitions, examples and time-complexity of the different supported algorithms.

Follow-up sections I was thinking we can add:

  • Notes on negative weights and cylces
  • Translating path from list of nodes into list of edges

Render

image image

@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.

Thanks for this @amcandio , this is a nice high-level overview of shortest paths and IMO the top-level reference docs are a great place for it! A few minor grammatical nits but other than that LGTM - thanks for writing this up!

@amcandio

Copy link
Copy Markdown
Contributor Author

Thanks Ross! @dschult what do you think?

@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.

This is exactly where such a description should go. Thanks for putting this together! I have some suggestions here (and a nitty grammar suggestion below).

This description makes it seem like the four algorithms are the inputs to the method parameter. It also includes "Floyd-Warshall" even though that is not available via the simplified interface at all. And it combines three ideas into one description: the algorithm names, the simplified interface, and the time complexity. But it leaves out whether this is a single path, all paths from a single source/target, or all pairs all paths.

Perhaps the table can replace column "Graph Type" with "Path Type". And add a sentence stating how complexity changes with how many paths are returned.

Can we remove discussion of the simplified interface in the paragraph about algorithm selection (and maybe call it "algorithms" instead of "algorithm selection"? We have also left out the johnson algorithm and bidirectional dijkstra. So maybe its 6 algorithms now.

Separating the discussion of algorithms from algorithm selection in the simplified interface is probably helpful here. So, call the section Algorithms and describe them without reference to the simplified interface.

Then as an intro to the simplified interface, put some text saying that Path Type is determined by whether either of method or weight is provided. And add another paragraph about what is returned based on values of source and target.

The code for dispatching the simplified interface only accepts method values "None", "dijkstra" and "bellman-ford". And the algorithm is completely determined by the method, weight, source and target inputs. If this is too much info for this part of the docs, we can refer to the simplified interface function docs.

@amcandio

Copy link
Copy Markdown
Contributor Author

This is great feedback Dan, thanks! I separated algorithms and interface sections. In the algo section I added the missing algorithms and kept the complexity table per Graph type. In the simplified interface section I explain the different "path types", which I refer to as "query types".

What do you think?

@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.

I like the changes... I still find the complexity description incomplete though. What is this the complexity of? doesn't that depend on the query type? Complexity is misused so often. Let's be specific about what we are saying. It'd be good to identify what the asymptotic limit is as well [It's really too bad that Big-O doesn't say what the limit is. O(x^2) is usually x-> 0, while O(N^2) is usually N->\infty. And mixing symbols like V and E in the same expression means we don't know which limit is happening first, or whether there is a relation between the symbols in that limit. And this doesn't even get to the point that it all gets multiplied by a constant that differs between algorithms.]

What does the "Graph Type" mean? Isn't it the query that is weighted or unweighted? We can certainly ask for an unweighted path on a weighted graph.

And I don't think it is true that the user can control which specific algorithm to use via the method parameter. We should include a little more details. For example, you can't set method to be "Breadth-First Search" or "BFS". And setting method="djikstra" might give you Bidirectional Djikstra.

Maybe the paragraph after the table could include something about:
For an unweighted query, BFS is used, with a bidirectional BFS when both source and target are specified. For a weighted query, Dijkstra is used and again bidirectional when both source and target are specified. Bellman Ford is used for weighted queries when method=="bellman-ford" and no bidirectional version is supported.

Looks like bidirectional might be better described with a single sentence in the algorithms section and remove that row from the table. But you can choose. The issue is that both dijkstra and BFS have bidirectional versions. So we should either add bi-BFS to the table or leave out both bi- algs. The complexity is the same IIUC so taking that out of the table and describing with a sentence might be clearest.

:)

@amcandio

Copy link
Copy Markdown
Contributor Author

Okay so next round of changes!

  • I clarified unweighted vs weighted and query type on time complexity table
  • I introduced different query types on Algorithm section, mentioning special cases such as bi-directional variants
  • Clarified algorithm selection on simplified interface

@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 sticking with this @amcandio
I've only got a couple of comments.

  • The abbreviation BFS is used before introduced, so I suggested it in the table (but maybe its better elsewhere.
  • The method parameter is only effective if specified as "bellman-ford". There isn't a way to specify unweighted/BFS and specifying "djikstra" is the same as the default. I tried to suggest a reword that puts forward the limitations of the method parameter. Other wording can work too.
  • The last sentence/paragraph is basically the same as another.

Thanks for you work on this!

@amcandio

Copy link
Copy Markdown
Contributor Author

Thanks! I applied your suggestions and deduped the bidirectional comment

@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 @amcandio ! This is very helpful!

@dschult
dschult merged commit a7bb0e1 into networkx:main Aug 31, 2025
@jarrodmillman jarrodmillman added this to the 3.6 milestone Aug 31, 2025
alfred-mikhael pushed a commit to alfred-mikhael/networkx that referenced this pull request Sep 3, 2025
* Adding shortest-paths documentation

* Fix bellman ford negative cycles

* Addressing PR feedback

* Apply suggestion from @dschult

Co-authored-by: Dan Schult <[email protected]>

* Reworking docs

* Fix trailing whitespaces

* Addressing write-up feedback

* Apply suggestion from @dschult

Co-authored-by: Dan Schult <[email protected]>

* Apply suggestion from @dschult

Co-authored-by: Dan Schult <[email protected]>

* Deduping bi-directional doc

---------

Co-authored-by: Dan Schult <[email protected]>
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