Recursive Algorithmic Reasoning

Jonas Jürß, Dulhan Hansaja Jayalath, Petar Veličković

Abstract

Learning models that execute algorithms can enable us to address a key problem in deep learning: generalizing to out-of-distribution data. However, neural networks are currently unable to execute recursive algorithms because they do not have arbitrarily large memory to store and recall state. To address this, we (1) propose a way to augment graph neural networks (GNNs) with a stack, and (2) develop an approach for sampling intermediate algorithm trajectories that improves alignment with recursive algorithms over previous methods. The stack allows the network to learn to store and recall a portion of the state of the network at a particular time, analogous to the action of a call stack in a recursive algorithm. This augmentation permits the network to reason recursively. We empirically demonstrate that our proposals significantly improve generalization to larger input graphs over prior work on depth-first search (DFS).

Introduction

Deep learning models are notoriously limited in their ability to generalize to "out-of-distribution" (OOD) data. While they perform exceptionally well on data similar to their training sets, they often fail when presented with larger or more complex instances. In contrast, classical algorithms are guaranteed to be correct regardless of input size. The field of Neural Algorithmic Reasoning (NAR) attempts to bridge this gap by training neural networks to mimic step-by-step algorithmic execution.

While Graph Neural Networks (GNNs) have become adept at iterative algorithms, they struggle with recursive algorithms like Depth-First Search (DFS). Recursion requires a "call stack" to store and recall previous states as the algorithm explores deeper into a problem. Standard neural networks typically rely on fixed-size hidden states, which cannot handle the arbitrarily deep state-tracking required for recursion on large graphs.

Methods: Stack-Augmented GNNs

To address the memory limitations of standard GNNs, the authors introduce a structured stack memory into the architecture. At each time step, the network predicts not just the next state, but also a specific stack operation: push, pop, or noop.

The stack-augmented GNN architecture, showing the integration of a stack for state storage and retrieval.

Two primary variants were explored: a Node-wise Stack, where every node in the graph maintains its own stack, and a Graph-level Stack, where a single global stack stores pooled representations of the graph state. Crucially, the authors removed the standard recurrent hidden state ($p^{t-1}$). This architectural constraint forces the network to rely entirely on the stack for long-term memory, preventing it from attempting to compress global information into a fixed-size vector—a strategy that often fails during OOD generalization.

Improved Algorithmic Alignment

The researchers discovered that standard benchmarks like CLRS-30 often provided "hints" that were too informative. For example, providing the predecessor of every node at every step allowed models to solve DFS iteratively rather than recursively. To force true recursive reasoning, they modified the hints to be Graph Hints. These hints provide information only about the current node being explored, mimicking the local scope of a real recursive function call and requiring the model to use its stack to "remember" the traversal path.

Key Results

The study primarily used the CLRS-30 algorithmic reasoning benchmark, focusing on DFS. Models were trained on graphs with up to 32 nodes and tested on graphs with 96 nodes to evaluate OOD performance.

Ablation study showing the impact of different architectural choices on DFS performance. Note the 100% accuracy achieved by the node-wise stack models.

As shown in the results, the proposed stack-augmented models achieved a massive leap in performance:

  • Baseline (Standard GNN): Achieved only ~54% accuracy on 96-node graphs.
  • Node-Wise Stack + Modified Hints: Achieved 100% accuracy on both in-distribution and OOD (96-node) graphs.
  • Efficiency: Beyond accuracy, the stack-augmented models were more memory-efficient, using approximately 7GB of peak GPU memory compared to 22GB for the baseline.

Conclusion

This work demonstrates that augmenting GNNs with a stack and refining how algorithmic trajectories are sampled allows neural networks to learn the underlying logic of recursive computation. By replacing fixed-size hidden states with structured stack memory, the researchers achieved perfect OOD generalization on DFS. Future work may explore using Reinforcement Learning to let networks discover stack operations autonomously and applying these techniques to broader classes of recursive algorithms.

Summary updated

BibTeX
@inproceedings{jur2023recursive,
  author = {Jonas Jürß and Dulhan Hansaja Jayalath and Petar Veličković},
  title = {{Recursive Algorithmic Reasoning}},
  booktitle = {LoG},
  year = {2023},
  url = {https://openreview.net/forum?id=43M1bPorxU},
}
Figure