Repository navigation
Faster intersection_array computation for checking distance-regularity - #7181
Conversation
Peiffap
left a comment
There was a problem hiding this comment.
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?
| 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)) |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Personally, though, I don't think it's worth emphasizing again, since it's mentioned higher up on L161.
There was a problem hiding this comment.
That's a leftover from what is currently in networkx, I'm removing it.
| raise nx.NetworkXError("Graph is not distance regular.") | ||
|
|
||
| # compute needed path lengths | ||
| for n in G[v]: |
There was a problem hiding this comment.
| for n in G[v]: | |
| for n in G._adj[v]: |
There was a problem hiding this comment.
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)?
There was a problem hiding this comment.
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!
minor change in comments Co-authored-by: Gilles Peiffer <[email protected]>
Peiffap
left a comment
There was a problem hiding this comment.
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.
| raise nx.NetworkXError("Graph is not distance regular.") | ||
|
|
||
| # compute needed path lengths | ||
| for n in G[v]: |
There was a problem hiding this comment.
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!
| bint[i] = b | ||
| cint[i] = c | ||
|
|
||
| # diameter = max(max(path_length[n].values()) for n in path_length) |
There was a problem hiding this comment.
I'm not sure what this comment is trying to say.
There was a problem hiding this comment.
That's a leftover from what is currently in networkx, I'm removing it.
| 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)) |
There was a problem hiding this comment.
Personally, though, I don't think it's worth emphasizing again, since it's mentioned higher up on L161.
|
Can you add the timing script used to generate the output in the PR description? |
See attached zip file, which includes the datasets as well. |
…tworkx into improvement-distance-regular
There was a problem hiding this comment.
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] |
There was a problem hiding this comment.
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.
| 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): |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
Co-authored-by: Gilles Peiffer <[email protected]>
Co-authored-by: Gilles Peiffer <[email protected]>
Co-authored-by: Dan Schult <[email protected]>
rossbar
left a comment
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
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?
Co-authored-by: Dan Schult <[email protected]>
Co-authored-by: Dan Schult <[email protected]>
|
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! |
I made a number of improvements to the
intersection_arrayfunction:nx.is_regularinstead of manually checking whether the input graph is regular;nx.diameterat the end of the function, since we can update its value as we go with simple calls tomax.bandc, since we are only interested in cardinality.intersection_array.Results on distance-regular graphs
I compared the running times of the current version of
intersection_arraywith mine on the ten largest distance-regular graphs I could get (data comes from https://www.distanceregular.org/).is_distance_regularis NetworkX's current implementation,my_is_distance_regularuses the proposed improvement, andmy_intersection_array_2computes (u, v)-paths rather than the single source paths to all vertices.Results on random regular graphs
Same as above, but with random regular graphs.