When Do Transformers Learn Heuristics for Graph Connectivity?

Qilin Ye, Deqing Fu, Robin Jia, Vatsal Sharan
10/22/2025
cs.LG

No confident code implementation found for this paper.

Abstract

Transformers often fail to learn generalizable algorithms, instead relying on brittle heuristics. Using graph connectivity as a testbed, we explain this phenomenon both theoretically and empirically. We consider a simplified Transformer architecture, the disentangled Transformer, and prove that an $L$-layer model has capacity to solve for graphs with diameters up to exactly $3^L$, implementing an algorithm equivalent to computing powers of the adjacency matrix. We analyze the training-dynamics, and show that the learned strategy hinges on whether most training instances are within this model capacity. Within-capacity graphs (diameter $\leq 3^L$) drive the learning of a correct algorithmic solution while beyond-capacity graphs drive the learning of a simple heuristic based on node degrees. Finally, we empirically demonstrate that restricting training data within a model's capacity leads to both standard and disentangled transformers learning the exact algorithm rather than the degree-based heuristic.

View on arXivView PDF

Code Implementations

No confident code match yet

We couldn't find an author-owned or strongly-evidenced community implementation for this paper. Any repos shown below are weak matches — verify before relying on them.

No code implementations found yet.

Know of an implementation? Let us know in the comments below!

Similar Papers

Cite this paper

@article{ye2025when,
  title  = {When Do Transformers Learn Heuristics for Graph Connectivity?},
  author = {Qilin Ye and Deqing Fu and Robin Jia and Vatsal Sharan},
  year   = {2025},
  eprint = {2510.19753},
  archivePrefix = {arXiv},
  url    = {http://arxiv.org/abs/2510.19753v1}
}

Add a badge to your README

Shows whether official code is linked for this paper.

Code of Paper badge
[![Code of Paper](https://codeofpaper.com/badge/2510.19753.svg)](https://codeofpaper.com/paper/2510.19753)

Discussion