CS224W Lecture 6

Theory of GNNs

How expressive are GNNs? This is not a soft question — it has a precise mathematical answer. GNNs are exactly as powerful as the Weisfeiler-Leman graph isomorphism test, and GIN achieves that maximum.

Prerequisites: GNN basics (Lec 3) + MSG+AGG framework (Lec 4). That's it.
10
Chapters
4+
Simulations
0
Assumed Knowledge

Chapter 0: Can GNNs Tell Nodes Apart?

You have a social network. Alice follows Bob, Charlie, and Dana. Bob follows Alice and Charlie. They have the same number of friends — but their local neighborhoods are different. A good GNN should give Alice and Bob different embeddings, because their structural role in the graph differs.

But will it? This depends entirely on the aggregation function. If two nodes happen to have neighbor features that average to the same value — even though the neighbor multisets are different — the GNN cannot tell them apart. It will assign them identical embeddings, regardless of how many layers you stack or how wide you make the network.

This is not a bug you can tune away. It's a fundamental mathematical limit, tied to what it means for a function to be injective — to map distinct inputs to distinct outputs. If the aggregation function is not injective over neighbor multisets, the GNN loses information on every single layer, and there's no recovery.

The central question: Under what conditions can a GNN assign different embeddings to nodes with different local neighborhoods? And what is the most expressive possible message-passing GNN? This lecture answers both questions precisely.

What We Mean by "Expressive"

A GNN is more expressive than another if it can distinguish more pairs of nodes (or graphs) that are structurally different. Maximum expressiveness means: any two nodes with genuinely different K-hop neighborhoods get different embeddings after K layers. No information is lost.

Expressiveness has a cost: less expressive models are often more regularized and generalize better on small datasets. But understanding the theoretical ceiling is essential — it tells you what's impossible, and where to look when your model fails.

Two Nodes — Can the GNN Tell Them Apart?

Node A and Node B have different neighbor structures. An expressive GNN should give them different embeddings. Click "Aggregate" to see what mean vs. sum aggregation produces. Watch what information gets lost.

Node A: neighbors {1, 1, 2}. Node B: neighbors {1, 2}. Different neighborhoods — should get different embeddings.
What does it mean for a GNN aggregation to be "injective"?

Chapter 1: Computational Graphs = Rooted Subtrees

A GNN doesn't see the whole graph at once. With K layers, each node sees exactly its K-hop neighborhood — the subgraph reachable within K steps. This neighborhood, unfolded into a tree structure rooted at the node, is called its computational graph.

Here's how it works. Before the first layer (K=0), a node only knows its own features. After one layer (K=1), it has aggregated from its immediate neighbors. After two layers (K=2), it has aggregated from its neighbors' neighbors. The "view" expands with each layer, but it's always a tree — because we unfold the graph by tracing paths outward, and the same node may appear multiple times in the unfolding.

Key insight: Two nodes with identical rooted K-hop subtrees MUST receive the same embedding after K layers. The GNN is a deterministic function of the computational graph — identical inputs, identical outputs. This sets a lower bound on what any GNN can achieve.

The Two Conditions for Perfect Expressiveness

For a K-layer GNN to be maximally expressive:

  1. Same subtrees → same embeddings. This is guaranteed by the GNN's computation structure — it always holds.
  2. Different subtrees → different embeddings. This is NOT guaranteed. It depends on whether every aggregation function in every layer is injective over multisets of neighbor features.

The second condition is the hard one. It's asking: can the aggregation function distinguish any two distinct multisets it might see? If yes at every layer, the GNN is as expressive as the Weisfeiler-Leman test. If no at any layer, some structural information is permanently discarded.

Unfolding a Graph into Rooted Subtrees

A 4-node graph. Select a node to see its 2-hop rooted subtree. Notice how the same neighbor can appear multiple times in the unfolding — the computational graph is a tree, not the original graph.

Select a node to unfold its computational graph.
In a GNN with K layers, what does each node's computational graph look like?

Chapter 2: Multiset Functions

Here's a concept that makes all the difference: the difference between a set and a multiset. A set is a collection where each element appears at most once: {red, blue, green}. A multiset allows repeats: {red, red, blue}. {red, blue} and {red, red, blue} are the same set of colors, but different multisets.

When a GNN aggregates neighbor features, it's computing a function over a multiset. Node A might have neighbors with features {1, 1, 2} — two neighbors with value 1, one with value 2. Node B might have neighbors with features {1, 2} — one of each. Same set of distinct values, different multisets. An expressive aggregation must treat them differently.

Why this matters: If your aggregation ignores multiplicity — how many times each feature value appears — then a node with five blue neighbors looks the same as a node with one blue neighbor. You've thrown away information about neighbor counts, which is fundamental structural information about the graph.

Three Aggregations, Three Levels of Sensitivity

Consider these three aggregation functions on a multiset of scalar values:

Mean and max both discard information. Sum preserves everything — and this turns out to be the key property for maximal GNN expressiveness.

Multiset Distinguisher

Two multisets of neighbor features. All three aggregations are computed. Green = aggregations differ (can distinguish). Red = aggregations are equal (cannot distinguish). Drag the sliders to construct failure cases for mean and max.

Multiset A: count of 1s2
Multiset A: count of 2s1
Multiset B: count of 1s1
Multiset B: count of 2s2
Why is aggregating over a "multiset" the right framing for GNN neighbor aggregation?

Chapter 3: Why GCN Fails

GCN uses mean aggregation: hv(k) = MLP(mean({hu(k-1) : u ∈ N(v)}). The mean is the average — it divides the sum by the number of neighbors. This normalization is exactly the problem.

Mean aggregation cannot distinguish two different multisets that have the same average. Consider: multiset {1, 1, 1} has mean 1. Multiset {1} also has mean 1. To GCN, a node surrounded by three identical neighbors looks exactly the same as a node with one such neighbor. Three blue nodes or one blue node — the GCN can't tell.

Concrete failure: Suppose neighbors have feature values in {red=0, blue=1}. Two nodes: Node A has neighbors {red, red, blue, blue} — equal mix of red and blue. Node B has neighbors {red, blue} — also equal mix. Mean(A) = 0.5 red + 0.5 blue. Mean(B) = 0.5 red + 0.5 blue. Identical. GCN assigns A and B the same embedding even though they have structurally different neighborhoods (4 neighbors vs 2).

What GCN Can and Cannot Distinguish

GCN CAN distinguish nodes whose neighbor feature distributions differ. If A's neighbors are 80% red and B's neighbors are 60% red, GCN gets different means and can tell them apart.

GCN CANNOT distinguish nodes whose neighbor feature distributions are identical but counts differ. {red×2, blue×2} and {red×1, blue×1} have the same mean — 50% red, 50% blue — regardless of how many neighbors there are. This is a systematic blind spot, not fixable by increasing depth or width.

GCN Mean Aggregation Failure

Two nodes with different neighborhoods. Both have the same proportion of red/blue neighbors, but different counts. GCN computes identical means and assigns identical embeddings. Adjust the node counts to find cases where GCN succeeds vs. fails.

Node A: reds2
Node A: blues2
Node B: reds1
Node B: blues1
Why does GCN's mean aggregation fail to distinguish {red, red, blue, blue} from {red, blue}?

Chapter 4: Why GraphSAGE Fails

GraphSAGE uses max-pool aggregation: for each neighbor, apply an MLP to get a transformed feature vector, then take the element-wise maximum across all neighbors. This is more sophisticated than mean — it can capture the "most extreme" feature value in the neighborhood.

But max-pool has its own blind spot: it completely ignores multiplicity. max({red, red, blue}) = max({red, blue}). Once you've seen that red is present, seeing more reds adds nothing to the maximum. The max is determined by which distinct values are present, not how many of each.

Think of it this way: max-pool answers the question "which colors are present in the neighborhood?" But it cannot answer "how many of each color are there?" A node with five red neighbors and a node with one red neighbor look identical to max-pool — the maximum is "red" either way.

The Exact Failure Mode

Consider two nodes: Node A has neighbors {red, red, red} — three identical red neighbors. Node B has neighbors {red} — just one red neighbor. Max-pool on A: max(red, red, red) = red. Max-pool on B: max(red) = red. Identical outputs. GraphSAGE cannot distinguish A from B.

Contrast with GCN's failure. GCN fails when proportions are identical. GraphSAGE fails when the set of distinct values is identical. These are different failure modes — and both can occur in real graphs. Neither model is a superset of the other in expressiveness.

Max-Pool Failure Cases

Two nodes with different neighborhoods. Max-pool aggregation is shown. When both nodes have the same set of distinct neighbor features (regardless of counts), max-pool assigns identical embeddings. Toggle the neighbor configurations to find failure cases.

The Expressiveness Hierarchy (So Far)

We now have two concrete failure modes:

Neither is strictly better than the other. Both are strictly worse than some ideal injective aggregation. What would that look like?

GraphSAGE max-pool aggregation cannot distinguish {red, red, red} from {red}. Why?

Chapter 5: The WL Test

The Weisfeiler-Leman (WL) graph isomorphism test was invented in 1968, long before GNNs existed. It's a classical algorithm for deciding whether two graphs might be isomorphic — that is, structurally identical up to node relabeling. The key word is "might": the WL test can definitively prove two graphs are NOT isomorphic, but it cannot guarantee they ARE.

The WL Algorithm

Start by giving every node a label (initially, just its feature or a constant). Then repeat:

  1. Collect each node's current label plus the sorted list of its neighbors' labels.
  2. Hash this tuple into a new label.
  3. Update all nodes simultaneously with their new labels.

After K iterations, each node has a label that summarizes its K-hop rooted subtree. Compare the multisets of labels across the two graphs. If the multisets ever differ, the graphs are NOT isomorphic. If they never differ after convergence, the test is inconclusive — the graphs might be isomorphic or they might not be.

The connection to GNNs is exact: One step of WL is exactly one layer of an idealized GNN. Both collect neighbor labels, combine them with the node's own label, and produce a new label. The GNN generalizes WL by using learned weights and continuous features instead of discrete hash labels. This isn't an analogy — it's mathematically identical in structure.
Interactive WL Test — Step Through the Algorithm

Two graphs (left and right). Each node starts with a color label. Step through the WL iterations. Watch how labels refine to capture local structure. The algorithm terminates when labels stop changing or when the label multisets diverge.

When WL Fails

The WL test cannot distinguish certain pairs of non-isomorphic graphs. A classic example: two regular graphs of the same degree where every node looks locally identical. After any number of iterations, all nodes have the same label in both graphs — but the graphs are genuinely different (perhaps they have different global structures). This is a hard limit of the 1-WL test, and it's inherited by all message-passing GNNs.

In the WL test, what does one iteration do?

Chapter 6: GIN: The Most Expressive GNN

We now have a target: build a GNN that is exactly as powerful as the WL test. Xu et al. (ICLR 2019) proved that this is achievable, and designed the Graph Isomorphism Network (GIN) to achieve it. The key theorem is elegant.

Theorem (Xu et al., 2019): A GNN is at most as powerful as the WL test. A GNN with sum aggregation + a sufficiently powerful (universal) function achieves this maximum. GIN is such a network.

Why Sum is the Key

Recall: mean fails because it normalizes away counts. Max fails because it ignores multiplicity. What about sum?

Sum does not normalize. Sum({1, 1, 2}) = 4. Sum({1, 2}) = 3. Different. Sum({1, 1, 1}) = 3. Sum({1}) = 1. Different. In general, if two multisets have different counts of any element, their sums will differ — provided the values themselves differ. The formal statement is: there exists a function f such that SUM of f(x) over a multiset is injective.

This is not trivial — it requires f to map feature values to numbers in a way that makes the sums unique. For discrete features, such f always exists. For continuous features, an MLP can approximate it. This is the connection between sum aggregation and injectivity.

The Universal Multiset Function Theorem

Any injective function over multisets can be written in the form:

Φ(∑x ∈ S f(x))

Where f maps individual elements and Φ maps the resulting sum. Both Φ and f can be approximated to arbitrary precision by MLPs. This is why GIN uses MLPs for both the feature transformation and the final output — it's not just a design choice, it's a theoretical necessity for achieving maximum expressiveness.

Why Sum is Injective: A Visualization

Compare mean, max, and sum on four canonical multiset pairs. Green = can distinguish (injective for this pair). Red = cannot distinguish (failure). Sum always gets it right.

Why does sum aggregation (unlike mean or max) achieve maximum expressiveness for GNNs?

Chapter 7: The GIN Update Rule

GIN's update rule has one subtle detail beyond "use sum + MLP." The node needs to aggregate information from both its own current representation AND its neighbors' representations. Naively, you might just sum them all together — but then the node's own features get mixed in with the neighbor features in a way that loses the distinction between "self" and "neighbor."

The full GIN update:

hv(k) = MLP(k)((1 + ε(k)) · hv(k-1) + ∑u ∈ N(v) hu(k-1))

The (1 + ε) factor is the key. It ensures the node's own embedding is weighted separately from the neighbor sum. Without it, a node with feature vector h and no neighbors would look identical to a node with no self-features but neighbors that sum to h — they'd both produce the same input to the MLP. The (1+ε) breaks this ambiguity.

ε in practice: ε can be a learnable parameter (learned from data) or a fixed constant (often just 0, meaning the node's own feature counts once, same as a self-loop). In either case, the (1+ε) factor distinguishes the node's contribution from its neighbors'. Xu et al. found that fixing ε=0 works nearly as well as learning it — the MLP itself can compensate.

Data Flow Through One GIN Layer

Let's trace a concrete example. Node v has feature hv = [0.5, 0.3] and two neighbors with features [1.0, 0.2] and [0.7, 0.8]. With ε=0:

  1. Scale self: (1+0) · [0.5, 0.3] = [0.5, 0.3]
  2. Sum neighbors: [1.0, 0.2] + [0.7, 0.8] = [1.7, 1.0]
  3. Add: [0.5, 0.3] + [1.7, 1.0] = [2.2, 1.3]
  4. MLP: hv(new) = MLP([2.2, 1.3])

The MLP is a multi-layer perceptron (at least 2 layers — shallow MLPs are not universal approximators over multisets). GIN uses batch normalization between MLP layers for training stability.

GIN = Neural WL: If you replace the MLP with a perfect injective hash, GIN exactly simulates the WL test. The MLP is the "neural" version of the hash — it approximates injectivity by learning from data. This is why GIN is called the Graph Isomorphism Network: it's a differentiable, learnable implementation of WL.

Graph-Level Readout with GIN

For graph classification (not just node classification), GIN uses a concatenation readout across all layers:

hG = CONCAT(READOUT({hv(k) : v ∈ G}) | k = 0, 1, ..., K)

This preserves embeddings from all depths. Shallow layers capture local structure (triangles, paths of length 2). Deep layers capture global structure. Concatenating all layers is more expressive than just using the final layer — it uses structural information at every scale.

Why does GIN include the (1+ε)·hv term instead of just summing hv with the neighbors?

Chapter 8: What GNNs Cannot Do

GIN achieves the maximum expressiveness of message-passing GNNs — but that maximum is not unlimited. The WL test has known failure cases, and GIN inherits every one of them. Understanding these limits is essential for knowing when a GNN will fail you in practice.

The Classic WL Failure: Regular Graphs

A k-regular graph is one where every node has exactly k neighbors. Consider two different k-regular graphs on the same number of nodes — say, two different 3-regular (cubic) graphs with 6 nodes. After any number of WL iterations, every node in both graphs has the same label: "3-regular node surrounded by 3-regular nodes." The label multisets are identical. WL — and therefore any message-passing GNN — cannot distinguish them.

This is not a flaw that more layers, wider MLPs, or better training can fix. It's a structural impossibility. The 1-WL test has blind spots, and GIN has exactly the same blind spots.

Why it matters in practice: Many real-world graph tasks require distinguishing regular substructures. Counting triangles, detecting cycles of specific length, recognizing subgraph patterns — these all require expressiveness beyond 1-WL. GNN-based molecular property prediction, for example, can fail to distinguish certain non-isomorphic molecules that WL also cannot distinguish.

The Expressiveness Hierarchy

Researchers have developed a hierarchy of more expressive (and more expensive) graph algorithms:

MethodExpressivenessCostExample
GCN (mean)< 1-WLO(E)Loses count info
GraphSAGE (max)< 1-WLO(E)Loses multiplicity
GIN (sum)= 1-WLO(E)Maximum for 1-hop MSG
k-WL / k-GNN> 1-WLO(nk)Exponential in k
Random features~WL + randomnessO(E)Probabilistic ID

Higher-order GNNs (k-WL) can distinguish structures that 1-WL cannot. k=2 GNNs operate on pairs of nodes. k=3 on triples. Each step up in k multiplies the computational cost by n, making them impractical for large graphs.

Practical Workarounds

When you need expressiveness beyond 1-WL but cannot afford k-WL, several practical approaches exist:

  • Random node features: Give each node a random ID at inference time. With high probability, the random IDs break symmetries that WL cannot. Loses consistency across runs, but works empirically.
  • Structural features: Precompute features like cycle membership, eigenvectors, or subgraph counts. Feed these as node features. Offloads expressiveness to preprocessing.
  • Port the WL test and augment: Run WL first, then use its labels as additional input features. The GNN can use these to bootstrap expressiveness.
Why can't adding more GIN layers or a wider MLP overcome the WL expressiveness limit?

Chapter 9: Connections & What's Next

This lecture gave a complete theoretical picture of message-passing GNN expressiveness. Let's place it in context.

The Expressiveness Hierarchy in Full

GNNAggregationExpressivenessKey Paper
GCNMeanStrictly < WLKipf & Welling 2017
GraphSAGEMax-poolStrictly < WLHamilton et al. 2017
GATAttention-weighted meanStrictly < WLVelickovic et al. 2018
GINSum + MLP= WL (maximum)Xu et al. 2019

Note that GAT, despite its attention mechanism, still uses a weighted mean — so it's strictly less expressive than WL, just like GCN. Attention helps with training stability and task performance, but not with theoretical expressiveness.

Where to Go Next

Lec 4
GCN, GraphSAGE, GAT architectures — the models whose limits we analyzed in this lecture
Lec 6 (this)
Theory — WL expressiveness, GIN achieving the maximum, limits of 1-WL
Lec 7
Designing more powerful graph encoders — beyond 1-WL, positional encodings, higher-order GNNs
Lec 8+
Knowledge graphs, scalable GNNs, applications to biology and chemistry

Related Micro-Lessons

Lec 3 — GNN Basics — The node classification task and the GNN framework from scratch.
Lec 4 — GCN, GraphSAGE, GAT — The three main architectures whose expressiveness we analyzed here.
Lec 5 — GNN Augmentation & Training — Feature engineering and training pipelines for GNNs.

The GIN Paper

Read the full analysis in Veanors: How Powerful are Graph Neural Networks? (Xu et al., ICLR 2019). The paper contains the formal proofs, experimental results on bioinformatics benchmarks, and the exact conditions under which sum aggregation is injective.

The one-line summary: Mean and max aggregations are provably limited. Sum aggregation + MLP = Graph Isomorphism Network = as powerful as WL = the theoretical maximum for message-passing GNNs. Anything beyond this requires fundamentally different architectures (k-WL, structural encodings, or randomness).

A Closing Thought

The WL test was invented in 1968 to solve a combinatorics problem. It took 50 years for the deep learning community to realize it was secretly describing the limits of neural message passing. This is one of the most satisfying theoretical results in geometric deep learning: a simple classical algorithm, a new class of neural networks, and a precise mathematical equivalence between them.

"What I cannot create, I do not understand." — Richard Feynman

You now understand GNNs well enough to know exactly where they fail, and why. That understanding is the foundation for building better ones.