Skip to content

Faster intersection_array computation for checking distance-regularity - #7181

Merged
dschult merged 12 commits into
networkx:mainfrom
alabarre:improvement-distance-regular
Aug 2, 2025
Merged

dschult merged 12 commits into
networkx:mainfrom
alabarre:improvement-distance-regular

Conversation

@alabarre

Copy link
Copy Markdown
Contributor

I made a number of improvements to the intersection_array function:

  1. calling nx.is_regular instead of manually checking whether the input graph is regular;
  2. the input graph is very unlikely to be distance-regular: the number b(n) of distance-regular graphs (https://oeis.org/A241814) is a tiny fraction of the number of a(n) of connected simple graphs (https://oeis.org/A001349). In light of this, computing shortest path lengths as we go instead of precomputing them all seems more reasonable.
  3. checking connectedness first allows us to avoid catching exceptions in the main loop.
  4. we don't need to call nx.diameter at the end of the function, since we can update its value as we go with simple calls to max.
  5. moreover, keeping track of the diameter as we go also allows us to stop early: distance-regular graphs on n vertices are known to have a diameter of at most (8 * log_2 n) / 3 (see https://doi.org/10.1016/j.ejc.2004.07.004, Theorem 1.5 page 81), so if at any point the current diameter's value exceeds that then we can give up early.
  6. no need to build lists for computing b and c, since we are only interested in cardinality.
  7. I thought bidirectional searches for shortest (u, v)-paths would (asymptotically) amount to more work than launching single-source shortest paths algorithms, since the former uses two BFS anyway. After comparing both approaches in practice, I turned out to be right: the bidirectional searches often take much longer than the original version of intersection_array.

Results on distance-regular graphs

I compared the running times of the current version of intersection_array with mine on the ten largest distance-regular graphs I could get (data comes from https://www.distanceregular.org/). is_distance_regular is NetworkX's current implementation, my_is_distance_regular uses the proposed improvement, and my_intersection_array_2 computes (u, v)-paths rather than the single source paths to all vertices.

Comparing running times for distance regularity checking

The following instances are known to be distance-regular
now dealing with instance  ./tests/test_data/distance-regular/coset-externarygolay.s6             729 vertices, 8748 edges
    now running is_distance_regular     done in 2.1808555030147545 seconds, answer is True
    now running my_is_distance_regular  done in 1.7023403490020428 seconds, answer is True
    now running my_intersection_array_2 done in 4.411169429979054 seconds, answer is ([24, 22, 20], [1, 2, 12])

now dealing with instance  ./tests/test_data/distance-regular/shortened-extended-ternary-golay.s6 729 vertices, 8019 edges
    now running is_distance_regular     done in 3.3089937710028607 seconds, answer is True
    now running my_is_distance_regular  done in 2.775795002002269 seconds, answer is True
    now running my_intersection_array_2 done in 4.316444240976125 seconds, answer is ([22, 20, 18, 2, 1], [1, 2, 9, 20, 22])

now dealing with instance  ./tests/test_data/distance-regular/games.s6                            729 vertices, 40824 edges
    now running is_distance_regular     done in 7.66312413700507 seconds, answer is True
    now running my_is_distance_regular  done in 6.080848899000557 seconds, answer is True
    now running my_intersection_array_2 done in 8.80728219798766 seconds, answer is ([112, 110], [1, 20])

now dealing with instance  ./tests/test_data/distance-regular/witt.s6                             759 vertices, 11385 edges
    now running is_distance_regular     done in 2.950715120008681 seconds, answer is True
    now running my_is_distance_regular  done in 2.4445975279959384 seconds, answer is True
    now running my_intersection_array_2 done in 5.15949073000229 seconds, answer is ([30, 28, 24], [1, 3, 15])

now dealing with instance  ./tests/test_data/distance-regular/iif.s6                              990 vertices, 3465 edges
    now running is_distance_regular     done in 2.6043268000066746 seconds, answer is True
    now running my_is_distance_regular  done in 2.290155616006814 seconds, answer is True
    now running my_intersection_array_2 done in 11.459608876990387 seconds, answer is ([7, 6, 4, 4, 4, 1, 1, 1], [1, 1, 1, 2, 4, 4, 6, 7])

now dealing with instance  ./tests/test_data/distance-regular/sksgraph.s6                         1024 vertices, 16896 edges
    now running is_distance_regular     done in 5.97368586299126 seconds, answer is True
    now running my_is_distance_regular  done in 5.078227958001662 seconds, answer is True
    now running my_intersection_array_2 done in 9.856664725986775 seconds, answer is ([33, 30, 15], [1, 2, 15])

now dealing with instance  ./tests/test_data/distance-regular/cube10.s6                           1024 vertices, 5120 edges
    now running is_distance_regular     done in 3.033381476998329 seconds, answer is True
    now running my_is_distance_regular  done in 2.5856517939828336 seconds, answer is True
    now running my_intersection_array_2 done in 30.335086243983824 seconds, answer is ([10, 9, 8, 7, 6, 5, 4, 3, 2, 1], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])

now dealing with instance  ./tests/test_data/distance-regular/hamming4.6.s6                       1296 vertices, 12960 edges
    now running is_distance_regular     done in 6.618309289013268 seconds, answer is True
    now running my_is_distance_regular  done in 5.388119548995746 seconds, answer is True
    now running my_intersection_array_2 done in 28.552194002026226 seconds, answer is ([20, 15, 10, 5], [1, 2, 3, 4])

now dealing with instance  ./tests/test_data/distance-regular/grassmann263.s6                     1395 vertices, 68355 edges
    now running is_distance_regular     done in 32.80391989799682 seconds, answer is True
    now running my_is_distance_regular  done in 24.02292016299907 seconds, answer is True
    now running my_intersection_array_2 done in 39.16650592099177 seconds, answer is ([98, 72, 32], [1, 9, 49])

now dealing with instance  ./tests/test_data/distance-regular/odd7.s6                             1716 vertices, 6006 edges
    now running is_distance_regular     done in 7.135661044012522 seconds, answer is True
    now running my_is_distance_regular  done in 6.108615503995679 seconds, answer is True
    now running my_intersection_array_2 done in 54.69806831300957 seconds, answer is ([7, 6, 6, 5, 5, 4], [1, 1, 2, 2, 3, 3])

Comparing totals:
    is_distance_regular    : 74.27297290205024
    my_is_distance_regular : 58.477272361982614
    my_intersection_array_2: 196.76251468193368

Results on random regular graphs

Same as above, but with random regular graphs.

The following 10 instances random 20-regular graphs
now dealing with instance 0 -- 2048 vertices, 20480 edges
    now running is_distance_regular     done in 2.2792291679943446 seconds, answer is False
    now running my_is_distance_regular  done in 0.0033934070088434964 seconds, answer is False
    now running my_intersection_array_2 done in 0.002207571000326425 seconds, answer is False

now dealing with instance 1 -- 2048 vertices, 20480 edges
    now running is_distance_regular     done in 2.315355560014723 seconds, answer is False
    now running my_is_distance_regular  done in 0.003575303009711206 seconds, answer is False
    now running my_intersection_array_2 done in 0.0019738929986488074 seconds, answer is False

now dealing with instance 2 -- 2048 vertices, 20480 edges
    now running is_distance_regular     done in 2.2876819689990953 seconds, answer is False
    now running my_is_distance_regular  done in 0.003264581988332793 seconds, answer is False
    now running my_intersection_array_2 done in 0.001720152999041602 seconds, answer is False

now dealing with instance 3 -- 2048 vertices, 20480 edges
    now running is_distance_regular     done in 2.2935480609885417 seconds, answer is False
    now running my_is_distance_regular  done in 0.003167785005643964 seconds, answer is False
    now running my_intersection_array_2 done in 0.002119503973517567 seconds, answer is False

now dealing with instance 4 -- 2048 vertices, 20480 edges
    now running is_distance_regular     done in 2.2664987099997234 seconds, answer is False
    now running my_is_distance_regular  done in 0.0032383989891968668 seconds, answer is False
    now running my_intersection_array_2 done in 0.00220975698903203 seconds, answer is False

now dealing with instance 5 -- 2048 vertices, 20480 edges
    now running is_distance_regular     done in 2.3034619920072146 seconds, answer is False
    now running my_is_distance_regular  done in 0.0033776959753595293 seconds, answer is False
    now running my_intersection_array_2 done in 0.0020072120241820812 seconds, answer is False

now dealing with instance 6 -- 2048 vertices, 20480 edges
    now running is_distance_regular     done in 2.36016765399836 seconds, answer is False
    now running my_is_distance_regular  done in 0.0032529000018257648 seconds, answer is False
    now running my_intersection_array_2 done in 0.002387827989878133 seconds, answer is False

now dealing with instance 7 -- 2048 vertices, 20480 edges
    now running is_distance_regular     done in 2.5374458819860592 seconds, answer is False
    now running my_is_distance_regular  done in 0.00350969098508358 seconds, answer is False
    now running my_intersection_array_2 done in 0.002192362997448072 seconds, answer is False

now dealing with instance 8 -- 2048 vertices, 20480 edges
    now running is_distance_regular     done in 2.6173524199984968 seconds, answer is False
    now running my_is_distance_regular  done in 0.004194754990749061 seconds, answer is False
    now running my_intersection_array_2 done in 0.0025923799839802086 seconds, answer is False

now dealing with instance 9 -- 2048 vertices, 20480 edges
    now running is_distance_regular     done in 2.5984127949923277 seconds, answer is False
    now running my_is_distance_regular  done in 0.0035534100024960935 seconds, answer is False
    now running my_intersection_array_2 done in 0.002110676985466853 seconds, answer is False

Comparing totals:
    is_distance_regular    : 23.859154210978886
    my_is_distance_regular : 0.034527927957242355
    my_intersection_array_2: 0.02152133794152178

@Peiffap Peiffap 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 is a nice and thorough pull request, and the speed-up is impressive, especially for the non-distance regular (distance irregular?) graphs. Thank you!

I've added some comments and suggestions, let me know what you think of them.

This feels like a good candidate pull request for a benchmark, too. What do you think?

Comment thread networkx/algorithms/distance_regular.py Resolved
Comment thread networkx/algorithms/distance_regular.py Resolved
Comment thread networkx/algorithms/distance_regular.py Resolved
Comment thread networkx/algorithms/distance_regular.py Outdated
if not nx.is_regular(G) or not nx.is_connected(G):
raise nx.NetworkXError("Graph is not distance regular.")

# path_length = dict(nx.all_pairs_shortest_path_length(G))

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.

Nit: leaving this comment is just going to confuse future readers; perhaps if you want to emphasize that we don't precompute, it's simpler to just say that

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.

Personally, though, I don't think it's worth emphasizing again, since it's mentioned higher up on L161.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

That's a leftover from what is currently in networkx, I'm removing it.

Comment thread networkx/algorithms/distance_regular.py Resolved
Comment thread networkx/algorithms/distance_regular.py Outdated
raise nx.NetworkXError("Graph is not distance regular.")

# compute needed path lengths
for n in G[v]:

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.

Suggested change
for n in G[v]:
for n in G._adj[v]:

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I remember reading somewhere in networkx's documentation that G[v] was to be preferred over G.adj[v]. I don't remember reading about G._adj[v]. Perhaps someone with more experience than the both of us could chime in (@dschult for instance)?

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.

From what I've seen in other threads, the rule of thumb is "use only in inner loops, where speed-up would be most significant"—and I believe that applies here. If we wanted to be really thorough, it'd be nice to check by explicitly timing both alternatives. I'll see if I can get to that later today!

Comment thread networkx/algorithms/distance_regular.py Resolved
Comment thread networkx/algorithms/distance_regular.py Resolved
minor change in comments

Co-authored-by: Gilles Peiffer <[email protected]>

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

I remembered liking this PR a lot last year, and since it didn't get looked at yet, I went over my comments to see if I still agreed with them. Most should probably be disregarded :).

I also still think it would be nice to add some benchmarks to the PR, since it's mainly a performance optimization.

Comment thread networkx/algorithms/distance_regular.py Resolved
Comment thread networkx/algorithms/distance_regular.py Resolved
Comment thread networkx/algorithms/distance_regular.py Resolved
Comment thread networkx/algorithms/distance_regular.py Outdated
raise nx.NetworkXError("Graph is not distance regular.")

# compute needed path lengths
for n in G[v]:

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.

From what I've seen in other threads, the rule of thumb is "use only in inner loops, where speed-up would be most significant"—and I believe that applies here. If we wanted to be really thorough, it'd be nice to check by explicitly timing both alternatives. I'll see if I can get to that later today!

Comment thread networkx/algorithms/distance_regular.py Resolved
Comment thread networkx/algorithms/distance_regular.py Outdated
bint[i] = b
cint[i] = c

# diameter = max(max(path_length[n].values()) for n in path_length)

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.

I'm not sure what this comment is trying to say.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

That's a leftover from what is currently in networkx, I'm removing it.

Comment thread networkx/algorithms/distance_regular.py Outdated
if not nx.is_regular(G) or not nx.is_connected(G):
raise nx.NetworkXError("Graph is not distance regular.")

# path_length = dict(nx.all_pairs_shortest_path_length(G))

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.

Personally, though, I don't think it's worth emphasizing again, since it's mentioned higher up on L161.

@Peiffap

Peiffap commented May 27, 2025

Copy link
Copy Markdown
Member

Can you add the timing script used to generate the output in the PR description?

@alabarre

Copy link
Copy Markdown
Contributor Author

Can you add the timing script used to generate the output in the PR description?
bench-dr-graphs.zip

See attached zip file, which includes the datasets as well.

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

I still like this a lot, so let's try to get it in!
Thanks for sharing the benchmarking code! Could you add your benchmarks to the benchmarking suite as well? #8059 is a recent PR that added benchmarks if you want an example. Once we have that in place we can discuss whether the suggestions to use G._adj make sense.

Some more comments below. The linter is complaining, could you run pre-commit run --all-files to fix those issues? This probably has to do with line lengths.

for x, distance in path_length[u].items():
path_length[x][u] = distance

i = path_length[u][v]

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.

Nit: I know you didn't add this, but could we do something like pl instead? i is usually an iteration variable. We can also leave it as it is to reduce the diff.

Comment thread networkx/algorithms/distance_regular.py Resolved
Comment thread networkx/algorithms/distance_regular.py Resolved
k = knext
path_length = dict(nx.all_pairs_shortest_path_length(G))
diameter = max(max(path_length[n].values()) for n in path_length)
if not nx.is_regular(G) or not nx.is_connected(G):

@Peiffap Peiffap Jul 14, 2025 •

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.

Do we need to precompute these checks? Can't we just check that len(path_length[u]) == len(G) and G.degree(u) == cst inside the loop?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I cache calls to nx.is_regular and nx.is_connected in the script I'm writing, so for my purposes this way is both more readable and more efficient. If the graph is not regular or disconnected, my gut feeling is that we may end up with many more calls to degree and len than with these preliminary checks -- but feel free to benchmark these changes and find out.

Comment thread networkx/algorithms/distance_regular.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.

The main payoff here is the avoidance of pre-computing/storing the all-pairs shortest path length. Even without explicit benchmarks, this is an obvious improvement for cases where the input graph is not distance regular. I'm +1 to get this improvement in and happy to iterate on other decisions in follow-up PRs. Thanks @alabarre !

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

My quick exploration here agrees with @rossbar's point that the short-cut of exiting before computing all pairs shortest path is huge. Let's do that!

Other possible minor optimizations involve reducing repeated dict lookups. Changing from G[v] to G._adj[v] is unlikely to affect much here, but storing the result (vnbrs = G[v]) instead of calling it 3 times, and similarly with path_length (pl_u = path_length[u] and then using pl_u instead of path_length[u] later -- and similarly with path_length[n]) will help. Another spot is the lookup for path_length[n][u] inside the comprehensions that compute b and c: couldn't that be path_length[u][n] and then use pl_u instead of path_length[u] to avoid inner loop dict lookups? The question mark is about whether we are sure this dict-of-dict is symmetric -- and I think it is.

I'm ready to approve and merge this. Would you prefer I do that and add mini-optimizations in a separate PR? Or would you like to add them here first?

Comment thread networkx/algorithms/distance_regular.py Resolved
Comment thread networkx/algorithms/distance_regular.py Resolved
@alabarre

Copy link
Copy Markdown
Contributor Author

Hi Dan,

I approved your changes, I think we can merge the current version and process further optimizations in a later PR.

Thank you all!

@dschult
dschult merged commit f618240 into networkx:main Aug 2, 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.

4 participants