Skip to content

54 laplacian centrality - #159

Open
eriknw wants to merge 1 commit into
python-graphblas:20-wiener-efficiencyfrom
eriknw:54-laplacian-centrality
Open

eriknw wants to merge 1 commit into
python-graphblas:20-wiener-efficiencyfrom
eriknw:54-laplacian-centrality

Conversation

@eriknw

@eriknw eriknw commented Dec 3, 2026

Copy link
Copy Markdown
Member

Add laplacian_centrality. NetworkX builds the dense Laplacian and, for each node, copies it
without that node's row and column and sums its squares: O(n3) time and O(n2) memory. Here
each node's drop in energy comes from a closed form in a few sparse products, O(m).

  • With d the Laplacian's diagonal (the weighted degree less any self-loop, as NetworkX's
    laplacian_matrix builds it), the energy is sum(d**2) plus the squared weights off the
    diagonal. NetworkX removes node i by taking |L[j, i]| off each other diagonal entry, so
    the drop is d[i]**2 + 2 * sum_j |w_ij| * d[j] + sum_j w_ij**2, the size of a negative
    weight included. A self-loop changes nothing.
  • Integer weights, or none, give NetworkX's values exactly while the energy is below 2**53
    (the sums here are in float64). With float weights the values agree to within about
    4e-16 of the whole energy, its rounding: NetworkX subtracts two energies, so a
    node that drops little carries that rounding, and this answer is the nearer to the exact
    one (on a random graph with weights of both signs, a relative error of 1e-13 against
    NetworkX's 2e-11).
  • NetworkX's checks, in its order: the null graph (NetworkXPointlessConcept), weights that
    sum to 0 (ZeroDivisionError, or int 0 for every node of the graph when not normalized,
    whatever the nodelist), then the nodelist (NetworkXError). When only self-loops carry
    weight the energy is 0 and every value is NaN, NetworkX's 0 / 0. The keys are the
    nodelist's items in its order, and walk_type and alpha are unused, as in NetworkX.
  • Left to NetworkX: a directed graph, whose directed Laplacian is dense and comes from an
    eigenvector ARPACK finds from a random start (its own answer moves in the last bits from
    call to call); a nodelist that is not a list of hashable nodes, which NetworkX meets with
    errors of its own; a Laplacian energy that is not finite (a NaN or infinite weight, or a
    square past the float range); float weights of both signs whose sum is so near 0 that
    NetworkX's order of summation decides whether it raises; and complex weights, which the
    sums here would read as their real parts. The weight sum is decided here when its order
    cannot matter: weights of one sign, multiples of one power of 2 whose sum stays exact, or
    an exact sum past the rounding a sum of that many terms can make. Complex weights are
    refused by the converted matrix's type, with "Edge attribute 'weight' has complex values;
    networkx serves the call", the words and rule 49 complex weights #149 uses for every name that reads weights.
    Speed, Indicative (one shared machine at load 3.8 to 4.6; each call in a fresh process from a
    NetworkX graph, conversion included; best of two interleaved rounds): about 760 to 3700 times
    faster than NetworkX on graphs where NetworkX takes 1.7 to 16 s; the gap widens with n, as
    NetworkX's time grows as n**3 and this one's with the edges.
graph nodes edges graphblas networkx
random, float weights 1000 5000 2.3 ms 1.75 s
random, float weights 2000 10000 4.3 ms 15.7 s
random, unweighted 1500 7500 3.8 ms 3.7 s
Barabasi-Albert, float weights 1500 4491 2.2 ms 3.4 s

Stacked on #121.

Each node's drop in Laplacian energy has a closed form, so the answer
takes a few sparse products where NetworkX rebuilds and sums the dense
Laplacian once per node. With d the Laplacian's diagonal (the weighted
degree less any self-loop, as NetworkX builds it), the energy is the sum
of d**2 and of the squared weights off the diagonal, and NetworkX's drop
for node i is d[i]**2 + 2 * sum_j |w_ij| * d[j] + sum_j w_ij**2. Integer
weights give NetworkX's values exactly while the energy is below 2**53;
float weights agree to within the rounding of the energy, which NetworkX
keeps by subtracting two energies.

The checks come in NetworkX's order: the null graph, then weights that
sum to 0 (ZeroDivisionError, or 0 for every node unnormalized), then the
nodelist. The keys are the nodelist's items, in its order.

Left to NetworkX: a directed graph, whose directed Laplacian is dense
and built from an eigenvector found iteratively; a nodelist that is not
a list of hashable nodes; a Laplacian energy that is not finite; float
weights of both signs whose sum is so near 0 that NetworkX's order of
summation decides whether it raises; and complex weights, which the
sums here would read as their real parts, refused by the converted
matrix's type.
@codecov

codecov Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.12%. Comparing base (5b54ace) to head (2f23fa0).

Additional details and impacted files
@@                   Coverage Diff                    @@
##           20-wiener-efficiency     #159      +/-   ##
========================================================
+ Coverage                 92.88%   93.12%   +0.24%     
========================================================
  Files                       182      185       +3     
  Lines                     11227    11623     +396     
  Branches                   1274     1321      +47     
========================================================
+ Hits                      10428    10824     +396     
  Misses                      546      546              
  Partials                    253      253              
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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.

1 participant