Repository navigation
A minimal fix for is_aperiodic - #8029
Conversation
Peiffap
left a comment
There was a problem hiding this comment.
With the understanding that this will be superseded by #8021, this looks good to me as a minimal fix. We can handle the remaining issues there (error type, short-circuiting when g == 1, documentation updates, more efficient/readable algorithm, and randomized testing).
|
I do not consider this a minimal fix. Can we just change It would be much more straight-foward to start a new branch from And I'm fine with you adding the new tests along with the simple fix. That will also help separate the discussion of correctness from the discussion of code refactoring. |
367cab9 to
9ab8604
Compare
The reason why we can't fix it by just changing I rebased the branch, so the history should be clean now. |
9ab8604 to
496ea9c
Compare
|
The minimal fix has been updated according to the discussion in #8021 . Changes:
|
|
@dschult Could you take a look at this? The code change is only one line :) |
dschult
left a comment
There was a problem hiding this comment.
This may only be a couple of lines of code, but it is not a simple PR to review. There is a lot here. :)
The comment I made here about removing the if-structure near the end makes me hope that we can avoid the extra computation of is_strongly_connected(G) by using some if clause at the end of the BFS... but maybe we can't...
And anyway, whatever kind of optimization we can make should be in a different PR. That was the point of making this PR: to fix the mistaken code separately from worrying about optimization.
| is *not aperiodic*:: | ||
| is *not aperiodic*. | ||
|
|
||
| >>> DG = nx.DiGraph([(1, 2), (2, 1)]) | ||
| >>> nx.is_aperiodic(DG) | ||
| False | ||
| >>> G = nx.DiGraph([(1, 2), (2, 1)]) | ||
| >>> nx.is_aperiodic(G) | ||
| False |
There was a problem hiding this comment.
I think sphinx doc building system wants the :: and indentation. Am I missing something?
And let's leave the DG rather than switching to G so this part of the docs remains the same.
There was a problem hiding this comment.
You are right, it is a more standard style to use :: and indentation. I saw some unindented examples elsewhere so I mistook that for a standard.
Now the first two examples are restored as they were.
|
I believe we cannot avoid an explicit check like We can use DFS however. Consider Tarjan's strongly connected components algorithm (it is the underlying algorithm of most NetworkX SCC functions), which uses DFS to find a DFS tree of a directed graph. The And such a DFS tree can tell us whether the graph is strongly connected:
So we are basically performing Tarjan's algorithm and periodicity checking simultaneously. Note that switching from BFS to DFS doesn't affect the correctness. I will be glad to implement that in the full fix PR. But it depends on whether you think it is worthy, since it will completely change how the function works and thus make the code harder to audit. (This is not in the scope of this PR anyway) |
Peiffap
left a comment
There was a problem hiding this comment.
Thanks! 2 nits and some ambiguity in tests.
Co-authored-by: Gilles Peiffer <[email protected]>
…aperiodic` Co-authored-by: Fei Pan <[email protected]>
…aperiodic` Co-authored-by: Fei Pan <[email protected]>
…aperiodic` Co-authored-by: Fei Pan <[email protected]>
…aperiodic` Co-authored-by: Fei Pan <[email protected]>
* fix: add suggestions from gh-8021 and gh-8029 to `is_aperiodic` Co-authored-by: Fei Pan <[email protected]> * clarify null graph test for * Don't compute gcd for forward edges * Add test with multiedges * add benchmark for * switch to dfs and add comments * rename benchmark Co-authored-by: Ross Barnowski <[email protected]> --------- Co-authored-by: Fei Pan <[email protected]> Co-authored-by: Ross Barnowski <[email protected]>
This is a minimal fix for
is_aperiodic. See #8021 .Description: The original logic of
is_aperiodiccan be used to calculate period of a graph, so it is moved to_period, with an extra check to make sure it do not go outside the current strongly connected component.is_aperiodicnow simply returns_period(G) == 1.