Skip to content

58 simrank - #160

Open
eriknw wants to merge 1 commit into
python-graphblas:20-wiener-efficiencyfrom
eriknw:58-simrank
Open

eriknw wants to merge 1 commit into
python-graphblas:20-wiener-efficiencyfrom
eriknw:58-simrank

Conversation

@eriknw

@eriknw eriknw commented Dec 3, 2026

Copy link
Copy Markdown
Member

Add simrank_similarity. NetworkX iterates S = c * P.T @ S @ P from the identity and sets the
diagonal back to 1 at each step, where P is the adjacency matrix with each column divided by
its count. It takes two dense n by n products a step, O(n**3). Here P stays sparse, so a step
is two products of the state by P, O(n * m); the state stays sparse too while few pairs are
similar, as on a tree. S stays symmetric, so P.T @ S @ P is Y.T @ P with Y = S @ P.

  • The step NetworkX stops at. The iteration stops at the first step where every entry is
    within tolerance + 1e-5 * |new| of the step before (NetworkX's
    np.allclose(prev, new, atol=tolerance)), and ExceededMaxIterations is raised, with
    NetworkX's message, when that step is the last one allowed, even one that converged, as
    NetworkX raises it. Each step here and in NetworkX is within about
    2 * n * 2**-53 / (1 - |c|) of the exact one (P is NetworkX's to the bit), so the
    largest change is checked against the threshold with a margin of 32 times that rounding,
    and a step inside the margin is left to NetworkX: a step apart would move every value by
    up to the tolerance.

  • Values agree with NetworkX's within 2.8e-16: the largest difference over 330 random
    graphs of up to 40 nodes, with factors from -0.5 to 0.95 and tolerances from 1e-6 to 0.05,
    and six larger graphs of up to 2025 nodes (grid, tree, dense and sparse random, directed
    random, preferential attachment) at three settings each. The step stopped at was
    NetworkX's in every case, and none came near enough to the threshold to be declined.

  • As in NetworkX: a dict of dicts in node order, a dict for a source, a float for a
    source and target; a target alone is checked and then ignored. NodeNotFound
    ("Source node 9 not in G", then the target's) comes before anything else. A directed graph
    uses in-neighbors, so a node with no in-edge is 0 to every other node; a self-loop counts
    as an in-edge; the null graph gives {}.

  • Left to NetworkX: an edge "weight" other than 1, which NetworkX's to_numpy_array
    reads although the dispatcher converts no attribute for this name; more than 5181 nodes,
    past which the n by n matrices could pass 2 GiB (they peak at 49 to 70 bytes a pair on
    graphs of 3000 to 5792 nodes, and the bound takes 80), declined before anything is
    converted; an importance_factor that is not a float in (-1, 1), outside which the
    iteration need not converge; max_iterations that is not an int of at least 1, where
    NetworkX raises UnboundLocalError or TypeError; a tolerance that is not a float, or is
    negative or NaN; and an unhashable source or target, which NetworkX finds in a list of
    the nodes. np.allclose also takes exactly equal entries as close, which decides only when
    the tolerance is negative or NaN: then NetworkX stops only where its iteration reaches a
    fixed point to the bit (on path_graph(4) it returns), which its own rounding decides.

  • NetworkX's own code. The public name calls _simrank_similarity_numpy in 3.6.1, 3.7
    and dev; the dict loop _simrank_similarity_python is still there, called only from its
    own docstring and tests.

  • Tests. 330 random graphs of up to 40 nodes: directed and undirected; int, string and
    tuple labels; isolated nodes, self-loops and nodes with no in-edge; edges with no data, a
    "weight" of 1, 1.0 or True, or another attribute; each answer form, sometimes for a node
    not in the graph; factors from -0.5 to 0.95 and tolerances from 1e-6 to 0.05. Each is also
    called at the two max_iterations around NetworkX's stopping step, which pins the step.
    Also six larger graphs, the errors, each decline, a step on the threshold and one 1e-13
    either side of it, and backend graphs, through backend= and with this backend first in
    NetworkX's priority. 33 mutants of the kernel and the wrapper each fail them, in NetworkX
    dev, 3.7 and 3.6.1.

  • NetworkX's tests (test_similarity.py) through the backend in dev, 3.7 and 3.6.1: the
    same 73 pass before and after. With fallback off its 16 simrank tests pass, all 12 of
    their calls to the public name served.

Speed, Indicative (one shared machine; each call from a NetworkX graph with its conversion
cache cleared and the whole answer built; best of three, interleaved with NetworkX's numpy
path). The load was 3.3 to 6.7 for the first four rows and 13 to 18 after, from work outside
the benchmark slot; two earlier runs rose the same way and gave 4.2x to 8.8x and 5.5x to 8.7x
for every pair, and 16.5x and 16.7x on the tree.

graph nodes edges answer networkx graphblas ratio load
random 2000 10000 every pair 1.39 s 0.27 s 5.2x 3.5
random 3000 15000 every pair 2.78 s 0.50 s 5.6x 3.3
random 4500 22500 every pair 5.54 s 1.00 s 5.5x 5.1
random 5000 25000 one source 6.98 s 0.54 s 12.9x 6.7
random 3000 15000 one source 2.77 s 0.24 s 11.3x 12.8
random, directed 3000 15000 every pair 2.66 s 0.48 s 5.6x 13.1
grid 3025 5940 every pair 8.71 s 0.84 s 10.3x 15.0
tree, edges from the root 2000 1999 every pair 3.62 s 0.16 s 22.0x 16.2
random, sparse 3000 3000 every pair 8.30 s 0.91 s 9.2x 18.4

The gain for every pair on dense random graphs is modest, about 5x: the answer itself, a dict
of n**2 Python floats built as NetworkX builds it, costs about as much as the iteration here.
The iteration alone, conversion included and no answer built, is 11x to 13x faster than
NetworkX's dense loop (100x on the tree, whose state stays sparse) and 2.7x to 4.1x faster
than scipy's sparse P times a dense state (22x on the tree).

Stacked on #121.

NetworkX iterates S = c * P.T @ S @ P from the identity and sets the
diagonal back to 1 at each step, where P is the adjacency matrix with
each column divided by its count (the in-degree, a self-loop included),
so a node with no in-edge is 0 to every other node. It takes dense n by
n products; here P stays sparse, and so does S while few pairs are
similar, as on a tree. S stays symmetric, so the step is two products
by P: Y = S @ P, then Y.T @ P.

The iteration stops where NetworkX's does: at the first step where
every entry is within tolerance + 1e-5 * |new| of the step before, its
np.allclose test. ExceededMaxIterations is raised when that step is the
last one allowed, as NetworkX raises it, even after converging there.
Each step here and in NetworkX is within rounding of the exact one, so
the largest change is checked against the threshold with a margin for
that rounding, and a step inside the margin is left to NetworkX.

Left to NetworkX: an edge "weight" other than 1, which NetworkX's
adjacency matrix reads but the dispatcher does not convert for this
name; more than 5181 nodes, past which the n by n matrices would pass
2 GiB; an importance_factor that is not a float in (-1, 1);
max_iterations that is not an int of at least 1, where NetworkX raises
UnboundLocalError or TypeError; a tolerance that is not a float, or is
negative or NaN, where np.allclose's taking exactly equal entries as
close decides; and an unhashable source or target, which NetworkX looks
up in a list.
@codecov

codecov Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 98.50746% with 6 lines in your changes missing coverage. Please review.
✅ Project coverage is 93.07%. Comparing base (5b54ace) to head (e30c96c).

Additional details and impacted files
@@                   Coverage Diff                    @@
##           20-wiener-efficiency     #160      +/-   ##
========================================================
+ Coverage                 92.88%   93.07%   +0.19%     
========================================================
  Files                       182      185       +3     
  Lines                     11227    11629     +402     
  Branches                   1274     1325      +51     
========================================================
+ Hits                      10428    10824     +396     
- Misses                      546      550       +4     
- Partials                    253      255       +2     
🚀 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