Repository navigation
Avoid re-exploring nodes in Kosaraju's SCC algorithm - #8056
Conversation
|
|
||
| class DirectedAlgorithmBenchmarks: | ||
| timeout = 120 | ||
| _graphs = [ |
There was a problem hiding this comment.
This is loaded every time you create the class. Would you do it lazily and load the graph on a setup call?
There was a problem hiding this comment.
+1 for doing this, but in a follow-up PR. We can do the same for UndirectedAlgorithmBenchmarks.
aebee6a to
dbfbd03
Compare
amcandio
left a comment
There was a problem hiding this comment.
Looks good to me. You might want to update the benchmark results after the latest changes!
|
Hm... I've been playing around with timing for a few hours now and can't seem to get the same results anymore for Kosaraju. I'm not sure why that is. Right now, my timing experiments show that the NetworkX implementation is slightly faster (~1.25x) than in this PR when the number of edges is huge and there is only one SCC. This makes sense when you consider that the change only affects the scenario where a bunch of nodes have been explored in a previous SCC, and don't need to be re-explored). Something about However, we can easily get the best of both worlds if we adapt the |
dbfbd03 to
e6a4cc6
Compare
|
If you want to optimize for dense cases you can do the same trick as in #7971. What do you think? |
|
Seems like it's much better now! |
|
Interesting that the speed-up you get from that is so significant... Thanks for trying it out! |
* Add `source` to docstring * Simplify examples
|
I've merged in the changes to the benchmarking suite for SCC algorithms from #8059. That should make the effect of this change easier to validate. |
|
For reference, running benchmarks on my machine (with a virtualenv instead of conda) gives a significant speed-up: |
|
Is Erdos Renyi really representative here? How many SCCs do these random graphs usually have? Do you have any benchmark suite covering the worst case? |
|
ER is representative as a model for random graphs, but the parameters aren't chosen very intelligently for SCC purposes— |
|
I've updated the benchmarks to have a more representative set of graphs for SCC algorithms to compare against. Here are the numbers (for both the Tarjan and Kosaraju algorithms) comparing both on the last commit before the changes to the Tarjan implementation vs the last change on this branch. cc @amcandio |
amcandio
left a comment
There was a problem hiding this comment.
Sweet. Thank you so much Gilles!
dschult
left a comment
There was a problem hiding this comment.
This looks good to me!
Thanks @Peiffap for implementing this long-time Issue/suggestion.
It is well worth doing and you've done it well. :)
Thanks also to @JohnyXia001 for the suggestion!
Fixes #4181.
EDIT
This PR speeds up the Kosaraju SCC computation by
connected_componentsandweakly_connected_components#7971.sourceargument and simplifying the examples.Outdated description
This PR does three things:seenset inkosaraju_strongly_connected_componentsto avoid revisiting nodes that have already been visited—I'm 99% sure this is what the OP in kosaraju_strongly_connected_components is n^2 #4181 was referring to.G._adjin the inner loops ofstrongly_connected_componentsto achieve a reasonably significant speed-up.It's always messy for me to try to run
asvbenchmarks locally, so I've included some ugly, manual testing code to get an idea for the speed-up.fast_ksccandtarjan_sccare the updated versions of the Kosaraju and Tarjan algorithms:Running this on my machine gives the following speed-ups: