Skip to content

ENH: add directed kwarg to edges_equal - #8192

Merged
MridulS merged 9 commits into
networkx:mainfrom
Peiffap:enh/directed_edges_equal
Sep 10, 2025
Merged

MridulS merged 9 commits into
networkx:mainfrom
Peiffap:enh/directed_edges_equal

Conversation

@Peiffap

@Peiffap Peiffap commented Aug 11, 2025

Copy link
Copy Markdown
Member

Fixes #8137.

This PR adds a directed kwarg to edges_equal to indicate edgelists should be treated as coming from directed graphs.

@Peiffap
Peiffap force-pushed the enh/directed_edges_equal branch from 972b317 to 451d94b Compare August 11, 2025 15:16
@Peiffap
Peiffap force-pushed the enh/directed_edges_equal branch from 451d94b to bd6da3f Compare August 11, 2025 15:17
@Peiffap
Peiffap force-pushed the enh/directed_edges_equal branch from f2a8378 to 5761dfd Compare August 11, 2025 15:57
@Peiffap
Peiffap force-pushed the enh/directed_edges_equal branch from 5761dfd to ccde6f0 Compare August 11, 2025 16:08

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

Generally LGTM, thanks for sorting through the tests to find the instances where directed= was called for! The only one I was surprised by was pajek, which I thought didn't involve directed graphs but I may have read the tests wrong.

Comment thread networkx/utils/misc.py Outdated
Edge data must match if included.
The order of the edges is not relevant.
Equality here means equal as Python objects. Edge data must match
if included. The order of the edges is not relevant.

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.

"order of the edges" is a bit ambiguous in the context... before it was true because both the order of the edge tuples (i.e. (u, v) or (v, u)) and the ordering of the tuples in edges1 or edges2 was irrelevant. Now the former depends on directed. I think the wording may need an additional update to capture the new distinction. Alternatively perhaps we could just remove the sentence and rely on the extensive examples to make clear exactly what is meant.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I've added some (hopefully) clearer wording. Also happy to just get rid of these sentences and rely on the examples as you've suggested—whatever you decide is best!

Comment thread networkx/utils/misc.py Resolved
Comment thread networkx/readwrite/tests/test_pajek.py Outdated
H = nx.read_pajek(fh)
assert nodes_equal(list(G), list(H))
assert edges_equal(list(G.edges()), list(H.edges()))
assert edges_equal(G.edges(), H.edges(), directed=True)

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.

I'm ambivalent about this - AFAICT neither G nor H are actually directed graphs, so the fact that the edge tuples are in the same order after a round trip is really just an implementation detail and not something that needs to be enforced. I'm inclined to leave the directed= out of the pajek tests.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I went over it again. Everywhere self.G or self.data is used, it should use directed comparisons IMO, as these are directed (self.data has an *arcs marker, which means the graph is directed). Similarly, when the pajek file has a *Matrix marker, the resulting graph defaults to nx.DiGraph.

The unicode test you've commented on seems to be undirected though, so I've removed the directed=True here.

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

Thanks @Peiffap , this LGTM now!

@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 looks good!
One change seems like a whitespace change, but I can't tell if maybe there is something else going on there. Otherwise this is ready.

Comment on lines +406 to +407
G.edges(),
[("A", "B"), ("A", "C"), ("B", "D"), ("C", "B"), ("C", "D")],

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.

Is there a change here? Looks like maybe whitespace or clarity of separating G.edges from rest -- but later in test_line.py you leave a longer line with the same structure. Probably nothing important, but I wondered if there is more than whitespace going on here?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Nothing more than whitespace as far as I can tell...
I think I remember what happened here; I started out with this file and tried to follow the "use edgelists, not lists of edges" pattern wherever edges_equal is used by always relying on the output of a Graph.edges call instead of manually declared lists (for the reasons detailed in the added doc examples)...
But that quickly became a lot of churn and I figured it would already be tedious to review this, so I went back and undid those changes at some point—manually.
My best guess is that I went back to this one, undid the change to a Graph.edges() call, noticed the line length was too long (I believe this file doesn't get linted for some reason? Otherwise I expect ruff should have caught this) and accidentally added this whitespace change.

I think that probably counts as "nothing important." :)

@Peiffap

Peiffap commented Sep 10, 2025

Copy link
Copy Markdown
Member Author

Do you want me to undo the whitespace change or is this one good to go? It has two approving reviews from maintainers!

@MridulS

MridulS commented Sep 10, 2025

Copy link
Copy Markdown
Member

The linters are happy with the whitespace. I press merge.

@MridulS
MridulS merged commit 1364988 into networkx:main Sep 10, 2025
@MridulS MridulS added this to the 3.6 milestone Sep 10, 2025
@Peiffap
Peiffap deleted the enh/directed_edges_equal branch September 10, 2025 19:44
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.

edges_equal implicitly assumes edges are undirected

4 participants