Skip to content

Avoid O(E) lookup in nx.display -issue8639 - #8663

Merged
amcandio merged 2 commits into
networkx:mainfrom
mcbosch:fix-performance-nx.display-8639
May 30, 2026
Merged

amcandio merged 2 commits into
networkx:mainfrom
mcbosch:fix-performance-nx.display-8639

Conversation

@mcbosch

@mcbosch mcbosch commented May 25, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #8639

This PR is linked to issue #8639, in which a performance problem with the function nx.display was revealed. The concern origins in calling nx.get_[node/edge]_attributes inside a for. This was solved doing a pre-procesing over the parameters kwargs, introduced when the function is called, and considering the following cases for the values, as @dschult suggested:

  • kwarg_value is a dict (node or edge keyed) -- Not treated in this PR, but check with isinstance if it's a dict.. This case is considered in the PR Draw functions wraps display #8255
  • if the kwarg value is a key in any of the node/edge attributes in the entire graph, then it is a graph-attribute-name which we will use to look up the kwarg specific attribute value for each node. If it is not defined in some node/edge we use the default value.
  • if the kwarg value is not any of these, assume it is a value that will be used throughout the graph.

The proposed solution is to create two dicts: knwon_node_attr and known_edge_attr. Which stores weather if kwarg_value is a graph-attribute-name of any node/edge of the graph respectively. This dict are updated when we set new node attributes to avoid future errors (so far only happens with position). Then we can change an O(E) lookup for a O(1) lookup:

- nx.get_node_attributes(node_subgraph, attr) == {}
+ not known_node_attr.get(attr, False)

- nx.get_edge_attributes(edge_subgraph, attr) == {}
+ not known_edge_attr.get(attr, False)

Note that we can't do this change in line 849 because pos might not be a kwarg_value and be defined in some node.

Examples run in my machine:
Performance:

>>> B = nx.barabasi_albert_graph(400, m=3, seed=19320)
>>> %timeit -n1 -r1 nx.display(B)
1.41 s ± 0 ns per loop (mean ± std. dev. of 1 run, 1 loop each)

An error of calling the function with the default value has been solved:

>>> G = nx.Graph()
>>> G.add_edge(1,2); G.add_edge(2,3);
>>> nx.display(G, edge_color = 'color'); plt.show() # <-- Shows both edges black

Examples with different attributes

>>> G = nx.Graph()
>>> G.add_edge(1,2, attr1 = 'red'); G.add_edge(2,3, color = 'blue');
>>> nx.display(G, edge_color = 'attr1'); plt.show() # <-- Plots (1,2) red and (2,3) gray
>>> nx.display(G); plt.show() # <-- Plots (1,2) black and (2,3) blue
>>> nx.display(G, edge_arrowstyle = '->', edge_arrowsize = 10); plt.show(); # <-- Shows (2,3) blue, both with arrows

Example defining specific position:

>>> G = nx.Graph()
>>> G.add_edge(1,2); G.add_edge(2,3);
>>> nx.set_node_attributes(G, {1: [0,0], 2: [0,1], 3: [1,0]}, name='pos')
>>> nx.display(G); plt.show() # <-- nodes in specified positions
>>> nx.set_node_attributes(G, {1: [0,0], 2: [1,0], 3: [0,1]}, name='attr1')
>>> nx.display(G, node_pos = 'attr1'); plt.show() # <-- nodes in specified positions

Avoid O(E) lookup

store known node-edge attr-keys

update known node attr pos

avoid O(E) lookup

Avoid O(E) lookup and error networkx#8639
@mcbosch
mcbosch force-pushed the fix-performance-nx.display-8639 branch from 6356c3f to cf7b5f4 Compare May 25, 2026 16:07
@mcbosch mcbosch changed the title Avoid O(E) lookup in nx.display Avoid O(E) lookup in nx.display -issue #8639 May 25, 2026
@mcbosch mcbosch changed the title Avoid O(E) lookup in nx.display -issue #8639 Avoid O(E) lookup in nx.display -issue8639 May 25, 2026

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

Great idea to keep it simple and just address the O(|E|^2) timing problem.
That makes it so much easier to review...

It will also make it easy to merge with the ongoing work not only in main but also in our review and merging with gh-8255.

Thank you!

I reran the timing comparison from #8639:

In [1]: G = nx.barabasi_albert_graph(400, m=3, seed=19320)
    ...: pos = nx.spring_layout(G, iterations=100, seed=5040)
    ...: nx.set_node_attributes(G, pos, name="pos")
    ...: %timeit -n1 -r1 nx.draw(G, pos=pos, with_labels=True);
    ...: %timeit -n1 -r1 nx.display(G);
185 ms ± 0 ns per loop (mean ± std. dev. of 1 run, 1 loop each)
382 ms ± 0 ns per loop (mean ± std. dev. of 1 run, 1 loop each)

So this reduces the time difference to 2x slower for display (from ~12x). This is good!

This PR sets up pre-processing the kwargs and stores any kwarg_value that appears in the graph as an attribute name in a dict known_node_attr with bool value True. It might seem better to use a set instead of a dict. And maybe it would be. But we may want to hang other info off of this kwarg info later -- so I'm good with leaving it as is.

Optional code idiom suggestions:

  • the checking code is arguably more readable as and attr not in known_edge_attr instead of and known_edge_attr.get(attr, False). But that choice is subjective... You can choose...
  • Similarly not known_node_attr.get(attr, False) could be attr not in known_node_attr.

I approve this PR with or without these suggestions.

Comment thread networkx/drawing/nx_pylab.py Resolved

@amcandio amcandio 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. I approve this PR.

@amcandio
amcandio merged commit da42a10 into networkx:main May 30, 2026
44 checks passed
@dschult dschult added this to the 3.7 milestone May 30, 2026
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.

nx.display drawing performance for edges

3 participants