Repository navigation
58 simrank - #160
Open
eriknw wants to merge 1 commit into
Open
58 simrank#160eriknw wants to merge 1 commit into
eriknw wants to merge 1 commit into
Conversation
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 Report❌ Patch coverage is 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:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Add simrank_similarity. NetworkX iterates
S = c * P.T @ S @ Pfrom the identity and sets thediagonal back to 1 at each step, where
Pis the adjacency matrix with each column divided byits count. It takes two dense n by n products a step, O(n**3). Here
Pstays sparse, so a stepis two products of the state by
P, O(n * m); the state stays sparse too while few pairs aresimilar, as on a tree.
Sstays symmetric, soP.T @ S @ PisY.T @ PwithY = 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'snp.allclose(prev, new, atol=tolerance)), and ExceededMaxIterations is raised, withNetworkX'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 (Pis NetworkX's to the bit), so thelargest 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 asourceandtarget; atargetalone 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_arrayreads 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_factorthat is not a float in (-1, 1), outside which theiteration need not converge;
max_iterationsthat is not an int of at least 1, whereNetworkX raises UnboundLocalError or TypeError; a
tolerancethat is not a float, or isnegative or NaN; and an unhashable
sourceortarget, which NetworkX finds in a list ofthe nodes.
np.allclosealso takes exactly equal entries as close, which decides only whenthe 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_numpyin 3.6.1, 3.7and dev; the dict loop
_simrank_similarity_pythonis still there, called only from itsown 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_iterationsaround 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 inNetworkX'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: thesame 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.
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
Ptimes a dense state (22x on the tree).Stacked on #121.