Grammar Reinforcement Learning: path and cycle counting in graphs with a Context-Free Grammar and Transformer approach

Jason Piquenot, Maxime Berar, Romain Raveaux, Pierre Héroux, Jean-Yves Ramel, Sébastien Adam

Introduction & Motivation

Counting paths (sequences of connected nodes) and cycles (paths that start and end at the same node) is a cornerstone of graph theory. It is essential for everything from analyzing social networks and biological pathways to improving the "expressive power" of Graph Neural Networks (GNNs).

While we have known formulas for these counts, the search for optimal formulas is a massive combinatorial challenge. Most existing methods rely on matrix multiplications, which are computationally expensive ($O(n^3)$). The researchers asked: Can a deep learning agent discover more compact, cleverer ways to combine these matrix operations to get the same result with less computational overhead?

To answer this, the authors introduce Grammar Reinforcement Learning (GRL), a framework that uses a novel "Gramformer" architecture to discover highly efficient mathematical formulas for counting paths and cycles in graphs. By treating formula discovery as a language generation task constrained by a context-free grammar, GRL found new matrix-based identities that are 2x to 6.25x faster than current state-of-the-art methods.

Methodology

The GRL framework combines three sophisticated components to navigate the vast search space of mathematical formulas:

1. Context-Free Grammar (CFG) and the Gramformer

Instead of letting an AI guess random equations, the researchers define a specific grammar ($G_3$) that only allows valid matrix operations (like Hadamard products, matrix multiplications, and traces). This ensures every "sentence" the AI generates is a mathematically valid formula.

To process this grammar, the paper introduces the Gramformer. This Transformer architecture is designed to model a Pushdown Automaton (PDA). Since PDAs are the computational equivalent of CFGs, the Gramformer acts as a neural generator that navigates the hierarchical structure of mathematical expressions, handling nested and recursive logic (like parentheses) much better than a standard sequence model.

Derivation tree and stack operations for formula generation

2. Monte Carlo Tree Search (MCTS) and Evaluation

The system uses MCTS to explore the "tree" of possible formulas. It generates a set of candidate formulas and evaluates them by comparing their results against ground-truth counts on random graphs. The agent is rewarded for finding formulas that are both accurate and computationally "cheap" (requiring fewer operations).

The Grammar Reinforcement Learning (GRL) pipeline

Datasets

The researchers evaluated GRL using a mix of synthetic and real-world data:

  • Synthetic Random Graphs: Used during the training and search phase to provide ground-truth path and cycle counts.
  • IMDB-MULTI: A real-world graph dataset used to benchmark the empirical time savings of the newly discovered formulas against existing ones.
  • Directed Graphs: The authors extended their grammar to handle asymmetric adjacency matrices, testing GRL's ability to generalize to more complex graph types.

Theoretical Insights

  • Grammar as a Constraint: The most powerful idea here is using a formal grammar to bound the search space. By forcing the RL agent to speak the "language" of matrix algebra, the researchers turned a needle-in-a-haystack search into a structured optimization problem.
  • The 3-WL Connection: The researchers proved that their grammar is as expressive as the 3-order Weisfeiler-Leman (3-WL) test. This provides a theoretical "ceiling"—it explains why the system can count cycles up to length seven but hits a wall beyond that, as 3-WL cannot distinguish certain longer structures.
  • Efficiency over Complexity: The agent didn't just find new formulas; it found shorter ones. For a path of length 6, the discovered formula is significantly more compact than the previous standard, leading to massive speedups.

Empirical Results

The GRL agent successfully rediscovered all known state-of-the-art formulas and, more importantly, found several superior ones:

  • Significant Speedups: The new formulas reduced the number of required matrix operations, leading to theoretical and empirical speedups:
  • Length 3 paths: 2x faster.
  • Length 4 paths: 2.25x faster.
  • Length 5 paths: 4x faster.
  • Length 6 paths: 6.25x faster.
  • Directed Graph Discovery: GRL was the only tested method (beating out random search and standard MCTS) capable of discovering novel formulas for paths of length 4 and 5 in directed graphs.
  • Hardware Validation: Tests on the IMDB-MULTI dataset confirmed that the theoretical reduction in operations translated directly into faster execution times on real CPUs and GPUs.

Limitations and Future Work

While highly effective, GRL is computationally heavy. Finding the formulas for length-5 and length-6 paths took several weeks of training on high-end hardware (A100 GPUs and 124-core CPUs). Furthermore, because the current grammar is tied to 3-WL expressivity, it cannot count very long paths or complex substructures. The authors suggest that future work should focus on characterizing "$k$-WL grammars" to break this limit. Finally, while the resulting formulas are explicit and easy to use, the internal "reasoning" of the Gramformer as it navigates the grammar remains an area for further exploration.

Summary updated

BibTeX
@inproceedings{piquenot2025grammar,
  author = {Jason Piquenot and Maxime Berar and Romain Raveaux and Pierre Héroux and Jean-Yves RAMEL and Sébastien Adam},
  title = {{Grammar Reinforcement Learning: path and cycle counting in graphs with a Context-Free Grammar and Transformer approach}},
  booktitle = {International Conference on Learning Representations},
  year = {2025},
  url = {https://openreview.net/forum?id=yEox25xAED},
}
Figure