Skip to content

Refactored the working of chordless_cycles to handle self loops - #7901

Merged
dschult merged 6 commits into
networkx:mainfrom
akshitasure12:bugfix-for-issue-7867
Mar 24, 2025
Merged

dschult merged 6 commits into
networkx:mainfrom
akshitasure12:bugfix-for-issue-7867

Conversation

@akshitasure12

@akshitasure12 akshitasure12 commented Mar 8, 2025 •

Copy link
Copy Markdown
Contributor

This PR addresses an issue in the chordless_cycles function where multigraphs with self-loops were not being correctly handled. Added test cases under test_chordless_cycles_multigraph_self_loops that verify :

  1. Self-loops as single-node chordless cycles.
  2. Parallel edges in a multigraph as two-node chordless cycles.
def test_chordless_cycles_multigraph_self_loops(self):
        G = nx.MultiGraph([(1, 1), (2, 2), (1, 2), (1, 2)])
        expected = [[1], [2]]  
        self.check_cycle_algorithm(G, expected, chordless=True)
        
        G.add_edges_from([(2, 3), (3, 4), (3, 4), (1, 3)])
        expected = [[1], [2], (3, 4)] 
        self.check_cycle_algorithm(G, expected, chordless=True)

Changes made include :

  1. Interpretation of self-loops to be chordless cycles, except in multigraphs with multiple loops in parallel.
  2. Removing self loop nodes from being a further part of chordless cycles of length greater than 1.

Addresing issue #7867

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

Nice test -- and I think the changes work -- but include unnecessary extra work. Can you check if the comment/suggestions below work too? Which would you prefer?

Comment thread networkx/algorithms/cycles.py Resolved
Comment thread networkx/algorithms/cycles.py Resolved
@akshitasure12

akshitasure12 commented Mar 12, 2025 •

Copy link
Copy Markdown
Contributor Author

Thank you for the input @dschult, will check it accordingly!

@akshitasure12

Copy link
Copy Markdown
Contributor Author

@dschult Please let me know if I can proceed with this implementation, so I can commit the necessary changes!

@dschult

dschult commented Mar 16, 2025

Copy link
Copy Markdown
Member

You can always make changes to your own PR. No need to ask for permission.
Use the same branch. Make a new commit. Push it to the same branch on your github fork.
That will automatically update the PR on the main repo.

@dschult

dschult commented Mar 17, 2025

Copy link
Copy Markdown
Member

Ack -- I think you replaced the entire list comprehension with Gless instead of replacing Gless in this line with G (the way it used to be). Then removing the creation of Gless and use F everywhere below that Gless is currently used. Can you see how this might work?

  • F as originally defined does not hold any selfloop nodes So it is like Gless. The line defining F was Ok before.
  • Then later on below when we now use Gless we could use F instead because it (like Gless) doesn't include those nodes.
  • That means we don't have to construct Gless.

Make sure you can see how this would work before making the changes. Ask if it isn't clear.

@akshitasure12

akshitasure12 commented Mar 18, 2025 •

Copy link
Copy Markdown
Contributor Author

Understood!
I implemented the following changes suggested (as it was before) and also the replaced the later code that involved Gless with F :

if directed:
        F = nx.DiGraph((u, v) for u, Gu in G.adj.items() if u not in Gu for v in Gu)
        B = F.to_undirected(as_view=False)
    else:
        F = nx.Graph((u, v) for u, Gu in G.adj.items() if u not in Gu for v in Gu)
        B = None

This led me to the following error :
Screenshot 2025-03-18 at 10 21 26 AM
Does this imply that parallel pairs of undirected edges i.e two-cycles are not chordless-cycles? If that is the case, I'd have to make the respective changes to the test I added since I considered two-cycles to be valid chordless-cycles (back-and-forth).

@dschult

dschult commented Mar 19, 2025

Copy link
Copy Markdown
Member

We do not report single undirected edges as chordless cycles. We do report parallel undirected edges as chordless 2-cycles:

G = nx.MultiGraph([(0, 1), (0, 1)])
list(nx.chordless_cycles(G))  # ->  [[1, 0]]

I think the "expected_cycles" should write the 2-cycle as [3, 4] instead of (3,4). But I haven't worked through if this is causing the error you show above.

@akshitasure12

Copy link
Copy Markdown
Contributor Author

I made the recommended adjustment, but the output remains unchanged. Could the issue be related to how the code handles two-cycles?

@dschult

dschult commented Mar 24, 2025

Copy link
Copy Markdown
Member

Sorry for the delayed response...
I found a test that didn't set the random number generator seed -- so it was producing a different test each time we ran it. So i added a seed for that. And I added a test to hunt for these errors.

I eventually found the trouble... I had read the creation of F as ruling out nodes with self-loops. But it didn't. While u was ruled out of being put into F if any self-loops (u, u) exist, v was added to F without checking anything about self-loops. I also added a line in the loop about multigraphs that ruled out nodes with self-loops there too.

Hopefully the changes here will help it pass the tests. You will need to "pull" this branch from your fork to your local machine to pick up these changes before you do anything more locally.

@dschult
dschult force-pushed the bugfix-for-issue-7867 branch from b096fc4 to 279b60f Compare March 24, 2025 03:18
@dschult
dschult force-pushed the bugfix-for-issue-7867 branch from 279b60f to a5f823c Compare March 24, 2025 03:25

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

LGTM - thanks @akshitasure12 and @dschult ! Yet another instance of a problem that is more complex than it seems at face value - very nice find with unreliable/intermittent tests!

@dschult
dschult merged commit 0a58c91 into networkx:main Mar 24, 2025
@dschult dschult linked an issue Mar 24, 2025 that may be closed by this pull request
@jarrodmillman jarrodmillman added this to the 3.5 milestone May 29, 2025
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.

chordless_cycles raises an exception for some multi-graphs

4 participants