Skip to content

Performance improvement and tests for edges_equal - #8077

Merged
dschult merged 6 commits into
networkx:mainfrom
Peiffap:speed-up-edges_equal
Jul 9, 2025
Merged

dschult merged 6 commits into
networkx:mainfrom
Peiffap:speed-up-edges_equal

Conversation

@Peiffap

@Peiffap Peiffap commented May 29, 2025

Copy link
Copy Markdown
Member

A little maintenance PR while reading over the functions in the utils/ folder.

Mini benchmark.
import time
from itertools import zip_longest
import networkx as nx

trials = 10
n = 1000

def take_best(arr):
    sarr = sorted(arr)[:5]
    return sum(sarr) / len(sarr)

g1, g2 = nx.complete_graph(n), nx.complete_graph(n)
#g2.remove_edge(n-1, n-2)

nx_times = [0] * trials
for i in range(trials):
    start = time.time()
    a = nx_ee(g1.edges(), g2.edges())  # the current function
    nx_times[i] = time.time() - start

print(take_best(nx_times))

it_times = [0] * trials
for i in range(trials):
    start = time.time()
    b = new_ee(g1.edges(), g2.edges())  # the new function
    it_times[i] = time.time() - start

assert a == b
print(take_best(it_times))

Benchmarking these changes shows something like a 40-50% time reduction for the complete graph in the timing code.

K_1000 vs K_1000:

1.8564390659332275  # old
0.8450418949127197  # new

K_1000 vs K_1000 with one edge removed:

1.3363954544067382  # old
0.7565835952758789  # new

When comparing the complete graph to a much smaller graph, short-circuiting on the length gives an even more significant improvement:

K_1000 vs K_10:

0.5531469345092773  # old
7.357597351074219e-05  # new

@Peiffap
Peiffap force-pushed the speed-up-edges_equal branch from 082ef99 to 7834679 Compare May 29, 2025 22:10
@Peiffap
Peiffap force-pushed the speed-up-edges_equal branch from 7834679 to d2fd929 Compare May 29, 2025 22:12
Comment thread networkx/utils/misc.py Outdated
return False
return True
return all(
n in d2 and nbr in d2[n] and datalist.count(data) == d2[n][nbr].count(data)

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.

Don't you need to check the len of data lists matches too?

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.

This situation is what the comment on L#552 refers to.
Afaik (but this took some testing and thinking) the only situation where datalist has a length greater than 1 is when comparing the edges of MultiGraphs with keys=False, data=True - but in that situation, the length check in the for loop catches the discrepancy.
If you have a weird situation where the difference in lengths because of one edge would be countered by a reversed difference in another edge, then the all check will fail (in that direction) on the other edge.

A concrete example would be:

G = nx.path_graph(3, create_using=nx.MultiGraph)
I = nx.path_graph(3, create_using=nx.MultiGraph)

G.add_edge(0, 1, 0, attr1="blue")
G.add_edge(1, 2, 1, attr2="green")
I.add_edge(0, 1, 0, attr1="blue")
I.add_edge(0, 1, 1, attr2="green")
edges_equal(G.edges(data=True), I.edges(data=True))

Both of these graphs have 3 edges, so the length check passes. When comparing the attribute dicts, (0, 1) passes (you could say erroneously), since we are only looking at the attributes in d1 which refers to G. However, (1, 2) doesn't - because now there are 2 attributes to check for G where I only has one.

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.

Unless you meant "after changing the way data is parsed in the for loop", for which I've added a new test as well.

Comment thread networkx/utils/misc.py Resolved
Comment thread networkx/utils/misc.py Resolved

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

To me the stronger motivation here isn't necessarily anything to do with performance but rather the improvement in readability, at least related to _update_dict.

I personally find the triply-nested comprehension harder to follow than the original loop and would advocate for undoing that particular change, but pulling the repeated logic for constructing the dicts strikes me as an improvement!

Comment thread networkx/utils/misc.py Resolved
@Peiffap
Peiffap force-pushed the speed-up-edges_equal branch from 890d26c to d5a9827 Compare July 3, 2025 18:24
@Peiffap

Peiffap commented Jul 3, 2025

Copy link
Copy Markdown
Member Author

Since we seem to have settled on going for readability over performance, I've gone with Ale's suggestion as well. The comprehension should now be a lot more readable. :)

Comment thread networkx/utils/misc.py Outdated
Comment on lines +542 to +543
d[u, v] = data
d[v, u] = data

@Peiffap Peiffap Jul 6, 2025 •

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.

Putting the data in both edge directions means this doesn't work for directed graphs, right? If that's the case, I feel it's worth mentioning somewhere.
Does anyone have an explanation for why the original function was written that way?

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.

Putting the data in both edge directions means this doesn't work for directed graphs, right?

Yes I think that's right... whether or not it's intentional I don't know. The docstring does say:

The order of the edges is not relevant.

However it is a bit of a jump for the casual reader/user to go from that sentence to the fact that directedness of edges is ignored.

IMO this is worth a new issue for further discussion!

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

This LGTM - the encapsulation of the dict creation into an internal function improves code reuse, and zip_longest is a slick way of getting rid of some of the explicit membership checks when these dicts are eventually compared. The end result is more readable IMO - thanks @Peiffap !

Comment thread networkx/utils/misc.py Resolved
Co-authored-by: Dan Schult <[email protected]>

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

Looks good!

@dschult
dschult merged commit 8aefda8 into networkx:main Jul 9, 2025
@Peiffap
Peiffap deleted the speed-up-edges_equal branch July 9, 2025 15:11
amcandio pushed a commit to amcandio/networkx that referenced this pull request Jul 20, 2025
* Performance improvement and tests for `edges_equal`

* Address code review comments

* Clean up docstring

* Remove triply-nested comprehension

* Rename tuple

* refactor: use new syntax

Co-authored-by: Dan Schult <[email protected]>

---------

Co-authored-by: Dan Schult <[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