ML Systems Lab Open interactive version →
Foundational 35 min read graphadjacencyCSRhomophilypermutation invariance

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

Takeaway

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

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.

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.

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?

Q4. Which two of the following statements about heterophilic graphs and GNN aggregation are TRUE? (Select two.)

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 →