Skip to content

perf(analyze): sample edge betweenness on large graphs in _cross_community_surprises - #5445

Open
Gaurav-meena95 wants to merge 1 commit into
Graphify-Labs:v8from
Gaurav-meena95:perf/sampled-edge-betweenness
Open

Gaurav-meena95 wants to merge 1 commit into
Graphify-Labs:v8from
Gaurav-meena95:perf/sampled-edge-betweenness

Conversation

@Gaurav-meena95

@Gaurav-meena95 Gaurav-meena95 commented Nov 24, 2026 •

Copy link
Copy Markdown

Fixes #5444

What does this PR do?

Optimizes edge betweenness centrality fallback in graphify/analyze.py (_cross_community_surprises) by sampling $k = \min(50, |V|)$ source nodes with seed=42 when $|V| > 500$, bringing execution time on 3,000-node graphs from ~20s down to ~0.3s (~60x speedup).

Problem

When communities is empty, _cross_community_surprises() falls back to nx.edge_betweenness_centrality(G) for graphs up to 5,000 nodes. Without sampling, pure Python computes exact edge betweenness via $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.

While node betweenness in suggest_questions() (analyze.py#L602) already uses sampling, edge betweenness was left unsampled.

Solution

  • Added sample gating: k = min(50, G.number_of_nodes()) if G.number_of_nodes() > 500 else None with seed=42.
  • Graphs with $\le 500$ nodes retain un-sampled exact betweenness.
  • Deterministic behavior guaranteed across runs via pinned seed=42.
  • Graphs with $> 5,000$ nodes continue to return [] immediately as before.

Benchmark Results

Nodes Edges Unsampled (Before) Sampled k=50 (After) Speedup
1,000 2,495 1.0864s 0.0535s 20.3x
2,000 9,941 6.1835s 0.1559s 39.7x
3,000 22,340 19.4922s 0.3244s 60.1x

Type of change

  • Refactor

Verification & Invariants

  • Read the CONTRIBUTING.md guide.
  • Reproduced the issue and identified the invariant.
  • Made the smallest fix necessary.
  • Added a regression test (tests/test_edge_betweenness_sampling.py verifying small graphs exact output, large graph sampling, determinism with seed=42, empty edges, and >5000 nodes guard).
  • Kept the PR description synchronized with the final implementation.
  • Documented any limitations / unsupported cases explicitly.

How was this tested?

uv run pytest tests/test_edge_betweenness_sampling.py tests/test_analyze.py -q
PATH="$(pwd)/.venv/bin:$PATH" uv run pre-commit run --all-files
uv run graphify update .

Graphify-specific checklist

  • I updated generated skill artifacts (uv run python -m tools.skillgen --bless) when changing their source fragments.
  • I confirmed that AST/structural extraction remains deterministic (pinned seed=42).
  • I reviewed changes for security implications.
  • I confirmed no API keys or local-only graph data are included.
  • (If applicable) I disclosed AI authorship in my commit messages.

…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>
@github-actions

Copy link
Copy Markdown

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.

@graphify-labs graphify-labs Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Graphify review View in Graphify

Nothing blocking · 7 possible issues to check · Merge confidence 5/5

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

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

@Deepjyoti-Sarmah

Copy link
Copy Markdown
Contributor

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 extract --code-only --no-cluster and compared the top 5 edges with exact betweenness. I had to fold parallel edges first, because of the crash below.

graph nodes edges exact k=50 top 5 kept
test_extract.py 628 731 0.06 s 0.01 s 0/5
test_languages.py 745 1154 0.10 s 0.02 s 1/5
a bundled main.js 2085 4700 1.65 s 0.07 s 0/5

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 main.js in 0.08 s and took 3.8 s on your graph. Dense graphs cost a few seconds more than in this PR because it tries exact first.

Separately, this line crashes on multigraphs, with or without sampling, because networkx keys their scores (u, v, key). graphify://surprises hits it on graphs from extract --no-cluster. I opened #5446 for that. The two PRs conflict here, and I'm happy to rebase #5446 if yours lands first.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug/Perf]: _cross_community_surprises runs unsampled O(V * E) edge betweenness on up to 5,000 nodes

2 participants