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
Iterator over all orientations of a graph #21415
Comments
comment:1
Here is some code that does it: def all_orientations(G):
from itertools import product
E = [[(u,v,label), (v,u,label)] for u,v,label in G.edges()]
verts = G.vertices()
for edges in product(*E):
yield DiGraph([verts, edges], format='vertices_and_edges') |
comment:2
Seems reasonable. So this needs just definition, examples and so on. The graph may not have multiple edges nor loops, but it can have weight (or other labels) assigned to edges. I can see no easy way to optimize this, |
comment:3
Something like this for docstring?
Should the name be |
comment:4
|
comment:5
Also, the docstring looks good. We will just have to add some tests for multiedges and/or loops. However, the order of the iterator and edges should be consistent when the vertices have a total ordering (like the integers). |
comment:6
I didn't know that Many graph functions already have both list and iterator versions, so I guess it makes sense to have both. But if unsure, add only iterator. Python seems to be changing more iterator-style, |
comment:7
See also this ask question. The suggested solution is of course not optimal since we create list and then check membership (the idea was to show how to easily reuse existing code), but it deals with data structure, embedding, etc. |
comment:8
The solution proposed in this ask question is certainly more expensive than the solution proposed above. However, the decoration of the method (data structure, etc.) is interesting. I suggest to avoid a method able to return all orientations at once: too huge list. An iterator seem much more appropriate. Another option is to use gray codes or revolving door principles to ensure that two consecutive orientations have very little modifications. It will not change the validity of the method, just the ordering. So we may save some time using the copy method of digraph and then applying the minor changes instead of creating a completely new graph. |
Commit: |
Author: Travis Scrimshaw |
comment:9
I would say Python is not switching to more iterator style per-se, but having default behavior that is more practical for the typical programming purpose. I put the code into a branch, taking some from the ask answer, and kept only an iterator because of how big is does get. New commits:
|
comment:10
Otherwise it seems to work, attributes for allowing loops and/or multiedges works and so on. |
comment:11
Replying to @jm58660:
This is not included in any documentation. I am following suit.
Will fix.
Should we add "directed" to the front of the name or just drop it altogether? I don't have a preference on this.
I will also add tests for non-simple graphs. |
comment:12
Replying to @tscrim:
OK.
I do not understand. Both parameters are optional, as both have a default value. But whatever, won't block me from giving review.
No, not "directed". Maybe "An orientation of ..." or nothing. I can't say either.
OK. I can review this. |
Reviewer: Jori Mäntysalo |
Branch pushed to git repo; I updated commit sha1. New commits:
|
Branch pushed to git repo; I updated commit sha1. New commits:
|
comment:15
Replying to @jm58660:
How I see it,
I added "An orientation of" when a name is specified. I've addressed all of the mentioned issues. |
comment:16
Seems good and tests passed, documentation builds etc. But only now, when you added a test for multigraphs, I really thinked about this; sorry about being late. What about orientations of I don't know how we should address this. But at a minimun this should be documented. |
comment:17
Actually, the loop is wrong; for that, either orientation is equivalent. However, for the multiedges, you just can't distinguish them. I think it would be much harder to code that, so I will just document the behavior. |
Branch pushed to git repo; I updated commit sha1. New commits:
|
comment:19
OK, I am happy with these. Please mark as positive_review if you are ready; as you did not put this to needs_review, I am not sure if you still want to make some changes. |
comment:21
I just forgot to set it back. Thanks! |
Changed branch from public/graphs/orientations_iter-21415 to |
I was asked if Sage provides code to iterate over all orientations of a graph. We should add this to Sage.
CC: @jm58660
Component: graph theory
Author: Travis Scrimshaw
Branch/Commit:
ce48418
Reviewer: Jori Mäntysalo
Issue created by migration from https://trac.sagemath.org/ticket/21415
The text was updated successfully, but these errors were encountered: