Skip to content

A minimal fix for is_aperiodic - #8029

Merged
rossbar merged 3 commits into
networkx:mainfrom
fei0319:fix-is-aperiodic-minimal
May 18, 2025
Merged

rossbar merged 3 commits into
networkx:mainfrom
fei0319:fix-is-aperiodic-minimal

Conversation

@fei0319

@fei0319 fei0319 commented May 7, 2025

Copy link
Copy Markdown
Contributor

This is a minimal fix for is_aperiodic. See #8021 .

Description: The original logic of is_aperiodic can 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_aperiodic now simply returns _period(G) == 1.

@fei0319 fei0319 mentioned this pull request May 7, 2025

@Peiffap Peiffap left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

@dschult

dschult commented May 7, 2025 •

Copy link
Copy Markdown
Member

I do not consider this a minimal fix. Can we just change and to or?

It would be much more straight-foward to start a new branch from main. Then you aren't backing out changes you already made in the other PR.

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.

@fei0319
fei0319 force-pushed the fix-is-aperiodic-minimal branch from 367cab9 to 9ab8604 Compare May 7, 2025 19:07
@fei0319

fei0319 commented May 7, 2025

Copy link
Copy Markdown
Contributor Author

I do not consider this a minimal fix. Can we just change and to or?

It would be much more straight-foward to start a new branch from main. Then you aren't backing out changes you already made in the other PR.

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.

The reason why we can't fix it by just changing and to or is at #8021 (comment).

I rebased the branch, so the history should be clean now.

@fei0319
fei0319 force-pushed the fix-is-aperiodic-minimal branch from 9ab8604 to 496ea9c Compare May 12, 2025 06:32
@fei0319

fei0319 commented May 12, 2025 •

Copy link
Copy Markdown
Contributor Author

The minimal fix has been updated according to the discussion in #8021 . Changes:

  • Raise an error for non-strongly connected graphs
  • Add examples for usage with Markov chains, including one using nx.attracting_components.
  • Fix tests accordingly.

@fei0319

fei0319 commented May 14, 2025

Copy link
Copy Markdown
Contributor Author

@dschult Could you take a look at this? The code change is only one line :)

@dschult dschult left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread networkx/algorithms/dag.py Outdated
Comment on lines +601 to +617
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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@fei0319 fei0319 May 17, 2025 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread networkx/algorithms/dag.py Resolved
Comment thread networkx/algorithms/dag.py Resolved
Comment thread networkx/algorithms/tests/test_dag.py Resolved
@fei0319

fei0319 commented May 17, 2025

Copy link
Copy Markdown
Contributor Author

I believe we cannot avoid an explicit check like nx.is_strongly_connected with BFS.

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 levels[u] - levels[v] + 1 computations can be processed in DFS easily, as in BFS.

And such a DFS tree can tell us whether the graph is strongly connected:

  • The tree must be the same size as G
  • Any node in the tree must not have more than one child
  • The root is reachable from the only leaf of the tree

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)

@dschult dschult left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I approve this PR.
We can talk more about optimization of the algorithm in the larger refactor PR.
:)

The CircleCI test failure is due to network connectivity errors that are affecting multiple projects, so unrelated to this PR.

@Peiffap Peiffap left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! 2 nits and some ambiguity in tests.

Comment thread networkx/algorithms/dag.py Resolved
Comment thread networkx/algorithms/tests/test_dag.py Resolved
Comment thread networkx/algorithms/dag.py Resolved
@dschult dschult added this to the 3.5 milestone May 17, 2025

@rossbar rossbar left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A very fine culmination of some excellent research into the semantics here - many thanks to both @fei0319 and @dschult for taking that deep dive and sharing you learned!

@rossbar
rossbar merged commit 86e143d into networkx:main May 18, 2025
@fei0319
fei0319 deleted the fix-is-aperiodic-minimal branch May 18, 2025 09:01
Peiffap added a commit to Peiffap/networkx that referenced this pull request Jul 14, 2025
Peiffap added a commit to Peiffap/networkx that referenced this pull request Jul 14, 2025
Peiffap added a commit to Peiffap/networkx that referenced this pull request Jul 14, 2025
Peiffap added a commit to Peiffap/networkx that referenced this pull request Jul 14, 2025
Peiffap added a commit to Peiffap/networkx that referenced this pull request Jul 14, 2025
dschult pushed a commit that referenced this pull request Jan 18, 2026
* 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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

4 participants