Graph Reasoning with Large Language Models via Pseudo-code Prompting

Konstantinos Skianis, Giannis Nikolentzos, Michalis VazirgiannisarXiv 2024 · DOI

Introduction

While Large Language Models (LLMs) have demonstrated remarkable capabilities in natural language processing, their performance on structured reasoning tasks—specifically graph theory—remains a significant bottleneck. Graphs are essential for representing complex relationships in fields ranging from social network analysis to molecular biology. However, when graphs are presented as unstructured text, LLMs frequently struggle with basic operations, such as counting edges or identifying connectivity, often resulting in logical hallucinations or missed steps.

The primary challenge lies in the ambiguity of natural language instructions for algorithmic tasks. While techniques like Chain-of-Thought (CoT) prompting provide some improvement, they often fail to enforce the rigid, step-by-step logic required for graph algorithms.

Methodology: Pseudo-code Prompting

To address these limitations, this research introduces pseudo-code prompting. This technique involves augmenting the model's prompt with a high-level, structured algorithm written in pseudo-code. By providing a clear blueprint for the task, the model is encouraged to adopt a modular reasoning process that aligns with the underlying algorithm.

Workflow of the pseudo-code prompting technique, illustrating how a graph task and pseudo-code are combined into a final prompt to generate a step-by-step execution output.

The approach relies on three key components:

  1. Algorithmic Alignment: The pseudo-code serves as a structured guide, reducing the likelihood of the model skipping critical logical steps.
  2. Prompt Variants: The study evaluated PSEUDO (instructions + code) and PSEUDO+1-SHOT (code + one completed example) configurations.
  3. Graph Encoding: Graphs are represented as edge lists (e.g., [(0,1), (1,2)]), a format previously identified as highly effective for LLM consumption.

The Benchmark Dataset

The authors developed a comprehensive benchmark consisting of 6,600 problems across ten distinct graph reasoning tasks, including node/edge counting, cycle detection, and finding minimum spanning trees (MST). The dataset categorizes problems by graph size: Small (5-11 nodes), Medium (11-21 nodes), and Large (21-51 nodes), utilizing Erdős–Rényi random graph distributions to ensure variety.

Experimental Results

Experiments were conducted using GPT-3.5-Turbo and Mixtral 8x7B. The results indicate that pseudo-code prompting significantly enhances performance, particularly on complex tasks where standard zero-shot or one-shot prompting fails.

Performance comparison across complex graph reasoning tasks (Connected Components, Cycle Check, MST, Shortest Path, Bipartite Check, and Topological Sorting) for Small (S), Medium (M), and Large (L) graphs.

Key findings include:

  • Edge Counting: Accuracy for GPT-3.5 on medium graphs improved from 16% (zero-shot) to 34% with pseudo-code.
  • Shortest Path & MST: On large graphs, the PSEUDO+1-SHOT method enabled GPT-3.5 to achieve 34% accuracy for shortest path tasks and 27% for MST—tasks where baseline models often hovered near 0% accuracy.
  • Model Sensitivity: While pseudo-code generally improved GPT-3.5's performance, Mixtral showed mixed results, occasionally seeing performance drops in specific tasks like cycle detection, suggesting that model architecture influences the effectiveness of algorithmic prompting.

Discussion and Limitations

Despite the improvements, several challenges remain. LLMs still face a "complexity ceiling," struggling with nested loops or recursive logic. Furthermore, performance degrades significantly as graph size exceeds 50 nodes, indicating a "scalability wall" likely tied to the models' limited working memory for large adjacency lists. Finally, the effectiveness of this method depends heavily on the quality of the pseudo-code; poorly designed prompts can increase model confusion rather than resolve it.

Future research should focus on making LLMs more robust to recursive logic and exploring performance across more diverse graph topologies, such as scale-free networks.

Summary updated

BibTeX
@misc{skianis2024graph,
  author = {Konstantinos Skianis and Giannis Nikolentzos and Michalis Vazirgiannis},
  title = {{Graph Reasoning with Large Language Models via Pseudo-code Prompting}},
  year = {2024},
  eprint = {2409.17906},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2409.17906},
}
Figure