Weisfeiler-Lehman test

Also known as: WL test · Weisfeiler-Leman algorithm

On this page 8

The Weisfeiler-Lehman test, or WL test, is an iterative color refinement algorithm that assigns labels to graph nodes based on their neighborhoods and compares the resulting label histograms to decide whether two graphs are possibly isomorphic. Also known as the Weisfeiler-Leman algorithm, it is a necessary but not sufficient test: different histograms prove the graphs differ, while identical histograms do not prove they are the same.

In graph learning, the WL test has become the standard yardstick for the expressive power of graph neural networks. Since 2019, results have shown that message passing neural networks can distinguish at most the graphs that the one-dimensional WL test distinguishes, which turned a classic algorithm from 1968 into a central concept of modern artificial intelligence on graphs.

How Weisfeiler-Lehman color refinement works

The algorithm starts by giving every node an initial color, either the same color for all nodes or a color derived from node features or degree. In each iteration, a node’s new color is obtained by hashing its current color together with the multiset of its neighbors’ colors. Nodes with the same color and the same neighbor-color multiset receive the same new color.

  1. Assign initial colors to all nodes in both graphs.
  2. For each node, collect its color and the multiset of neighbor colors.
  3. Map each distinct combination to a new color with an injective relabeling.
  4. Compare the color histograms of the two graphs; if they differ, stop and report non-isomorphic.
  5. Repeat until the partition of nodes into color classes stops changing.

The refinement stabilizes after at most n iterations for a graph with n nodes, and in practice usually after a few rounds. A carefully implemented version runs in O((n + m) log n) time for m edges, which makes it fast enough to serve as a first filter in graph isomorphism software.

Both graphs must be refined with a shared color dictionary, so that the same neighborhood pattern receives the same label in each graph. Otherwise the histograms cannot be compared.

Variants and the WL hierarchy

The basic procedure is 1-WL, also called color refinement. Higher-dimensional versions color k-tuples of nodes instead of single nodes, forming a hierarchy of increasing power and cost. Two conventions exist, so the literature distinguishes k-WL from the folklore variant k-FWL; k-FWL has the same power as (k+1)-WL.

VariantColors assigned toApproximate costDistinguishing power
1-WLSingle nodesNear-linear in edgesFails on regular graphs
2-WLPairs of nodesPolynomial, about n squared memoryEqual to 1-WL
3-WL (2-FWL)Pairs, with folklore updateAbout n cubed timeCounts triangles and cycles
k-WLk-tuplesAbout n to the k memoryStrictly stronger as k grows
Levels of the Weisfeiler-Lehman hierarchy

No fixed k distinguishes all non-isomorphic graphs: the Cai-Fürer-Immerman construction produces pairs of graphs that fool k-WL for any chosen k. The hierarchy is also tied to logic, since k-WL equivalence matches equivalence in first-order logic with counting restricted to a bounded number of variables.

Why the WL test bounds GNN expressiveness

A message passing GNN updates each node embedding by aggregating neighbor embeddings and combining the result with the node’s own state, which mirrors one WL iteration. Consequently, if 1-WL assigns two graphs the same histogram, any message passing GNN produces the same graph-level representation for them, regardless of its weights.

The Graph isomorphism network (GIN) was designed to reach this upper bound by using sum aggregation and an MLP, which together approximate an injective function on multisets. Mean and max aggregation lose information about multiplicities, so models built on them are strictly weaker than 1-WL.

ModelAggregation or mechanismPower relative to WL
Graph convolutional networkNormalized meanBelow 1-WL
GraphSAGE (mean or max)Mean or max poolingBelow 1-WL
Graph isomorphism networkSum with MLPEqual to 1-WL
k-GNN, PPGNHigher-order tuplesUp to 3-WL level
GNN with random or positional featuresNode identifiers or encodingsCan exceed 1-WL
Expressive power of common graph models relative to WL

This equivalence assumes discrete or injectively encoded features and enough layers. With continuous features, limited depth or limited numeric precision, a real model can fall short of the theoretical bound.

What the WL test cannot distinguish

The best-known blind spot of 1-WL is regular graphs: in a graph where every node has the same degree, uniform initial colors never split, so all regular graphs of the same size and degree look identical. A six-node cycle and two disjoint triangles receive the same coloring, although only one contains triangles.

PropertyDetected by 1-WLPractical consequence
Node degree distributionYesCaptured after one iteration
Tree structureYes, for treesTrees are identified up to isomorphism
Triangle and cycle countsNo, in generalWeak on chemistry and social motifs
ConnectivityNo, in generalMay confuse connected and split graphs
Strongly regular structureNoFails even for some 3-WL cases
Structural properties and 1-WL detectability

Practical uses in graph learning

Beyond theory, the WL procedure is a practical feature generator. The Weisfeiler-Lehman subtree kernel counts shared WL colors across iterations and remains a strong, fast graph kernel baseline for graph classification, often competitive with neural models on small molecular benchmarks.

  • Benchmark design: researchers test new architectures on graph pairs that 1-WL cannot separate.
  • Model selection: if a task depends on cycles or substructure counts, plain message passing is a risky choice.
  • Expressiveness upgrades: subgraph GNNs, Laplacian positional encoding and higher-order models aim to go beyond 1-WL.
  • Dataset diagnostics: running 1-WL reveals graphs with identical colorings but different labels, an upper bound on achievable accuracy.

Limitations and common misconceptions

Matching WL power is a statement about distinguishing structures, not about generalization, optimization or accuracy on a given dataset. A more expressive model can perform worse if it overfits or is harder to train, and many real benchmarks are already almost fully separable by 1-WL.

  • WL is not a full isomorphism test: equal colorings do not prove isomorphism.
  • Higher k is not always practical: memory grows roughly as n to the power k.
  • Expressiveness is not the only bottleneck: over-smoothing and over-squashing also limit GNNs.

The short version

Imagine two networks of people and a question: are they arranged the same way? The WL test answers by giving each person a name tag based on their friends. Then it updates the tag again, using the friends’ new tags. After a few rounds, it counts the tags in each network.

If the counts differ, the networks are definitely different. If they match, the networks are probably the same, but not certainly. Graph neural networks work in a similar way, so they share the same blind spots. That is why researchers use this test to judge what a graph model can tell apart.

Where it makes a difference

A team predicting molecular toxicity trains a GCN on a few thousand compounds and sees accuracy plateau. Toxicity in their data depends partly on ring structures, and running 1-WL on the dataset shows that several pairs of molecules with different labels receive identical colorings, so no message passing model could separate them.

The team switches to a GIN and adds cycle-count features and random-walk positional encodings to each node. The previously indistinguishable pairs now produce different embeddings, and validation ROC-AUC improves by several points. The WL analysis took minutes and explained a limit that further hyperparameter tuning could never have removed.