GCoder: Improving Large Language Model for Generalized Graph Problem Solving

Qifan Zhang, Xiaobin Hong, Jianheng Tang, Nuo Chen, Yuhan Li, Wenzhong Li, Jing Tang, Jia LiarXiv 2024 · DOI

Abstract

Large Language Models (LLMs) often struggle with complex graph computational tasks due to unverifiable logic and context window constraints. We present GCoder, a framework that shifts graph problem-solving from error-prone natural language reasoning to a deterministic "code paradigm." By fine-tuning on the massive GraphWild dataset and employing Reinforcement Learning from Compiler Feedback (RLCF), GCoder enables LLMs to generate executable Python code for graph analysis. Our approach outperforms GPT-4o by over 16% and successfully processes graphs with millions of nodes—far exceeding traditional context limits—by decoupling reasoning from computation.

Introduction

While LLMs have demonstrated impressive reasoning capabilities, they frequently stumble when faced with graph computational problems, such as finding the shortest path or detecting cycles. Traditionally, researchers have relied on "Chain-of-Thought" (CoT) prompting, where the model explains its reasoning step-by-step. However, this approach suffers from three major flaws: unverifiable logic (hallucinations), long-term reasoning limits as graphs scale, and strict context window constraints that prevent the processing of large datasets.

GCoder addresses these challenges by fundamentally changing how LLMs interact with graph data. Instead of attempting to "be" the computer, the LLM is trained to "write" for the computer, generating deterministic Python scripts to solve the task at hand.

Comparison between the traditional reasoning steps paradigm and the proposed GCoder code paradigm.

Methodology

The Code Paradigm and Multi-stage Training

GCoder moves away from natural language descriptions toward executable Python code, primarily utilizing the NetworkX library. The training pipeline consists of two primary stages:

  1. Supervised Fine-Tuning (SFT): The model is trained on GraphWild, a new dataset featuring 45,236 cleaned problem-code pairs covering over 300 graph algorithms.
  2. Reinforcement Learning from Compiler Feedback (RLCF): This novel approach uses a Python compiler as an objective judge. If generated code is runnable and produces the correct answer, it is treated as a preferred sample, bypassing the subjectivity and cost of human labeling.

Handling Scale and Novelty

To solve the "Context Wall," GCoder generates code that reads graph data from external files (e.g., .txt) rather than embedding the entire graph structure within the prompt. For tasks not seen during training (Out-of-Domain), GCoder employs a Hybrid RAG (Retrieval-Augmented Generation) system. This system uses vector similarity and keyword searches to find relevant code templates from a library, which are then injected into the prompt to guide the model.

Detailed architecture of GCoder, illustrating the fine-tuning stages (SFT and RLCF) and the inference pipeline including RAG.

Key Insights and Results

Performance and Scalability

GCoder (based on Llama 3.1-8B and Qwen 2.5-Coder) achieved an average accuracy of 94.39%, significantly outperforming GPT-4o (81.08%). Its ability to decouple reasoning from computation allows it to scale to massive datasets. In a case study on the Web-Wikipedia2009 graph, GCoder successfully solved problems on a graph with 1.9 million nodes and 4.5 million edges—a feat impossible for standard reasoning-based LLMs.

Robustness through RLCF and RAG

The use of RLCF ensures the model produces logically sound code rather than just "code-like" text. Furthermore, the RAG component proved vital for robustness; performance on unseen tasks like Maximum Clique jumped from near 0% in zero-shot settings to 100% accuracy when relevant documentation was provided.

Conclusion

GCoder demonstrates that offloading heavy computational lifting to a Python interpreter allows LLMs to focus on their strength: understanding intent and data structure. While the system currently depends on a secure execution sandbox and the breadth of its algorithm library, it represents a significant step toward generalized, scalable graph problem-solving in AI. Future work will focus on refining RAG relevance filtering to prevent performance degradation from incompatible retrieved snippets.

Summary updated

BibTeX
@misc{zhang2024gcoder,
  author = {Qifan Zhang and Xiaobin Hong and Jianheng Tang and Nuo Chen and Yuhan Li and Wenzhong Li and Jing Tang and Jia Li},
  title = {{GCoder: Improving Large Language Model for Generalized Graph Problem Solving}},
  year = {2024},
  eprint = {2410.19084},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2410.19084},
}
Figure