Graph Neural Networks Can (Often) Count Substructures

Paolo Pellizzoni, Till Hendrik Schulz, Karsten BorgwardtICLR 2025

TL;DR

Standard Graph Neural Networks (GNNs) are theoretically proven to be incapable of counting arbitrary subgraphs, yet they perform this task surprisingly well in practice. This paper bridges that gap by moving beyond worst-case theoretical scenarios, proving that real-world graphs possess local asymmetries that allow standard GNNs to sample-efficiently learn and count substructures by simulating a novel dynamic programming algorithm.

Introduction

In fields like chemistry and biology, detecting and counting specific graph substructures—such as functional groups in a molecule—is a critical task. However, a highly influential 2020 paper proved that standard message-passing GNNs are fundamentally limited by the 1-Weisfeiler-Leman (1-WL) test. Theoretically, this means they cannot count arbitrary subgraphs, prompting researchers to design increasingly complex, memory-heavy, and computationally expensive "higher-order" GNN architectures.

But there is a catch: in practice, standard GNNs actually can count graph patterns quite accurately on real-world datasets. The authors tackle this apparent contradiction. They aim to explain why the worst-case theoretical limitations of GNNs rarely apply to real-world data, and they seek to establish the mathematical conditions under which standard GNNs can successfully and efficiently count subgraphs.

Methods

Instead of analyzing GNNs on arbitrary, worst-case graphs (which are often highly symmetric and adversarial), the authors analyze them on restricted classes of graphs that better represent reality.

  • Fine-Grained Distinguishability: They introduce the concept of $(\ell, k)$-identifiability. Because subgraph counting is a "local" task, a GNN doesn't need to distinguish the entire graph; it only needs to distinguish local neighborhoods (ego-nets of radius $k$) using $\ell$ message-passing layers.
  • Algorithmic Alignment: The authors develop a novel dynamic programming (DP) algorithm called TREE-COLSI for solving "colorful subtree isomorphism." This is inspired by classical color-coding algorithms but adapted so it doesn't require random color assignments.
  • GNN Simulation: They mathematically prove that standard message-passing GNNs can efficiently simulate TREE-COLSI. The GNN uses the node embeddings generated in early layers as "colors" to ensure that when it pieces together a larger subgraph from smaller parts, it doesn't accidentally map different parts of the pattern to the same node in the target graph (a condition they call "quite-colorful").
Examples of graphs and their unwrapped tree representations (universal covers).

Datasets

The theoretical findings were validated on both real-world and synthetic datasets:

  • Real-world molecular benchmarks: Mutagenicity, MCF-7, ZINC, ogbg-molhiv, ogbg-molpcba, Peptides-func, and PCQM-Contact. Node features were restricted to atom types to prevent the models from "cheating" using pre-computed subgraph features.
  • Synthetic datasets: Custom-generated graphs with specific node degree sequences and cycle constraints, designed specifically to test the theoretical bounds of their algorithms without the bias of star-shaped patterns.

Key Insights

  • Worst-case theory isn't practical reality: The theoretical failure of GNNs relies on pairs of highly symmetric, indistinguishable graphs. In real-world datasets, these indistinguishable pairs almost never exist.
  • Local uniqueness is enough: You don't need infinite parameters to count subgraphs. Because the task only depends on local structures, the sample complexity (how much data the model needs to learn) depends on the diversity of local neighborhoods, not the size of the entire graph. This explains why GNNs generalize well on this task.
  • Message passing creates unique IDs: After just a few message-passing steps, the embeddings of nodes in real-world graphs become unique enough to act as distinct "colors." The GNN uses these colors to track injectivity—ensuring it is actually finding a valid subgraph rather than folding a pattern back onto itself.

Key Results

  • Empirical Identifiability: On real datasets, the authors found that more than 97% of local neighborhoods can be uniquely identified by a node's WL color after just a few iterations ($\ell = k+2$).
  • High Counting Accuracy: A simple 4-layer standard GNN achieved remarkable accuracy in counting various tree and cyclic patterns across molecular datasets. For example, on the ZINC dataset, the model achieved a normalized Mean Absolute Error (nMAE) mostly below 0.03 and AUCs frequently above 0.95.
  • Colorful Mappings in Practice: The theoretical requirement that subgraph mappings be "quite-colorful" holds true in practice. For datasets like ZINC and MCF-7, nearly 100% of subgraph isomorphisms become quite-colorful after just 3 iterations of color refinement.

Limitations and Future Work

Limitations: The core dynamic programming algorithm (TREE-COLSI) and its strictest theoretical guarantees apply primarily to tree patterns. While the authors discuss extensions to cyclic patterns using universal covers (unwrapped trees, as visualized above), these come with weaker guarantees and potential false positives. Additionally, they noted that a few specific patterns resist becoming "quite-colorful" regardless of how many message-passing layers are used.

Future Work: This paper proves that raw expressivity (the ability to distinguish graphs) is rarely the actual bottleneck for standard GNNs in practice. Therefore, the authors suggest a pivot in GNN research: if standard GNNs are expressive enough, why do complex higher-order architectures often perform better on prediction tasks? Future work should investigate if these complex models benefit from easier optimization landscapes or better sample complexity, rather than just raw representational power.

Summary updated

BibTeX
@inproceedings{pellizzoni2025graph,
  author = {Paolo Pellizzoni and Till Hendrik Schulz and Karsten Borgwardt},
  title = {{Graph Neural Networks Can (Often) Count Substructures}},
  booktitle = {International Conference on Learning Representations},
  year = {2025},
  url = {https://openreview.net/forum?id=sZQRUrvLn4},
}
Figure