Graph Kernels

Machine Learning
Introduced by
Vishwanathan, Schraudolph, Kondor & Borgwardt
Year
2010
Type
Positive semi-definite kernel function over graphs
Family
Graph mining

What does it do?

Graph kernels are positive semi-definite kernel functions that measure the similarity between two graphs by comparing their shared substructures, such as random walks, shortest paths, or subtree patterns.

Vishwanathan, Schraudolph, Kondor, and Borgwardt (2010) placed these kernels within a unified framework, linking kernel methods to graph-structured data. Because the kernel is a valid similarity function, algorithms such as SVMs can operate on graphs directly, without first converting each graph into a fixed-length vector.

The intuition behind the method

Consider two molecular graphs. Instead of flattening each molecule into a fixed-length vector, a graph kernel counts how many small structural motifs, such as chains of bonds, ring patterns, or local neighbourhoods, the two molecules share. The more fragments they have in common, the more similar the kernel judges them to be. The inner product is computed without building the full high-dimensional feature vectors explicitly, although the number of pairwise kernel values still grows with the size of the dataset.

History

Graph kernels developed at the intersection of kernel methods and graph theory. Vishwanathan, Schraudolph, Kondor, and Borgwardt (2010) provided a unifying framework for these kernels in the Journal of Machine Learning Research, linking kernel methods to graph-structured data. Weisfeiler-Lehman subtree kernels are among the variants commonly used in practice, and graph neural networks have since become a common alternative for large-scale graph tasks.

When to use it

Graph kernels suit problems where each data point is a graph, such as molecules, protein contact maps, parse trees, or ego-networks, and the task is supervised (classification or regression) with a modest number of labelled graphs. They assume that overlap in substructures is a reasonable proxy for similarity in the target property. Because the method needs a Gram matrix over the training graphs, it is less suitable when the training set is very large or when the graphs are large enough that pairwise kernel computation becomes impractical. In those settings, graph neural networks, which learn representations directly from the data, are a common alternative. Graph kernels are also a weaker choice when the relevant patterns are not captured by the chosen substructure type.

Limitations and considerations

  • Pairwise cost: building the Gram matrix requires kernel evaluations for all pairs of training graphs. The matrix stores n² entries for n training graphs, so memory grows quadratically with the number of graphs, and SVM training adds a further cost that depends on the optimiser used and on the number of support vectors.
  • Fixed substructure type: a kernel built on one class of substructures may miss task-relevant patterns that it does not count.
  • Dynamic and heterogeneous graphs: the standard formulations assume a static graph with a fixed structure, so changing or multi-typed graphs generally require custom extensions.
  • Limited expressiveness for Weisfeiler-Lehman-based variants: these kernels cannot distinguish some non-isomorphic graphs, which can receive identical kernel values.

Source conditions do not replace independent scientific review or validation by an analysis engine.

Strengths

  • Valid kernel machinery: positive semi-definiteness means the Gram matrix defines a valid similarity, so the SVM dual is a convex quadratic program, and standard kernel-method theory applies.
  • No explicit feature engineering: structural features are extracted from graph topology and any available node or edge labels.
  • Choice of substructure: random-walk, Weisfeiler-Lehman, shortest-path, and graphlet kernels capture different kinds of structure, so the kernel can be matched to the domain.
  • Traceable similarity: for count-based kernels, the kernel value decomposes into contributions from specific shared substructures, which domain experts can inspect.

Applications

  • Molecular property prediction and drug discovery (chemical compound classification by structural similarity).
  • Protein function prediction using protein contact-map graphs.
  • Social network analysis: classifying ego-networks by structural role.
  • Natural language processing: comparing syntactic parse trees for sentiment or relation classification.
  • Malware detection by comparing program control-flow graphs.

How it works

How it works

1

Each graph G is implicitly mapped into a high-dimensional feature space by counting occurrences of structural primitives h, such as walks, paths, subtrees, or graphlets. The vector phi(G) collects these counts, where c(h, G) is the count or weight of substructure h in G.

Formula
\phi(G) = \sum_{h \in \mathcal{H}} c(h, G) \cdot \mathbf{e}_h
2

The kernel value k(G, G') is the inner product of the two feature vectors. It sums the products of the counts of each substructure that appears in either graph, giving a scalar similarity without constructing the feature vectors explicitly. Substructures absent from one of the two graphs contribute nothing to the sum.

Formula
k(G, G') = \langle \phi(G), \phi(G') \rangle = \sum_{h \in \mathcal{H}} c(h, G) \cdot c(h, G')
3

For a dataset of n graphs, all pairwise kernel values are assembled into the n x n Gram matrix K. The condition that K is positive semi-definite is what makes it a valid kernel matrix, which is the mathematical requirement for the kernel-based learning methods that consume it, such as SVMs.

Formula
\mathbf{K}_{ij} = k(G_i, G_j), \quad \mathbf{K} \succeq 0
4

The Gram matrix is passed to a kernel SVM or another kernel method. The SVM solves a quadratic program for a maximum-margin separator in the implicit feature space, using only pairwise kernel values. A new graph is classified by computing its kernel values against the training graphs that define the decision function.

Formula
\min_{\mathbf{w}, b, \xi} \frac{1}{2}\|\mathbf{w}\|^2 + C \sum_i \xi_i \quad \text{s.t.} \quad y_i(\mathbf{w}^\top \phi(G_i) + b) \geq 1 - \xi_i

Common pitfalls

  • Indefinite or improperly normalised kernel matrices violate the assumptions behind the SVM's convex formulation, so the optimisation may become non-convex and the resulting classifier unreliable.
  • Counting frequent but uninformative substructures, such as single edges in dense graphs, can dominate the inner product and inflate similarity scores unless the counts are weighted or normalised.
  • Because the kernel sums substructure counts, raw kernel values depend on graph size, and comparing graphs of very different sizes without normalisation biases the similarity towards the larger graph. Cosine normalisation, k(G, G') / sqrt(k(G, G) k(G', G')), is the usual remedy. For non-negative substructure counts it lies between 0 and 1 and gives every graph unit self-similarity. Dividing by a positive diagonal does not destroy positive semi-definiteness, so the normalised Gram matrix remains a valid kernel matrix. The choice of normalisation still changes which pairs appear similar, so it should be validated.
  • Ignoring node and edge labels where they exist discards domain-specific information that label-aware variants can use.

Frequently asked questions

What is the difference between a graph kernel and a graph neural network?

A graph kernel computes a fixed similarity measure between graph pairs using handcrafted substructures; it does not learn representations from data. A graph neural network learns node and graph embeddings end-to-end via gradient descent, adapting representations to the task. Graph kernels are often more interpretable and can be a practical choice for smaller datasets, while GNNs scale better to large datasets and can capture richer patterns.

Do graph kernels require node or edge labels?

No, but they can exploit them when available. Label-aware variants (e.g., labeled random walks, Weisfeiler-Lehman) incorporate node and edge attributes into substructure counting, which can improve discrimination on labelled datasets. Unlabeled variants rely solely on topological structure. Ignoring available labels can reduce predictive performance when labels carry task-relevant information.

How do I choose among random-walk, shortest-path, and Weisfeiler-Lehman kernels?

The Weisfeiler-Lehman subtree kernel is a commonly used starting point in the literature, but no kernel is best for every dataset. Shortest-path kernels are informative when global path structure matters. Random-walk kernels are theoretically elegant, but their computation involves linear systems defined on the product of the two graphs, which can be expensive. In practice, the choice is best settled by comparing candidate kernels with cross-validation on the training data.

Related methods

Sources

  1. Vishwanathan, S. V. N., Schraudolph, N. N., Kondor, R., & Borgwardt, K. M. (2010). Graph kernels. Journal of Machine Learning Research, 11, 1201–1242.

    Open publication reference