TL;DR
The Weisfeiler-Lehman test is a fast procedure that gives every vertex of a graph a label, then updates each label using the labels of its neighbours, and keeps going until nothing changes. If two graphs end up with different label counts, they cannot be the same shape. If the counts match, the test says nothing, because some different graphs get identical labels. It is a cheap first check for graph isomorphism, and it also describes what a standard graph neural network can and cannot tell apart.
Builds on: Graph, Graph Isomorphism, Multiset · Leads to: Graph Neural Network, GIN
Where it's used
Filtering graph isomorphism checks. Software that compares graphs, such as a chemical database that stores molecules as graphs, needs a quick way to rule out most pairs. Running the test on two graphs costs little more than reading their edges, and any difference in the final label counts settles the question immediately. Only the pairs that pass need a slower exact check.
Molecular fingerprints. In cheminformatics a molecule is a graph whose vertices are atoms and whose edges are bonds. Fingerprints built by repeatedly combining each atom's neighbourhood, which follow the same idea as the test, turn a molecule into a list of features used for similarity search and property prediction.
Understanding graph neural networks. A message-passing Graph Neural Network (see Message Passing Neural Network) updates each node by combining its own features with a summary of its neighbours' features, and repeats this for several layers. That is the same shape as one round of the test. The 2018 paper by Xu and colleagues, How Powerful are Graph Neural Networks?, uses this link to show that standard message passing is never more powerful than the test, and it designs a layer that matches the test's power.
How it works
Here is a tiny comparison of two graphs with four vertices each. Graph G is a square: vertices a, b, c, d joined in a cycle a-b, b-c, c-d, d-a. Graph H is a path: vertices p, q, r, s joined as p-q, q-r, r-s. We want to know whether G and H could be the same shape. Both have four vertices, but G has four edges and H has three, so they are certainly different. Pretend we did not look at the edges and use the test to find out.
-
Start every vertex with the same label, call it 0. In both graphs every vertex gets label 0. Check: G has four vertices labelled 0, and so does H.
-
Build a signature for each vertex. The signature is the vertex's own label together with the multiset of its neighbours' labels. A multiset keeps repeats, so a vertex with two neighbours both labelled 0 has the signature (0, 0). In G every vertex has two neighbours, so every signature is (0, 0). In H the endpoints p and s have one neighbour each, so their signature is (0, 0). The middle vertices q and r have two neighbours, so their signature is (0, 0).
-
Turn the signatures into new labels. Give each distinct signature a new name, the same name in both graphs. Call (0, 0) label 1 and (0, 0) label 2. Check: the naming must be shared, otherwise the two graphs are never compared fairly.
-
Count the labels in each graph. G has four vertices with label 1 and none with label 2. H has two vertices with label 1 (q and r) and two with label 2 (p and s). The counts differ.
-
Decide. Because the label counts differ, the test says G and H are not isomorphic. This is correct, and it was reached without comparing edge counts directly. Note that the decision comes from the label counts, not from the edge counts.
The test always runs to a fixed point: once a round produces no new labels, the labels are final. Each round uses only the previous round's labels, so the procedure takes at most as many rounds as there are vertices.
flowchart LR A["Give every vertex the same starting label"] --> B["Build each vertex's signature: own label plus multiset of neighbour labels"] B --> C["Rename signatures with shared names"] C --> D["Repeat until no new names appear"] D --> E["Compare label counts of both graphs"]
The math
Given a graph with vertex set V and neighbourhood N(v) for each vertex v, let c^0(v) be a starting label. For each round t from 0 upward, define
c^(t+1)(v) = name of the pair ( c^t(v), the multiset of c^t(u) for u in N(v) ).
Two graphs are compared by running the same rounds on both, with one shared naming rule. The test answers "not isomorphic" if the multisets of labels at some round differ.
Step 1 in symbols: the starting labels
Set c^0(v) to the same value for every vertex, or to its degree, which is the number of its neighbours. Starting with the same value is the plainest form of the test. Starting with the degree is a common variant, but it adds little, because the neighbour multiset in round 1 already records how many neighbours each vertex has.
Step 2 in symbols: the signature
The signature of v at round t is the pair
where the double square brackets denote a multiset, so repeated neighbour labels are counted each time they occur. Writing it as a pair keeps the vertex's own label, which matters: a vertex should not lose its identity when it looks at its neighbours.
Step 3 in symbols: naming the signatures
Assign an integer to each distinct signature in a fixed order, and use the same assignment for both graphs. This is a lookup table, not a Hash Function, because two signatures that collided would merge into one label. This naming function is injective: two different signatures never get the same integer, which is what makes the label counts meaningful. See Injective Function for why this matters.
Step 4 in symbols: the comparison
For each round t, let the colour histogram of G be the function that counts, for each label k, how many vertices of G have label k. The test rejects isomorphism if
histogram of G at round t is not equal to histogram of H at round t
for some t. Equal histograms at every round do not prove isomorphism. They only mean the test cannot tell the two graphs apart.
Worked examples
Graph T is a triangle on three vertices, every vertex of degree 2. Graph P is a path on three vertices, with degrees 1, 2, 1. Start every vertex with label 0.
- In T every vertex has two neighbours, all labelled 0. Every signature is (0, 0).
- In P the two endpoints each have one neighbour, so their signature is (0, 0). The middle vertex has signature (0, 0).
- Name the signatures: (0, 0) gets label 1 and (0, 0) gets label 2.
- Count labels. T has three vertices with label 1. P has one vertex with label 1 and two with label 2.
- The counts differ at round 1, so T and P are not isomorphic.
Interpretation: a single round is enough to see that the triangle and the path differ, because the path has a vertex with fewer neighbours.
Use the graphs G (square) and H (path) from the walkthrough. Start with label 0 everywhere.
- Round 1 signatures in G: each vertex has signature (0, 0). All four are label 1.
- Round 1 signatures in H: the endpoints p and s have (0, 0), and the middle vertices q and r have (0, 0). Two are label 1, two are label 2.
- Compare: G has four label-1 vertices, H has two label-1 and two label-2. The histograms differ.
- Second check, for completeness: in round 2, G still has four label-1 vertices, since its signatures do not change. The difference has already been found.
Interpretation: the test separates the two graphs in round 1 by degree information only. A different pair of graphs with the same degree sequence would need more rounds, or the test might never separate them.
Butane and isobutane have the same molecular formula, C4H10, but different structures. Look at their carbon skeletons, ignoring hydrogens. Butane is a path of four carbons, P4. Isobutane has a central carbon bonded to three others, a star with three leaves, K1,3. Both skeletons have four vertices and three edges.
- Start all four carbons with label 0.
- In butane the two end carbons have one neighbour each, giving signature (0, 0). The two middle carbons have two neighbours each, giving (0, 0).
- In isobutane the central carbon has three neighbours, giving signature (0, 0). The three outer carbons each have one neighbour, giving (0, 0).
- Name the signatures: (0, 0) gets label 1, (0, 0) gets label 2, and (0, 0) gets label 3.
- Count. Butane has two label-1 vertices and two label-2 vertices. Isobutane has three label-1 vertices and one label-3 vertex.
- The counts differ, so the skeletons are not isomorphic, as expected for two different molecules.
Try it: is a path on five vertices distinguishable from a cycle on five vertices by one round of the test, starting from label 0 everywhere? Answer: yes. In the cycle every vertex has two neighbours, so all five signatures are (0, 0). In the path the two endpoints have signature (0, 0), so the label counts differ.
Common mistakes and tradeoffs
It is tempting to think that equal label counts prove two graphs are the same shape. In fact the test can be fooled. Take a 6-cycle and two disjoint triangles. Every vertex in both has exactly two neighbours, all starting labels stay equal in every round, and the test reports no difference. Yet the graphs are different, because one is connected and the other is not. The test is a one-sided check: it can prove non-isomorphism, but it cannot prove isomorphism.
It is tempting to think that more rounds always help. In fact the labels stabilise after finitely many rounds, and after that extra rounds add nothing. The test's power is fixed by the refinement rule, not by running longer.
It is tempting to think the naming of labels matters. In fact names do not matter, as long as the same signature gets the same name in both graphs. Forgetting to share names is the most common implementation mistake, and it makes two graphs look different for the wrong reason.
Use the test when you need a fast, cheap filter, or when you want to understand a message-passing model. Do not use it when you need a certain answer about isomorphism; an exact algorithm is required there. Alternatives include canonical labelling methods and higher-order versions of the test, which refine tuples of vertices rather than single vertices and separate more graphs, at higher cost.
History
Boris Weisfeiler and Andrei Lehman described the refinement procedure in a 1968 paper, which proposed it as a way to reduce a graph to a canonical form. For a long time it was studied mainly as a tool in graph isomorphism theory.
Its significance grew when it was found to be the same kind of computation as the message passing used in graph neural networks. The 2018 paper by Xu and colleagues made this link explicit, which is why the test is now taught alongside graph learning.
Recap
- Each vertex gets a label, and each round it is renamed from its own label and the multiset of its neighbours' labels.
- The names must be shared across the two graphs, and the naming must be injective.
- If the label counts differ in some round, the graphs are certainly not isomorphic.
- If the counts match, the test is silent, and there are different graphs it cannot separate, such as a 6-cycle and two triangles.
- Next, read Graph Neural Network and the GIN paper, which show how this refinement is the upper limit on standard message passing.
Check yourself
QuestionRecall: what does the test report if the label counts of two graphs differ at some round?Show answer
QuestionComputation: starting from label 0 everywhere, what are the round 1 signatures of a star with three leaves, with center c and leaves x, y, z?Show answer
QuestionWhy: why must the same signature receive the same name in both graphs?Show answer
QuestionApplied: a colleague says the test has shown two networks have the same structure because their label counts match after five rounds. What do you tell them?Show answer
Sources
- Graph isomorphism (Wikipedia): https://en.wikipedia.org/wiki/Graph_isomorphism
- Xu et al., How Powerful are Graph Neural Networks?, arXiv 1810.00826: https://arxiv.org/abs/1810.00826