-
-
Notifications
You must be signed in to change notification settings - Fork 596
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
{c,sparse}_graph: systematically turn integer-like vertices into ints #22374
Comments
Commit: |
Replying to @jdemeyer:
I agree that "Leaving the vertices alone would be a better fix". But I don't understand why you don't just do that. You say that you want to access the vertices using Sage integers, but isn't that possible regardless of whether the vertices are internally stored as |
comment:6
If I remember right, generic graphs internally store their vertices as ints, and keep a separate mapping between those ints and the public labels, which can be more or less arbitrary objects. As an (apparently crucial?) optimization, public labels that are (small?) integers are used as internal labels, bypassing this translation layer. Storing Integer vertex labels as Integers would mean using the indirect representation. This would be fine if graph vertices were compared by identity rather than by equality, but while this might have been a saner design, that's clearly not what people expect from the existing code. If however we want to keep the ability to add and access integer vertices indifferently by passing integers of any type, then it seems more reasonable to me to stick to a single representation whenever possible. And in any case, I really don't understand the graphs code well enough to have anything else to suggest. |
comment:7
Don't break this doctest: diff --git a/src/sage/graphs/generic_graph.py b/src/sage/graphs/generic_graph.py
index a3fec74..26b2c83 100644
--- a/src/sage/graphs/generic_graph.py
+++ b/src/sage/graphs/generic_graph.py
@@ -20161,7 +20161,7 @@ class GenericGraph(GenericGraph_pyx):
Relabeling using a dictionary. Note that the dictionary does not define
the new label of vertex `0`::
- sage: G.relabel({1:2,2:1}, inplace=False).am()
+ sage: G.relabel({int(1):2,int(2):1}, inplace=False).am()
[0 0 1]
[0 0 1]
[1 1 0] |
comment:8
Replying to @mezzarobba:
Do you know where this is implemented? Maybe we can just fix that to support Sage Integers too. |
comment:9
Replying to @jdemeyer:
I think part of it happens in |
comment:10
Ccing you in case there would be something to salvage from here, since you've apparently be doing lots of related work recently. |
comment:11
To the best of my understanding (this code is not easy):
This said, I don't understand why we have that
Requires more investigation... |
comment:12
When you create an empty digraph using,
Consider the following example: sage: g = DiGraph()
sage: g.add_vertices([9, 10, 0])
sage: [(v, type(v)) for v in g]
[(0, <type 'sage.rings.integer.Integer'>),
(10, <type 'sage.rings.integer.Integer'>),
(9, <type 'int'>)] When adding vertex The motivation of this optimization is to save space and few tests when the user creates vertices in the right order. But above example shows how easy it is to loose this optimization. Shouldn't we should simplify the code and use mappings for all vertices ? |
Instead of doing it or not depending on the phase of the moon:
Leaving the vertices alone would be a better fix in principle. Unfortunately, it would break compatibility too badly: for example, people clearly expect to be able to create graphs in library code, using Python ints as labels, and then access the vertices using Sage integers.
CC: @dcoudert
Component: graph theory
Author: Marc Mezzarobba
Branch/Commit: u/jdemeyer/_c_sparse__graph__systematically_turn_integer_like_vertices_into_ints @
61feee1
Issue created by migration from https://trac.sagemath.org/ticket/22374
The text was updated successfully, but these errors were encountered: