-
-
Notifications
You must be signed in to change notification settings - Fork 510
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
faster bidirectional dijkstra using pairing heap #39228
faster bidirectional dijkstra using pairing heap #39228
Conversation
a few timings to see the advantage of the new implementation sage: G = graphs.PetersenGraph()
sage: %timeit G._backend.bidirectional_dijkstra_old(0, 1)
10.6 µs ± 31.9 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)
sage: %timeit G._backend.bidirectional_dijkstra(0, 1)
10.4 µs ± 10.4 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)
sage: G = graphs.RandomGNP(10, .5)
sage: %timeit G._backend.bidirectional_dijkstra_old(0, 1)
23.2 µs ± 65 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each)
sage: %timeit G._backend.bidirectional_dijkstra(0, 1)
20.6 µs ± 105 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each)
sage: G = graphs.RandomGNP(10, .3)
sage: %timeit G._backend.bidirectional_dijkstra_old(0, 1)
17.2 µs ± 41.8 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)
sage: %timeit G._backend.bidirectional_dijkstra(0, 1)
15.6 µs ± 41.6 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)
sage: G = graphs.RandomGNP(100, .05)
sage: %timeit G._backend.bidirectional_dijkstra_old(0, 1)
309 µs ± 908 ns per loop (mean ± std. dev. of 7 runs, 1,000 loops each)
sage: %timeit G._backend.bidirectional_dijkstra(0, 1)
259 µs ± 677 ns per loop (mean ± std. dev. of 7 runs, 1,000 loops each)
sage: G = graphs.RandomGNP(100, .1)
sage: %timeit G._backend.bidirectional_dijkstra_old(0, 1)
587 µs ± 1.48 µs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)
sage: %timeit G._backend.bidirectional_dijkstra(0, 1)
437 µs ± 1.2 µs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)
sage: G = graphs.RandomGNP(100, .3)
sage: %timeit G._backend.bidirectional_dijkstra_old(0, 1)
1.61 ms ± 3.18 µs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)
sage: %timeit G._backend.bidirectional_dijkstra(0, 1)
1.2 ms ± 1.67 µs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)
sage: G = graphs.RandomGNP(10000, .0005)
sage: %timeit G._backend.bidirectional_dijkstra_old(0, 1)
35.6 ms ± 170 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)
sage: %timeit G._backend.bidirectional_dijkstra(0, 1)
31.7 ms ± 311 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)
sage: G = graphs.RandomGNP(10000, .001)
sage: %timeit G._backend.bidirectional_dijkstra_old(0, 1)
61.8 ms ± 137 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)
sage: %timeit G._backend.bidirectional_dijkstra(0, 1)
52.5 ms ± 615 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)
sage: G = graphs.RandomGNP(10000, .005)
sage: %timeit G._backend.bidirectional_dijkstra_old(0, 1)
119 ms ± 542 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)
sage: %timeit G._backend.bidirectional_dijkstra(0, 1)
103 ms ± 1 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) |
Documentation preview for this PR (built with commit 3962a93; changes) is ready! 🎉 |
It does seem to be faster indeed. Is there a theoretical guarantee for this? Can you remove the |
With a priority queue, insertion is done in With a pairing heap, insertion is done in |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good. What about replacing the priority queue
with a pairing heap
everywhere in the file?
I did the changes in |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM.
Thanks for the review. |
We improve methods `bidirectional_dijkstra` and `bidirectional_dijkstra_special` from `c_graph.py` by using the new pairing heap data structure (see sagemath#39046) instead of a `priority_queue`. ### 📝 Checklist <!-- Put an `x` in all the boxes that apply. --> - [x] The title is concise and informative. - [x] The description explains in detail what this PR is about. - [ ] I have linked a relevant issue or discussion. - [ ] I have created tests covering the changes. - [ ] I have updated the documentation and checked the documentation preview. ### ⌛ Dependencies <!-- List all open PRs that this PR logically depends on. For example, --> <!-- - sagemath#12345: short description why this is a dependency --> <!-- - sagemath#34567: ... --> URL: sagemath#39228 Reported by: David Coudert Reviewer(s): David Coudert, gmou3
We improve methods `bidirectional_dijkstra` and `bidirectional_dijkstra_special` from `c_graph.py` by using the new pairing heap data structure (see sagemath#39046) instead of a `priority_queue`. ### 📝 Checklist <!-- Put an `x` in all the boxes that apply. --> - [x] The title is concise and informative. - [x] The description explains in detail what this PR is about. - [ ] I have linked a relevant issue or discussion. - [ ] I have created tests covering the changes. - [ ] I have updated the documentation and checked the documentation preview. ### ⌛ Dependencies <!-- List all open PRs that this PR logically depends on. For example, --> <!-- - sagemath#12345: short description why this is a dependency --> <!-- - sagemath#34567: ... --> URL: sagemath#39228 Reported by: David Coudert Reviewer(s): David Coudert, gmou3
We improve methods
bidirectional_dijkstra
andbidirectional_dijkstra_special
fromc_graph.py
by using the new pairing heap data structure (see #39046) instead of apriority_queue
.📝 Checklist
⌛ Dependencies