-
-
Notifications
You must be signed in to change notification settings - Fork 489
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
Hash and equality for graphs #15278
Comments
comment:1
Shouldn't the hash of graphs be cached, for efficiency? Currently it does Note that #12601 makes it possible to comfortably turn the hash (and other special methods) into a cached method. |
This comment has been minimized.
This comment has been minimized.
comment:2
Concerning "weighted": What exactly is the semantics? Is it the case that a weighted graph has positive integral edge weights, that could be taken as the multiplicity of this edge? But then, do we want that a weighted graph is equal to a multigraph with the corresponding multiplicity of edges? In any case, the hash is currently broken, since it does not take into account weightedness, but equality check does. It seems to me that the following is the easiest way to go:
|
Dependencies: #12601 |
comment:3
Replying to @simon-king-jena:
Perhaps I am over-eager at this point. The hash is not broken. Being broken means that two graphs that evaluate equal have distinct hash values. But this can currently not happen. So, it's fine. Therefore I modify the "easiest way to go":
Do you agree on using #12601? |
comment:4
A related question: Shouldn't
Moreover, |
comment:5
Moreover, copying a graph currently erases the
|
Branch: u/SimonKing/ticket/15278 |
Commit: |
New commits:
|
New commits:
|
New commits:
|
Author: Simon King |
comment:10
I have uploaded a preliminary "patch" (or branch, actually). The existing tests pass. What it already does:
So far, I am not setting the |
comment:11
Sorry for the delay. I moved a lot through Paris with my backpack during the Nathann |
comment:12
This has a branch, but its status is "new", not "needs review". Is it ready for review, Nathann? |
comment:13
Oooops. I just notice that I am the author. So, I should read my own code in order to see whether it is ready for review or not... |
comment:14
Back at questions on the static graph backend... In
For immutability, it is thus essential that these methods do not change the answers after applying non-underscore methods.
So, my question boils down to this, Nathann: Is it possible for the static backend that the return value of the above methods 1.--4. changes, if the user is only using non-underscore methods on the graph? If it is not possible, then the The hash only takes into account the tuple of vertices and edges (which includes their labels). You have already confirmed that this is fine with the static backend. |
comment:15
Replying to @nathanncohen:
It will probably be fine to keep them in
OK, but removing ".weighted()" from
Nope.
With the decorator, an error would be raised whenever Best regards, Simon |
comment:16
Yooooooooo !!!
Nope.
You can't be Happy. This guy is already named Happy, and there can't be two.
While saying each time that the labels CAN change if they are lists and that the users append/remove things from them. But indeed Nathann |
comment:17
Replying to @nathanncohen:
Honorary!
While replying each time that I consider this a misuse, and if a user mutates labels manually (without using "official" methods) then (s)he should be ready to bear the consequences. So, I don't care about mutable labels |
comment:18
Replying to @simon-king-jena:
To my automatic spell checker: "Hooray", not "honorary"... |
comment:19
Indeed, indeed.
Ahahahah. Yeah yeah we understand each other. I'm just making it impossible for you to quote me saying that there will never be such problems in the future It should all work fine. I just looked at these Have fuuuuuuuuuuuuuuuuuuuuun ! Nathann |
comment:20
Is this a bug?
|
comment:21
Replying to @simon-king-jena:
It is ! My mistake ! And it is fixed by #15491, which removes the ten characters that shouldn't have been there Nathann |
comment:67
Oh. I see. Hacks. Well, that makes sense indeed. Looks like a proof that we shouldn't rely on this Is there an usual "is_immutable" function in immutable objects ? Perhaps we should do this. This function would check the actual backend, and return its answer accordingly. This being said, checking the backend does not exactly mean that all other properties of graphs are "protected", like the embedding and everything. Hmmm... Okay, I just read your latest post : the point is that this ._immutable flag is a hack. Shouldn't we just make their code use the new backend, so that their code is not hacked anymore ? Right now they claim their graphs are immutable, though they aren't. Seems like the way to go, isn't it ? Nathann |
comment:69
Argh! It happened again!!! I merged "develop" into my branch and forgot to remove it before pushing. Did I mention that I don't like git? Anyway, the commit that is now being pushed should solve the problem. New commits:
|
comment:70
Well the problem it solves is making this ticket compatible with beta2. So the hack stays ? Nathann |
comment:71
Replying to @nathanncohen:
That's why both the flag and the type of the backend are checked before returning "self" as a copy.
In a way, yes. In a different way: Are we supposed to clean up their mess? If they use the static backend, as they should, then the should also let |
comment:72
Replying to @nathanncohen:
I only see one place where
So, perhaps it would not be too much of a problem to change this to using the static sparse backend. But then, we also need to identify where stuff is copied. |
comment:73
Okay.
... You say that it may be easy in the end, but let's not take chances with this patch either. I'll check it again, give it a positive review, and we'll move on to another ticket. By the way, as we are talking about all we hate : does it take you several minutes to load that page each time you want to answer to a comment on this ticket ? This is a major source of frustration too Nathann |
comment:74
I get a doctest error in generic_graph.py :
Nathann |
comment:75
Meanwhile I see at what point the copy is taken: It is def transitive_reduction(self):
r"""
Returns a transitive reduction of a graph. The original graph is
not modified.
A transitive reduction H of G has a path from x to y if and only if
there was a path from x to y in G. Deleting any edge of H destroys
this property. A transitive reduction is not unique in general. A
transitive reduction has the same transitive closure as the
original graph.
A transitive reduction of a complete graph is a tree. A transitive
reduction of a tree is itself.
EXAMPLES::
sage: g=graphs.PathGraph(4)
sage: g.transitive_reduction()==g
True
sage: g=graphs.CompleteGraph(5)
sage: edges = g.transitive_reduction().edges(); len(edges)
4
sage: g=DiGraph({0:[1,2], 1:[2,3,4,5], 2:[4,5]})
sage: g.transitive_reduction().size()
5
"""
from copy import copy
from sage.rings.infinity import Infinity
G = copy(self)
for e in self.edge_iterator():
# Try deleting the edge, see if we still have a path
# between the vertices.
G.delete_edge(e)
if G.distance(e[0],e[1])==Infinity:
# oops, we shouldn't have deleted it
G.add_edge(e)
return G Hence, it seems that we indeed want a mutable copy here. And perhaps it isn't their mess after all, as this method is broken for all truely immutable graphs. |
comment:76
Ahahaha. Well, given that there are no immutable graphs in Sage at the moment, this function can't really be said to be broken They needed immutable graphs and didn't want to implement them, i.e. to deal with this kind of things. So well, that's our job now and we have to find out when they need immutable graphs and when they don't This copy has to be flagged with "immutable=False" (but that's only available in my patch) or with a specific data structure. So let's finish this patch with your fix, I'll give it a positive review once it passes the tests. Then there is my patch that enables "copy" (that I will have to rebase), then on top of it there will be a patch to use immutable graphs in posets as they should be. Aaaaaaaand now I have to go eat Nathann |
comment:77
I'm back ! If you can fix your last commit we can set this patch to positive review again. And considering how fast Volker merges them it could be available soon Nathann |
Branch pushed to git repo; I updated commit sha1. New commits:
|
comment:79
Fixed the syntax error, and checked that the failing test passes. So, with the last two commits, we counter-hacked the hack with the |
comment:80
Yep. And they just passed on my computer too in graph/ combinat/ and misc/. Soooo
Nod.
Nod. Plus I will probably have some conflicts because of what I do in
Nod. I fear that there may be nasty surprises hiding, though. For instance, I'm rather convinced that the static backend will produce a HUGE slowdown in their code, because right now distance computations (which translates as comparison in posets) is done by some code specific to the sparse backend. And thus does not exist for the static backend. So this will have to be written too. And then it should be much faster Nathann P.S. : it takes a lifetime to add a comment on a ticket. |
comment:81
Question, after some off-ticket discussion: Pickling of immutable graphs does not work. Should we remove the positive review and implement it here, or shall we open a new ticket? |
comment:82
Well. What's the difference with implementing it in the ticket that will use it as the default backend for posets ? Nobody will use it in the meantime, and if you want to get technical it's not related to "hash and equality for graphs" either. And this patch will be in Nathann |
comment:83
Replying to @nathanncohen:
Good point. But I think it isn't in the "poset" ticket either". Would you mind if I created a new ticket "pickling of immutable graphs", that will end up to be a dependency for the poset ticket? |
comment:84
Ahahahah... Okay, if you think you would sleep better this way, no problem. I just created ticket #15619 for that, with part of the patch I was writing for the immutable backend of posets. Nathann |
At #14806, an immutable graph backend has been introduced. However, the resulting graphs are not known to be immutable.
So, when the immutable graph backend is used, then the
._immutable
attribute should be set to True.But hang on: Does using the immutable graph backend really result in immutable graphs? I.e., would it really be impossible to change the
==
- andcmp
-classes of a graph by "official" methods such as "add_vertex()
"? And is the current equality testing really what we want to test?Currently,
__eq__()
starts like this:.allows_loops()
.._weighted
be totally removed? Note the following comment in the documentation of the.weighted()
method: "edge weightings can still exist for (di)graphsG
whereG.weighted()
isFalse
". Ouch.Note the following annoying example from the docs of
.weighted()
:So, calling the method
weighted
with an argument changes the==
-class, even though I guess most mathematicians would agree thatG
has not changed at all. And this would actually be the case even ifG
was using the immutable graph backend!!!The aim of this ticket is to sort these things out. To the very least, graphs using the immutable graph backend should not be allowed to change their
==
-class by callingG.weighted(True)
.Depends on #12601
Depends on #15491
CC: @nathanncohen
Component: graph theory
Author: Simon King
Branch/Commit: u/SimonKing/ticket/15278 @
2525d22
Reviewer: Nathann Cohen
Issue created by migration from https://trac.sagemath.org/ticket/15278
The text was updated successfully, but these errors were encountered: