-
-
Notifications
You must be signed in to change notification settings - Fork 504
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
add a pairing heap data structure #39046
add a pairing heap data structure #39046
Conversation
Note that several exceptions raised by the methods ( |
Documentation preview for this PR (built with commit e15825a; changes) is ready! 🎉 |
It seems that the meson/conda CI is not using the current stable version of Cython. Reported errors about |
One remark after a quick look at the C++ code: why do you use a std::map and not a std::unordered_map ?
But for unordered_map
It implies, for exemple, that with the current code, the method |
If I understand well, the objects you use with |
Indeed, it is a additional constraint on the type The use of Why do you need the |
I find it easier for the user to interact with this class using an item rather than with an object of type |
You could implement a
This structure would be very similar to |
We can certainly do that but then the use of the data structure will be more complex and some parts of the complexity are only deported to the user. For instance, if I want to implement Dijkstra, I will need a mapping for the association between the nodes of the graph and the iterator. I might be wrong, but if we have examples of hash functions and how to use them, then we can use |
For information, I am interested in this PR because I contemplated the idea of implementing something similar to have cleaner code in my PR #39038 (and because it could be of interested in other case). There would have been two differences with what you have implemented:
So i spent some time looking at the code of |
As it is an internal data structures, you can assume to have some control on which types will be used for |
We can provide multiple implementations. This PR already has 3 ;) and it can become a subproject like |
I changed to It is much faster this way. |
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.
I just reviewed the C++ code for now
I was finally able to integrate all your suggestions in |
May be we can skip that for the moment. It can be added later if really needed. |
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.
I've seen people insist on it, so I assume it has some importance.
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.
Can the third C++
implementation be used? If not, why is it included?
I feel there is some redundancy with having a PairingHeap_class
. There should essentially only be a PairingHeap_of_n_integers
with a PairingHeap_of_n_hashables
delegating to PairingHeap_of_n_integers
after some dictionary translations (similar architecture to DisjointSet
).
Yes, it can be used as shown in methods
Doing so would force to also maintain the bitset |
Ok. Another thing I was thinking about is to use a PS. there remain two |
But then, how do you know the association between an item and the pointer / node in the heap ? With current implementation it is transparent for users. |
Instead of an |
I understand what you have in mind, but I don't think it is a good idea. Instead of targeting the fastest possible implementation, we should target the most convenient one / the most suited for the kind of usage we will have. This is why I proposed 3 versions. Note that we can add methods to |
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.
Ok then.
Thank you all for the thorough review. The code has been greatly improved ! |
This PR adds a pairing heap data structure, that is a min-heap data structure with decrease key operation (see https://en.wikipedia.org/wiki/Pairing_heap). 3 implementations are proposed: - one with fixed capacity, items in range $0..n-1$ and values of any comparable type. - one with fixed capacity accepting any type of hashable items. Values can be of any comparable type. - one with unbounded capacity where the types of items and values must be defined at C level. It is an interface to a templated C++ implementation and is for internal use only. The 2 other implementations can be accessed from the shell. This can be useful for example for algorithms Dijkstra. ### 📝 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. - [x] I have created tests covering the changes. - [x] 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#39046 Reported by: David Coudert Reviewer(s): cyrilbouvier, David Coudert, gmou3, user202729
This PR adds a pairing heap data structure, that is a min-heap data structure with decrease key operation (see https://en.wikipedia.org/wiki/Pairing_heap). 3 implementations are proposed: - one with fixed capacity, items in range $0..n-1$ and values of any comparable type. - one with fixed capacity accepting any type of hashable items. Values can be of any comparable type. - one with unbounded capacity where the types of items and values must be defined at C level. It is an interface to a templated C++ implementation and is for internal use only. The 2 other implementations can be accessed from the shell. This can be useful for example for algorithms Dijkstra. ### 📝 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. - [x] I have created tests covering the changes. - [x] 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#39046 Reported by: David Coudert Reviewer(s): cyrilbouvier, David Coudert, gmou3, user202729
Similarly to sagemath#39216, we avoid the conversion to `short_digraph` when the input graph is an instance of `StaticSparseBackend`. Furthermore, we use the new `PairingHeap_of_n_integers` data structure (sagemath#39046) instead of a `priority_queue` to emulate a max-heap. This is slightly faster this way and cleaner. ### 📝 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. - [x] I have linked a relevant issue or discussion. - [x] 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#39217 Reported by: David Coudert Reviewer(s): Travis Scrimshaw
Similarly to sagemath#39216, we avoid the conversion to `short_digraph` when the input graph is an instance of `StaticSparseBackend`. Furthermore, we use the new `PairingHeap_of_n_integers` data structure (sagemath#39046) instead of a `priority_queue` to emulate a max-heap. This is slightly faster this way and cleaner. ### 📝 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. - [x] I have linked a relevant issue or discussion. - [x] 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#39217 Reported by: David Coudert Reviewer(s): Travis Scrimshaw
This PR adds a pairing heap data structure, that is a min-heap data structure with decrease key operation (see https://en.wikipedia.org/wiki/Pairing_heap).
3 implementations are proposed:
This can be useful for example for algorithms Dijkstra.
📝 Checklist
⌛ Dependencies