Skip to content

Improved running time for harmonic centrality - #7595

Merged
dschult merged 4 commits into
networkx:mainfrom
285714:harmonic-centrality-runtime-improvement
Aug 21, 2024
Merged

dschult merged 4 commits into
networkx:mainfrom
285714:harmonic-centrality-runtime-improvement

Conversation

@285714

@285714 285714 commented Aug 11, 2024

Copy link
Copy Markdown
Contributor

Computing the harmonic centrality was unnecessarily slow in many cases, for instance when computing the harmonic centrality for just a single vertex and all vertices as sources. This was due to a shortest path search starting from each vertex in sources. If sources is larger than nbunch, it is faster to do a shortest path search on the reversed graph starting from each vertex in nbunch. I implemented the graph reversal for this case, and a test case.

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

Since this PR focuses on performance, I took the liberty of pushing up some benchmarks that should cover these changes. When I run these benchmarks locally, I see the drastic speedup expected for the nbunch < G.nodes cases; however, there is actually a performance degradation due to these changes in the case of small, directed graphs. This is due to call to nx.reverse, which for small-ish graphs actually dominates the runtime.

If we can work out a way to avoid the copy in nx.reverse (e.g. with copy=False, though ensuring that the input graph is not mutated!) then I think this is an unambiguous improvement!

Setting copy=False returns a view and avoids overhead from copying the graph

Co-authored-by: Ross Barnowski <[email protected]>

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

Thanks @285714 , this LGTM!

FWIW here's the benchmark comparisons I get on my local machine:

Benchmark results
All benchmarks:
Change Before [956a348] After [93cd468] Ratio Benchmark (Parameter)
49.6±0.1μs 49.3±0.9μs 0.99 benchmark_harmonic_centrality.HarmonicCentralityBenchmarks.time_harmonic_centrality('directed_wheel(10)')
3.29±0.01ms 3.23±0.06ms 0.98 benchmark_harmonic_centrality.HarmonicCentralityBenchmarks.time_harmonic_centrality('directed_wheel(100)')
332±4ms 327±2ms 0.99 benchmark_harmonic_centrality.HarmonicCentralityBenchmarks.time_harmonic_centrality('directed_wheel(1000)')
43.4±0.4μs 43.7±0.6μs 1.01 benchmark_harmonic_centrality.HarmonicCentralityBenchmarks.time_harmonic_centrality('wheel_graph(10)')
2.14±0.03ms 2.13±0ms 0.99 benchmark_harmonic_centrality.HarmonicCentralityBenchmarks.time_harmonic_centrality('wheel_graph(100)')
182±1ms 177±1ms 0.97 benchmark_harmonic_centrality.HarmonicCentralityBenchmarks.time_harmonic_centrality('wheel_graph(1000)')
- 45.6±0.2μs 24.2±0.1μs 0.53 benchmark_harmonic_centrality.HarmonicCentralityBenchmarks.time_harmonic_centrality_node_subset('directed_wheel(10)')
- 2.53±0.03ms 116±3μs 0.05 benchmark_harmonic_centrality.HarmonicCentralityBenchmarks.time_harmonic_centrality_node_subset('directed_wheel(100)')
- 252±0.7ms 1.08±0.01ms 0 benchmark_harmonic_centrality.HarmonicCentralityBenchmarks.time_harmonic_centrality_node_subset('directed_wheel(1000)')
- 38.9±0.1μs 19.5±0.09μs 0.5 benchmark_harmonic_centrality.HarmonicCentralityBenchmarks.time_harmonic_centrality_node_subset('wheel_graph(10)')
- 1.34±0.03ms 86.6±0.09μs 0.06 benchmark_harmonic_centrality.HarmonicCentralityBenchmarks.time_harmonic_centrality_node_subset('wheel_graph(100)')
- 95.9±1ms 717±6μs 0.01 benchmark_harmonic_centrality.HarmonicCentralityBenchmarks.time_harmonic_centrality_node_subset('wheel_graph(1000)')
- 41.3±0.2μs 8.26±0.06μs 0.2 benchmark_harmonic_centrality.HarmonicCentralityBenchmarks.time_harmonic_centrality_single_node('directed_wheel(10)')
- 2.49±0.05ms 9.22±0.09μs 0 benchmark_harmonic_centrality.HarmonicCentralityBenchmarks.time_harmonic_centrality_single_node('directed_wheel(100)')
- 250±1ms 16.6±0.09μs 0 benchmark_harmonic_centrality.HarmonicCentralityBenchmarks.time_harmonic_centrality_single_node('directed_wheel(1000)')
- 35.7±0.06μs 6.26±0.04μs 0.18 benchmark_harmonic_centrality.HarmonicCentralityBenchmarks.time_harmonic_centrality_single_node('wheel_graph(10)')
- 1.26±0.02ms 24.2±0.08μs 0.02 benchmark_harmonic_centrality.HarmonicCentralityBenchmarks.time_harmonic_centrality_single_node('wheel_graph(100)')
- 95.1±1ms 186±2μs 0 benchmark_harmonic_centrality.HarmonicCentralityBenchmarks.time_harmonic_centrality_single_node('wheel_graph(1000)')

FWIW the benchmark comparisons can be run with the following commands (from the benchmarks/ directory on this branch):

$ asv continuous --bench HarmonicCentrality main harmonic-centrality-runtime-improvement
$ asv compare main harmonic-centrality-runtime-improvement

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

Thanks @285714 for this -- and @rossbar for the nice benchmarks and helpful review!

@dschult
dschult merged commit 8b81a52 into networkx:main Aug 21, 2024
@dschult dschult added this to the 3.4 milestone Aug 28, 2024
rossbar added a commit to Peiffap/networkx that referenced this pull request Sep 6, 2024
* consider graph reversal

* Add benchmarks for harmonic centrality.

* Add directed graphs to harmonic centrality benchmarks.

* Update networkx/algorithms/centrality/harmonic.py

Setting copy=False returns a view and avoids overhead from copying the graph

Co-authored-by: Ross Barnowski <[email protected]>

---------

Co-authored-by: Ross Barnowski <[email protected]>
Co-authored-by: Ross Barnowski <[email protected]>
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.

3 participants