Graph kernel

On this page 7

A graph kernel is a positive semidefinite similarity function that measures how alike two graphs are by comparing their substructures, such as walks, paths, subtrees or small subgraphs. In graph learning, graph kernels let classical kernel methods, most notably the support vector machine, operate directly on graph-structured data without first converting each graph into a hand-crafted vector.

Graph kernels were the dominant approach to graph classification in chemistry and bioinformatics before graph neural networks became popular, and they remain strong, reproducible baselines within artificial intelligence research. Their central idea is simple: two graphs are similar when they share many matching patterns.

How a graph kernel measures similarity

A graph kernel computes an inner product in a feature space whose coordinates count substructures. Formally, a kernel k(G, G’) equals the dot product of two feature maps, φ(G) and φ(G’), where each entry of φ records how often a given pattern occurs in the graph. Because the function is positive semidefinite, it produces a valid Gram matrix that any kernel algorithm can consume.

Most practical kernels follow the R-convolution framework, which decomposes each graph into parts and sums a base kernel over all pairs of parts. The design choice is therefore which parts to compare and how to match them. Labels on nodes and edges, such as atom types or bond orders, are usually required for exact matching, while continuous attributes need special kernels.

The table below summarizes the main components of the computation and what each one controls.

ComponentRoleTypical choice
Substructure typeDefines what counts as a shared patternWalks, shortest paths, subtrees, graphlets
Feature mapTurns a graph into pattern countsSparse histogram of labels
Base kernelCompares two individual partsExact label match or Gaussian
NormalizationRemoves the effect of graph sizeCosine normalization
LearnerUses the Gram matrix for predictionSupport vector machine, kernel ridge
Building blocks of a graph kernel

Main families of graph kernels

Graph kernels are grouped by the substructure they enumerate, and this choice determines both expressiveness and cost. The Weisfeiler-Lehman subtree kernel is the most widely used because it is fast and accurate on labeled molecular graphs.

Kernel familyCompared substructureTime complexityStrength
Random walk kernelCommon walks in the product graphRoughly cubic in node countCaptures long-range patterns
Shortest-path kernelPairs of nodes with equal path lengthQuartic in the worst caseSimple and interpretable
Graphlet kernelSmall induced subgraphs of 3 to 5 nodesSampling makes it near linearWorks on unlabeled graphs
Weisfeiler-Lehman subtree kernelRooted subtrees from label refinementLinear in edges per iterationFast and accurate baseline
Optimal assignment kernelsBest matching between node setsDepends on the base kernelStrong on chemistry benchmarks
Common graph kernel families compared

The Weisfeiler-Lehman kernel repeatedly replaces each node label with a compressed label of its own label and its sorted neighbor labels, typically for 3 to 5 iterations. Counting labels at every iteration yields a feature vector that reflects increasingly large neighborhoods, which mirrors the logic of the Weisfeiler-Lehman test for graph isomorphism.

Graph kernels versus graph neural networks

Graph kernels and graph neural networks solve the same tasks with fixed versus learned representations. A kernel defines its features in advance, whereas a message passing network learns them from data through neighborhood aggregation. Theory links the two: standard message passing networks are at most as expressive as the one-dimensional Weisfeiler-Lehman test, so the Weisfeiler-Lehman kernel sets a meaningful reference point.

AspectGraph kernelGraph neural network
RepresentationPredefined substructure countsLearned node and graph embeddings
Training data needsWorks with hundreds of graphsUsually needs thousands or more
ScalabilityGram matrix grows quadraticallyMini-batch training scales well
Continuous featuresRequires special kernelsHandled natively
ReproducibilityDeterministic, few hyperparametersSensitive to seeds and tuning
Kernel methods and neural approaches on graphs

Careful benchmark studies have shown that on many small datasets, a well-tuned Weisfeiler-Lehman kernel matches or beats neural models. The advantage shifts toward neural methods when graphs carry rich continuous attributes or datasets grow large enough to support representation learning.

Choosing and applying a graph kernel

The right kernel depends on label availability, dataset size and graph density. Practitioners usually start with a Weisfeiler-Lehman baseline and test alternatives only when the data have properties it handles poorly.

SituationRecommended kernelReason
Labeled molecules, small datasetWeisfeiler-Lehman subtreeFast, accurate and well studied
Unlabeled graphsGraphlet or shortest-pathRely on topology rather than labels
Continuous node attributesPropagation or hash graph kernelsCompare real-valued features
Large graphs with many nodesSampled graphlet kernelAvoids exhaustive enumeration
Need for interpretabilityShortest-path or WL featuresFeatures map to readable patterns
Selecting a kernel for the data at hand

A typical workflow computes the Gram matrix once, normalizes it, and then tunes the SVM regularization constant and the number of kernel iterations with nested cross-validation. Libraries such as GraKeL and the kernel collections built around the TU Dortmund benchmark datasets provide tested implementations, which reduces the risk of subtle errors.

Limitations and common mistakes

The main weakness of graph kernels is quadratic memory and time in the number of graphs, because every pair must be compared. Beyond tens of thousands of graphs, the Gram matrix becomes impractical, and explicit feature maps or neural models become more attractive.

  • Diagonal dominance: graphs are far more similar to themselves than to others, which can make the SVM memorize rather than generalize.
  • Size bias: larger graphs contain more patterns, so unnormalized kernels favor them.
  • Too many iterations: deep Weisfeiler-Lehman refinement produces unique labels that no two graphs share.
  • Data leakage: tuning hyperparameters on the test folds inflates reported accuracy.
  • Expressiveness limits: kernels based on 1-WL cannot distinguish certain regular graphs.

Researchers also note that no single kernel dominates across benchmarks, so honest evaluation compares several kernels under the same cross-validation protocol before drawing conclusions.

A non-technical view

Imagine comparing two road maps. You count how many crossroads, loops and dead ends each map has. If the counts look alike, the maps are probably similar. A graph kernel does this for networks. It counts small shared patterns and turns them into one similarity score.

A computer can then sort networks into groups. It might separate safe molecules from harmful ones. The key point is simple: similar patterns mean similar networks, and the kernel makes that comparison exact and repeatable.

A concrete example

A pharmaceutical research team wants to predict whether candidate compounds are likely to be mutagenic before ordering laboratory tests. They have about 4,000 labeled molecules, each represented as a graph with atoms as nodes and bonds as edges, labeled by element and bond type.

The team computes a Weisfeiler-Lehman subtree kernel with four iterations, applies cosine normalization and trains a support vector machine with ten-fold cross-validation. The model reaches accuracy in the low-to-mid 80 percent range, comparable to the graph neural network they also tested, while training in minutes on a single workstation.

Because each feature corresponds to a readable atomic neighborhood, chemists can inspect which substructures push a prediction toward mutagenicity. The team uses the kernel model to rank new candidates for testing, reducing the number of laboratory assays spent on compounds that are likely to fail.