A Short Tutorial on The Weisfeiler-Lehman Test And Its Variants

Ningyuan Huang, Soledad Villar

Introduction

In the past few years, deep learning has completely revolutionized entire fields: Convolutional neural networks have changed the landscape of computer vision, and recurrent neural networks significantly improved the state of the art in natural language processing . Deep learning is now being applied, with different degrees of success, to more general problems and datasets, arising from scientific and industrial applications. There is a natural flow in the field towards the study of geometric deep learning beyond Euclidean data , where the network architecture encodes relevant theoretical properties of the problems they are trying to solve (symmetries, invariances, conservation laws). This is best exemplified by data structures like manifolds and graphs.

Graphs are one of the most common abstractions to represent data, and unsurprisingly, the study of graph neural networks (GNNs) is a rapidly growing field. There are many types GNN architectures, based on message passing , convolutional filters , generalization of spectral methods , invariant linear functions , to name a few.

Graph neural networks are typically formulated as functions that take a graph as input and output a representation of the graph. The representation usually takes the form of an embedding of the graph nodes in Euclidean space. One fundamental property of most graph neural networks is the invariance (or equivariance) with respect to permutations of the input. The philosophy is that the learned representation of the graph should be consistent with any relabeling of the nodes. This sometimes restricts the class of functions that graph neural networks can express, and constrains the architectural design of the neural network. In a nutshell, there seems to be a trade-off between invariance and expressibility. Being able to approximate invariant functions is closely related to being able to decide whether any pair of graphs are isomorphic, which is not an easy problem .

Graph isomorphism is a long-standing problem in theoretical computer science. It was suspected to be NP-hard until quite recently, when Lázló Babai produced a quasi-polynomial time algorithm to decide whether two graphs are isomorphic . There are other, less sophisticated, tests for graph isomorphism that don’t fully characterize graphs modulo isomorphisms, but can distinguish large sets of non-isomorphic graphs.

The Weisfeiler-Lehman (WL) algorithm is a classical isomorphism test based on color refinement . Each node keeps a state (or color) that gets refined in each iteration by aggregating information from their neighbor’s states. The refinement stabilizes after a few iterations and it outputs a representation of the graph. Two graphs with different representations are not isomorphic. The test can uniquely identify a large set of graphs up to isomorphism , but there are simple examples where the test tragically fails—for instance, two regular graphs with the same number of nodes and same degrees cannot be distinguished by the test, even if one is connected and the other one is not.

A natural extension of the test provides a hierarchy of algorithms. Instead of keeping the state of one node, they keep the state of kk-tuples of nodes. These algorithms are called kk-dimensional Weisfeiler-Lehman tests or kk-WL. There are two main versions of the algorithm with slightly different update rules, the one studied in has recently been named kk-folklore-WL (kk-FWL) , while the one studied in is known as kk-WL.

The power of kk-WL and kk-FWL to distinguish non-isomorphic graphs is well understood. Very beautiful mathematical work has characterized their discriminative power in terms of the satisfiability of quantified logical formulas on finite variables and pebble games , and in terms of the Sherali-Adams linear programming hierarchy . We refer the reader to the very comprehensive book by Martin Grohe .

The WL hierarchy of graph isomorphism tests has shown to be a great inspiration to define functions on graphs, in particular graph kernels , and graph neural network architectures ; and it has been proven to be a powerful tool to theoretically analyze the expressive power of graph neural networks .

In this short note, we explicitly state the differences between the test formulations, and we point out the literature of graph neural network inspired by the WL hierarchy, complementing the very nice survey by Sato .

Setting and notation

We say G=(V,E,XV)G=(V,E,X_{V}) and G′=(V′,E′,XV′)G^{\prime}=(V^{\prime},E^{\prime},X^{\prime}_{V}) are isomorphic if there exists a relabeling of the nodes of GG that produce the graph G′G^{\prime}. In other words, they are isomorphic if there exists a permutation Π∈Sn\Pi\in S_{n} so that Π V=V′\Pi\,V=V^{\prime}, Π E=E′\Pi\,E=E^{\prime} where Π (u,v):=(Π u,Π v)\Pi\,(u,v):=(\Pi\,u,\Pi\,v) and Π XV=XV′\Pi\,X_{V}=X^{\prime}_{V} where ΠXv=XΠv\Pi X_{v}=X_{\Pi v}.

The Weisfeiler-Lehman test keeps a state (or color) for every node (or tuples of nodes denoted by v⃗=(v1,…,vk)∈Vk\vec{v}=(v_{1},\ldots,v_{k})\in V^{k} in its kk-dimensional versions). It refines the node states by aggregating the state information from their neighbors. In order to compute the update, WL uses an injective hash function defined in different objects modulo equivalence classes. In particular, for all v,w∈Vv,w\in V hash(Xv)=hash(Xw)\text{hash}(X_{v})=\text{hash}(X_{w}) iff Xv=XwX_{v}=X_{w}. For v⃗=(v1,…vk)\vec{v}=(v_{1},\ldots v_{k}), v⃗′=(v1′,…vk′)\vec{v}^{\prime}=(v^{\prime}_{1},\ldots v^{\prime}_{k}) we define the hash function such that hash(G[v⃗])=hash(G[v′⃗])\text{hash}(G[\vec{v}])=\text{hash}(G[\vec{v^{\prime}}]) iff (1) Xvi=Xvi′∀i∈[k]X_{v_{i}}=X_{v^{\prime}_{i}}\forall i\in[k]; and (2) (vi,vj)∈E(v_{i},v_{j})\in E iff (vi′,vj′)∈E′,∀i,j∈[k](v^{\prime}_{i},v^{\prime}_{j})\in E^{\prime},\forall i,j\in[k].

Weisfeiler Lehman variants

The classical WL (or 1-WL) test , keeps a state for each node that refines by aggregating their neighbors state. It outputs an embedding of the graph that corresponds to the state of every node. We say that the WL succeeds at distinguishing a pair of non-isomorphic graphs G,G′G,G^{\prime} if WL(G)≠WL(G′)\text{WL}(G)\neq\text{WL}(G^{\prime}).

cv0←c_{v}^{0}\mathrel{\leftarrow} hash(Xv)(X_{v}) for all v∈Vv\in V

The WL algorithm successfully distinguishes most pairs of graphs , but it fails to distinguish some basic examples, such as all regular graphs on nn nodes and degree dd. In the context of GNNs, and show that under the anonymous setting, if fθf_{\theta} is a function implemented by a message passing neural network (MPNN), then fθ(G)=fθ(G′)f_{\theta}(G)=f_{\theta}(G^{\prime}) for all G,G′G,G^{\prime} such that WL(G)=WL(G′)\text{WL}(G)=\text{WL}(G^{\prime}). In particular, MPNNs cannot express some trivial functions, such as the number of connected components of a graph, given that the graph does not have node features and XvX_{v} is taken as the same for all nodes.

2 k𝑘k-WL

The kk-dimensional Weisfeiler Lehman test extends the test to coloring kk-tuples of nodes. It is defined as:

cv⃗0←c_{\vec{v}}^{0}\mathrel{\leftarrow} hash(G[v⃗])(G[\vec{v}]) for all v⃗∈Vk\vec{v}\in V^{k}

The node neighborhood Ni(v⃗)\mathcal{N}_{i}(\vec{v}) is the set of kk-tuples that differ with v⃗\vec{v} only in the position ii. For v⃗=(v1,…,vk)\vec{v}=(v_{1},\ldots,v_{k}) we have

Inspired by k-WL, propose a GNN architecture based on a version of kk-WL on sets, which is strictly more expressive than MPNN.

3 k𝑘k-FWL

The version of the kk-Weisfeiler Lehman test studied by Cai, Furer, and Immmerman considers a slightly different definition for the updates than the definition in Section 3.2. It has recently been renamed as folklore-WL (FWL) by . It is computationally more efficient than kk-WL and it has been used in to design GNN architectures.

cv⃗0←c_{\vec{v}}^{0}\mathrel{\leftarrow} hash(G[v⃗])(G[\vec{v}]) for all v⃗∈Vk\vec{v}\in V^{k}

Note that cv⃗[i]←wc_{\vec{v}[i]\leftarrow w} is a tuple that differs from v⃗\vec{v} in the position ii, where viv_{i} is exchanged by ww. In particular, if v⃗=(v1,…,vk)\vec{v}=(v_{1},\ldots,v_{k}) then

Thus, the node neighborhood NiF(v⃗)\mathcal{N}_{i}^{F}(\vec{v}) in kk-FWL is

We can see that in kk-WL, Ni(v⃗)\mathcal{N}_{i}(\vec{v}) is a set of nn elements where each element is kk-dimensional; in kk-FWL, NiF(v⃗)\mathcal{N}^{F}_{i}(\vec{v}) is a set of kk elements where each element is nn-dimensional. The definition of neighborhood underpins the differences in kk-WL and kk-FWL. More explanations and illustrations are given in Section 3.5.

4 Comparisons between the WL variants

Note that 1-WL is not the same as kk-WL with k=1k=1. In fact, 1-WL≡\equiv2-WL.

Consider kk-WL defined in Section 3.2 with k=1k=1: then cv⃗,11c^{1}_{\vec{v},1} would be the same for all v⃗\vec{v} (the multiset of cv⃗0c^{0}_{\vec{v}} for v⃗∈V1\vec{v}\in V^{1}), so the algorithm stabilizes in one step, coinciding with the initialization. This is because in kk-WL the neighboring tuples do not have information about the edges of the graph. The edges are only considered in the initialization hash(G[v⃗])(G[\vec{v}]).

Let G=(V,E,XV)G=(V,E,X_{V}), v⃗=(vi,vj)∈V2\vec{v}=(v_{i},v_{j})\in V^{2} and v⃗′=(vk,vl)∈V2\vec{v}^{\prime}=(v_{k},v_{l})\in V^{2}. After one step of 2-WL we have cv⃗1=cv⃗′1c^{1}_{\vec{v}}=c^{1}_{\vec{v}^{\prime}} if and only if (1) cv⃗0=cv⃗′0c^{0}_{\vec{v}}=c^{0}_{\vec{v}^{\prime}}, (2) { ⁣ ⁣{cw,vj0:w∈V} ⁣ ⁣}={ ⁣ ⁣{cw,vl0:w∈V} ⁣ ⁣}\{\!\!\{c^{0}_{w,v_{j}}:w\in V\}\!\!\}=\{\!\!\{c^{0}_{w,v_{l}}:w\in V\}\!\!\}, and (3) { ⁣ ⁣{cvi,w0:w∈V} ⁣ ⁣}={ ⁣ ⁣{cvk,w0:w∈V} ⁣ ⁣}\{\!\!\{c^{0}_{v_{i},w}:w\in V\}\!\!\}=\{\!\!\{c^{0}_{v_{k},w}:w\in V\}\!\!\}. Note that (2) holds if and only if { ⁣ ⁣{hash(w):w∈N(vj)} ⁣ ⁣}={ ⁣ ⁣{hash(w):w∈N(vl)} ⁣ ⁣}\{\!\!\{\text{hash}(w):w\in\mathcal{N}(v_{j})\}\!\!\}=\{\!\!\{\text{hash}(w):w\in\mathcal{N}(v_{l})\}\!\!\} and similarly for (3).

This argument shows that in each iteration, color refinement in 2-WL is equivalent to implementing 1-WL in each coordinate. This argument inductively shows that 2-WL(vi,vj)=(1-WL(vi),1-WL(vj))\text{2-WL}(v_{i},v_{j})=(\text{1-WL}(v_{i}),\text{1-WL}(v_{j})) and therefore it has the same distinguishing power as 1-WL.

The discriminating power of kk-WL is equivalent to the one of (k−1)(k-1)-FWL for k≥3k\geq 3. To the best of our knowledge, there is no explicit proof of the equivalence only relying on the definitions of kk-WL and (k−1)(k-1)-WL. However, in Section 5 of the authors prove that (k−1)(k-1)-FWL is equivalent to CkC^{k}, the set of quantified first order formulas of G=(V,E)G=(V,E) in kk variablesOne example of such formulas is ∀x1∃!dx2(E(x1,x2))\forall x_{1}\exists!dx_{2}(E(x_{1},x_{2})). This means that for all x1x_{1} node in GG, there exists exactly dd nodes x2x_{2} (!! means exactly) such that there is an edge between x1x_{1} and x2x_{2} (i.e. the graph GG has degree dd).. This proof is reformulated in Theorem 3.5.7 of for kk-WL, showing that kk-WL is equivalent to CkC^{k}.

5 Example: 222-WL and 222-FWL on regular graphs

Below we show a canonical example of two regular non-isomorphic graphs (Figure 1), where the classical WL test and 22-WL test both fail to distinguish, but 22-FWL succeeds.

We first go over the steps for 22-WL, illustrated in Figure 2. For simplicity we assume that all node features are the same. At the initialization, there are only two isomorphic types: (1) (vi,vj)∈E(v_{i},v_{j})\in E (i.e, connected); (2) (vi,vj)∉E(v_{i},v_{j})\notin E (i.e., not connected). We color v⃗\vec{v} as yellow for type (1) and grey for type (2). As shown in Figure 2 left panel, G0G^{0} and H0H^{0} (the coloring at initialization) has the same elements with equal multiplicities (24 greys; 12 yellows), and thus they produce the same hash value. Now, we can view V2V^{2} as a 6×66\times 6 matrix, and the neighbors of a tuple (vi,vj)(v_{i},v_{j}) is given by the jj-th row and the ii-th column. Examples of neighbors for (3,3),(3,2)(3,3),(3,2) are shown in middle inset of Figure 2. Observe that for any node in G0G^{0}, cv⃗,11=cv⃗,21={ ⁣ ⁣{4 greys,2 yellows} ⁣ ⁣}c_{\vec{v},1}^{1}=c_{\vec{v},2}^{1}=\{\!\!\{4\text{ greys},2\text{ yellows}\}\!\!\}. Thus all nodes have the same neighborhood but may differ in the initial color. Let hash(grey,cv⃗,11,cv⃗,21)(\text{grey},c_{\vec{v},1}^{1},c_{\vec{v},2}^{1}) be orange, hash(yellow,cv⃗,11,cv⃗,21)(\text{yellow},c_{\vec{v},1}^{1},c_{\vec{v},2}^{1}) be brown, and we obtain G1,H1G^{1},H^{1} as shown on the right panel of Figure 2. Notice that the color patterns (i.e., the multiset of G,HG,H) do not change after the first iteration, and thus the 22-WL test terminates, which fails to distinguish GG and HH.

Figure 3 illustrates the color refinement of 22-FWL. Note that 22-FWL has the same color assignment as 22-WL at the initialization. However, 22-FWL defines the tuple neighborhood as nn elements of length 22, unlike 22 vectors of length nn in 22-WL. Examples of neighbors defined by 22-FWL are shown at the right panel of Figure 3. Now, there are 3 isomorphic types in G0G^{0}, which are hashed as brown, blue, and orange. Intuitively, they represent -hop, 11-hop, and disjoint neighborhood in GG, respectively. In contrast, HH has 4 isomorphic types hashed as brown, purple, green, and orange, which characterizes the 0,1,2,30,1,2,3-hop neighborhood in HH. Hence, in the first iteration, 22-FWL outputs two different color patterns for GG and HH. One can check the refinement stabilizes in one step, correctly concluding that GG and HH are non-isomorphic.

Conclusion

The Weisfeler-Lehman test and its kk-dimensional generalizations are powerful tools to study the expressive power of invariant functions on graphs. Mathematically, this test is very well understood thanks to the work by Cai, Furer, and Immerman in the 90’s and the work by Grohe and collaborators in the past decade.

In the context of graph neural networks, the Weisfeiler-Lehman test has been used to analyze their theoretical properties. It has also inspired the design of expressive and invariant GNN architectures.

In this work, we explain the context of kk-WL and kk-FWL and provide a simple tutorial that illustrates the differences between them. We refer the reader to for a comprehensive survey.

References