Repository navigation
fix: add suggestions from #8021 and #8029 to is_aperiodic - #8148
Conversation
…aperiodic` Co-authored-by: Fei Pan <[email protected]>
is_aperiodicis_aperiodic
| for u, v, d in nx.bfs_labeled_edges(G, s): | ||
| if d == "tree": | ||
| levels[v] = levels[u] + 1 | ||
| elif (g := gcd(g, levels[u] - levels[v] + 1)) == 1: |
There was a problem hiding this comment.
I don't think it has any real implications for performance reasons, but I'm curious whether I'm understanding the algorithm correctly...
AIUI the gcd check is only necessary when a cycle is detected, correct? And a cycle can only come about from a "reverse" edge? In other words, could this statement be:
elif (d == "reverse") and (g := gcd(g, levels[u] - levels[v] + 1)) == 1FWIW I've had to stare at this for a while for it to make sense to me. That's not a knock on the implementation - the use of bfs_labeled_edges is definitely more clear IMO, but I realized I didn't really have a great handle on the original logic either!
There was a problem hiding this comment.
Admittedly, I've had to think about this too... I believe your suggestion will break for self-loops, i.e. cycles of length 1. These get reported as "level" edges. I can't think of an example where a "forward" edge creates a cycle, and I'm reasonably confident it's not possible: creating a cycle requires that lvls[u] >= lvls[v] for some edge, and "forward" edges must have lvls[u] < lvls[v] by definition.
There was a problem hiding this comment.
Admittedly, I've had to think about this too... I believe your suggestion will break for self-loops, i.e. cycles of length 1. These get reported as "level" edges.
More than just self-loop edges it turns out. I went ahead and just tried it to see what breaks and there's an example of a non-selfloop level edge creating a cycle in the test suite:
G = nx.cycle_graph(4, create_using=nx.DiGraph)
G.add_edge(1, 3)So I'm convinced now that both "reverse" and "level" edges are relevant for cycle detection.
IMO there's enough going on here to warrant additional code or at least comments to aid readers in understanding how this works with the different bfs_labeled edge types. Here's a rough proposal:
# There are four categories of edges from bfs_labeled_edges:
# "tree", "reverse", "level" and "forward".
for u, v, d in nx.bfs_labeled_edges(G, s):
# "tree" edges indicate a new level in the bfs tree
if d == "tree":
levels[v] = levels[u] + 1
# Only "reverse" or "level" edges are involved in cycles
elif (d == "reverse") or (d == "level"):
# When a new potential cycle is encountered, check whether it's
# length is divisible by a common factor with all other
# encountered cycles
if (g := math.gcd(g, levels[u] - levels[v] + 1)) == 1:
return True
# "forward" edges are not involved in either cycles nor expanding
# tree depth and are therefore ignored
elif d == "forward":
continue
else:
raise NetworkXError(f"Unrecognized edge label '{d}' from bfs_labeled_edges")There was a problem hiding this comment.
I've added some comments inspired by your example. I also switched from using bfs_labeled_edges to dfs_labeled_edges because the edge types are slightly more intuitive IMO.
|
Running benchmarks locally, I get this |
| import networkx as nx | ||
|
|
||
|
|
||
| class DAGBenchmarks: |
There was a problem hiding this comment.
The naming here reflects the module naming scheme - I'd vote to instead call this (and rename the filename BenchmarkAperiodic or similar.
There was a problem hiding this comment.
Thanks, I've updated the names.
Somewhat related: should this function really be in dag.py given that it raises for DAG inputs?
There was a problem hiding this comment.
The is_aperiodic function doesn't raised for all DAGs right?
My interpretation of the dag.py module name is that these functions are relevant for DAG graphs. It does not imply that they don't raise for all DAGs. But maybe I'm missing something.
aperiodic is a subset of DAG right?
There was a problem hiding this comment.
With the updates in #8029, is_aperiodic raises if the graph is not strongly connected, which excludes all DAGs except for the single-node empty graph IIUC. Can we really say is_aperiodic is relevant for DAGs in that case? To me, the two concepts seem orthogonal.
Thinking about this some more, I also have a suggestion for how we might be able to make the API more clear with the changes discussed in #8021 and #8029. Since the decision was made to restrict aperiodicity to single SCC graphs, with several ways of extending it to graphs with multiple SCCs (using aperiodicity for each SCC or even attracting component), how would you feel about creating an is_aperiodic_component(G, component) function in a new module algorithms/components/aperiodic.py, and moving is_aperiodic to it as well?
Co-authored-by: Ross Barnowski <[email protected]>
|
I think the only leftover discussion from this PR is whether to move |
This PR combines some of the changes suggested in #8021 and #8029. CC @fei0319.
Changelist:
not_implemented_forinstead of checking manually (this changes the exception type but everyone seemed to agree no deprecation was necessary in the original PRs).bfs_labeled_edges. I originally tried to get a working solution usingdfs_labeled_edges, without usingnx.strongly_connected_components, but kept finding edge cases that it wouldn't work for. I kept the BFS because it's slightly more readable; both versions are ~30% faster over 100 runs forG = nx.complete_graph(300, create_using=nx.DiGraph):with pytest.raisesinstead ofpytest.raises, and added the edge cases I came up with to the "not strongly connected" tests. These should be purely incremental, i.e. no previously tested behavior is now untested.One point raised in the discussions on previous PRs remains: it's strange to have a function in
dag.pythat raises for DAGs... We should find a new home for it. :) How aboutcycles.py?