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.
| Component | Role | Typical choice |
|---|---|---|
| Substructure type | Defines what counts as a shared pattern | Walks, shortest paths, subtrees, graphlets |
| Feature map | Turns a graph into pattern counts | Sparse histogram of labels |
| Base kernel | Compares two individual parts | Exact label match or Gaussian |
| Normalization | Removes the effect of graph size | Cosine normalization |
| Learner | Uses the Gram matrix for prediction | Support vector machine, kernel ridge |
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 family | Compared substructure | Time complexity | Strength |
|---|---|---|---|
| Random walk kernel | Common walks in the product graph | Roughly cubic in node count | Captures long-range patterns |
| Shortest-path kernel | Pairs of nodes with equal path length | Quartic in the worst case | Simple and interpretable |
| Graphlet kernel | Small induced subgraphs of 3 to 5 nodes | Sampling makes it near linear | Works on unlabeled graphs |
| Weisfeiler-Lehman subtree kernel | Rooted subtrees from label refinement | Linear in edges per iteration | Fast and accurate baseline |
| Optimal assignment kernels | Best matching between node sets | Depends on the base kernel | Strong on chemistry benchmarks |
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.
| Aspect | Graph kernel | Graph neural network |
|---|---|---|
| Representation | Predefined substructure counts | Learned node and graph embeddings |
| Training data needs | Works with hundreds of graphs | Usually needs thousands or more |
| Scalability | Gram matrix grows quadratically | Mini-batch training scales well |
| Continuous features | Requires special kernels | Handled natively |
| Reproducibility | Deterministic, few hyperparameters | Sensitive to seeds and tuning |
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.
| Situation | Recommended kernel | Reason |
|---|---|---|
| Labeled molecules, small dataset | Weisfeiler-Lehman subtree | Fast, accurate and well studied |
| Unlabeled graphs | Graphlet or shortest-path | Rely on topology rather than labels |
| Continuous node attributes | Propagation or hash graph kernels | Compare real-valued features |
| Large graphs with many nodes | Sampled graphlet kernel | Avoids exhaustive enumeration |
| Need for interpretability | Shortest-path or WL features | Features map to readable patterns |
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.