Exact Distance Kernels on Dense Graphs via Twin Compression and Tree-Metric Certificates

Many graph similarity measures used in machine learning are built from shortest-path distances, for example histograms of how often two node labels sit at distance $d$ from each other. Computing them exactly needs all-pairs distances. That costs roughly cubic time, and the shortest-path kernel can reach $O(N^4)$ as noted by Feragen et al.. The recent fast kernels, graph random features and Graph Voyagers, are built for random-walk kernels on sparse graphs. Dense graphs, such as distance-threshold graphs of point clouds, graph powers, and contact networks, are therefore where exact metric kernels are least affordable. These dense graphs are often highly redundant. Many vertices have identical neighborhoods, and what remains after merging them looks like cliques glued together in a tree.

This note proposes computing distance-based graph kernels exactly on a certified compression of each graph, so that cost scales with the size of a small "core" rather than with node or edge count. The compression has three parts. First, merge vertices with identical neighborhoods ("twins"). This preserves every shortest-path distance exactly. Second, delete a small set of "portal" vertices so that the rest is a disjoint union of cliques and trees. Third, read distances in that remainder off a tree, using the classical four-point condition and the median, or "fourth-point," condition as the certificate that a tree representation is faithful. The proposal also commits to two honest empirical questions. Do standard benchmarks contain enough of this redundancy to matter? And does exactness beat simply sampling pairs?

Core Claim

Twin quotienting is the right compression for metric kernels. The finer-grained compression people usually reach for, merging vertices that 1-WL color refinement cannot tell apart, is the right compression for walk kernels but not for distance kernels. Two true twins (equal closed neighborhoods) are at distance 1 from each other and at equal distance from every other vertex. False twins behave the same way, at distance 2 from each other. A quotient graph that keeps class sizes as multiplicities therefore reproduces any labeled distance histogram exactly. Color refinement merges far more vertices but can put structurally different graphs, such as two triangles versus a hexagon, into the same class even though their distance profiles differ. So it is exact for walk counts and wrong for distances.

On top of the quotient, suppose $X$ is a vertex set whose removal leaves a disjoint union of cliques and trees, the target class of Cliques or Trees Vertex Deletion. Then every distance factors through a tree-structured remainder and $k=|X|$ portal vertices:

$$d(u,v) = \min\Big( d_R(u,v),\ \min_{x,y \in X} \big[a_u(x) + D_X(x,y) + a_v(y)\big] \Big).$$

Here $d_R$ is the distance inside the remainder $R$ (infinite across components), $a_u(x)$ is the distance from $u$ to portal $x$, and $D_X$ is the portal-to-portal distance in the whole graph. Because $R$ is a union of cliques and trees, $d_R$ is answered in constant time after linear preprocessing with lowest-common-ancestor queries on a tree, so no $N\times N$ matrix is ever stored. If the quotient has $q$ vertices, the claimed cost is $O(N+M)$ for compression plus $O(q^2 k)$ for the exact histogram, with $O(qk)$ memory. Plain breadth-first search gives $O(NM)$, which is cubic on dense graphs.

The result is a novel exact algorithm only in the regime where $q \ll N$ or the quotient stays dense. Measuring whether that regime exists in practice is part of the claim.

Research Program

The first component is the compression with its exactness proof. Modular and twin decomposition run in linear time. The deliverable is a short theorem: labeled shortest-path histograms and distance-based Wasserstein features are invariant under twin quotienting with multiplicities. It comes with a counterexample showing they are not invariant under the color-refinement quotient.

The second component is finding the portal set. Here the forbidden-subgraph direction supplies the method. A Polynomial Kernel for Deletion to the Scattered Class of Cliques and Trees characterizes the cliques-and-trees class by excluding three induced patterns: the paw, the diamond, and cycles of length at least four. It builds a 4-approximate deletion set as a first step. Exactness of our kernel does not depend on $X$ being minimum, since any valid $X$ yields correct distances and a larger $X$ only costs time. We therefore reuse the approximation, with obstruction branching in the style of A Fast Branching Algorithm for Cluster Vertex Deletion used only when $k$ is small. This also gives a new structural parameter: the size of the smallest set whose removal leaves only cliques and trees, measured after twin quotienting. It generalizes twin-cover from Improving Vertex Cover as a Graph Parameter, where removal leaves only cliques, toward graphs made of dense blocks joined like a tree.

The third component is where the fourth-point condition enters. A characterization of minimum spanning tree-like metric spaces proves that a finite metric admits a tree representation with every point as a node if and only if it satisfies both the four-point condition and the median condition. That representation is then unique and equals the minimum spanning tree of the complete distance graph. For unweighted graph inputs, acyclicity is checked directly, so the theorem adds little. It earns its place in two other settings. The first is weighted remainders. The second is inputs given as distance matrices, such as the 3D coordinates in BZR and COX2. In both cases we want to replace a stored metric by its spanning tree only when that is exact. The same paper leaves open the $O(n^3)$ cost of checking triplets and the lack of tolerance to noise. We address both by sampling triplets and quadruples to test the conditions. If the measured violation rate and magnitude fall below a threshold, the minimum spanning tree is accepted. Otherwise the offending points are moved into $X$. Moving points into $X$ keeps the downstream kernel exact for whatever metric we store. The cost is that the stored metric may differ from the original inside $R$, and we measure that difference.

The fourth component connects to the forbidden-subgraph impossibility result. k-Leaf Powers Cannot be Characterized by a Finite Set of Forbidden Induced Subgraphs for k ≥ 5 uses the four-point condition to show that graphs generated by thresholding tree distances at $k\ge5$ have infinitely many minimal obstructions. Such graphs are dense, rich in twins (leaves hanging from the same tree node are true twins), and generated by a tree metric. They are therefore a natural stress test where finite pattern search fails but metric compression should succeed. We do not claim to solve leaf-power recognition. We only use these graphs as a regime where the compression's behavior can be predicted in advance.

Baselines

The naive reference point is the exact shortest-path kernel computed by breadth-first search from every vertex, with the identical labeled histogram and the same downstream classifier. It defines correctness: our features must match it bit for bit. It also defines the speed and memory baseline, run on the same hardware with a 24-hour cap per dataset, mirroring the timeout regime reported in Optimal Time Complexity Algorithms for Computing General Random Walk Graph Kernels on Sparse Graphs. Beating it means identical features at lower wall-clock time and memory on graphs where $q/N$ is small.

The strongest scalable competitors attack a different kernel, so the comparison is on downstream accuracy at a matched compute budget.

Graph Voyagers couple random walks on two graphs to estimate random-walk kernels in linear time on sparse graphs. They report up to 27× speedups over fixed-point iteration and an 8.5% relative improvement over geometric random-walk kernels on MUTAG. Graph random features from Taming graph kernels with random features estimate regularized Laplacian kernels with under 2% relative Frobenius error at 80 walks.

Propagation kernels hash diffused label distributions in time linear in the number of edges. They report raising 3D object categorization accuracy from 75.4% to 80.7%, which is the relevant reference for the point-cloud arm.

Wasserstein Weisfeiler-Lehman Graph Kernels. report 84.42% accuracy on BZR and 78.29% on COX2. Those numbers set the bar on continuous-attribute data. Wasserstein Embedding for Graph Learning reports 79.1% test ROC-AUC on ogbg-molhiv with an automated model-search head, which sets the bar at dataset scale. We feed our exact features through the same linear optimal-transport embedding so that the cost across the dataset is also linear in the number of graphs.

No reported result found on Lacuna for exact shortest-path kernels on NCI1, PROTEINS, or D&D in this file. Those baselines will be run by us under identical 10-fold splits, using the same hyperparameter grid size for every method.

Three ablations isolate the ingredients. The first uses the twin quotient alone, with breadth-first search on the quotient. The second uses the portal decomposition alone, without quotienting. The third substitutes the color-refinement quotient, which should yield wrong distance features. We measure how wrong and whether accuracy suffers.

Why not just…?

Why not just sample random node pairs and estimate the distance histogram? This is the most serious threat. With $s$ sampled pairs, histogram error shrinks like $1/\sqrt{s}$ by standard concentration, and the method needs no structure at all. Our experiments run it at equal wall-clock budget. The comparison should be decided by rare long-range or rare label-pair distances, whose sampling variance is large. If sampled histograms match exact ones in downstream accuracy on every benchmark, the honest conclusion is that exactness buys certification and memory savings rather than accuracy. We would then report the method as a verified fast path, not as an accuracy gain.

Why not just quotient by color refinement and run fast walk kernels, as Graph Voyagers and graph random features do? For walk kernels this is the right move, and we will not claim speedups there. But walk kernels and distance kernels measure different things. The two-triangles-versus-hexagon example shows that color-refinement classes do not preserve distances. Whether that difference matters for labels is settled empirically: we compare walk-kernel features and exact distance features at matched dimension on the same splits.

Why not just use treewidth-based dynamic programming? A clique already has treewidth $N-1$, while its twin-cover is empty, as Improving Vertex Cover as a Graph Parameter notes. Dense block-structured graphs are therefore exactly where treewidth methods blow up and our parameter stays small. We will still report treewidth estimates alongside $q$ and $k$ so that readers can see where each parameter is small.

Why not just quotient twins and stop? This is the first ablation, and on graphs whose quotient is sparse it may capture most of the gain. The tree-metric remainder should matter only when the quotient stays dense. Block graphs with many cut vertices per clique are the canonical case, since members of such a clique are not twins. We test this ablation directly on that synthetic family.

Proposed Experiments

The first and decisive descriptive check is how much redundancy real data contains. We measure the compression ratio $q/N$ and portal size $k$ on MUTAG, NCI1, ENZYMES, PROTEINS, D&D, BZR, COX2, and ogbg-molhiv. We expect sparse molecular graphs to have $q\approx N$ and $k$ near the number of rings, so speed gains there should be negligible. Stating this up front keeps the claim honest.

The method's regime is then built from the same data. We take $r$-th graph powers of PROTEINS and D&D graphs for $r$ from 2 to 6, which become dense and leaf-power-like. We also build distance-threshold graphs from the 3D object categorization point clouds and from BZR and COX2 coordinates. Finally, we generate synthetic families: $k$-leaf powers of random trees for $k$ from 2 to 8, and random block graphs with injected noise edges. On these we report time and memory against exact breadth-first search, verify bit-identical features, and plot cost against $q^2k$ to test the predicted scaling.

Downstream, we report 10-fold accuracy on the TU datasets and test ROC-AUC on ogbg-molhiv against the baselines above at matched compute.

For the point-cloud arm, we report how often the sampled four-point and median checks accept a spanning-tree representation. We report the resulting distortion relative to the true metric and accuracy relative to the 80.7% propagation-kernel reference.

The proposal is falsified in two ways. It fails if graph powers and threshold graphs show no gap between $q$ and $N$. It also fails if the equal-budget sampling estimator matches exact features in accuracy everywhere. In the second case the contribution reduces to a certified memory-saving path for dense structured graphs.