Shortest-Path Encodings for Graph Transformers

6 minute read

Published:

TL;DR: For Graph Transformers, the shortest-path distance \(\mathrm{dist}(i,j)\) between every node pair can be added to the attention logits as a learned scalar bias. This injects the graph's metric structure directly into attention without any message passing — at the price of computing and storing all-pairs distances, which is \(O(N(N+\lvert E\rvert))\) time and \(O(N^2)\) memory.

Intuition First

In a standard Transformer, every token can attend to every other token — but the attention score is purely based on content similarity. For graphs, two distant nodes might have very similar features yet share no direct structural relationship. SPD encoding adds a “distance penalty” to attention: nodes far apart in the graph should attend less strongly, regardless of feature similarity.

Think of it loosely like gravity — attraction weakens with distance. But the analogy only goes so far: gravity decays as a fixed \(1/d^2\), whereas here the model learns one free scalar per distance value and is under no obligation to make the profile decreasing at all.

SPD as attention bias: closer nodes attend more strongly v dist=1, φ(1)=+1.2 dist=2, φ=+0.4 dist=3, φ=−0.1 dist=4, φ=−0.6 Learned φ_SPD values (one scalar per distance bucket) bias attention scores
Graphormer's spatial encoding: attention from node \(v\) to its 1-, 2-, 3- and 4-hop neighbours. The bias \(\varphi(d)\) is one learned scalar per distance. The decreasing profile shown here is what typically emerges from training, not something the architecture imposes — the values are free parameters.

Why Shortest Paths?

In Graphormer (Ying et al., 2021), every pair of nodes can attend to each other. But without structural information, the model has no way to know that nodes 1 hop apart should interact more strongly than nodes 10 hops apart.

Shortest-path distance (SPD) encoding injects this directly as an attention bias:

\[ A_{ij} = \operatorname*{softmax}_{j}\!\left(\frac{q_i^{\top} k_j}{\sqrt{d}} + \varphi\big(\mathrm{dist}(i,j)\big)\right). \]

\(\varphi\) is a lookup table of learned scalars, one per distance value \(0, 1, 2, \dots, d_{\max}\), plus one for “no path”. In Graphormer it is learned separately per attention head, so different heads can specialise to different ranges.

What It Captures

  • Self (\(\mathrm{dist} = 0\)): its own entry
  • Immediate neighbours (\(\mathrm{dist} = 1\)): typically the largest bias
  • 2-hop neighbours (\(\mathrm{dist} = 2\)): moderate
  • Distant nodes: small or negative
  • Disconnected (\(\mathrm{dist} = \infty\)): a dedicated “no path” entry

Two things are worth being precise about. First, nothing constrains \(\varphi\) to decrease with distance — it is a free table of scalars, and the model may well learn a non-monotone profile if the task rewards it. Second, it is a bias on the logits, not a mask: a large content match \(q_i^{\top}k_j\) can still outweigh a strongly negative \(\varphi\), so distant nodes remain reachable. That is precisely what distinguishes it from a hard \(k\)-hop restriction.

All-Pairs Shortest Paths: Computation Cost

On an unweighted graph, BFS from every node gives all-pairs distances in \(O(N(N + \lvert E\rvert))\) time. The output is an \(N \times N\) integer matrix, so memory is \(\Theta(N^2)\) regardless of how sparse the graph is — and in a Transformer that matrix must sit alongside the attention matrix, which is also \(N \times N\).

For small graphs (\(N < 1000\)) this is cheap and computed once per graph as a preprocessing step. At \(N > 10^5\) the memory alone rules it out, well before the time cost does. This is why SPD encoding is used mainly for molecules (\(N\) in the tens) and small protein or structure graphs.

SPD vs LapPE vs RWPE

EncodingTypeCapturesCost
LapPENode PELow-frequency global position\(O(k\lvert E\rvert T)\), \(T\) gap-dependent
RWPENode PELocal closed-walk structure\(O(K N \lvert E\rvert)\) time, \(O(N^2)\) memory
SPDPairwise biasExact graph metric between every pair\(O(N(N+\lvert E\rvert))\) time, \(O(N^2)\) memory

SPD is a pairwise encoding — a property of a pair, not of a node. It cannot be concatenated to node features; it has to enter through the attention mechanism as a bias. That is a genuine architectural constraint: SPD is unavailable to a plain message-passing GNN, which has no pairwise scoring step to attach it to.

Beyond SPD: Distance Encoding and Anchor Sets

Two related ideas turn pairwise distances into something node-level, and they are often conflated.

Distance Encoding (Li et al., 2020) measures distance from every node to the target node set of the prediction — the node or node pair being classified. For link prediction on \((u,v)\), each node \(w\) is featurised by its distances to \(u\) and to \(v\). The encoding is therefore task-relative, recomputed per query, and this is exactly what gives it provable power beyond 1-WL: it breaks the symmetry between nodes that are structurally equivalent but differently placed relative to the target.

Anchor sets (P-GNN, You et al., 2019) take the other route: sample random anchor sets \(S_1, \dots, S_k\) once, and featurise each node by its distance to each anchor:

\[ p_v = \big[\,\mathrm{dist}(v, S_1),\; \mathrm{dist}(v, S_2),\; \dots,\; \mathrm{dist}(v, S_k)\,\big]. \]

This is a fixed node-level encoding, cheaper than full APSP, and it is genuinely positional — it depends on the random anchor draw, so it is not a function of the graph alone and two runs give different encodings.

The distinction matters: DE is task-relative and permutation-equivariant; anchor distances are position-like but carry an arbitrary random choice, much as Laplacian eigenvectors carry an arbitrary sign.

Key Insight: SPD is the only encoding here that injects pairwise metric information directly. LapPE and RWPE are node-level: each node gets a vector, and the model must infer relationships between nodes from those vectors. SPD hands the relationship over explicitly. That is more informative about distance specifically — but it is not uniformly stronger, since it says nothing about a node's own local structure, which is exactly what RWPE supplies. The three are complementary, not ranked. The \(O(N^2)\) memory is what confines SPD to molecule-scale graphs.

Summary

SPD encoding injects graph metric structure straight into Graph Transformer attention. It is simple, interpretable, and effective on small graphs. Its ceiling is memory, not cleverness: the \(N \times N\) distance matrix scales with the attention matrix, so SPD is viable exactly where full attention already is. For larger graphs, use RWPE or LapPE, which encode structure per node rather than per pair — accepting that neither gives the model an exact distance between two specified nodes.

References