Introduction
Graph reasoning—solving problems like finding the shortest path or the optimal traveling salesman route—is a cornerstone of computer science. Recently, researchers have attempted to solve these problems by feeding graph descriptions directly to Large Language Models (LLMs) in natural language. However, this approach suffers from two fatal flaws: poor accuracy due to the LLM's struggle with spatial relationships and prohibitive costs caused by massive token counts in prompts.
The PIE Framework
To overcome these hurdles, we introduce PIE (Pseudocode-Injection-Enhanced LLM Reasoning). PIE shifts the LLM's role from a "graph solver" to a "code generator." Instead of asking the model to compute an answer autoregressively, PIE asks it to write a Python program that solves the problem.
The framework operates in three distinct steps:
- Problem Understanding: The LLM is given a task description but not the specific graph data, which is handled separately by a Python interpreter.
- Pseudocode Injection: To prevent the LLM from defaulting to inefficient brute-force solutions, PIE injects standardized pseudocode of research-backed algorithms into the prompt. The LLM acts as a translator, turning this pseudocode into executable Python code.
- Code Generation & Execution: The generated code is tested on small "toy" graphs. If it fails, the error message is fed back to the LLM for self-debugging. Once validated, the code is locked in and reused for all large-scale graphs.
Experimental Evaluation
PIE was evaluated across nine representative graph tasks, including polynomial-time problems (e.g., Connected Components, Shortest Path) and NP-complete problems (e.g., Maximum Independent Set, Traveling Salesman Problem). Using the GraphArena benchmark, the authors tested the framework on 500 small and 500 large graphs per task using models like Llama3-8B and Llama3-70B.
Key Results
The results demonstrate that decoupling reasoning from execution provides massive benefits in both performance and efficiency.
- Superior Accuracy: PIE achieved 100% accuracy on all polynomial-time tasks. For NP-complete problems, it improved accuracy by an average of ~60 percentage points compared to baselines like GraphArena and NLGraph.
- Guaranteed Feasibility: Unlike traditional LLM solvers that often output nonsensical routes, PIE guarantees mathematically feasible solutions because it relies on deterministic code execution.
- Drastic Cost Reduction: Because the LLM is only called during the initial code-generation phase, the number of calls per task dropped from 1,000 to an average of just 4 to 10. Furthermore, removing serialized graph data from prompts reduced the time cost of individual LLM calls by up to 72.8%.
Conclusion
By letting the LLM write the "recipe" and letting a Python interpreter "cook," PIE leverages the strengths of both tools. This "write once, run anywhere" paradigm effectively solves the scalability and accuracy issues that have long plagued LLM-based graph reasoning. Future work may explore automating the retrieval of optimal pseudocode from academic literature to further generalize the framework.
Summary updated