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
sage/graphs/graph.py: multigraph recognition in init fails #18067
Comments
comment:1
Hello ! I was actually thinking of this very behaviour last week, as many things in the (di)Graph constructors should be rewritten. A lot of time is also wasted because we have to detect what the user 'intends' to build, and at some point I thought that this could be solved by asking the user to tell us explicitly whether (s)he wants a simple graph or a multigraph, using the In this specific case, I also believe that, as you advise, we should return a multigraph. But to understand better what it works this way, see how it works for dictionary input (note that 'list of edges' input is converted into dictionary input):
What do you think the user wants in this case ? Is (s)he giving us a multigraph, or merely giving the adjacencies between the vertices in both directions? Right now, giving the adjacencies in both direction or giving them only once yields the same result
And of course, saying twice that 2 is a neighbor of 1 yields a multigraph
I will fix this bug soon, by making Nathann |
comment:2
Sorry, Nathann; I have no idea how to fix this mess short of replacing the catch-all |
comment:3
Don't worry, I will write some code for that very soon. I am about to leave to Liege (Belgium) for some Sage days so I will not be able to do it this week-end most probably (I'll be backpacking) but it will definitely be done during the week. I would also like to turn this huge
I agree with you. But I already wrote something to make those argument mandatory, and noticed that it broke doctests.. Because a complete graph stored as a multigraph is not (for Sage) equal to a complete graph (stored as a simple graph) and other problems like that. Well. 1) I will fix this in the next days and 2) I am aware that this code has to be changed, and I will do something about it. Especially since David Coudert would like to be able to load huge graphs in Sage, and that it is currently impossible. Nathann |
comment:4
Thanks a lot! Yes, efficiency is too a reason to get rid of this ducktyping orgy. |
Branch: public/18067 |
Author: Nathann Cohen |
This comment has been minimized.
This comment has been minimized.
comment:5
Just added a branch, and explained what in does in this ticket's description. Needs review Nathann |
Branch pushed to git repo; I updated commit sha1. New commits:
|
Commit: |
This comment has been minimized.
This comment has been minimized.
comment:8
more beautiful description! |
This comment has been minimized.
This comment has been minimized.
Branch pushed to git repo; I updated commit sha1. New commits:
|
This comment has been minimized.
This comment has been minimized.
New commits:
|
comment:11
I have nowhere near the expertise required to review this, but you have my thanks. This is a courageous and overdue step towards cleaning up the Augean stables of Sage's constructors. Meanwhile, this:
is so ugly it is almost ridiculous. Good job improving the if-condition, but IMHO the whole logic should go to hell. To drive the point home, maybe add a doctest with the matrix
as either incidence or adjacency matrix. |
comment:12
Hello,
Well, this code really needs to be rewritten properly.
+1
Ahahaha. Well, I also agree with you on that point, but I cannot get code in Sage by myself. I know that I only fixed a small part of the problem in this ticket, but doing too many things at once makes it impossible to find a reviewer. Soooooo while I already know what the next ticket will be about, I thought that it was better to write a smaller patch first. Then the other will come.
I do not believe in adding doctests to explain that our code is bad. Let us change the code. Nathann |
comment:13
That's fine -- it doesn't need to be fixed in this one ticket. For reference, what is the rationale behind replacing |
comment:14
Nothing smart. Only the trace of some debugging I did, before I noticed that I did not set Nathann |
comment:15
Hellos guys! It would help if somebody could reviw this ticket, for I have more changes to make in the constructors (speed and clarity). Nathann |
Reviewer: David Coudert |
comment:16
For me the patch is OK. All tests pass (sage/graphs/ and sage/graphs/*/) and the doc builds normally. |
comment:17
THaaaaaaaaaaaaaanks !!!! Nathann |
comment:19
Fixed merge failure... |
Changed branch from public/18067 to |
Sage is slightly inconsistent when it comes to detect multiple edges in
Graph(list_of_edges)
:This branch fixes that bug while removing code from
Graph.__init__
andDiGraph.__init__
, which now reliesGenericGraph.add_edges
. More precisely:Make Sage build a (di)graph from a list of edges with the following procedure:
self.add_edges(list_of_edges)
pre-existing warnings accordingly.
allow_loops
andallow_multiple_edges
parametersaccordingly.
When Sage does not know how to create a graph from the provided data, it
currently gives it to NetworkX hoping that it will work, then convert the
result into a Sage graph. With this branch, we now raise an exception instead.
As a consequence it is not possible to create a graph from a Numpy matrix
anymore, though that can be done easily through
Graph(Matrix(my_matrix))
soit is not that bad either (in particular, copying the matrix is not more
expensive than creating a NetworkX graph)
Furthermore, the graphs created from a Numpy matrix inherited the 'weird'
NetworkX standards:
Several corner-cases of Graph creation are now handled differently as a
result. I merely removed the doctests, thinking that they were not actually
testing a property that we want to keep, e.g.:
Before:
After:
Note that it actually makes
Graph(list_of_edges)
behave asadd_edges
already does, as in the latest release we have:
GenericGraph.has_multiple_edges
:What this branch does will also help us reduce further the complexity of those
(awful)
__init__
functions.Nathann
CC: @nathanncohen @sagetrac-sage-combinat @sagetrac-tmonteil @videlec @dcoudert
Component: graph theory
Keywords: sage-combinat
Author: Nathann Cohen
Branch/Commit:
197bc18
Reviewer: David Coudert
Issue created by migration from https://trac.sagemath.org/ticket/18067
The text was updated successfully, but these errors were encountered: