Repository navigation
Conversation
amcandio
left a comment
There was a problem hiding this comment.
Thanks for this!
Looks good. Would be great to have some data around running time so we are sure we are not degrading on that front (it's probably already faster than previous version).
Regarding correctness, it would be great to have some randomized script that compares old and new versions over several graphs. We usually don't commit those but you can include it on the description.
Just added into the PR body what I ran 🗡️ lmk if this is how it should be done, tbh I've never included code in a PR body so I hope this is okay! |
rossbar
left a comment
There was a problem hiding this comment.
Closing per agentic PR policy.
Hey @rossbar could you please clarify whjch part violated the policy? I've already checked the guidelines before today and tbh my understanding was that the policy allows AI assisted work if I manually review, understand the change, and ofc disclose AI use (which I did). I answered questions about the code as well.. Ik you guys reserve the right to close without explanation but if you could please lmk so I don't make the same mistake again, I really don't want to give off any wrong impressions. Thanks! ( @amcandio what are your thoughts on this? ) |
|
I haven't had time to finish reviewing but not seeing red flags. That 2x is great! If you have time, It would be great to add official benchmarks so we can track future improvements and ease reproducing. You can look at the other examples of benchmarks in the code base. I'm not seeing "spammy" AI related behavior so happy to keep this one open and continue reviewing it as I find time. |
cool! I added official ASV benchmarks for I kept the randomized correctness script only in the PR description and committed only the benchmark source like you said ASV built both commits and the report was that some benchmarks was that performance increased some results were: |
|
I see that the behavior is different for this graph (your version returns 1). Is that the bug you mention? Can you elaborate more? >>> 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)
...
Group: [3, 4, 2]
Group betweenness centrality: 3.0 |
yes this is the bug! the old implementation returns 3.0 on this graph but the correct answer is 1.0 from my understanding. the non-group nodes are {0, 1, 5}. Of the 6 directed pairs among them, only (5, 0) matters -- the unique shortest path is 5->3->2->0 (cost 6), which goes through group nodes 3 and 2. the other two reachable pairs (1,0) and (1,5) go direct so GBC = 0 + 0 + 1 = 1.0. the old code gets 3.0 because its endpoint correction is computed from forward reachability only (D[group_node]) which misses paths arriving at group nodes from non-group sources. node 5 can reach 3 and 2 but dneither can reach 5, so on a directed non-strongly-connected graph the subtraction undershoots and you get 9 - 6 = 3 instead of 1 |
|
@amcandio :) |
|
pinging here again for visibility |
|
I think it would be worth fixing the bugs in the original implementation as part of #8854 and then revisit this PR. Full rewrites are tricky to review and especially if they also change behavior! |
Fixes #8559.
Summary
group_betweenness_centralitycould return negative values or raiseKeyErroron directed non-strongly-connected graphs. The endpoint correction and pairwise overlap terms assumed reachability that these graphs do not guarantee.this replaces the Puzis matrix approach with a direct computation so for each source count shortest paths through the group as
(sigma - sigma_avoid) / sigmawhere
sigma_avoidis computed by a predecessor-DAG DP over shortest paths avoiding the group.This also fixes wrong results that were silent for groups of size 3 or larger across directed, undirected, and weighted graphs.
Tests
Added two regression tests. Also checked against a brute-force reference, against
betweenness_centralityfor single-node groups, and against existing tests. Previously correct inputs keep the same output.Notes
prominent_grouphas a separate deeper bug so it's left for future follow ups.Compared old vs new against a brute-force
nx.all_shortest_pathsground truth on 2000 random graphs covering directed/undirected, weighted/unweighted,n = 5–11and group sizes 1–3:So the new version matches the ground truth in all cases tested and is considerably faster.
comparison script
AI disclosure
I used an AI assistant to help double check the code + make sure I didn't miss anything stupid + run some local tests