English

Transformers Provably Learn Algorithmic Solutions for Graph Connectivity, But Only with the Right Data

Machine Learning 2026-02-19 v2

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 LL-layer model can compute connectivity in graphs with diameters up to 3L3^L, implementing an algorithm equivalent to computing powers of the adjacency matrix. By analyzing training dynamics, we prove that whether the model learns this strategy hinges on whether most training instances are within this model capacity. Within-capacity graphs (diameter 3L\leq 3^L) drive the learning of the algorithmic solution while beyond-capacity graphs drive the learning of a simple heuristic based on node degrees. Finally, we empirically show that restricting training data to stay within a model's capacity makes both standard and Disentangled Transformers learn the exact algorithm.

Keywords

Cite

@article{arxiv.2510.19753,
  title  = {Transformers Provably Learn Algorithmic Solutions for Graph Connectivity, But Only with the Right Data},
  author = {Qilin Ye and Deqing Fu and Robin Jia and Vatsal Sharan},
  journal= {arXiv preprint arXiv:2510.19753},
  year   = {2026}
}
R2 v1 2026-07-01T07:00:07.440Z