Repository navigation
Conversation
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
left a comment
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
| 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] | ||
| ): |
There was a problem hiding this comment.
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:
| 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) | |
| ): |
Co-authored-by: Dan Schult <[email protected]>
| ) | ||
| C = [0, 1, 2] | ||
| b = nx.group_betweenness_centrality(G, C, normalized=False, endpoints=False) | ||
| assert b == 0.0 |
There was a problem hiding this comment.
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?
Fixes #8827
Problem
group_betweenness_centralityreturns 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:Root cause
The sequential peeling applies the Puzis–Elovici–Dolev recurrences while iterating over group nodes. Two bugs in the term computation:
One shared zero-guard gates all three fraction terms. The guard required
sigma_m[x][v],sigma_m[v][y]andsigma_m[x][y]to be nonzero before computing any ofdxyv,dxvy,dvxy. But each term needs a different set of counts — in particulardxyv = sigma_m[x][y] * sigma_m[y][v] / sigma_m[x][v]needssigma_m[y][v], notsigma_m[v][y]. In directed graphs it is common for every shortestv -> ypath to pass through an earlier-peeled group node, zeroingsigma_m[v][y]and spuriously skipping a subtraction that must still happen. (For undirected graphssigma_m[y][v] == sigma_m[v][y], which is why this only manifested on digraphs.)dvxymixed peeled and unpeeled counts:sigma_m[v][x] * sigma[x][y] / sigma[v][y]used the originalsigmain the second factor and denominator instead of the successively-updatedsigma_m.Fix
Each fraction now guards only the counts it actually uses, and all three are computed consistently from the peeled
sigma_m:Validation
0.0.prominent_groupbuilds on the same peeling code path and benefits from the same fix.7932 passed); two new regression tests added (undirected + directed group-of-three cases, both failing before the fix).Notes
The
endpoints=Truepath 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.