GRAIL: Graph Edit Distance and Node Alignment using LLM-Generated Code

Samidha Verma, Arushi Goyal, Ananya Mathur, Ankit Anand, Sayan Ranu

Abstract

Computing Graph Edit Distance (GED) is a fundamental but NP-hard problem in graph analysis. Traditional neural network approximations require vast amounts of labeled data and operate as uninterpretable black boxes. This paper introduces GRAIL, which leverages Large Language Models (LLMs) to generate symbolic Python programs for computing GED. By optimizing these programs to minimize the upper bound of the edit distance, GRAIL achieves state-of-the-art accuracy without requiring any ground-truth training data. GRAIL demonstrates superior generalization across diverse graph domains and provides interpretable alignment logic.

Introduction: The Challenge of Graph Similarity

Graph Edit Distance (GED) is the standard metric for quantifying the similarity between two graphs. It is defined as the minimum cost of a sequence of edit operations—node/edge insertions, deletions, or substitutions—required to transform one graph into another.

An illustration of Graph Edit Distance (GED) calculation through node substitution and insertion operations.

Despite its utility, exact GED computation is computationally prohibitive for large graphs. Existing Graph Neural Network (GNN) approaches attempt to approximate GED but face significant hurdles: they require weeks of compute time to generate ground-truth labels for training, they lack interpretability, and they often fail to generalize to new graph domains.

The GRAIL Framework

GRAIL (Graph Edit Distance and Node Alignment using LLM-Generated Code) addresses these limitations by shifting the focus from predictive modeling to algorithmic discovery. Instead of training a model to output a distance value, GRAIL prompts an LLM to write Python functions that solve the underlying node alignment problem.

Bipartite Matching and Upper Bounds

GRAIL frames GED as a maximum weight bipartite matching problem. The LLM generates code to assign similarity weights between nodes in two graphs. A key insight of this work is that any valid node mapping provides an upper bound on the true GED. By iteratively refining programs to minimize this upper bound, GRAIL can be "trained" in an entirely unsupervised manner, bypassing the need for expensive ground-truth labels.

To find optimal algorithms, GRAIL employs a self-evolutionary prompt tuning strategy. Inspired by evolutionary algorithms, it maintains a pool of generated programs and uses Gemini 1.5 Pro to mutate and improve the most effective ones. To ensure robustness, GRAIL uses submodular optimization to select a diverse "committee" of programs (typically around 15) that collectively handle various graph structures, taking the minimum GED found across the ensemble.

Experimental Results

The authors evaluated GRAIL across seven diverse datasets, including chemical molecules (AIDS), software dependence graphs (Linux), and protein-protein interactions (ogbg-ppa).

  • Superior Accuracy: GRAIL achieved the top average rank across benchmarks, outperforming leading neural methods like GREED and GEDGNN in both Root Mean Square Error (RMSE) and Exact Match Ratio (EMR).
  • Zero-Shot Generalization: On the massive ogbg-ppa dataset—where generating training data for neural models is impossible—GRAIL successfully provided accurate approximations.
  • Interpretability: Unlike black-box GNNs, GRAIL produces human-readable code. For example, in social network tasks, the generated code explicitly utilized node degrees and neighbor connectivity, providing clear insights into the matching logic.

Conclusion and Future Directions

GRAIL represents a significant step toward interpretable and data-efficient graph analysis. By combining the symbolic reasoning of LLMs with the mathematical rigor of bipartite matching, it eliminates the data bottleneck of neural GED solvers. While current inference speeds are limited by Python execution on CPUs, future work focusing on code compilation to C or Rust could make GRAIL a highly competitive tool for real-time applications in drug discovery and program analysis.

Summary updated

BibTeX
@inproceedings{verma2025grail,
  author = {Samidha Verma and Arushi Goyal and Ananya Mathur and Ankit Anand and Sayan Ranu},
  title = {{GRAIL: Graph Edit Distance and Node Alignment using LLM-Generated Code}},
  booktitle = {International Conference on Machine Learning},
  year = {2025},
  url = {https://openreview.net/forum?id=NzoZXju2bL},
}
Figure