Abstract
Large Language Models (LLMs) frequently struggle with symbolic and algorithmic reasoning in graph-based tasks. This paper presents a framework for Structured Context Injection, a model-agnostic method that enhances graph reasoning by mapping abstract data to narrative structures. By leveraging "real-world" entity mapping (e.g., Game of Thrones characters) and a multi-model context generation pipeline, the proposed method allows lightweight models to achieve state-of-the-art performance on complex graph problems without fine-tuning.
Introduction
While LLMs demonstrate remarkable capabilities in natural language processing, their performance degrades significantly when faced with abstract graph problems, such as determining connectivity or finding shortest paths. This "grounding gap" stems from the models' difficulty in reasoning over symbolic numeric identifiers (e.g., Node 0, Node 1). Current solutions, such as extensive fine-tuning or multi-step querying, are often computationally expensive or model-specific.
This research addresses these challenges by introducing a framework that aligns graph structures with grounded conceptual representations, enabling LLMs to "see" the graph through familiar narrative lenses.
Methodology: Structured Context Injection
The proposed framework consists of three primary components designed to be cost-efficient and model-agnostic:
- Task-Specific Context: Instead of generic prompts, the framework embeds specific constraints and graph-aware cues directly into the input. This context is defined once at the task level, minimizing per-query overhead.
- Narrative-Based Entity Mapping: The core innovation involves mapping numeric node IDs to characters from well-known narratives, such as Game of Thrones (GOT). This allows the model to utilize its pre-trained semantic knowledge of relationships (alliances, rivalries) to interpret graph properties like edge weights.
- Approximate Subgraph Matching: To maintain mathematical integrity, specialized algorithms map the original numeric graph to a character-based subgraph, ensuring that structural properties like connectivity are preserved within the narrative.
The framework employs a "committee" approach for context generation, as illustrated in the following diagram.
In this pipeline, multiple models (GPT-4, LLaMa3, and Gemini) contribute to context generation. These outputs are consolidated by GPT-4 into a single, high-quality structured input for the final downstream graph task.
Experimental Setup
The framework was evaluated using the NLGraphs benchmark (easy split) across four fundamental tasks: Connectivity, Cycle Detection, Topological Sort, and Shortest Path. Testing was conducted on Meta-LLaMa-3-8B-Instruct and Gemini 1.5 Flash to demonstrate the framework's effectiveness on both lightweight open-source and fast proprietary models.
Key Results and Discussion
The "GOT Subgraph" method consistently outperformed all baselines, including standard instruction-based prompting and Chain-of-Thought (CoT) techniques.
As shown in the performance table above, the GOT Subgraph method achieved significant accuracy gains:
- Connectivity: Reached 80.68% on LLaMa 3 8B and 95.74% on Gemini 1.5 Flash, a 6–7% improvement over the best baselines.
- Shortest Path: On Gemini 1.5 Flash, path prediction accuracy reached 90.00%, significantly higher than zero-shot or few-shot approaches.
- Efficiency: The results demonstrate that structured prompting can bridge the performance gap between small and large models. A well-prompted 8B model can rival much larger models using generic prompts.
Conclusion
Structured Context Injection provides a scalable, efficient path for improving LLM graph reasoning. By transforming abstract math into social network problems, models can leverage internal semantic maps for more accurate navigation. Future work will explore automating narrative template selection and testing the framework on larger, denser graphs where narrative construction becomes more complex.
Summary updated