Skip to content

BUG: Fix group_betweenness_centrality for groups of 3+ nodes - #8854

Closed
aryansk wants to merge 2 commits into
networkx:mainfrom
aryansk:fix-group-betweenness-8827
Closed

aryansk wants to merge 2 commits into
networkx:mainfrom
aryansk:fix-group-betweenness-8827

Conversation

@aryansk

@aryansk aryansk commented Aug 16, 2026

Copy link
Copy Markdown

Fixes #8827

Problem

group_betweenness_centrality returns wrong values for groups of 3 or more nodes, on both connected undirected graphs and strongly connected digraphs (groups of size 1 and 2 are exact). The issue's reproduction:

G = nx.Graph([(0, 1), (0, 2), (0, 3), (0, 4), (1, 3), (2, 3), (2, 4), (3, 4), (4, 5)])
print(nx.group_betweenness_centrality(G, {0, 1, 2}, normalized=False, endpoints=False))
# before: 0.125   (true value: 0.0)

Root cause

The sequential peeling applies the Puzis–Elovici–Dolev recurrences while iterating over group nodes. Two bugs in the term computation:

  1. One shared zero-guard gates all three fraction terms. The guard required sigma_m[x][v], sigma_m[v][y] and sigma_m[x][y] to be nonzero before computing any of dxyv, dxvy, dvxy. But each term needs a different set of counts — in particular dxyv = sigma_m[x][y] * sigma_m[y][v] / sigma_m[x][v] needs sigma_m[y][v], not sigma_m[v][y]. In directed graphs it is common for every shortest v -> y path to pass through an earlier-peeled group node, zeroing sigma_m[v][y] and spuriously skipping a subtraction that must still happen. (For undirected graphs sigma_m[y][v] == sigma_m[v][y], which is why this only manifested on digraphs.)

  2. dvxy mixed peeled and unpeeled counts: sigma_m[v][x] * sigma[x][y] / sigma[v][y] used the original sigma in the second factor and denominator instead of the successively-updated sigma_m.

Fix

Each fraction now guards only the counts it actually uses, and all three are computed consistently from the peeled sigma_m:

# fraction of x->v paths that pass through y
dxyv = sigma_m[x][y] * sigma_m[y][v] / sigma_m[x][v]
# fraction of x->y paths that pass through v
dxvy = sigma_m[x][v] * sigma_m[v][y] / sigma_m[x][y]
# fraction of v->y paths that pass through x
dvxy = sigma_m[v][x] * sigma_m[x][y] / sigma_m[v][y]

Validation

  • The issue's reproduction now returns 0.0.
  • Verified against a brute-force group betweenness (sum over ordered pairs of non-group nodes of the fraction of shortest paths with an interior node in the group) on ~1,700 random connected undirected graphs and ~570 random strongly connected digraphs, group sizes 1–5: zero mismatches before/after for the previously failing sizes, unweighted and weighted.
  • prominent_group builds on the same peeling code path and benefits from the same fix.
  • Full test suite passes (7932 passed); two new regression tests added (undirected + directed group-of-three cases, both failing before the fix).

Notes

The endpoints=True path has a separate pre-existing quirk (pairs whose source or target is a group member are included in the sum rather than subtracted). That behavior is unchanged by this PR; this PR only corrects the peeling inaccuracy described in #8827.

The sequential peeling in group_betweenness_centrality computed the
path-fraction terms for the Puzis-Elovici-Dolev recurrence under a single
zero-guard that required sigma_m[x][v], sigma_m[v][y] and sigma_m[x][y] to
be nonzero for all three terms. Each term needs a different combination:

- dxyv (fraction of x->v paths through y) needs sigma_m[y][v], not
  sigma_m[v][y]; in directed graphs all shortest v->y paths can pass
  through an earlier-peeled group node, zeroing sigma_m[v][y] and
  spuriously skipping the subtraction.
- dvxy mixed the peeled counts sigma_m with the original sigma.

This produced wrong results starting at group size 3, on both connected
undirected graphs and strongly connected digraphs. Both issues are fixed
here: each fraction now guards only its own required counts, and dvxy is
computed consistently from sigma_m. Verified against brute-force group
betweenness over random undirected and directed graphs (unweighted and
weighted), plus the issue's reproduction.

Generated with Codebuff 🤖
Co-Authored-By: Codebuff <[email protected]>

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

Certainly, it looks like a typo in the formula for dvxy where it uses sigma instead of sigma_m.

And it also seems clear that while the undirected case can check sigma_m[x][v] for v-x or x-v, the directed version will need to check each direction for zero values.

So both changes seem straightforward. Thanks!!
I have a suggestion for test-doc addition calling attention to endpoints=False. Push back if that doesn't make sense.

Q: How did you come up with the graphs used for the tests?

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

This graph discussed in #8666 raises a KeyError after this change:

import networkx as nx

G = nx.DiGraph()

G.add_weighted_edges_from([
    (1, 0, 2),
    (1, 5, 4),
    (2, 0, 1),
    (2, 3, 5),
    (3, 2, 2),
    (5, 3, 3),
])
 
G.add_node(4)

group = [3, 4, 2]

result = nx.group_betweenness_centrality(
    G,
    group,
    weight="weight",
    normalized=False,
    endpoints=False,
)

print("Group:", group)
print("Group betweenness centrality:", result)

I left a suggestion on how to fix it:

Group: [3, 4, 2]
Group betweenness centrality: 3.0

Although as per @Ijtihed's comment #8666 (comment) that is also the wrong answer. We can fix that one in a separate PR.

Comment on lines +146 to 167
if (
D[x][v] == D[x][y] + D[y][v]
and sigma_m[x][y]
and sigma_m[y][v]
and sigma_m[x][v]
):
# fraction of x->v paths that pass through y
dxyv = sigma_m[x][y] * sigma_m[y][v] / sigma_m[x][v]
if (
D[x][y] == D[x][v] + D[v][y]
and sigma_m[x][v]
and sigma_m[v][y]
and sigma_m[x][y]
):
# fraction of x->y paths that pass through v
dxvy = sigma_m[x][v] * sigma_m[v][y] / sigma_m[x][y]
if (
D[v][y] == D[v][x] + D[x][y]
and sigma_m[v][x]
and sigma_m[x][y]
and sigma_m[v][y]
):

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.

This graph discussed in #8666 raises a KeyError after this change:

import networkx as nx

G = nx.DiGraph()

G.add_weighted_edges_from([
    (1, 0, 2),
    (1, 5, 4),
    (2, 0, 1),
    (2, 3, 5),
    (3, 2, 2),
    (5, 3, 3),
])
 
G.add_node(4)

group = [3, 4, 2]

result = nx.group_betweenness_centrality(
    G,
    group,
    weight="weight",
    normalized=False,
    endpoints=False,
)

print("Group:", group)
print("Group betweenness centrality:", result)

This should fix it:

Suggested change
if (
D[x][v] == D[x][y] + D[y][v]
and sigma_m[x][y]
and sigma_m[y][v]
and sigma_m[x][v]
):
# fraction of x->v paths that pass through y
dxyv = sigma_m[x][y] * sigma_m[y][v] / sigma_m[x][v]
if (
D[x][y] == D[x][v] + D[v][y]
and sigma_m[x][v]
and sigma_m[v][y]
and sigma_m[x][y]
):
# fraction of x->y paths that pass through v
dxvy = sigma_m[x][v] * sigma_m[v][y] / sigma_m[x][y]
if (
D[v][y] == D[v][x] + D[x][y]
and sigma_m[v][x]
and sigma_m[x][y]
and sigma_m[v][y]
):
if (
v in D[x]
and y in D[x]
and v in D[y]
and D[x][v] == D[x][y] + D[y][v]
and sigma_m[x].get(y, 0)
and sigma_m[y].get(v, 0)
and sigma_m[x].get(v, 0)
):
# fraction of x->v paths that pass through y
dxyv = sigma_m[x][y] * sigma_m[y][v] / sigma_m[x][v]
if (
y in D[x]
and v in D[x]
and y in D[v]
and D[x][y] == D[x][v] + D[v][y]
and sigma_m[x].get(v, 0)
and sigma_m[v].get(y, 0)
and sigma_m[x].get(y, 0)
):
# fraction of x->y paths that pass through v
dxvy = sigma_m[x][v] * sigma_m[v][y] / sigma_m[x][y]
if (
y in D[v]
and x in D[v]
and y in D[x]
and D[v][y] == D[v][x] + D[x][y]
and sigma_m[v].get(x, 0)
and sigma_m[x].get(y, 0)
and sigma_m[v].get(y, 0)
):

)
C = [0, 1, 2]
b = nx.group_betweenness_centrality(G, C, normalized=False, endpoints=False)
assert b == 0.0

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.

This is the example from #8827 - thanks for that.

I'm still confused as to why this is the correct answer though - can you respond to my comment?

@dschult

dschult commented Aug 30, 2026

Copy link
Copy Markdown
Member

Thanks for this!
We have fixed the issue with a larger scale fix in #8879 and #8881.

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

4 participants