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
Distance matrix #14056
Comments
comment:1
So far you can get the distances as a dict of dict but not as a matrix. I think it could be useful. |
comment:2
I also think this would be useful. The dict of dicts is a bit cumbersome to work with sometimes. Plus, it would be nice to have
You might give some thought to the name, I tend to think of "distance matrix" as the 0-1 matrix just above and not the matrix of distances, even though that is a perfectly rational choice. |
comment:3
Thanks for the feedback guys! As far as the name goes, I would like to have distance_matrix for the distance matrix since this is usually what it is referred to in graph theory (http://en.wikipedia.org/wiki/Distance_matrix) As for the (0,1)-matrix of vertices at distance i, this sounds like a generalization of the adjacency_matrix and I would suggest it is implemented by introducing an additional parameter to adjacency_matrix. Like
What you guys think? |
comment:4
Hello ! Well, I think that it is a matrix only if all vertices are numbered from 0 to n-1, which is not the case in Sage. The reason why distance_all_pairs returns a dict of dict is precisely because vertices are not necessarily integers. If you prefer to have a matrix instead, and because I am always scared of the number of functions we have in graph, I would prefer (whatever you want the future name to be.. By the way distance_matrix is nice indeed) that distance_all_pairs disappear, and become an optional result of distance_matrix. Like I had a lot of wine, and a lot of good duck, and a lot of good cheese. I may make more sense tomorrow. Anyway, have fun ! Nathann |
comment:5
I agree with distance_matrix returning a matrix and this function could have an argument to return a binary matrix for pairs at distances i. I disagree with adjacency_matrix returning the binary matrix, and I don't like the proposition of Nathann of a function called distance_matrix able to return a dictionary. But Nathann is right that the distance_matrix works only for graphs with vertices labeled from 0 to n-1. A solution is to return the map vertex->int as well. |
comment:6
Hello! Nathann I don't think its polite to mention your dinner without offering it to us as well!!! At least for the cheese :))) As for the comments! The distance matrix is an algebraic object and is not "affected" by the ordering of the vertices. In the same manner as the adjacency_matrix does not return a vertex -> int map. If you still think it makes to return a map then we could add (analogously to the automorphism_group function) an option "translation=True" in order to return such a map as well. We'd need to add this to the adjacency_matrix() as well for the sake of consistency. As for the pair at distances k. The adjacency matrix of a graph is defined as the (0,1) matrix such that an entry (i,j) has a 1 if and only if the vertices v_i,v_j are at distance 1. Hence to me it makes pretty much sense to add an option here to generalize this to the case when v_i,v_j are at distance k. Why don't you agree dcoudert? Thanks for the comments and have a nice day! |
comment:7
This is a good idea to return map only if an optional argument is set to True. For the matrix of vertices at distance k, I don't know which is the best terminology/method. This is also related to the transitive closure of the graph. I let you choose. You could also have a method returning all such matrices at once to avoid recomputing all the distances. David. |
comment:8
Helloooooooooo !!!
Sorry about that. I'll be looking for a home in the next months, and you will be welcome there to eat cheese and drink wine to compensate
Well .... How would you get the distance between two vertices of your graph given the adjacency matrix unless you know how they are associated to each other ?
Yep. Less easy to use than what we have right now, but then we uselessly store
I think that he would like to have something like My opinion on that is that it is "personal code". Stuff that you need yourself but isn't really relevant for Sage... Have fuuuuuuuuuuuuuuuuuuuuuuunn !! Nathann |
comment:9
Hm.. LabeledMatrix class appears exactly as what we need - that would be nice! As for the "personal code" remark, I am not sure! The notion of the distance matrix is a standard one in graph theory (http://en.wikipedia.org/wiki/Distance_matrix). If we have the adjacency matrix, Kirchhoff matrix and incidence matrix I don't see a reason why we should not have the distance matrix as well. In addition, Mathematica has a function returning the distance matrix of a graph (http://mathworld.wolfram.com/GraphDistanceMatrix.html) hence I am not really sure it is just a personal thing of me :) |
comment:10
You mean that it exists in Sage already ? That would be Cooooooooool !!
??? Nonono, my "personal code" thing was about David's Nathann |
comment:11
Hello! Attached you will find the patch for the distance matrix and some additional documentation for the incidence matrix. Let me know if there is anything to fix. Jernej |
comment:12
Attachment: trac_14056_distancem.patch.gz Well, my comment from 3 months ago about Nathann |
comment:13
I also don't get why the method should only work on strongly connected graphs. Nathann |
comment:14
Hello! As far as I see the distances in a weakly connected digraph are not defined for all pair of vertices! |
comment:15
What's wrong with setting those entries to infinity ? And what about merging distances all pairs and the distance matrix into the same function ? Nathann |
comment:16
Replying to @nathanncohen:
What exactly? I am a bit confused since at that time you had some debate about your research code or something and when I asked about that you said it's related to some code of David? |
comment:17
(just merging the distance_all_pairs and distance_matrix. Distance matrix would return the distance matrix by default, and if you ask for labels you would get either a Labeled matrix if such a thing exists somewhere, or the current double dictionary ?) Nathann |
comment:18
We can do that as well, though I would then still like to have a method distance_matrix() calling distance_all_pairs with the appropriate argument. At least for consistency with adjacency,incidence and Kirchhoff matrix. As for infinity.. Well I like to think of these matrices as well defined algebraic objects and I am not even sure if a matrix can digest Infinity as its entry... |
comment:19
What do you mean ?
I don't understand what you mean here either.
Ookok.. I don't like it, but it makes sense. Nathann |
comment:20
Replying to @nathanncohen:
What I meant was that If we choose to put this thing under distance_all_pairs and add an argument returning the distance matrix, then I would still like to have a method called distance_matrix() that would call distance_all_pairs asking for the distance matrix. And I would want to have that since I would like to have a method called distance_matrix() as we do for the other mentioned matrices! That being said I am now starting to doubt it is a good idea to mix this distance matrix with the shortest path algorithm. Especially if we don't have the indexed matrix thing and since (apperently) infinity is not digested by matrices. What do you think
|
comment:21
Whaaaattttttt ?? "call distance_all_pairs asking for the distance matrix" ? What does that mean ? What I described above was a function names "distance_matrix" which returns the distance matrix when no optional flags are given, and which returns the current result of "distance_all_pairs" when an optional flag, say And What about this ?
Is that still contradicting what I said above ? I don't understand it either.
Why would we mix "distance matrix" with a shortest path algorithm ? The
Let's make our previous messages clear for a start Sorry, I have been exchanging a couple of angry messages this morning so I am probably a bit unpleasant to deal with this morning Nathann |
comment:22
Replying to @nathanncohen:
Oookay now I think I do understand you. Somehow I still like it more the way it is structured now (an algorithmically inclined guy would look for the distance_all_pairs method) a math guy for the distance matrix and since we already have the distance_all_pairs method I don't see any good reasons for removing it. That said, why would you like this specific change?
Hahaha. No worries, I am also quite tense (GSOC is building unnecessary pressure :S). I can hardly imagining you writing unpleasant emails btw :-)
|
comment:23
Hmmmmmm... Because we already have far too much functions in the Graph classes to have aliases.
Ahahaha. Well, there's nothing to do before the 27th of May, is there ?
Ahahahahahah Half of the world can't, indeed. The other half can't picture me as doing anything else but waste their time. Usually, those are my colleagues Have fuuuuuuuuuuuuuuuuuuuuunnn !! Nathann |
comment:24
Ok, nowwwwwwwww the review !
Thinking about it again, if you replace the loop by what I just said, then you cannot save some time by doing Nathann |
comment:26
Hellooooooo again ! Ok, then this change in the loops is a wrong idea. If you want your "distance_matrix" to run fast on large instances though [1] you could implement the code of this distance_matrix function directly in the distances_all_pairs.pyx file. This way you would not have to go through a dictionary, and more importantly Sage would not create a dictionary from a matrix only to create a matrix from the dictionary afterwards. Before the dictionary is created, all the data is contained in a C array that is much easier to transform into a matrix than this dictionary. Not to mention that it takes MUuuuuuuuuuuuchh less space in memory. Have fuuuuuuuuuuuuuuuuuuuuuuuunnn !! Nathann [1] Really, if you want. Otherwise this patch is fine without it |
comment:27
Helloo!! For now I'd prefer to leave this as is because I think there are some other places (backend,khm,khm?) in which a speedup fix is required more than here! |
comment:28
No prob ! Nathann |
comment:29
Should we flag this as done then sir?? |
comment:30
Well, unless you tell me why you think that all comments about doc and infinity are irrelevant ?... Nathann |
comment:31
They were irrelevant because I completely forgot about them sorry. Will fix that I agree with them! |
comment:32
Btw, should seealso be added like
|
comment:33
I think that it's ok like that, but you should probably check by building the doc (
It's up to you ! Nathann |
comment:34
Attachment: trac_14056_distancem.2.patch.gz Took me a while to make this last changes since I wanted to make sure the docs are rendered properly. I think that the patch fixes everything you suggested. |
Reviewer: Nathann Cohen |
comment:35
Helloooooooooo !! There were some trailing whitespaces in your patch, and Sage's dogma says that this is wrong. I also added a warning saying that the ordering of vertices in the matrix may not be the ordering of vertices in If you agree with this patch, then you can set the ticket to Nathann |
Author: Jernej Azarija |
This comment has been minimized.
This comment has been minimized.
comment:36
Attachment: trac_14056-rev.patch.gz |
comment:38
Hello! I agree with your modifications. Thank you for the review. Jernej |
Merged: sage-5.10.beta5 |
Hello!
We have the adjacency_matrix and kirchhoff_matrix methods for graphs. I am very much missing a method for the distance_matrix since in the current project I am working with requires us to study some algebraic proprieties of the distance matrix.
It is obviously trivial to implement independently but I am tired of writing distance_matrix(G) all the time.
So before adding a patch implementing that I am wondering if you other guys agree with that and if perhaps I have overlooked that functionality.
Apply:
CC: @rbeezer @nathanncohen @dcoudert @Stefan
Component: graph theory
Author: Jernej Azarija
Reviewer: Nathann Cohen
Merged: sage-5.10.beta5
Issue created by migration from https://trac.sagemath.org/ticket/14056
The text was updated successfully, but these errors were encountered: