Graphs as ML Data Structures
Adjacency formats, task types, permutation invariance, homophily, inductive vs transductive
A fraud detection system has 50 million users and 5 billion transactions. A user who received money from 5 confirmed fraud accounts last week is almost certainly a fraud risk. But your ML model takes a feature vector per user — age, account balance, transaction volume. None of those features capture "connected to known fraudsters." The signal is real, it is strong, and it is invisible unless you model the graph structure. This is why graph ML exists.
Graphs appear wherever relationships between entities carry information: molecular property prediction (atoms as nodes, bonds as edges), citation networks (papers as nodes, citations as edges), knowledge graphs (entities as nodes, relations as edges), social recommendations. In each case, the graph structure encodes relational signals that a per-node feature vector cannot represent. Graph ML extracts that signal.
Before any model runs, you need the right data structure. A 50M-node social graph stored as a dense adjacency matrix requires 50M × 50M entries at 1 bit each: 312 terabytes. Stored as a CSR (Compressed Sparse Row) sparse matrix with only the 5B actual edges, it requires about 40 gigabytes. This is not a detail — it is the difference between a system that is buildable and one that is not.
Beyond data structures, graph tasks split into three types. Node-level tasks (fraud detection, protein function prediction) require a prediction per node, using each node's final embedding directly. Edge-level tasks (link prediction, drug-target interaction) require a prediction per edge, typically from a decoder applied to the two endpoint embeddings. Graph-level tasks (molecular property prediction) require one prediction for the entire graph, using a readout function that aggregates all node embeddings into a fixed-size vector.
NOT this. "Graphs are just for network analysis." Graphs appear wherever entities have relationships that carry information: molecular property prediction where the graph is a molecule, recommendation systems where the graph connects users to items, knowledge graphs that power QA systems, traffic routing where roads are edges. Any problem with entities and relations between them is potentially a graph problem. The question is whether the relational structure contains signal that a per-entity feature vector would miss — and in most domains, it does.
Key points
- Dense adjacency matrix A ∈ {0,1}^{NxN}: stores all N² entries regardless of edge count. O(N²) memory. Efficient only for dense graphs (|E| ≈ N²). For a social graph with N=50M nodes: 50M×50M = 2.5×10^{15} entries, ~312 TB even at 1 bit per entry. GCN on full adjacency requires O(N²) memory even at inference. Never use for large sparse graphs.
- CSR (Compressed Sparse Row): stores only nonzero entries in three arrays — values (edge weights), col_indices (column of each entry), row_ptr (start of each row in col_indices). Memory O(|E| + |V|). For a 50M-node graph with 5B edges: ~40 GB. SpMM (sparse × dense matrix multiply) is the core GNN operation — PyTorch Geometric and DGL both build on this. Message passing on CSR is naturally parallel across edges, which maps directly to GPU execution.
- Node-level, edge-level, and graph-level tasks require fundamentally different output structures. Node-level (fraud detection, protein function): each node gets a prediction, using the node's final embedding directly. Edge-level (link prediction, knowledge graph completion): each edge gets a prediction, typically from a decoder applied to the two endpoint embeddings. Graph-level (molecular property, circuit quality): the entire graph gets a prediction, requiring a readout function that aggregates all node embeddings into a fixed-size vector.
- Standard NNs cannot be applied to graphs for two reasons: variable input size (graphs differ in |V| and |E|), and no canonical node ordering — the same node appears at index 3 in one ordering and index 87 in another. Permutation invariance requires f(PAP^T, Px) = f(A, x) for any permutation matrix P. An MLP applied to a flattened adjacency matrix is not permutation invariant — different orderings of the same graph produce different outputs.
- Graph signal processing gives structural intuition: node features are signals on the graph. The graph Laplacian L = D - A captures discrete gradient structure. Multiplying by L computes the difference between each node's feature and its neighbors' mean — a high-pass filter that amplifies differences. GCN aggregation with self-loops is a low-pass filter that smooths features across edges. Stacking too many GCN layers over-smooths features to the point where all nodes become indistinguishable; residual/skip connections (carrying each layer's input forward) or simply keeping depth shallow (2–4 layers) are the standard fixes.
- Homophily vs heterophily determines whether mean aggregation helps or hurts. In homophilic graphs (social networks — connected nodes have similar properties), standard GNNs that average neighbor features work well because neighbors have informative features. In heterophilic graphs (fraud ring members connect to victims, bipartite recommendation graphs), mean aggregation destroys the discriminative signal because the fraudster's neighbors are predominantly legitimate. H2GCN addresses this by keeping the ego node's embedding separate from aggregated neighbor features, and by aggregating 1-hop and 2-hop neighborhoods separately and concatenating them rather than mixing all distances together — heterophilic signal often shows up more strongly at 2 hops than at 1.
- Inductive vs transductive is an architectural commitment with production consequences. Transductive GNNs (spectral methods, vanilla GCN) train and test on the same fixed graph — they cannot generate embeddings for unseen nodes without retraining. In production, new users and items arrive continuously. GraphSAGE, GAT, and spatial methods learn aggregation functions that apply to any neighborhood, making them inductive by design. Anything requiring full graph retraining to embed new nodes is not deployable.
- Heterogeneous graphs are the production default. Multiple node types (user, item, category) and edge types (click, purchase, co-viewed) are the norm in e-commerce and knowledge graphs. Homogeneous GNNs that ignore type information discard the relational semantics that distinguish a click from a purchase — which are often the most commercially important signals. Modeling heterogeneity is not an advanced feature; ignoring it is a lossy baseline.
The constraint that makes GNNs fundamentally different from every other neural network is permutation invariance — the same graph admits N! adjacency matrix representations, so any valid GNN must aggregate neighbor features with a permutation-invariant function (sum, mean, max). Everything else in GNN design follows from this constraint. In production, the adjacency matrix format is not a detail: for a 50M-node social graph, the choice between dense (312 TB) and CSR (~40 GB) determines whether the system is buildable at all.
Recap
- Graph ML exists for relational signal: "connected to known fraudsters" is invisible to a per-node feature vector.
- Dense adjacency is O(N²): 50M nodes → ~312 TB. CSR stores only edges → ~40 GB. Format decides buildability.
- Three task types: node-level (per-node embedding), edge-level (decoder on 2 endpoints), graph-level (readout aggregates all nodes).
- Permutation invariance is the defining constraint: N! orderings → aggregate with sum/mean/max; MLP on flattened $A$ is not invariant.
- Homophily → mean aggregation helps; heterophily → it destroys signal (fraud rings connect to victims).
- Inductive vs transductive is a deployment commitment: vanilla GCN can't embed unseen nodes; GraphSAGE/GAT can.
- Heterogeneous graphs are the production default — ignoring node/edge types is a lossy baseline.
Check your understanding
Q1. You have a social network with 50M users and 5B edges. Explain concretely why you cannot use a standard dense adjacency matrix, and what data structure you would use instead.
- A) Dense adjacency works if you quantize each entry to 2-bit fixed point and shard the matrix across 64 GPUs with NCCL all-reduce for every lookup
- B) Dense adjacency needs ~312 TB even at 1 bit per entry — infeasible; use CSR, which stores only the 5B real edges (~40 GB) and maps directly to sparse matrix multiply
- C) Dense adjacency is fine for 50M nodes on a distributed file system like HDFS with 3x replication and erasure coding — storage isn't a practical concern at this scale for training
- D) Use a dense adjacency matrix but restrict training to a random 1% of nodes per epoch via reservoir sampling, which keeps peak memory usage safely under 8 GB per worker node
Q2. Explain what permutation invariance means for a graph neural network, and show why a 2-layer MLP applied to the flattened adjacency matrix is not permutation invariant.
- A) Permutation invariance means output is unchanged regardless of node feature values; MLP fails this because it is sensitive to feature magnitude, not node order
- B) It means f(PAP^T, PX) = f(A, X) for any permutation P; a flattened-A MLP fails since two node orderings give different vectors and outputs
- C) MLPs become permutation invariant once input features are L2-normalized and zero-centered before the adjacency matrix is flattened into the input feature vector
- D) Permutation invariance only applies to graph-level readout functions; node-level MLPs are fully exempt because each node occupies a fixed matrix row index
Q3. Your GNN for citation network node classification achieves 85% accuracy with 2 layers, but drops to 60% with 8 layers. What is happening and how do you fix it?
- A) 8-layer GNNs need a 10x larger learning rate to converge; the drop is a pure optimization instability, not a structural property of the aggregation
- B) Over-smoothing: each layer is a low-pass filter, so 8 layers make embeddings converge to nearly one vector; fix with residual connections or shallow 2-4 layer depth
- C) 8 layers cause overfitting from excess capacity; halving the hidden dimension while keeping the full 8 layers fully resolves the accuracy drop on this exact benchmark
- D) The citation network has too few nodes for 8-layer GNNs; this depth requires graphs with at least several million labeled nodes to converge properly at all
Q4. Which two of the following statements about heterophilic graphs and GNN aggregation are TRUE? (Select two.)
- A) Standard mean/sum aggregation destroys the ego node's discriminative signal in heterophilic graphs because neighbors carry differing labels and averaging blends them away
- B) H2GCN handles heterophily by keeping the ego embedding separate from neighbor aggregations and concatenating 1-hop and 2-hop neighborhoods rather than mixing them together
- C) Standard GNNs fail on heterophilic graphs only because they cannot process graphs containing more than two distinct node types in the input schema
- D) Heterophilic and homophilic graphs are handled identically well by all GNN variants — any performance gap traces to feature quality, never to the aggregation choice
Try it interactively
ML Systems Lab is a free interview-prep platform for ML engineers — work through the full interactive module, quizzes, and drills.
Open ML Systems Lab →