Berlin Tech Meetup: The Future of Relational Foundation Models, Systems, and Real-World Applications

Register now:
PyG/Guide8 min read

Graph Rewiring: Modifying Graph Topology to Improve Information Flow

Graph rewiring adds shortcut edges to bypass bottleneck structures, directly addressing the over-squashing problem. It is a preprocessing step that modifies the computation graph while preserving the original structure as features.

PyTorch Geometric

TL;DR

  • 1Graph rewiring adds or removes edges to improve GNN message passing. It creates shortcuts past bottleneck edges, reducing effective graph diameter and mitigating over-squashing.
  • 2Three methods: spectral rewiring (increase spectral gap), curvature-based (fix negative-curvature bottlenecks), and random shortcuts (add random long-range edges). All reduce over-squashing.
  • 3The rewired graph is for computation only. Original graph structure is preserved as edge features or positional encodings. Rewired edges are computational shortcuts, not real relationships.
  • 4For enterprise data: rewiring bypasses bottleneck structures like rare merchants connecting customer communities or single warehouses linking supply chains.
  • 5Graph transformers are the ultimate rewiring: they implicitly add edges between ALL node pairs via global attention. Rewiring is the preprocessing analog of what transformers do architecturally.

Graph rewiring is a preprocessing technique that modifies the graph's edge structure to improve information flow during GNN message passing, primarily by adding shortcut edges that bypass bottleneck structures causing over-squashing. The rewired graph determines which nodes exchange messages during GNN computation. The original graph structure can be preserved as edge features or structural encodings. This separation of “computation graph” from “data graph” lets the GNN optimize information flow without losing structural semantics.

Why it matters for enterprise data

Enterprise relational graphs frequently contain topological bottlenecks:

  • Rare merchants: A merchant visited by only 2 customers from different communities. All information between those communities flows through one merchant node.
  • Single warehouses: A supply chain where one warehouse connects all suppliers to all retailers. All supply-demand signals flow through that bottleneck.
  • Bridge accounts: In fraud detection, a legitimate account that transacts with both a fraud ring and normal accounts. Fraud signals must pass through this narrow bridge.

Rewiring adds alternative paths for these signals, letting the GNN detect patterns that would be squashed in the original topology.

Three rewiring approaches

Spectral rewiring

Add edges that maximize the spectral gap (second-smallest eigenvalue of the graph Laplacian). A larger spectral gap means better connectivity and faster information propagation. The spectral gap is the algebraic measure of how well-connected the graph is.

Curvature-based rewiring (SDRF)

Compute the Ricci curvature of each edge. Edges with negative curvature are bottlenecks. Add shortcut edges near the most negative-curvature edges to provide alternative paths. This directly targets the topological source of over-squashing.

Random shortcuts

Add random edges between nodes that are far apart in the graph. Simple but effective: even random shortcuts reduce effective diameter and mitigate over-squashing. The GNN learns to use or ignore these edges as needed.

graph_rewiring.py
import torch
from torch_geometric.utils import to_dense_adj, dense_to_sparse

def add_random_shortcuts(edge_index, num_nodes, num_shortcuts=1000):
    """Add random long-range edges to reduce graph diameter."""
    # Sample random node pairs
    src = torch.randint(0, num_nodes, (num_shortcuts,))
    dst = torch.randint(0, num_nodes, (num_shortcuts,))
    # Remove self-loops
    mask = src != dst
    new_edges = torch.stack([src[mask], dst[mask]])
    # Add to existing edges
    return torch.cat([edge_index, new_edges], dim=1)

# Usage
data.edge_index_rewired = add_random_shortcuts(
    data.edge_index, data.num_nodes, num_shortcuts=data.num_edges // 10
)

# Train GNN on rewired graph
# Use original edge_index for edge features/structure
# Use edge_index_rewired for message passing
out = model(data.x, data.edge_index_rewired)

Random shortcuts: add 10% additional random edges. Simple but effective. The GNN learns to use helpful shortcuts and ignore noise.

Concrete example: cross-community fraud detection

A payment network has two customer communities connected by a single merchant:

  • Community A: 10,000 accounts, dense internal transactions
  • Community B: 8,000 accounts, dense internal transactions
  • Bridge: Merchant M, connected to 5 accounts in A and 3 accounts in B

A fraud ring spans both communities. Without rewiring, the fraud signal from Community A must flow through the 8 edges to Merchant M to reach Community B. The signal is squashed at this bottleneck.

After adding 50 random shortcut edges between Communities A and B, the fraud signal has multiple paths to flow. The GNN can now detect the cross-community ring pattern. Fraud detection AUROC improves from 71.2 to 76.8 on this graph.

Limitations and what comes next

  1. Memory and computation: Each added edge increases memory and computation for message passing. Adding too many edges (dense rewiring) approaches the cost of a full graph transformer without the benefits of learned attention.
  2. Which edges to add: Random shortcuts are simple but imprecise. Spectral and curvature-based methods are targeted but expensive to compute. Finding the right balance is task-dependent.
  3. Semantic mismatch: Rewired edges have no semantic meaning. The GNN must learn to distinguish real edges (with semantic content) from shortcut edges (for computation only). Using edge type features helps.

Frequently asked questions

What is graph rewiring?

Graph rewiring is the preprocessing technique of adding or removing edges in the graph before running GNN message passing. The goal is to improve information flow by creating shortcuts past bottleneck edges, reducing the effective graph diameter, and ensuring that every node can efficiently receive information from relevant parts of the graph. The modified graph is used only for GNN computation; the original graph structure is preserved as features.

Why is graph rewiring needed?

Standard message passing suffers from over-squashing: information from distant nodes gets compressed into fixed-size vectors as it travels through bottleneck edges. Graph rewiring adds shortcut edges that bypass these bottlenecks, allowing distant information to flow more directly. It also helps with over-smoothing by modifying the diffusion dynamics of the graph.

What are the main rewiring methods?

Three main approaches: (1) Spectral rewiring: add edges to increase the spectral gap (second-smallest Laplacian eigenvalue), improving connectivity. (2) Curvature-based rewiring (SDRF): add edges where Ricci curvature is most negative (bottleneck edges). (3) Random shortcut edges: add random edges between distant nodes. All reduce effective diameter and mitigate over-squashing.

Does rewiring change the graph's meaning?

The rewired graph is used only for GNN computation (defining which nodes exchange messages). The original graph structure can be preserved as edge features or positional encodings. Think of rewired edges as 'computational shortcuts' rather than real relationships. The GNN learns to use these shortcuts for information flow while the original structure provides semantic meaning.

How does graph rewiring apply to enterprise relational data?

Enterprise relational graphs often have bottleneck structures. A rare merchant connecting two customer communities creates a narrow bridge. A single warehouse connecting supply and demand chains is a bottleneck. Rewiring adds shortcuts across these bottlenecks, letting fraud signals and demand signals flow more efficiently through the GNN.

Learn more about graph ML

PyTorch Geometric is the open-source foundation for graph neural networks. Explore more layers, concepts, and production patterns.