Skip to content

Add single_source_all_shortest_paths and all_pairs_all_shortest_paths - #5959

Merged
dschult merged 17 commits into
networkx:mainfrom
dzy49:feature-for-issue-5949
Jul 27, 2023
Merged

dschult merged 17 commits into
networkx:mainfrom
dzy49:feature-for-issue-5949

Conversation

@dzy49

@dzy49 dzy49 commented Sep 1, 2022 •

Copy link
Copy Markdown
Contributor

as discussed in issue #5949, add single_source_all_shortest_paths and all_pairs_all_shortest_paths

add single_source_all_shortest_paths and all_pairs_all_shortest_paths
@dzy49 dzy49 changed the title Update generic.py add single_source_all_shortest_paths and all_pairs_all_shortest_paths Sep 1, 2022
@rossbar

rossbar commented Sep 13, 2022

Copy link
Copy Markdown
Contributor

Thanks for this @dzy49 ! I think there's general consensus that this is a good idea and the right way to go. It'd be good to add some tests for the new functions, though we likely don't need a ton of testing since these new functions are thin wrappers on top of existing (hopefully well-tested) functionality.

@dzy49

dzy49 commented Sep 16, 2022

Copy link
Copy Markdown
Contributor Author

@rossbar Hi, as you suggested, I added some simple tests, however automatic testing is saying "Your tests failed on CircleCI", and the output isn't making much sense, is there something I forgot to do?

@rossbar

rossbar commented Sep 19, 2022

Copy link
Copy Markdown
Contributor

however automatic testing is saying "Your tests failed on CircleCI", and the output isn't making much sense, is there something I forgot to do?

No these failures are unrelated. The underlying issue should be fixed on main so you should be able to fix this by simply merging with main and pushing!

@dzy49

dzy49 commented Sep 20, 2022 •

Copy link
Copy Markdown
Contributor Author

however automatic testing is saying "Your tests failed on CircleCI", and the output isn't making much sense, is there something I forgot to do?

No these failures are unrelated. The underlying issue should be fixed on main so you should be able to fix this by simply merging with main and pushing!

Okay. I merged with main but it seems like the issue is still there

@rossbar

rossbar commented Sep 21, 2022

Copy link
Copy Markdown
Contributor

Okay. I merged with main but it seems like the issue is still there

Oops - sorry about that. I thought we had fixed all of these but it turns out we missed one. Everything should now be fixed as of #5994 - one final merge with main should do the job.

@wsh2836741

Copy link
Copy Markdown

Thanks for useful work, and i want to know when we can use this function?

@dschult

dschult commented Sep 28, 2022

Copy link
Copy Markdown
Member

Well -- you can use it now via the branch of the PR, but I think you mean: when will it be merged with the NetworkX repo? We like these functions and the implementation, but are looking for time to review it.
It will happen "soon".

@wsh2836741

Copy link
Copy Markdown

@dschult Hi, thanks for your help, and i want to know how can i use the branch of the PR, i see the branch is "feature-for-issue-5949" and i use the command "git clone -b feature-for-issue-5949 [email protected]:networkx/networkx.git", but it said "fatal: Remote branch feature-for-issue-5949 not found in upstream origin". How can i use it now via the branch of the PR?

@MridulS

MridulS commented Oct 9, 2022

Copy link
Copy Markdown
Member

@wsh2836741 the quickest way here would be to pip install this branch in your python environment.

pip install https://github.com/dzy49/networkx/archive/feature-for-issue-5949.zip

@wsh2836741

Copy link
Copy Markdown

Hi all, i use this branch and find a bug when there is an unconnected node.
image

@dschult

dschult commented Oct 10, 2022

Copy link
Copy Markdown
Member

That is a reasonable result. If nodes in the graph are not connected then you can't compute all shortest paths between all pairs of nodes. There is no path between the nodes. So the function raises an exception.

@wsh2836741

Copy link
Copy Markdown

@dschult That is not a reasonable result. These is no exception when i use the function "all_pairs_shortest_path". If nodes in the graph are not connected then the function "all_pairs_all_shortest_paths" will not compute all shortest paths and the nodes will not appear in the return result of the function, but not raises an exception.

@dschult

dschult commented Oct 11, 2022

Copy link
Copy Markdown
Member

Point taken -- it is certainly not an optimal result if you want the partial results given by the pairs that are connected. As you point out, the current all_pairs_shortest_path functions don't raise an exception in these conditions. I'm +1 for making that happen with these functions too. Thanks for your input @wsh2836741

@dzy49

dzy49 commented Oct 11, 2022 •

Copy link
Copy Markdown
Contributor Author

Point taken -- it is certainly not an optimal result if you want the partial results given by the pairs that are connected. As you point out, the current all_pairs_shortest_path functions don't raise an exception in these conditions. I'm +1 for making that happen with these functions too. Thanks for your input @wsh2836741

I have thought about this, but in my personal use case all my graphs were connected, so I didn't pay much attention to the behaviors when graphs are not connected.
Also at the time I thought this was more similar to the behavior of all_shortest_paths, but now I think it makes more sense to output partial results

@wsh2836741

Copy link
Copy Markdown

@dzy49 Yes,i think output partial results is more useful. Can you modify the code? Thanks a lot.

@dzy49

dzy49 commented May 6, 2023 •

Copy link
Copy Markdown
Contributor Author

@dzy49 Yes,i think output partial results is more useful. Can you modify the code? Thanks a lot.

I made the change and now it outputs partial results, simialr to all_pairs_shortest_path.
(I ended up getting busy with the research project and forgot about this! And the work has just been accpeted for publicaiton, which reminded me)
Regardless of whether this can be merged or not, I think a note should be added to all_pairs_shortest_path indicating that it only outputs one shortest path, even if multiple shortest paths exist.

@dzy49 dzy49 closed this May 6, 2023
@dzy49 dzy49 reopened this May 6, 2023
@dzy49

dzy49 commented May 8, 2023 •

Copy link
Copy Markdown
Contributor Author

@dschult @rossbar since it has been a while, should I close this and open a new pull request?

@dschult

dschult commented May 10, 2023

Copy link
Copy Markdown
Member

No need to open a new PR. Its a busy time of year, but we'll get to it.
Thanks!!

@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 this and thanks for waiting for us to review it.

  • please go ahead and add a note to the all_pairs_shortest_path doc_string that If there are ties (more than one shortest path) only one of them is reported. And (maybe instead?) you could/should add these new functions to the "See Also" section in those doc_strings.

  • can you wrap the text in the doc_strings to be shorter than the (88) max text width for code? It's Ok to make that smaller if that is easier (like wrap to 80). That doesn't work well sometimes for examples where the output has to match what is actually created exactly. In that case (i.e. line 635 which prints all the possible shortest_paths), can you change the example so that it prints a shorter output -- like make the dict and then print some interesting parts of it. e.g. all_paths[0][3] and all_paths[0][2]. Otherwise the documentation gets hard to read. You can put the full output test into the tests.

  • Are the resulting orders of the paths consistent? I mean do we need the sorted function on the tests? Generally, if the algorithm uses dicts (which are now ordered) and lists, the order will be the same over different hardware and memory. If we use sets, then the order is arbitrary and can vary.

  • Other possible methods could be Floyd-W and Johnson. No reason to implement them as far as I'm concerned, but can you comment about whether there is a reason not to do that?

More comments below.

dzy49 added 3 commits June 9, 2023 06:03
add note to all_pairs_shortest_path
line width for example
fixed raise
@dzy49

dzy49 commented Jun 9, 2023 •

Copy link
Copy Markdown
Contributor Author

Hi, I made changes according to your suggestions!

added a note to all_pairs_shortest_path
fixed line width for example
changed the example to nx.cycle_graph(4) and fixed raise issue

For the sorted, I see that test_all_shortest_paths is using sorted, and since my methods are using similar internal implementation as all_shortest_paths, I am also using sorted, although I do see your point, they might not be necessary here

For other possible methods, for now, I am just leveraging the existing method since this is the easiest.
For reasons why not to use other algorithms: I think Floyd-W performs worse on sparse graphs than using Dijkstra’s for every vertex. it is also kind of complicated to handle ties for path reconstruction.
I think it might be good to implement with Johnson though, as it has similar runtime but in addition also handles negative weights. (I tested the existing nx.johnson method, it doesn't seem to handle ties)

@dschult

dschult commented Jun 9, 2023

Copy link
Copy Markdown
Member

The current Johnson method doesn't handle ties. I'm not sure whether it is worth adding it here. If you want to, go for it. But if you'd rather get this merged, we can put that in later.

  • Can you remove the "print" part of the examples? The >>> form in the examples prints the results so no print statement should be needed. That also makes it easier to read those commands on the doc-pages.
  • Many of the doc_string lines are longer than 88 chars. I can shift this, but if you want to you could do it.

Looking good!

@dzy49

dzy49 commented Jun 10, 2023 •

Copy link
Copy Markdown
Contributor Author

Thanks! I fixed the line width and print statement. The reason why I had print statement was that the example of all_shortest_paths used print.
And for Johnson, maybe I could do it later in a different PR. For consistency, it might be good to create versions of floyd_warshall and johnson that handle ties

@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 shortened the doc_string in unweighted.py.

This looks good to me -- ready for merge after another set of eyes.
Thanks!!

@dzy49

dzy49 commented Jun 19, 2023 •

Copy link
Copy Markdown
Contributor Author

@rossbar I am wondering if you can take a look at this? thanks!

@jarrodmillman jarrodmillman changed the title add single_source_all_shortest_paths and all_pairs_all_shortest_paths Add single_source_all_shortest_paths and all_pairs_all_shortest_paths Jul 26, 2023
@dschult
dschult merged commit 9da9a27 into networkx:main Jul 27, 2023
@jarrodmillman jarrodmillman added this to the 3.2 milestone Jul 27, 2023
Alex-Markham pushed a commit to Alex-Markham/networkx that referenced this pull request Oct 13, 2023
…networkx#5959)

* Update generic.py

add single_source_all_shortest_paths and all_pairs_all_shortest_paths

* test and code format

* black format

* add simple tests

* changed behavior when the graph is not connected

* address issues

add note to all_pairs_shortest_path
line width for example
fixed raise

* fix missing blank line

* format

* line width and print

* wrap doc_string from weighted.py

---------

Co-authored-by: Dan Schult <[email protected]>
dschult added a commit to BrunoBaldissera/networkx that referenced this pull request Oct 23, 2023
…networkx#5959)

* Update generic.py

add single_source_all_shortest_paths and all_pairs_all_shortest_paths

* test and code format

* black format

* add simple tests

* changed behavior when the graph is not connected

* address issues

add note to all_pairs_shortest_path
line width for example
fixed raise

* fix missing blank line

* format

* line width and print

* wrap doc_string from weighted.py

---------

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

dzy49 commented Apr 1, 2024

Copy link
Copy Markdown
Contributor Author

Hello, I've noticed that the documentation for the newly merged functions is not appearing on the NetworkX documentation site. Are there any specific steps or updates required to ensure these functions are included in the documentation?

@dschult

dschult commented Apr 1, 2024

Copy link
Copy Markdown
Member

Rats! I didn't look at the PR closely enough.

For each of the new functions you should include an entry in the appropriate .rst file in doc/reference/algorithms/shortest_paths.rst
If the pattern from the list of other functions is not clear, ask questions (here or in a new PR).

Those changes should be made in a new PR. The tests in the new PR include a test that builds the docs, and another test that loads the html pages to a temporary url (the test appears as "document artifact"). Look on the "Details" link for that test to go to the documentation webpage with that PRs changes included.

cvanelteren pushed a commit to cvanelteren/networkx that referenced this pull request Apr 22, 2024
…networkx#5959)

* Update generic.py

add single_source_all_shortest_paths and all_pairs_all_shortest_paths

* test and code format

* black format

* add simple tests

* changed behavior when the graph is not connected

* address issues

add note to all_pairs_shortest_path
line width for example
fixed raise

* fix missing blank line

* format

* line width and print

* wrap doc_string from weighted.py

---------

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.

6 participants