Skip to content

Optimise harmonic centrality - #8158

Merged
rossbar merged 3 commits into
networkx:mainfrom
akshitasure12:hc
Jul 21, 2025
Merged

rossbar merged 3 commits into
networkx:mainfrom
akshitasure12:hc

Conversation

@akshitasure12

@akshitasure12 akshitasure12 commented Jul 20, 2025 •

Copy link
Copy Markdown
Contributor

In the current implementation of harmonic centrality, we compute nbunch.intersection(dist) for every source node. This creates a temporary set at each iteration, which is unnecessary and slightly inefficient since we can achieve the same logic with a conditional check during iteration over dist.items().

What I changed

I replaced:

for v in sources:
        dist = spl(v)
        for u in nbunch.intersection(dist):
            d = dist[u]
            if d == 0:  # handle u == v and edges with 0 weight
                continue
            centrality[v if transposed else u] += 1 / d

with:

for v in sources:
        dist = spl(v)
        for u, d_uv in dist.items():
            if u in nbunch and d_uv != 0:
                centrality[v if transposed else u] += 1 / d_uv

Benchmarking results

I ran the following benchmark command to compare performance:

asv compare f750ce5b a9371f78

[EDIT]:
The most notable performance improvement is observed for wheel_graph(100) but other benchmarks also show modest improvements.

Change Before [f750ce5] After [a9371f7] Ratio Benchmark (Parameter)
- 1.76±0.06ms 1.58±0.01ms 0.89 benchmark_harmonic_centrality.HarmonicCentralityBenchmarks.time_harmonic_centrality('wheel_graph(100)') [Akshita-MacBook-Pro.local/virtualenv-py3.12-numpy-pandas-scipy]
2.63±0.1ms 2.41±0ms 0.92 benchmark_harmonic_centrality.HarmonicCentralityBenchmarks.time_harmonic_centrality('directed_wheel(100)') [py3.12]
795±5μs 768±2μs 0.97 benchmark_harmonic_centrality.HarmonicCentralityBenchmarks.time_harmonic_centrality_node_subset('directed_wheel(1000)') [py3.12]
70.3±0.6μs 65.6±0.08μs 0.93 benchmark_harmonic_centrality.HarmonicCentralityBenchmarks.time_harmonic_centrality_node_subset('wheel_graph(100)') [py3.12]
19.3±0.2μs 17.9±0.04μs 0.93 benchmark_harmonic_centrality.HarmonicCentralityBenchmarks.time_harmonic_centrality_single_node('wheel_graph(100)') [py3.12]
149±0.9μs 142±0.2μs 0.95 benchmark_harmonic_centrality.HarmonicCentralityBenchmarks.time_harmonic_centrality_single_node('wheel_graph(1000)') [py3.12]
... ... ... ...

@amcandio

Copy link
Copy Markdown
Contributor

I see some variance on the measurements of asv. You should probably retry your profiling making sure your computer is not overloaded with other tasks

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

See comment above

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

This looks good to me.

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

LGTM - I see no appreciable difference in performance but see no problem with the proposal either!

I'd propose to leave the comment, as it's useful IMO to remind readers that the condition can come about in multiple different ways (0-weight edges or self-loops). Thanks @akshitasure12 !

Co-authored-by: Ross Barnowski <[email protected]>
@rossbar
rossbar merged commit adfde01 into networkx:main Jul 21, 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