Repository navigation
optimise is_reachable() - #8112
Conversation
|
Looks like this needs:
And we should figure out how to handle backward compatibility for people who don't have numpy. It looks like this affects both @rossbar Do you think we need a fallback option in the code to allow it to work for folks that don't have numpy available? |
rossbar
left a comment
There was a problem hiding this comment.
Do you think we need a fallback option in the code to allow it to work for folks that don't have numpy available?
My preference is to err on the side of caution, so I'd vote for a fallback. I agree this probably isn't used a ton, but adding a new dependency to an existing function could be taking away functionality from a subset of users. If it's not difficult to do (and it doesn't seem to be in this case) I'd vote to keep the dependency-free implementation as well.
There is precedent for this as well - see e.g. #7716 and #7718 for the approach taken with random_k_out_graph to implement such a fallback mechanism.
is_reachable() with an adjacency matrix implementationis_reachable()
|
The current test failure seems unrelated to me. |
dschult
left a comment
There was a problem hiding this comment.
This looks good to me!
Thanks @akshitasure12
Peiffap
left a comment
There was a problem hiding this comment.
This looks nice :)!
Some comments below.
Co-authored-by: Gilles Peiffer <[email protected]>
|
Thank you so much @Peiffap for your suggestions! I've incorporated them in the recent commits :) |
Peiffap
left a comment
There was a problem hiding this comment.
That's exactly what I had in mind! If you have the time, maybe some benchmarks would make sense too (since this is primarily a performance improvement). Either way, LGTM! :)
rossbar
left a comment
There was a problem hiding this comment.
Honestly looking at the two different implementations I'm surprised that the numpy-based one is significantly faster... if anything dictionary lookups should be faster than array lookups (or at least not significantly slower.
I think it's worth looking at the Python implementation again to see if we can't improve the two_neighborhood implementation there. For example, that loops over all nodes in G, but can't we instead exploit the neighbor relationships to avoid looking at everything?
Anyways - IMO it's worth having a complete handle on it before introducing a numpy dependency. In other words, if we can improve performance without changing the dependency footprint, that is definitely a win!
Given the nature of these changes I agree with @Peiffap that some explicit benchmarks for this functionality would be extremely helpful!
dschult
left a comment
There was a problem hiding this comment.
A speed-up for the python version (to eliminate possible cubic dependence on len(G) and also eliminate all the checking in is_path that we know is true for these cases) might be something like (near line 406 in this PR... line 332 of the main branch):
return {
x for x in G if x == v or x in G[v] or any(z in G[v] for z in G.pred[x])
}Note that the set comprehension should cache G[v] automatically. So that lookup is only done once -- the lookup G.pred[x] gets done for each x. So another step in speedup might be to start with for x, xpred in G.pred.items() .... But either way, the inner loop is already reduced from is_simple_path(...) over all triples (v,z,x) with each node z in G, to calling z in G[v] for each node z that is a predecessor of x.
This should make a large improvement! :)
|
Thanks @dschult! The NumPy version is still consistently faster than the pure Python implementation, although the Python version’s runtime has improved a lot. So it seems that, for this graph (a tournament), the cost of Python dict lookups and iteration is still higher than scanning a NumPy matrix? |
|
@akshitasure12 When you say Python code, is that just by replacing |
I added the second optimisation as well! |
Peiffap
left a comment
There was a problem hiding this comment.
Some more performance-focused comments.
Co-authored-by: Gilles Peiffer <[email protected]>
|
I used From this, it looks like the Note: |
|
To speed up the |
|
With these micro-optimizations, the pure Python version now runs in about half the time of the NumPy-based implementation. This means we no longer need to rely on NumPy for this function :) |
There was a problem hiding this comment.
Interesting that the pure Python implementation ends up faster than the numpy one!
Could you add some benchmarks to the benchmarking suite to validate the performance improvements? #8059 is a recent PR that added benchmarks if you want an example (the "benchmarking" section of the contributor's guide isn't very complete...).
|
Added a
Benchmarks comparing the previous pure Python implementation vs the new version: This is my first time working with ASV benchmarks, so pls let me know if I'm missing something :) |
rossbar
left a comment
There was a problem hiding this comment.
Thanks for sticking with this through all the back-and-forth @akshitasure12 !
This LGTM, any additional comments of mine are not blockers.
|
This PR technically has multiple approvals but there were a lot of changes between the different approvals so I will hold off from pushing the green button until others have had a chance to look at latest! |
Co-authored-by: Ross Barnowski <[email protected]> Co-authored-by: Gilles Peiffer <[email protected]>
There was a problem hiding this comment.
Very impressive performance improvement from what ended up being a relatively minor change! Nice work everybody, and thanks for being patient with the many comments, Akshita! :)
I have one more comment about the benchmarks, but given the change in performance we can already measure, it's definitely not a blocker.
| def _make_tournament_benchmark_graphs(seed): | ||
| number_of_nodes = [10, 100, 1000] | ||
| graphs = {} | ||
| for nodes in number_of_nodes: | ||
| G = nx.tournament.random_tournament(nodes, seed=seed) | ||
| graphs[f"Tournament ({nodes}, seed={seed})"] = G | ||
| return graphs |
There was a problem hiding this comment.
It would be nice to split this up into two test functions—one where the nodes are known to be reachable, and one where they aren't reachable! You can compute SCCs to enforce this condition I believe.
In line with Ross' comment, I believe that should better showcase the usefulness and importance of short-circuiting—or, if the short-circuits are already being hit, whether the speed-up is equally impressive without short-circuiting. In this case, I believe both cases will be significantly faster than before, but it doesn't hurt to make sure!
Just tracking that I'm re-reviewing this due to many changes since my first approval.
dschult
left a comment
There was a problem hiding this comment.
This looks good!
I have one more tweak that I'd like you to test @akshitasure12. It probably doesn't make a difference in timing (based on the other timings you have done this isn't a critical pathway) so I think of it as a readability issue.
Thanks!
Co-authored-by: Dan Schult <[email protected]>
dschult
left a comment
There was a problem hiding this comment.
I approve this PR.
I'm going to merge as well -- we can tweak the benchmark by figuring out a way to pick source-targets that are reachable or not reachable in a separate PR.
As per the discussion in the recent networkx community meeting, these are the major changes introduced in this PR:
nx.to_numpy_array().is_path()with direct matrix-based checks to identify two-hop neighbors.[edit] : Heatmap for reference
