Skip to content

perf: sparse Laplacian + convergence check in spectral bisection#67

Open
sauravbhattacharya001 wants to merge 1 commit intomasterfrom
perf/sparse-spectral-bisection
Open

perf: sparse Laplacian + convergence check in spectral bisection#67
sauravbhattacharya001 wants to merge 1 commit intomasterfrom
perf/sparse-spectral-bisection

Conversation

@sauravbhattacharya001
Copy link
Owner

Summary

Replaces the dense O(n²) Laplacian with sparse adjacency-based matrix-vector multiplication in \GraphPartitioner.spectralBisect(), and adds a convergence check to power iteration.

Changes

  1. Sparse representation: \int[][] adj\ adjacency lists instead of \double[][] laplacian\ — mat-vec multiply is now O(m) instead of O(n²)
  2. No dense allocation: Memory drops from O(n²) to O(n + m)
  3. Convergence check: Early exit when ||v_new - v_old|| < 1e-10\ — small graphs converge in ~20 iterations instead of always running 200
  4. Max iterations raised to 500 for safety on ill-conditioned graphs

Performance Impact (n=5000 typical sparse graph)

Metric Before After
Memory ~200 MB (dense n×n × 3 matrices) ~100 KB (sparse adj lists)
Ops/iteration 25M (dense mat-vec) ~50K (sparse, avg degree 10)
Iterations Fixed 200 Adaptive, typically 20-50

Backward Compatibility

  • \LaplacianBuilder\ is unchanged and still available for other uses
  • Spectral bisection produces equivalent partitions (same algorithm, sparse implementation)
  • BFS and Kernighan-Lin strategies are unaffected

Closes #44

Replace dense O(n²) Laplacian matrix with sparse adjacency-based
matrix-vector multiplication in GraphPartitioner's spectral bisection.

Changes:
- Sparse representation using int[][] adjacency lists instead of double[][]
  Laplacian matrix — O(m) per mat-vec multiply vs O(n²)
- No dense matrix allocation — O(n + m) memory vs O(n²)
- Convergence check (||v_new - v_old|| < 1e-10) instead of fixed 200
  iterations — small/well-conditioned graphs converge in ~20 iterations
- Max iterations increased to 500 for safety on ill-conditioned graphs
- Removed dependency on LaplacianBuilder.buildSubgraphLaplacian for
  partitioning (LaplacianBuilder still available for other use cases)

Performance impact for n=5000 graph:
- Memory: 200 MB dense → ~100 KB sparse (typical sparse graph)
- Time: 5 billion ops → ~millions (sparse + early convergence)

Closes #44
@chatgpt-codex-connector
Copy link

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

@github-actions github-actions bot added visualization Graph visualization and UI size/m labels Mar 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/m visualization Graph visualization and UI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

perf: GraphPartitioner spectral bisection uses dense O(n^2) Laplacian and lacks convergence check

1 participant