Graph Neural Networks (GNNs) are the go-to tool for processing data structured as networks—think social media connections, protein structures, or navigation maps. Usually, we train them as "black boxes": we give them a graph and a target answer, and they learn a direct mapping. But what if we taught them the logic of the classical algorithms we already know and trust?
In this ICLR 2020 paper, researchers from DeepMind, Stanford, and Cambridge propose Neural Graph Algorithm Execution. Instead of just predicting a final result, they train GNNs to imitate the step-by-step execution of classical algorithms like Breadth-First Search (BFS) and Bellman-Ford.
The Challenge: Combinatorial Generalization
Standard GNNs often struggle with combinatorial generalization. A model trained to find the shortest path on a graph with 20 nodes might completely fall apart when faced with a graph of 100 nodes.
Classical algorithms (like those taught in Intro to CS) don't have this problem; the logic of the Bellman-Ford algorithm is the same whether the graph has 5 nodes or 5,000. The challenge is that these classical algorithms are "hard-coded" and discrete. The authors wanted to see if a neural network could capture that algorithmic "recipe" in a continuous, learnable way. By supervising the model on intermediate steps rather than just the final answer, they provide the network with a roadmap for how to solve the problem.
Methods: The Encode-Process-Decode Framework
The researchers used an Encode-Process-Decode framework to bridge the gap between continuous neural representations and discrete algorithmic logic:
- Encoder: Maps node/edge features and previous hidden states into a latent space.
- Processor: A shared GNN (the "engine") that performs message passing. Crucially, this processor is algorithm-agnostic—it doesn't know if it's doing BFS or Prim's.
- Decoder: Maps the latent states back to algorithm-specific outputs (like "is this node reachable?" or "what is the distance?").
- Termination Network: A small component that predicts when the algorithm should stop.
To train this system, the authors compared several GNN architectures, including Graph Attention Networks (GATs) and Message Passing Neural Networks (MPNNs) with different aggregation functions (mean, sum, and max). The models were trained on tiny graphs (only 20 nodes) and then tested on much larger graphs (up to 1500 nodes) to measure how well the learned "logic" scales.
Key Insights and Ideas
- The "Max" Factor: The authors hypothesized—and proved—that max-pooling (MPNN-max) is superior for algorithmic tasks. Many graph algorithms rely on discrete, extreme-value decisions (e.g., "pick the shortest edge" or "is any neighbor reachable?"). Max-pooling naturally aligns with this "OR" or "MIN/MAX" logic.
- Positive Transfer: This is the "aha" moment. They found that training a model on BFS (reachability) and Bellman-Ford (shortest paths) at the same time actually improved performance on the shortest path task. The model learned a shared "subroutine" for exploring neighborhoods that benefited both tasks.
- Supervision on Steps Matters: Training on the final solution alone (the "no-algo" baseline) led to much poorer generalization on large graphs compared to training on every intermediate step of the algorithm.
Key Results
- Near-Perfect Scaling: The MPNN-max model achieved nearly 100% accuracy on reachability and shortest-path tasks for graphs of 20, 50, and 100 nodes, even though it only saw 20-node graphs during training.
- Extreme Generalization: When pushed to graphs 75x larger (1500 nodes) than the training set, the MPNN-max model maintained a respectable ~75% step accuracy, while other architectures (like GAT or LSTM) failed significantly earlier.
- Sequential Success: The model also successfully learned Prim’s algorithm, a sequential process for finding Minimum Spanning Trees, showing that the framework isn't limited to parallelizable algorithms.
Limitations and Future Work
While groundbreaking, the study focused on relatively simple, polynomial-time classical algorithms. It remains to be seen how well this scales to NP-hard problems or more complex heuristics. Additionally, the termination network is still a bit of a weak point; if it fails to stop, the model is manually terminated after a set number of steps.
A promising next direction is "Program Synthesis"—learning to combine these learned neural "subroutines" to solve entirely new, complex problems without needing step-by-step supervision for the new task.
Summary updated