Skip to content

BUG: Group betweenness skips adjustments if any sigma is zero - #8881

Merged
dschult merged 7 commits into
networkx:mainfrom
dschult:group_btwn_if_logic
Aug 30, 2026
Merged

dschult merged 7 commits into
networkx:mainfrom
dschult:group_btwn_if_logic

Conversation

@dschult

@dschult dschult commented Aug 29, 2026

Copy link
Copy Markdown
Member

This PR builds on the group betweenness of the sigma typo #8879. That PR should be merged first. It doesn't interact with #8880 and so can be reviewed separately.

This fixes the if-logic in group_betweeness_centrality to no longer skip all 3 adjustments when considering a group node v when a denominator terms for any of the 3 is zero. Only the adjustment that has a zero divisor should be ignored.

Tests from #8666 are included and fixed.
Fixes #8827

The if-structure was getting long and repetitive and hard to read -- and it involved many lookups in the inner loop that only changed when an outer loop changed. So I restructured the nested loops. The outer loop must be over v so PB_m[v][v] is correctly updated when it is added to the total GBC. So the nesting stays the same. But at each nesting level we now store the lookups that don't depend on the next inner level. At the core we have 3 if-clauses for the 3 kinds of adjustments that might be needed: Order x-v-y, Order v-x-y and Order x-y-v. The adjustments are made inside each of the 3 if statements. I'm hoping that is easier to read than initializing the changes to 0, using the if to update them to nonzero and then applying the change in every case. Here whenever the change would have been zero, we just don't do the adjustment.

The extra dicts sigma_m_v and PB_m_v are not needed -- they duplicated sigma_m and PB_m in case we needed an old value before the updating overwrote it. That was written in the pseudo code of the article. But we had ordered the assignment statements so the extra copy was not needed. So I just removed them. We update the *_m dicts directly.

This fix allows the new tests to pass. So we are getting correct answers when we get answers now.
We still don't have the KeyErrors handled. Thats next.

Still to do:

  • 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 ! The bugfix is obviously the motivation, but I'm very much in favor of this refactor - it still takes some time/reading to sink in, but I do find it easier to follow than the original. I think the biggest improvement on that front (for my understanding anyway) was the switch away from tracking the three d{xyv} variables and instead modifying the PB_ and sig_ dicts directly when needed. Also, the getting rid of another state-snapshot by eliminating the _m_v deep copies is a very nice improvement 🎉

I still haven't had time to read the underlying paper(s) from which the formula is drawn so I haven't compared the implementation here to the reference(s); however, I did take the time to go through and compare the formulae at each condition to the current implemenation and they do seem consistent (though as noted the conditions under which they are skipped has changed - hence the bugfix!).

FWIW this is an alternative to #8854, which @amcandio pointed out failed on an additional weighted test case. That case was added in #8880 - so just to doublecheck I merged all of the tests together across the three PRs and made sure this passed - which it did 🚀

So - same deal here as #8880 - I approve the PR but will hold off merging until draft status is dropped. Thanks again @dschult !

@dschult
dschult marked this pull request as ready for review August 30, 2026 01:46
@dschult
dschult merged commit c279073 into networkx:main Aug 30, 2026
43 checks passed
@dschult dschult added this to the 3.7 milestone Aug 30, 2026
@dschult
dschult deleted the group_btwn_if_logic branch August 30, 2026 23:41
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 is inaccurate for groups of 3+ nodes, even on connected graphs

2 participants