Skip to content

BUG: Group betweenness centrality counts paths incorrectly for directed but not strongly connected. - #8880

Merged
dschult merged 6 commits into
networkx:mainfrom
dschult:group_btwn_endpoints
Aug 30, 2026
Merged

dschult merged 6 commits into
networkx:mainfrom
dschult:group_btwn_endpoints

Conversation

@dschult

@dschult dschult commented Aug 29, 2026 •

Copy link
Copy Markdown
Member

This PR builds on #8879 and should be consider after that is merged.
Fixes #8559
Replaces #8666

This PR fixes the subtraction of path counts for shortest paths that start or end in the group (handling endpoints in the group). The current code handles a strongly connected directed graph, but not a general directed graph. So this adds the missing section of the if-structure for counting paths in directed graphs. The old code used a single workflow for directed and undirected cases when not fully connected.

Two tests (that fail on main) are added in one commit and then the fix is in the next commit. One of the tests fails without #8879 which is why this builds on that. The code doesn't overlap, but the content does. :}

I changed the name scale to extra because scaling usually is multiplying by a scalar whereas this subtracts the extra path counts. I also rewrote the nested for loop using comprehensions -- which is hopefully more readable in this case. Suggestions welcome!

Still to do:

  • test and fix the bug of skipping all 3 adjustments when any denominator is zero (should only skip the one with zero denominator).
  • test and fix cases where not strongly connected directed graphs give KeyErrors.
  • Discover the meaning of keyword endpoints=True and figure out how to make it meaningful, or remove that option.
  • propagate these test changes to prominent_group_*, group_degree_* and group_closeness_*.

@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 @dschult , decomposing the problem is indeed very helpful and it's much easier to understand what's happening with this particular change. I especially like the switch from scale -> extra, and the refactor of how/where it is handled and ultimately applied.

The simple re-naming of the scale factor (with respect to normalization) is a nice touch as well - it's now much more recognizable as the "classic" 1 / (n(n-1)) factor!

I approve the PR and took the liberty of merging main in after #8879 was merged1. I'll hold off from merging since this is still in draft status in case there are other things you'd like to append here - but feel free to merge yourself or I'll do so once the draft status is removed!

Footnotes

  1. If one wants PRs broken down into smaller bits they better be willing to help with the merge conflicts 🙃 ↩

@dschult
dschult marked this pull request as ready for review August 30, 2026 01:45
@dschult
dschult merged commit 0e3f58e into networkx:main Aug 30, 2026
43 checks passed
@dschult dschult added this to the 3.7 milestone Aug 30, 2026
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.

group_betweenness_centrality returns negative values on directed non-strongly-connected graphs

2 participants