Repository navigation
Make a node of many twins cheap to take apart - #15
Conversation
A star's leaves are all twins, and so are the halves of a perfect matching. Such
a part can never be refined -- nothing distinguishes its members -- so
init_partition! is what takes it apart, one vertex per call, and it scanned the
whole part each time and then relabelled every element of it. A graph with n-1
edges cost Θ(n²).
Split the part by moving only x and its neighbours to the front of its range.
Whatever is left is already in place and becomes the third part, and the old
part number stays with the larger of the two sides so only the smaller side is
relabelled. That is O(deg x) against O(|p|).
classify_nodes had the same shape of problem waiting behind it. It decides a
node by comparing each child against each of the others, which stops early on a
prime node and runs to the end on a degenerate one, so a node with n-1 children
costs Θ(n²) however few edges there are. For a symmetric matrix the question is
only whether every pair of children is related alike, and that can be counted:
a pair crosses between two children at exactly one node, the lowest holding both
of its ends, so one pass over the stored entries gives every node its crossing
count and whether one label covers them all. Against the crossings there could
be, which the children's sizes give, that decides the node. Only for a matrix
that stores its edges; reading a dense one costs the Θ(n²) this avoids, and
there Θ(Σk²) is no worse than the input.
Neither is enough alone. Counting the crossings is worth 10% by itself, because
init_partition! dominated; with init_partition! fixed, the pairwise comparison
becomes the next wall for the same graphs.
Building the tree of a sparse graph, cost per word of the representation:
n before after ns/word after slope
star 4000 0.8402s 0.0082s 684.3 1.00
perfect match 4000 0.4032s 0.0081s 1017.5 0.97
random m≈3n 64000 0.1444s 0.1491s 332.8 1.19
path 64000 0.0422s 0.0423s 220.3 1.08
102x and 50x, and flat per word: linear in a sparse input for all four shapes,
where before two of them were quadratic. Graphs that were already linear are
unchanged.
Trees are identical to master over 3000 random graphs and digraphs. Dense and
sparse agree exhaustively over every digraph on n ≤ 4 and every symmetric graph
on n ≤ 6, which is the check that matters since they now take different paths
through both changes. 5,670,000 symmetric permutations, every graph on n ≤ 6
against every starting order, and 1,440,000 digraph permutations over four
generators: all modular.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013w7XdHqa7ynpy1nN1j19Qp
| and allocation-free and so is left in place. | ||
| """ | ||
| is_symmetric(G::AbstractMatrix) = issymmetric(G) | ||
| is_symmetric(G::SparseMatrixCSC) = G == permutedims(G) |
There was a problem hiding this comment.
Should we call issymmetric(G) as a fast path and then fall back to this?
There was a problem hiding this comment.
I looked into it and the answer is no, for three reasons — the first of which I
did not expect.
issymmetric can throw rather than answer. On a sparse matrix carrying
stored zeros it walks off the end of a column and indexes past the end of
rowval. Minimal case:
julia> S = SparseMatrixCSC(6, 6, [1,1,2,3,4,5,6], [4,4,3,6,1], [0,1,1,1,0]);
julia> Matrix(S) == Matrix(S)'
false
julia> issymmetric(S)
ERROR: BoundsError: attempt to access 5-element Vector{Int64} at index [6]So a fast path would need a try/catch as well as a re-check, which is not
much of a fast path.
The answer it gives quickly is the one we cannot use. The bug is false
positives — over 400,000 random sparse matrices it claimed symmetry 13 times
when there was none, and never denied symmetry that was there. So false is
trustworthy and true is not, and a fast path only saves work in the false
case.
And at the densities that matter it is not faster anyway. This is where I
had it wrong earlier in the thread — I measured it twice and got opposite
answers, because the two runs were at different densities:
| entries per column | issymmetric |
== permutedims |
ratio |
|---|---|---|---|
| 6 (n = 64,000) | 0.00401s | 0.00346s | 0.86× |
| 6 (n = 256,000) | 0.02222s | 0.01773s | 0.80× |
| 40 | 0.00582s | 0.00669s | 1.15× |
| 396 | 0.06142s | 0.11853s | 1.93× |
issymmetric wins by 1.9× at four hundred entries per column, and a matrix that
dense wants to be a Matrix, which takes the other method entirely. At the six
or so entries per column where a sparse matrix earns its keep, the direct
comparison is already the quicker of the two.
Pushed in 7fcb183, which puts all three reasons in the docstring so the next
person to ask does not have to measure it again. I have also added the
BoundsError to JuliaSparse/SparseArrays.jl#748 — it is the same cursor
overrun, but throwing rather than lying is a third symptom and worth recording
there.
Three reasons, none of them obvious from the outside: the answer that cannot be trusted is the one a fast path would want to keep, it can throw a BoundsError on a sparse matrix with stored zeros rather than answer at all, and at six entries per column the direct comparison is already the quicker of the two. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_013w7XdHqa7ynpy1nN1j19Qp
The last two quadratics in the symmetric path, both on graphs made of twins.
Taking apart a node of twins
A star's leaves are all twins, and so are the halves of a perfect matching. Such
a part can never be refined — nothing distinguishes its members — so
init_partition!is what takes it apart, one vertex per call. It scanned thewhole part each time and then relabelled every element of it, so a graph with
n−1 edges cost Θ(n²).
It now splits the part by moving only
xand its neighbours to the front of itsrange: whatever is left is already in place and becomes the third part, and the
old part number stays with the larger of the two sides so only the smaller side
is relabelled. O(deg x) rather than O(|p|).
I had this one wrong until I profiled it. I opened this branch to fix
classify_nodes, on the reasoning that a node with n−1 children costs Θ(k²).That reasoning is correct and it was not the bottleneck: rewriting it bought
10%, and the profile put 803 of 805 samples in
init_partition!.Deciding a node without comparing every pair
classify_nodesdecides a node by comparing each child against each of theothers. That stops early on a prime node and runs to the end on a degenerate
one, so a node with n−1 children costs Θ(n²) however few edges there are.
For a symmetric matrix the question is only whether every pair of children is
related alike, and that can be counted instead. A pair of vertices crosses
between two children at exactly one node — the lowest one holding both of its
ends — so one pass over the stored entries gives every node its crossing count
and whether a single label covers them all. Against the crossings there could be,
which the children's sizes give: none means complete on the non-edge label, all
under one label means complete on that label, anything else is prime.
Only for a matrix that stores its edges. Reading a dense one costs the Θ(n²) this
is avoiding, and there Θ(Σk²) is no worse than the input, so dense keeps the
comparison — same split as the cutters in #11.
Neither change is sufficient alone: counting is worth 10% while
init_partition!dominates, and once
init_partition!is fixed the pairwise comparison becomesthe next wall for exactly these graphs.
Effect
Building the tree of a sparse graph, with cost per word of the representation:
102× and 50×, flat per word. Linear in a sparse input for all four shapes,
where two of them were quadratic; the two that were already linear are unchanged.
Verification
masterover 3000 random graphs and digraphs.symmetric graph on n ≤ 6 — the check that matters, since they now take
different paths through both changes.
order (23,594,424 permutations); 1,440,000 digraph permutations over four
generators. All modular.
🤖 Generated with Claude Code
https://claude.ai/code/session_013w7XdHqa7ynpy1nN1j19Qp