Repository navigation
perf(analyze): sample edge betweenness on large graphs in _cross_community_surprises - #5445
Gaurav-meena95 wants to merge 1 commit into
Conversation
…unity_surprises When communities is empty, _cross_community_surprises() falls back to computing edge betweenness centrality for graphs up to 5,000 nodes. Previously, it called nx.edge_betweenness_centrality(G) without sampling, incurring an O(V * E) all-pairs shortest paths calculation in pure Python that caused the CLI to freeze for 16-60+ seconds on 2,000-5,000 node graphs. Use k = min(50, len(G)) sampling when len(G) > 500 with seed=42, matching the sampling pattern in suggest_questions(). Graphs with <= 500 nodes retain exact betweenness. Measured a 39x-73x speedup (16.5s -> 0.22s on 3,500 nodes) while preserving deterministic results. Co-authored-by: Antigravity <antigravity@google.com>
|
Thanks for the pull request, @Gaurav-meena95. A maintainer will review it soon. Want to talk it through while it is in review? Come join us on our Discord server. For longer-form discussion there is also GitHub Discussions. A couple of things that speed up review: make sure the test suite passes on Python 3.11 and 3.13, and that the change keeps extraction deterministic. |
There was a problem hiding this comment.
Graphify review 
7 possible issues to check · High impact
For graphs without community data, edge betweenness is now sampled with a fixed seed when the graph has more than 500 nodes.
7 possible issues we couldn't confirm
- Bridge notes for graphs with 501 to 5000 nodes report sampled betweenness as if it were exact, and the chosen bridges can change ·
graphify/analyze.py:413 - Sampled scores give many edges zero betweenness, so the bridge list can fill with arbitrary zero-score edges ·
graphify/analyze.py:413 - The over-5000-node test can pass even if the size guard is removed, because its nodes have no labels or source files ·
tests/test_edge_betweenness_sampling.py:106 - +4 more in Graphify
They don't change the score.
Code graph · 1089 functions depend on this change
- 1 function became more coupled.
_cross_community_surprises()(4 callers). - 64 of 621 test files are affected by this change.
- Gate, test list and every flagged function: in the Graphify check.
Reviewed 3d4b465 · Full report in the Graphify check
|
I ran this while looking into #5444, and it does fix the slowdown. Your graph took 94–104 s (wall) on v8 and 2.6 s CPU with this PR, on a slow machine. My worry is accuracy on sparse files. I built single-file graphs with
Graphs with planted bridges did better and kept 3–4 of 5. The server loads graphs as directed, so on these files most searches reach few nodes and exact is already cheap. You could cap the work instead of the node count: search exactly, and sample only once the edge visits pass a budget. I tried that with a list-based search like #5303's and 5M visits. It kept 5/5 on Separately, this line crashes on multigraphs, with or without sampling, because networkx keys their scores |
Fixes #5444
What does this PR do?
Optimizes edge betweenness centrality fallback in$k = \min(50, |V|)$ source nodes with $|V| > 500$ , bringing execution time on 3,000-node graphs from ~20s down to ~0.3s (~60x speedup).
graphify/analyze.py(_cross_community_surprises) by samplingseed=42whenProblem
When$O(V \cdot E)$ Brandes algorithm traversals from all source nodes, freezing the process for 20-60+ seconds on 2,000-5,000 node graphs.
communitiesis empty,_cross_community_surprises()falls back tonx.edge_betweenness_centrality(G)for graphs up to 5,000 nodes. Without sampling, pure Python computes exact edge betweenness viaWhile node betweenness in
suggest_questions()(analyze.py#L602) already uses sampling, edge betweenness was left unsampled.Solution
k = min(50, G.number_of_nodes()) if G.number_of_nodes() > 500 else Nonewithseed=42.seed=42.[]immediately as before.Benchmark Results
Type of change
Verification & Invariants
tests/test_edge_betweenness_sampling.pyverifying small graphs exact output, large graph sampling, determinism with seed=42, empty edges, and >5000 nodes guard).How was this tested?
Graphify-specific checklist
uv run python -m tools.skillgen --bless) when changing their source fragments.