Improving Expressivity of Graph Neural Networks using Localization
In this paper, we propose localized versions of Weisfeiler-Leman (WL)
algorithms in an effort to both increase the expressivity, as well as decrease
the computational overhead. We focus on the specific problem of subgraph
counting and give localized versions of $k-$WL for any $k$. We analyze the
power of Local $k-$WL and prove that it is more expressive than $k-$WL and at
most as expressive as $(k+1)-$WL. We give a characterization of patterns whose
count as a subgraph and induced subgraph are invariant if two graphs are Local
$k-$WL equivalent. We also introduce two variants of $k-$WL: Layer $k-$WL and
recursive $k-$WL. These methods are more time and space efficient than applying
$k-$WL on the whole graph. We also propose a fragmentation technique that
guarantees the exact count of all induced subgraphs of size at most 4 using
just $1-$WL. The same idea can be extended further for larger patterns using
$k>1$. We also compare the expressive power of Local $k-$WL with other GNN
hierarchies and show that given a bound on the time-complexity, our methods are
more expressive than the ones mentioned in Papp and Wattenhofer[2022a].
Anant Kumar, Shrutimoy Das, Shubhajit Roy, Binita Maity, Anirban Dasgupta
# Improving Expressivity of Graph Neural Networks using Localization

###### Abstract

In this paper, we propose localized versions of Weisfeiler-Leman (WL) algorithms in an effort to both increase the expressivity, as well as decrease the computational overhead. We focus on the specific problem of subgraph counting and give localized versions of \(k-\)WL for any \(k\). We analyze the power of Local \(k-\)WL and prove that it is more expressive than \(k-\)WL and at most as expressive as \((k+1)-\)WL. We give a characterization of patterns whose count as a subgraph and induced subgraph are invariant if two graphs are Local \(k-\)WL equivalent. We also introduce two variants of \(k-\)WL: Layer \(k-\)WL and recursive \(k-\)WL. These methods are more time and space efficient than applying \(k-\)WL on the whole graph. We also propose a fragmentation technique that guarantees the exact count of all induced subgraphs of size at most 4 using just \(1-\)WL. The same idea can be extended further for larger patterns using \(k>1\). We also compare the expressive power of Local \(k-\)WL with other GNN hierarchies and show that given a bound on the time-complexity, our methods are more expressive than the ones mentioned in Papp and Wattenhofer (2022).

## 1 Introduction

Graphs have been used for representing relational and structural data that appear in a variety of domains, ranging from social network analysis and combinatorial optimization to particle physics and protein folding Dill et al. (2008). In order to learn representations of these data for various downstream learning tasks such as graph classification, graph neural networks (GNNs) have emerged as very effective models. Given the various types of GNN-based models developed in recent years Kipf and Welling (2017); Velickovic et al. (2018); Hamilton et al. (2018); Xu et al. (2019), researchers have attempted to characterize the expressive power of these models. Morris et al. (2019) showed the equivalence between message-passing GNNs and 1-Weisfeiler-Leman (WL) algorithm Weisfeiler and Leman (1968), which is a well known combinatorial technique for checking graph isomorphism and similarity. They also showed the equivalence between \(k\)-GNNs and \(k-\)WL. Thus, in this paper, by \(k-\)WL, we will be referring to the equivalent \(k\)-GNN model. In general, the expressiveness of \(k-\)WL, for any \(k\), is measured by its ability to identify non-isomorphic graphs and subgraphs. In this paper, we are using _Folklore WL_ and state the results accordingly.

It has been shown that \((k+1)-\)WL is more expressive than \(k-\)WL. The time and space complexity increases exponentially with \(k\). Thus, it is infeasible to run \(k-\)WL on large graphs. Also, the \(k-\)WL hierarchy is crude as \(3-\)WL identifies almost all non-isomorphic graphs. Arvind et al. (2017) characterized the graphs that can be identified by \(1-\)WL. So, we are interested in coming up with a
GNN hierarchy that can be easily extended without much computational overhead. More specifically, we want to define a GNN hierarchy whose expressiveness lies between \(k-\)WL and \((k+1)-\)WL.

A count of specific patterns is very useful in determining the similarity between two graphs. However, detecting and counting the number of subgraphs is generally NP-complete as it is a generalization of the clique problem. There have been various works on efficient algorithms for some fixed patterns and restricted host graph classes Bressan (2018); Shervashidze et al. (2009); Bouritsas et al. (2020); Zhao et al. (2022); Shervashidze et al. (2011); Komarath et al. (2023); Ying et al. (2019); Liu et al. (2019). In Arvind et al. (2020) characterizes patterns whose count is invariant for \(1-\)WL and \(2-\)WL equivalent graphs. Also, there exists a GNN hierarchy, \(S_{k}\)Papp and Wattenhofer (2022), where each node has an attribute that counts the number of induced subgraphs of size at most \(k\), that the node is participating in. It would be interesting to see whether a scalable GNN hierarchy exists, that is comparable to the \(k-\)WL and \(S_{k}\)hierarchy. There also exists a GNN hierarchy, \(M_{k}\), Papp and Wattenhofer (2022); Huang et al. (2023)in which \(k\) vertices are marked or deleted and a GNN model is run on the modified graph.

Various subgraph based GNN models have been proposed that tackle these questions Zhao et al. (2022); Zhang and Li (2021); Morris et al. (2018); Alvarez-Gonzalez et al. (2022); Maron et al. (2019); You et al. (2021); Frasca et al. (2022); Morris et al. (2021); Bevilacqua et al. (2021); Papp and Wattenhofer (2022); Barcelo et al. (2021); Huang et al. (2023). These GNNs have been effective in capturing more fine-grained patterns and relationships within the graphs, and are scalable for large graphs. Also, it has been shown that subgraph GNNs are more expressive than the traditional ones. Frasca et al. (2022) gave an upper bound on the expressive power of subgraph \(1-\)WL. This leads to the question of coming up with upper and lower bounds for the expressiveness of subgraph \(k-\)WL for arbitrary \(k\).

Consider the task of counting the occurrence of \(H\) as subgraph or induced subgraph in the host graph \(G\). Given the effectiveness of subgraph \(k-\)WL methods in increasing the expressiveness of GNNs, we want to extend this method to the subgraph itself and check whether we can fragment the subgraph and learn the count of fragments of the subgraphs to get the actual count of \(H\) in the graph. We are interested in evaluating its expressiveness in terms of subgraph and induced subgraph counting. Also, if there exists a GNN hierarchical model, we want to compare its expressiveness to pre-existing GNN hierarchies, as done in Papp and Wattenhofer (2022).

### Our Contributions

In this paper, we attempt to answer these questions. The main contributions of our work are listed below:

1. **Characterizing expressiveness of _Local \(k-\)WL:_** Given a graph \(G=(V,E)\), we extract a \(r\)-hop subgraph rooted at each vertex \(v\in V\), say \(G_{v}^{r}\), and run \(k-\)WL on \(G_{v}^{r}\). While GNNs based on subgraphs have been proposed in recent papers, this is the first work that gives both upper and lower bounds for the expressiveness of Local \(k-\)WL. We are also the first to characterize patterns that can be counted exactly by Local \(k-\)WL.
2. _Layer \(k-\)WL:_ To improve the space and time complexity of Local \(k-\)WL, we propose the Layer \(k-\)WL method. For this method, instead of running \(k-\)WL on \(G_{v}^{r}\), we run it on two consecutive layers of vertices. Here, the \(i\)th layer of vertices refers to the vertices that appear at an \(i\)-hop distance from \(v\)(or the \(i\)th layer breadth-first search(BFS)).
3. _Recursive WL :_ Recursive WL is an alternative to \(k-\)WL. In this method, we first run \(1-\)WL to get a partitioning of vertices. Then we run \((k-1)-\)WL on the vertices of each partition separately. It can be shown that this method is more expressive than \((k-1)-\)WL and less expressive than \(k-\)WL. Also, since we are running \((k-1)-\)WL on a smaller set of vertices, it has better space and time complexity than running \(k-\)WL.
4. **Fragmentation :** For the counting task, based on the pattern \(H\) to be counted, the subgraph \(G_{v}^{r}\) is further decomposed into simpler patterns for which the exact counts of subpatterns are already known. Thus, we need to learn the easier tasks in the subgraphs of \(G_{v}^{r}\). So, a smaller \(k\) is sufficient to count the number of patterns. Using this method, we show that all the patterns appearing as induced subgraphs of size four can be counted using just \(1-\)WL. This technique can be useful for counting larger and more complicated patterns. Thus, instead of training a GNN for the larger subgraph, we can train GNN models for the smaller patterns
for counting and then combine their counts to get the count of the larger subgraph. Using the fragmentation technique, we use the model learned for predicting \(K_{3}\) or a triangle to predict the number of \(K_{4}\) in the graph. Similarly, if we have a model that can predict \(K_{n}\) in a graph, then we can use it to predict \(K_{n+1}\). In other words, we can reduce the counting of \(K_{n+1}\) to a triangle counting problem with a minimal increase in the number of parameters.
5. **Comparison with other GNN models :**Papp and Wattenhofer (2022a) shows an analysis of four GNN models. We do a similar analysis for our models and compare them with the models mentioned in Papp and Wattenhofer (2022a). We show that our models are more expressive than the ones presented in that paper.

Outline of the paper :In Section 2, we introduce some of the terms used throughout the paper. In Section 4, we introduce the localized variants of the \(k-\)WL algorithm and analyze their space and time complexities. In Section 5, we give theorems that characterize the expressiveness of the localized \(k-\)WL variants proposed in our work. In Section 6, we characterize the expressiveness of our methods in terms of subgraph and induced subgraph counting. We also discuss how to count the occurrences of \(H\) in \(G\), using localized algorithms. We discuss the fragmentation approach in Section 7, followed by a theoretical comparison of GNN models in Section 8. The model architecture, along with the parameters used for the experiment, is explained in Section 9 We report the results of our experiments in Section 10 and conclude the paper with Section 11.

## 2 Preliminaries

We consider a simple graph \(G(V,E)\). For basic definitions of graph theory, we refer the reader to West et al. (2001). The neighbourhood of a vertex \(v\) is the set of all vertices adjacent to it in \(G\) (denoted as \(N_{G}(v)\)). The _closed_ neighbourhood of \(v\) is the set of all neighbours, including the vertex \(v\) (denoted as \(N_{G}[v]\)). A graph whose all the vertices are of same degree are called _regular graph_.

A graph \(H\) is called a _subgraph_ of \(G\) if \(V(H)\subseteq V(G)\) and \(E(H)\subseteq E(G)\). The subgraph induced on \(S\subseteq V(G)\) is a graph whose vertex set \(S\) contains all the edges in \(G\) whose endpoints are in \(S\) and is denoted by \(G[S]\). The _induced subgraph_ on a \(r\)-hop neighbourhood around vertex \(v\) is denoted by \(G_{v}^{r}\). Attributed subgraphs are coloured subgraphs, also referred to as Motifs.

The maximum distance from a vertex to all other vertices is called the _eccentricity_ of the vertex. The minimum of the eccentricity over all the vertices is called the _radius_ of the graph. The _center_ of the graph is set of vertices such that eccentricity is minimum. For pattern counting, we pick one of the center vertex and call it _key vertex_.

_Homomorphism_ from graph \(H\) to \(G\) is a function from \(V(H)\) to \(V(G)\) such that if \(\{u,v\}\in E(H)\) then \(\{f(u),f(v)\}\in E(G)\). Given a pattern \(H\), the set of all of its homomorphic images is called _spasm_ of \(H\). Two graphs \(G\) and \(H\) are isomorphic if there exists a bijective function \(f:V(G)\to V(H)\) such that \(\{u,v\}\in E(G)\) if and only if \(\{f(u),f(v)\}\in E(H)\). The _orbit_ of a vertex \(v\) in \(G\) is the set of vertices to which \(v\) can be mapped, and that mapping can be extended to automorphism (denoted by \(Orbit_{G}(v)\)).

We mention some of the structural parameters of graphs. Problems on graph with bounded structural parameters can be solved efficiently on bounded graph parameters.

**Graph Parameters:**

We first define tree decomposition of the graph as:

Given a graph \(G\), we decompose it into tree structures, say \(T,\) where the set of vertices of \(T\) is a subset of set of vertices of \(G\). This decompostion has to satisfy the following constraints:

1. Every vertex of \(G\) must lie in some bag associated with a vertex of \(T\).
2. For each edge \(\{v_{i},v_{j}\}\), there exists a bag containing having \(v_{i}\) and \(v_{j}\).
3. If a vertex \(v_{i}\in V(G)\) belongs to two bags \(B_{i}\) and \(B_{j}\) associated with two vertices \(u_{i}\) and \(u_{j}\) of \(T,\) then \(v_{i}\) must be present in all the bags associated with the vertices belonging to the path connecting \(u_{i}\) and \(u_{j}\) in \(T.\)

The width of the tree decomposition is the maximum size of the bag minus one. The treewidth of the graph \(G,\)\(tw(G)\) is the minimum over all such decompositions. It is NP-hard to compute tree-width
of graphs. However, there exists an efficient algorithm that checks for a fixed \(k\), whether \(tw(G)\) is at most \(k\)Korhonen (2022), Korhonen and Lokshtanov (2022). Graph of bounded treewidth implies sparse graphs. However, for some sparse graph, the treewidth is unbounded. For example, grid graph on \(n\) vertices has treewidth \(\sqrt{n}\). The maximum of the treewidth over all its homomorphic images is called the _hereditary treewidth_ of pattern \(H\), denoted by \(htw(H)\).

_Planar graphs_ are graphs that are \(k_{5}\) and \(k_{3,3}\) minor free. One can also say the graph that can be redrawn such that no edges cross each other are planar graphs. Also, the number of edges can be at most linear in the number of vertices. The _Euler genus_ of a graph is defined in a similar manner. The genus of a graph is the minimum number such that the graph can be drawn on circle without intersecting edges.

Now, we look at graph classes that are dense but have nice structure such as complete graphs. _Clique width_ has been defined for dense graphs. However, there does not exist efficient algorithm to check whether clique width of the given graphs is \(k\) for \(k\geq 4\). Rankwidth has been defined by Robertson and Seymour to handle dense graph classes. Given a graph \(G\), to construct rankwidth decomposition, we define subcubic tree \(T\). Given a bijection from the set of leaves of a tree to the set of vertices of \(G\), we can construct rankwidth decomposition with respect to that bijection. The parent vertices in the tree contains union of vertices belonging to its two children. Note that deletion of single edge disconnects the tree and vertices of graph get partitioned into two subparts say \(X\) and \(V(G)\setminus X\). We define submatrix of adjacency matrix \(A(X,V(G)\setminus X)\) where \(a_{i,j}=1\) if and only if \(i\in X\) and \(j\in V(G)\setminus X\) are adjacent. Also, it has been shown that bounded clique width means bounded rank-width and vice versa Oum (2017).

## 3 Weisfeiler Leman Algorithm

Weisfeiler-Leman (WL) is a well known combinatorial algorithm that has many theoretical and practical applications. Color refinement(\(1-\)WL) was first introduced in 1965 in Morgan (1965). The algorithm goes as follows:

* Initially, color all the vertices as color 1.
* In the next iteration \(i\), we color the vertices by looking at the number of colors of vertices adjacent to each vertex \(v\), in the \((i-1)\)th iteration, as \[C_{i}(v)=(C_{i-1}(v),\{\{C_{i}(w)\}\}_{w\in N_{G}(v)})\] We assign a new color to the vertices, according to the unique tuple it belongs to. This partitions the vertex set in every iteration according to their color classes.
* The algorithm terminates if there is no further partition. We call the color set a _stable_ color set.
* We can also observe that if two vertices get different colors at any stage \(i\), then they will never get the same color in the later iterations. We can observe that the number of iterations is at most \(n\) as a vertex set,\(V(G)\), can be partitioned at most \(n\) many times.
* The color class of any vertex \(v\in V(G)\) can appear at most \(1+\log n\) times and the running time is \(\mathcal{O}(n^{2}\log n)\)Immerman and Sengupta (2019).

In case we need to run only \(h\) iterations and stop before getting the stable color, then the running time is \(O(nh)\).

The same idea was extended by Weisfeiler and Leman in which instead of coloring vertex, they colored all the two tuples based on edge, non-edge and \((v,v)\). In later iteration, the color gets refined for each two tuples based on their neighbourhood and common neighbourhood. This partition the set of two tuples of vertices. The iteration in which no further partition is being done are called _stable coloring_. Weisfeiler Leman algorithm which is known as \(2-\)WL algorithm.

Similar approach was extended later for coloring \(k\)-tuples and then do refinement of coloring in later iterations.

**Definition 1**.: _Let \(\vec{x}=(x_{1},...,x_{k})\in V^{k},y\in V\), and \(1\leq j\leq k\). Then, let \(x[j,y]\in V^{k}\) denote the \(k\)-tuple obtained from \(x\) by replacing \(x_{j}\) by \(y\). The \(k\)-tuples \(\vec{x}[j,y]\) and \(\vec{x}\) are said to be \(j\)-neighbors for any \(y\in V\). We also say \(\vec{x}[j,y]\) is the \(j\)-neighbor of \(\vec{x}\) corresponding to \(y\)._
* Color all the \(k\)-tuple vertices according to their isomorphic type. Formally, \((v_{1},v_{2},....,v_{k})\) and \((w_{1},w_{2},....,w_{k})\) get the same color if \(v_{i}=v_{j}\) then \(w_{i}=w_{j}\) and also, if \((v_{i},v_{j})\in E(G),\) then \((w_{i},w_{j})\in E(G).\)
* In every iteration, the algorithm updates the color of the tuple after seeing the color of its adjacent \(k\) tuple vertices. \[C_{i+1}^{k}(\vec{v}):=(C_{i}^{k}(\vec{v},M_{i}(\vec{v})\] where \(M_{i}(\vec{v})\) is the multiset \[\{\{(C_{i}^{k}(v_{1},v_{2},...,v_{k1},w),...,C_{i}^{k}(v_{1},v_{2},..,w,..,v_{ k}),...,C_{i}^{k}(w,v_{2},...,v_{k}))\mid w\in V\}\}\]
* The algorithm terminates if there is no further partition. We call the color set a stable color set.
* We also observe that if two tuples get different colors at any stage \(i\), then they will never get the same color in the later iterations. We can observe that the number of iterations is at most \(n^{k}\) as \(V^{k}\) can be partitioned at most \(n^{k}\) many times.
* The color class of any vertex \(\vec{v}\in V^{k}\) can appear at most \(\mathcal{O}(k\log n)\) times and running time is \(\mathcal{O}(k^{2}n^{k+1}\log n)\) Immerman and Sengupta (2019).

Two graphs \(G\) and \(H\) are said to be \(k-\)WL equivalent (\(G\simeq_{k}H\)), if their color histogram of the stable colors matches. We say that \(G\) is \(k-\)WL identifiable if there doesn't exist any non-isomorphic graphs that are \(k-\)WL equivalent to \(G\).

Color refinement (\(1-\)WL) can recognise almost all graphs Babai et al. (1980), while \(2-\)WL can recognise almost all regular graphs Bollobas (1982). The power of \(WL\) increases with an increase in the value of \(k\). The power of \(k-\)WL to distinguish two given graphs is same as with counting logic \(C^{k+1}\) with \((k+1)\)-variable. Also, the power of \(k-\)WL to distinguish two non-isomorphic graphs is equivalent to spoiler's winning condition in \((k+1)\)-bijective pebble game. Recently, Dell et al. (2018) has shown that the expressive power of \(k-\)WL is captured by homomorphism count. It has been shown that \(G_{1}\simeq_{k}G_{2}\) if and only if \(Hom(T,G_{1})=Hom(T,G_{2}),\) for all graphs \(T\) of treewidth at most \(k\).

The graphs that are identified by \(1-\)WL are _Amenable_ graphs. There is a complete characterization of the amenable graphs in Arvind et al. (2017); Kiefer et al. (2015). In the original algorithm, we initially color all the vertices with color \(1\). However, if we are given a colored graph as input, we start with the given colors as the initial colors. Also, we can color the edges, and run \(1-\)WL Kiefer et al. (2015).

Even if \(k-WL\) may not distinguish two non-isomorphic graphs, two \(k-WL\) equivalent graphs have many invariant properties. It is well known that two \(1-WL\) equivalent graphs have the same maximum eigenvalue. Two graphs that are \(2-WL\) equivalent are co-spectral and have the same diameter. Recently, V. Arvind et al. have shown the invariance in terms of subgraph existence and counts Arvind et al. (2020). They show the complete characterization of subgraphs whose count and existence are invariant for \(1-WL\) equivalent graph pairs. They also listed down matching, cycles and path count invariance for \(2-WL\). Also, there is a relation between homomorphism count and subgraph count Curticapean et al. (2017). The count of subgraphs is a function of the number of homomorphism from set of all homomorphic image of patterns. _Hereditary treewidth_ of graph is defined as maximum of treewidth over all homomorphic images. So, if two graphs are \(k-WL\) equivalent, then the count of all subgraphs whose \(htw(G)\) is almost \(k\) are same. However, running \(k-WL\) takes \(O(k^{2}\cdot n^{k+1}logn)\) time and \(O(n^{k})\) space Immerman and Sengupta (2019). So, it is not practically feasible to run \(k-WL\), for large \(k\), for graphs.

The expressive power of \(k-\)WL is equivalent to first order logic on \((k+1)\) variables with a counting quantifier. Let \(G=(V,E)\), where \(V\) is a set of vertices and \(E\) is a set of edges. In logic, we define \(V\) as the universe and \(E\) as a binary relation. In Cai et al. (1992), they have proved that the power to distinguish two non-isomorphic graphs using \(k-\)WL is equivalent to \(C^{k+1}\), where \(C^{k+1}\) represents first order logic on \((k+1)\) variables with counting quantifiers (stated in Theorem 1). To prove this, they define a bijective \(k\)-pebble game, whose power is equivalent to \(C^{k}\).
#### Bijective k-Pebble Game

The bijective k-Pebble game (\(BP_{k}(G,H)\)) has been discussed in Kiefer (2020); Cai et al. (1992); Grohe and Neuen (2021). Let graphs \(G\) and \(H\) have the same number of vertices and \(k\in\mathbb{N}\). Let \(v_{i},v\in V(G)\) and \(w_{i},w\in V(H)\).

**Definition 2**.: _The position of the game in bijective pebble game is the tuples of the vertices where the pebbles are placed._

The bijective \(k\)-pebble game is defined as follows:

1. Spoiler and Duplicator are two players.
2. Initially, no pebbles are placed on the graphs. So, the position of the game is ((),()) (the pairs of empty tuples.)
3. The game proceeds in the following rounds as follows: 1. Let the position of the game after the \(i^{th}\) round be \(((v_{1},...,v_{l}),(w_{1},w_{2},...,w_{l}))\). Now, the Spoiler has two options: either to play a pebble or remove a pebble. If the Spoiler wants to remove a pebble, then the number of pebbles on the graph must be at least one and if Spoiler decides to play a pebble then number of pebbles on that particular graph must be less than \(k\). 2. If the Spoiler wants to remove a pebble from \(v_{i},\) then the current position of the game will be \(((v_{1},v_{2},...v_{i-1},v_{i+1},..,v_{l}),(w_{1},w_{2},...w_{i-1},w_{i+1},..,w _{l}))\). Note that, in this round, the Duplicator has no role to play. 3. If the Spoiler wants to play a pair of pebbles, then the Duplicator has to propose a bijection \(f:V(G)\to V(H)\) that preserves the previous pebbled vertices. Later, the Spoiler chooses \(v\in V(G)\) and sets \(w=f(v)\). The new position of the game is \(((v_{1},...v_{l},v),(w_{1},w_{2},...,w_{l},w))\).

The Spoiler wins the game if for the current position \(((v_{1},...v_{l},v),(w_{1},w_{2},...,w_{l},w)),\) the induced graphs are not isomorphic. If the game never ends, then the Duplicator wins. The equivalence between the bijective \(k\)-pebble game and \(k-\)WL was shown in the following theorem.

**Theorem 1**.: _Cai et al. (1992)_ _Let \(G\) and \(H\) be two graphs. Then \(G\simeq_{k}H\) if and only if the Duplicator wins the pebble game \(BP_{k+1}(G,H)\)._

A stronger result, namely, the equivalence between the number of rounds in the bijective \((k+1)\)-pebble game and the iteration number of \(k-\)WL was stated in the following theorem.

**Theorem 2**.: _Kiefer (2020)_ _Let \(G\) and \(H\) be graphs of same size. The vertices may or may not be colored. Let \(\vec{u}:=(u_{1},...,u_{k})\in(V(G))^{k}\) and \(\vec{v}:=(v_{1},...,v_{k})\in(V(H))^{k}\) be any two arbitrary elements. Then, for all \(i\in\mathbb{N}\), the following are equivalent :_

1. _The color of_ \(\vec{u}\) _is same as the color of_ \(\vec{v}\) _after running_ \(i\) _iterations of_ \(k-\)_WL._
2. _For every counting logic formulae with_ \((k+1)\) _variables of quantifier depth at most_ \(i\)_,_ \(G\) _holds the formula if and only if_ \(H\) _does so._
3. _Spoiler does not win the game_ \(BP_{k+1}(G,H)\) _with the initial configuration_ \((\vec{u},\vec{v})\) _after at most_ \(i\) _moves._

## 4 Local k-WL based Algorithms for GNNs

In this section, we present the local \(k-\)WL based algorithms for GNNs. We also give runtime and space requirements for such GNNs.

### Local k-WL

Given a graph \(G\), we extract the subgraph induced on a \(r\)-hop neighbourhood around every vertex. We refer to it as \(G^{r}_{v}\), for the subgraph rooted at vertex \(v\) in \(G\). Then, we colour the vertices in \(G^{r}_{v}\) according to their distances from \(v\). Now, we run \(k-\)WL on the coloured subgraph \(G^{r}_{v}\). The stable
colour obtained after running \(k-\)WL is taken as the attributes of vertex \(v\). Then, we run a GNN on the graph \(G\) with the attributes on each vertex \(v\). This is described in Algorithm 1.

```
1:Input: \(G,r,k\)
2:for each vertex \(v\) in \(V(G)\)do
3: Find the subgraph induced on the \(r\)-hop neighborhood rooted at vertex \(v\) (\(G_{v}^{r}\)).
4: Color the vertices whose distance from \(v\) is \(i\), by color \(i\).
5: Run \(k-\)WL on the colored graph until the colors stabilize.
6:endfor
7:Each vertex has as an attribute the stable coloring of vertex \(v\) obtained from \(G_{v}^{r}\).
8:Run GNN on the graph \(G\) with each vertex having attributes as computed above.
```

**Algorithm 1** Local k-WL

Runtime and Space requirement Analysis :The time required to run \(k-\)WL on \(n\) vertices is \(O(n^{k+1}\log(n))\). Here, we run \(k-\)WL on a \(r\)-hop neighborhood instead of the entire graph. So, \(n\) is replaced by \(n_{1}\), where \(n_{1}\) is the size of the neighborhood. If a graph has bounded degree \(d\), and we run \(k-\)WL for a \(2\)-hop neighborhood, then \(n_{1}\) is \(O(d^{2})\). Also, we have to run Local \(k-\)WL for each vertex. Hence, the total time required is \(O(n\cdot d^{2k+2}\log(d))\). Also, running a traditional GNN takes time \(O((n+m)\log n),\) where \(m\) is the number of edges. So, if we assume that \(d\) is bounded, then the time required is linear in the size of the graph. Furthermore, the space required to run \(k-\)WL on \(n\) vertices graph is \(O(n^{k})\). Hence, for Local \(k-\)WL, it follows that the space requirement is \(O(n_{1}^{k})\).

### Layer k-WL

In order to make Local \(k-\)WL more time and space efficient, while maintaining the same expressive power, we propose a modification to Local \(k-\)WL. Instead of running \(k-\)WL on the entire \(r\)-hop neighbourhood, we run \(k-\)WL on consecutive layers of \(G_{v}^{r}\) (i.e., run \(k-\)WL on the set of vertices with colour \(i\) and colour \((i+1)\)). Initially, we run \(k-\)WL on the set of vertices that are at a distance of \(1\) and \(2\) from \(v\). Then, we run \(k-\)WL on the set of vertices with colors \(2\) and \(3\), and so on. While running \(k-\)WL, initially, it partitions the \(k\)-tuples based on the isomorphism type. However, in this setting, we incorporate the stabilized colouring obtained in the previous round. For \(l<k\), we define the color of \(l\) tuples as \(col(u_{1},u_{2},...,u_{l}):=col(u_{1},u_{2},...,u_{l},\underbrace{u_{1},u_{1} \ldots,u_{l}}_{(k-l)\text{lines}})\). Consider the mixed tuple (we call a tuple to be mixed if some of the vertices have been processed in the previous iteration and the remaining have not yet been processed) \((u_{1},v_{1},\ldots,u_{k})\) where \(col(u_{j})=i\) and \(col(v_{j})=i+1\) (i.e \(u_{i}^{\prime}\)s are the set of processed vertices and \(v_{i}^{\prime}\)s are yet to be processed). So, even if \((u_{1},v_{1},\ldots,u_{k})\) and \((u_{1}^{{}^{\prime}},v_{1}^{{}^{\prime}},\ldots,u_{k}^{{}^{\prime}})\) may be isomorphic, if \(col(u_{1},u_{2},\ldots u_{l})\neq col(u_{1}^{{}^{\prime}},u_{2}^{{}^{\prime}}, \ldots u_{l}^{{}^{\prime}})\) then \(col(u_{1},v_{1},\ldots,u_{k})\neq col(u_{1}^{{}^{\prime}},v_{1}^{{}^{\prime}}, \ldots,u_{k}^{{}^{\prime}})\). The algorithm is described in Algorithm 2. A GNN model incorporating Local+Layer \(k-\)WL is equivalent to running Layer \(k-\)WL in line 5 in Algorithm 1.

```
1:Given \(G_{v}^{r},k\).
2:Run \(k-\)WL on the induced subgraph of levels \(1\) and \(2\).
3:for each layer \(i\) of BFS(\(v\)), \(i\geq 2\)do
4: Initial colour of \(k-tuple\) incorporates the stabilized colour obtained from the previous iteration.
5: Run \(k-\)WL on the subgraph induced on the vertices in layer \(i\) and \((i+1)\)
6:endfor
```

**Algorithm 2** Layer k-WL(\(v\))

Runtime and Space requirement Analysis.

The running time and space requirement for Layer \(k-\)WL depends on the maximum number of vertices in any two consecutive layers, say \(n_{2}\). The time required to run \(k-\)WL is \(O(r\cdot(n_{2})^{k+1}\log(n_{2}))\). However, running only Local \(k-\)WL will require \(O((r\cdot n_{2})^{k+1}\log(r\cdot n_{2}))\) time. The space requirement is \(O(n_{2}^{k})\). Hence, running Layer \(k-\)WL is more efficient than running Local \(k-\)WL, especially when \(r\) is large.
### Recursive WL

Here, we present another variant of WL. The central idea is to decompose the graphs initially by running \(1-\text{WL}\). Then, further, decompose the graphs by running \(2-\text{WL}\) and so on. One can note that the final vertex partition that \(1-\text{WL}\) outputs after color refinement are regular if we restrict to a single color class. In other words, let \(G[X]\) be the induced graph on the vertices of same color. Then, \(G[X]\) is regular. Also, \(G[X,Y]\) where \(X\) and \(Y\) are sets of vertices of two different color classes. \(G[X,Y]\) is also a bi-regular graph. We run \(2-\text{WL}\) on the regular graph. Using Bollobas [1982], we can guarantee that it would distinguish almost all regular graphs. Similarly, running \(2-\text{WL}\) on \(G[X,Y]\) is bi-regular and thus can be distinguished by \(2-\text{WL}\). We again run \(1-\text{WL}\) on \(G\), using the colors obtained after running \(2-\text{WL}\). This further refines the colors of the vertices in \(G\). One can easily check that it is more expressive than \(1-\text{WL}\) and less expressive than \(2-\text{WL}\). We give the graph Figure 1 that can not be distinguished by Recursive \((1,2)-\text{WL}\) and \(1-\text{WL}\) but can be distinguished by \(2-\text{WL}\). This gives an intermediate hierarchy in the \(k-\text{WL}\) hierarchy. Also, the space and time required for running \(2-\text{WL}\) on the entire graph is more than that of Recursive \((1,2)-\text{WL}\). The running time and space required depend on the partition size obtained after running \(1-\text{WL}\).

Note that the color of vertex \(v\) is \(col(v,v)\) after running \(2-\text{WL}\).

```
1:Given \(G\)
2:Run \(1-\text{WL}\) and get the partition of vertices into colour classes.
3:Let \(S=\{C_{1},C_{2},\ldots C_{l}\}\) be the color classes obtained after running \(1-\text{WL}\).
4:for each color class \(C_{i}\) in \(S\)do
5:Run \(2-\text{WL}\) on the induced subgraph in \(C_{i}\) and get color partition.
6:Let \(C_{i}\) get partitioned into \(C_{i,1},C_{i,2},\ldots,C_{i,l}\)
7:endfor
8:Run \(1-\text{WL}\) on the colored graph \(G\) whose colors are given by steps 5 and 6.
9:for each new color class \(C^{\prime}_{i}\) and \(C^{\prime}_{j}\)do
10:Run \(2-\text{WL}\) on the induced subgraph on the vertices in color partitions \(C^{\prime}_{i}\) and \(C^{\prime}_{j}\) and get new color partition.
11:endfor
12:Repeat 5-11 till the colour stabilizes.
```

**Algorithm 3** Recursive(1,2) WL

This idea can be generalized for any suitable \(k\). We can run a smaller dimensional \(k_{1}-\text{WL}\) and then use the partition of \(k_{1}\) tuple vertices. Later, we can use this partition to get a finer partition of \(k_{2}\) tuples. Assuming \(k_{1}<k_{2}\), one can see that we have to run \(k_{2}-\text{WL}\) on smaller graphs. This reduces the time and space required for running \(k_{2}-\text{WL}\) on the entire graph. One can easily see that it is less expressive than \(k_{2}-\text{WL}\), however, more expressive than \(k_{1}-\text{WL}\).

More specifically, initially run \(1-\text{WL}\) and further run \((k-1)-\text{WL}\) on the colored classes. One can check that it is more expressive than \((k-1)-\text{WL}\) and less expressive than \(k-\text{WL}\).

Figure 1: Graph identifiable by 2-WL but not by Recursive \(1-\text{WL}\)
Theoretical Guarante of Expressive Power

In this section, we theoretically prove the expressive power of GNN models that we proposed in Section 4 in terms of graph and subgraph isomorphism. In the discussion below, we say that a GNN model \(A\) is at most as expressive as a GNN model \(B\) if any pair of non-isomorphic graphs \(G\) and \(H\) that can be distinguished by \(A\) can also be distinguished by \(B\). Also, we say a GNN model \(A\) is at least as expressive as a GNN model \(B\) if \(A\) can identify all the non-isomorphic graph pairs that can be identified by GNN model \(B\). The proof of the theorem and lemmas presented in the section mainly use pebble bijective game. Also, as mentioned earlier, there is equivalence between the expressivity of \(k-\)WL and \((k+1)\)-pebble bijective game.

### Local k-WL

It has been shown in recent works that running Local \(1-\)WL has more expressive power as compared to running \(1-\)WL. The upper bound on the expressive power of Local \(1-\)WL has been shown in Frasca et al. (2022). However, the expressive power of Local \(k-\)WL, for arbitrary \(k\), has not been studied. In the Theorem 3, we give a description of the expressive power of Local \(k-\)WL and show that it has more expressive power than \(k-\)WL. We also show that it is less expressive than \((k+1)-\)WL. The proof techniques that we used are different from Frasca et al. (2022).

**Theorem 3**.: _Running Local \(k-\)WL is more expressive than running \(k-\)WL on the entire graph. Also, running Local \(k-\)WL is at most as expressive as running \((k+1)-\)WL on the entire graph._

Proof.: Let \(G_{1}\) and \(G_{2}\) be two graphs distinguishable by \(k-\)WL. So, the Spoiler has a winning strategy in the game (\(G_{1}\),\(G_{2}\)). Suppose \(G_{1}\) and \(G_{2}\) do not get distinguished after running \(k-\)WL locally. That means the Duplicator has a winning strategy for all vertices individualized. Let \(v\) in \(G_{1}\) and \(u\) in \(G_{2}\) be the vertices that are individualized.

We play the \((k+1)\)-bijective pebble game on the entire graphs \((G_{1},G_{2})\) and the local subgraphs \((G_{1}^{v},G_{2}^{v})\) simultaneously. Let \(S_{1}\) and \(D_{1}\) be the spoiler and duplicator in game \((G_{1},G_{2})\) respectively, and \(S_{2}\) and \(D_{2}\) be the spoiler and duplicator in game \((G_{1}^{v},G_{2}^{u})\). We use the strategy of \(D_{2}\) to determine the move for \(D_{1}\) and the strategy of \(S_{1}\) to determine the move for \(S_{2}\).

Initially, \(D_{2}\) gives a bijection \(f\) from the vertex set of \(G_{1}^{v}\) to \(G_{2}^{u}\). We propose the same bijection \(f\) by \(D_{1}\), extending it by mapping \(v\) to \(u\). Now, the spoiler \(S_{1}\) places a pebble at some vertex \((v_{i},f(v_{i}))\). The spoiler \(S_{2}\) also places a pebble at vertex \((v_{i},f(v_{i}))\). We can show using induction on the number of rounds that if \(S_{1}\) wins the game, then \(S_{2}\) also wins the game. Our induction hypothesis is that \(S_{1}\) has not won till the \(j^{th}\) round and the positions of both the games are same. Let the current position of both the games after the \(j^{th}\) round be \(((v_{1},v_{2},\ldots,v_{l}),(f(v_{1}),f(v_{2}),\ldots,f(v_{l}))\). Now, \(S_{1}\) either decides to play a pair of pebbles or remove.

_Case(1): If \(S_{1}\) decides to remove a pebble._ In this case, the Duplicator \(D_{1}\) has done nothing to do. \(S_{2}\) will copy the same strategy as \(S_{1}\). Here, \(S_{1}\) cannot win in this round. Also, note that the positions of both games are the same.

_Case(2): If \(S_{1}\) decides to play a pebble._ In this case, \(S_{2}\) also decides to play a pebble. Duplicator \(D_{2}\) proposes a bijective function \(f\). The same bijective function is proposed by \(D_{1}\). Now, \(S_{1}\) places a pebble at \((v,f(v))\). \(S_{2}\) also chooses the same vertices. So, the position of both the game is the same. Therefore, if \(S_{1}\) wins the game, then \(S_{2}\) also wins the game. Thus, running \(k-\)WL locally is at least as expressive as running \(k-\)WL on the entire graph.

We can show that it is more expressive by looking at the simple example that running \(1-\)WL on a local substructure can count the number of triangles, whereas running \(1-\)WL on an entire graph does not recognize graphs having different triangle counts. Also, one can observe that running \(k-\)WL locally is running \(k-\)WL on the colored graph where vertices at distinct distances get distinct colors. Its power is the same as individualizing one vertex and running \(k-\)WL. Thus, running \(k-\)WL locally is more expressive than running \(k-\)WL on the entire graph.

Let \(G_{1}\) and \(G_{2}\) be two graphs that can be distinguished by running \(k-\)WL locally. Recall that, the key vertices refer to \(u\) and \(v\) in \(G_{1}\) and \(G_{2}\) such that they are the root vertices corresponding to \(G_{1}\) and \(G_{2}\), respectively. This means the Spoiler has a winning strategy in the \((k+1)\) bijective pebble game, where the key vertices are matched to each other. Now, we use the strategy of the Spoiler in the local substructure to get a winning strategy for the Spoiler in the entire graph. At first, when
the Duplicator gives a bijective function, the Spoiler places a pebble on the paired vertices. For the remaining moves, we copy the strategy of the Spoiler in the local structure, and the Duplicator's strategy of the entire graph is copied to the Duplicator's strategy of the local structures. Thus, if the Spoiler has a winning strategy in the local substructure, then the Spoiler wins the \((k+2)-\) bijective pebble game on entire graphs. 

### Layer k-Wl

We presented an algorithm (Algorithm 2) for applying \(k-\)WL to consecutive layers in a \(r\)-hop subgraph for a vertex \(v\in V.\) This improves the time and space efficiency of the Local \(k-\)WL method as we have discussed above. We now describe the expressive power of Layer \(k-\)WL. In the following lemmas, we show that the expressive power of Layer \(k-\)WL is the same as that of Local \(k-\)WL.

Lemma 1.: _Running \(k-\)WL on the entire \(r\)-hop neighbourhood is at least as expressive as running Layer \(k-\)WL._

Proof.: Let \(G\) and \(H\) be the subgraphs induced on the \(r\)-hop neighborhood. Let \((S,D)\) be the Spoiler-Duplicator pair for the game \((G,H)\). Similarly, let \((S_{i},D_{i})\) be the Spoiler-Duplicator pair for the game \((G_{i},H_{i})\), where \(G_{i}\) and \(H_{i}\) are the subgraphs induced on the vertices at the \(i\)th and \((i+1)\)th layers of \(G\) and \(H\), respectively. We claim that if any of the \(S_{i}^{\prime}\)s has a winning strategy in the game \((G_{i},H_{i})\), then \(S\) has a winning strategy in the game \((G,H)\). Here, the strategy of \(D\) is copied by \(D_{i}\), and the strategy of \(S_{i}\) is copied by \(S\). We prove this using induction on the number of rounds of the game. Our induction hypothesis is that the positions of both the games are same, and if \(S_{i}\) wins after \(t\) rounds, then \(S\) also wins after \(t\) rounds.

_Base case:_\(D\) proposes a bijective function \(f:V(G)\to V(H)\). Note that the bijection must be color-preserving; otherwise, \(S\) wins in the first round. Thus, we can assume that \(f\) is color-preserving. So, \(D_{i}\) proposes the restricted function \(f_{i}\) as a bijective function from \(V(G_{i})\) to \(V(H_{i})\). Now, \(\vec{S_{i}}\) plays a pair of pebbles in \((G_{i},H_{i})\), and \(S\) also plays the same pair of pebbles in the game \((G,H)\). It is easy to see that both games' positions are the same. Also, if \(S_{i}\) wins, then the number of vertices of a particular color is different. Hence, \(S\) also has a winning strategy.

By the induction hypothesis, assume that after the \(t^{th}\) round \(S_{i}\) did not win and the position of the game is the same for both games.

Consider the \((t+1)^{th}\) round in both games. \(S_{i}\) either chooses to play or remove a pebble. If \(S_{i}\) chooses to remove a pebble, so will \(S\). Again, the position of both the games is same. Now, if \(S_{i}\) decides to play a pair of pebbles, then \(S\) also decides to play a pair of pebbles. So, \(D\) proposes a bijective function and \(D_{i}\) proposes a restricted bijective function. Now, if \(S_{i}\) plays a pair of pebbles at \((v,f_{i}(v))\), then \(S\) also decides to play a pair of pebbles at \((v,f(v))\). Thus, the position of the game is same in both of the games. This ensures that if \(S_{i}\) has won, then \(S\) also wins.

Lemma 2.: _Running Layer \(k-\)WL is as expressive as running \(k-\)WL on the entire induced subgraph._

Proof.: Let \(G\) and \(H\) be the subgraphs induced on a \(r\)-hop neighborhood. Let \((S,D)\) be the Spoiler-Duplicator pair for the game \((G,H)\). Similarly, let \((S_{i},D_{i})\) be the Spoiler-Duplicator pair for the game \((G_{i},H_{i})\), where \(G_{i}\) and \(H_{i}\) are the subgraphs induced on the vertices at the \(i\)th and \((i+1)\)th layers of \(G\) and \(H\), respectively. We claim that if \(S\) has a winning strategy in the game \((G,H)\), then there exists \(S_{i}\) such that it has a winning strategy in the game \((G_{i},H_{i})\). Here, the strategy of \(D\) is copied by \(D_{i}\) and the strategy of \(S_{i}\) is copied by \(S\). We prove the lemma using induction on the number of rounds of the game. Our induction hypothesis is that the position of the game \((G,H)\) is same for \((G_{i},H_{i})\), for all \(i\), if we restrict it to the subgraph induced by the vertices of color \(i\) and \((i+1)\). Also, if \(S\) wins after round \(t\), then there exists \(S_{i}\) that wins after \(t\) rounds.

_Base case:_\(D\) proposes a bijective function \(f:V(G)\longrightarrow V(H)\). Note that the bijection must be color-preserving; otherwise \(S\) wins in the first round. Thus, we can assume that \(f\) is color-preserving. So, \(D_{i}\) proposes the restricted function \(f_{i}\) as a bijective function from \(V(G_{i})\) to \(V(H_{i}),\forall i\in[r]\). Now, \(S\) will play a pair of pebbles in the game \((G,H)\). Suppose \(S\) plays the pebbles at \((v,f(v))\) and \(color(v)=i\), then \(S_{i}\) and \(S_{i-1}\) play pebbles at \((v,f_{i}(v))\) in their first round. It is easy to see that the position of the games \((G,H)\) and \((G_{i},H_{i})\), for all \(i\in[r]\), is same if we restrict it to the subgraph
induced by vertices of colors \(i\) and \((i+1)\). Also, if \(S\) wins, then the number of vertices of particular color are not same. So, there exists some \(i\), such that \(S_{i}\) also has a winning strategy.

By induction hypothesis, assume that after the \(t^{th}\) round, \(S\) did not win and position of the game is same as defined.

Consider the \((t+1)^{th}\) round in both the games. \(S\) either chooses to play or remove a pebble. If \(S\) chooses to remove a pebble from vertex \((v,f(v))\), then, if \(v\) is colored with color \(i\), then \(S_{i}\) and \(S_{i-1}\) will remove a pebble from vertex \((v,f_{i}(v))\). Again, the position of both the games is same. Now, if \(S\) decides to play a pair of pebbles, then each \(S_{i}\) also decides to play a pair of pebbles. So, \(D\) propose a bijective function and \(D_{i}\) proposes a restricted bijective function. Now, suppose \(S\) plays a pair of pebbles at \((v_{1},f(v_{1}))\). If \(color(v_{1})=i\), then \(S_{i}\) and \(S_{i-1}\) also decides to play pebbles at \((v_{1},f_{i}(v_{1}))\). Thus, the position of the game is same as defined. Now, if \(S\) wins, then there exists \(u\) and \(v\) such that either \((u,v)\in E(G)\) and \((f(u),f(v))\notin E(H)\) or \((u,v)\notin E(G)\) and \((f(u),f(v))\in E(H)\). Similarly, there exists \(S_{i}\) for which these things happen as the position induced is same. Therefore, \(S_{i}\) wins for some \(i\). 

Thus, from above two lemmas we can say that the expressive power of Layer \(k-\)WL is the same as local \(k-\)WL.

## 6 Subgraph Counting Algorithms and Characterization of Patterns

Here, we characterize the expressive power of the proposed methods in terms of subgraph as well as induced subgraph counting. In this section, we provide algorithms and characterization of subgraphs that can exactly count the number of patterns appearing as subgraph or induced subgraph. As described above, we can see that the running time is dependent on the size of the local substructure and the value of \(k\). The size of the subgraph is dependent on the radius of the patterns. So, we have to take a \(r\)-hop neighbourhood for each vertex \(v\) in the host graph \(G\).

In Section 6.1, we show how the value of \(k\) can be decided based on the local substructure of the host graph. It is independent of the structure of the pattern. Also, it gives an upper bound on the value of \(k\) that can count pattern appearing as subgraph and induced subgraph. In Section 6.2, we first show that doing local count is sufficient for induced subgraph count and later we give the upper bound on \(k\) based on the pattern size. Note that the value of \(k\) for induced subgraph count is based only on the size of pattern not its structure. In Section 6.3, we again show that locally counting subgraph is sufficient. Also, we explore the value of \(k\) based on the structure of pattern. For subgraph counting the structure of pattern can be explored to get a better upperbound for the value of \(k\). Later, for the sake of completeness we give algorithm to count triangle, pattern of radius one and \(r\).

### Deciding k based on local substructure of host graph

Here, we explore the local substructure of the host graph in which we are counting pattern appearing as graphs and subgraphs. For a given pattern of radius \(r\), we explore \(r\)-hop neighbourhood around every vertex \(v\) in the host graph \(G\). If two graphs \(G_{1}\) and \(G_{2}\) are isomorphic then the number of subgraphs and induced subgraphs of both the graphs are the same. We use the same idea to count number of subgraphs.

Cai et. al. [1992], shown that \(\Omega(n)\) dimension is needed to guarantee graph isomorphism. However, for restricted graph classes, we can still guarantee isomorphism for small dimensions. It has been shown that \(3-WL\) is sufficient for planar graph Kiefer et al. [2019], \(k-WL\) for graphs with treewidth at most \(k\)Kiefer and Neuen [2019], \((3k+4)-WL\) for graphs with rankwidth at most \(k\)Grohe and Neuen [2021], and \((4k+3)\) for graphs with Euler genus at most \(k\)Grohe and Kiefer [2019]. We say these graph classes as _good_ graph classes. Note that, for non-isomorphic graphs, the graphs is not \(k-\)WL equivalent. Thus, running corresponding \(k-\)WL can count pattern of radius \(r\), appearing as subgraph and induced subgraph.

**Theorem 4**.: _Let \(G_{v}^{r}\) denote the \(r\)-hop neighborhood around \(v\). Given a pattern of radius \(r\), the values of \(k\) that are sufficient to guarantee the count of patterns appearing either as subgraphs or induced subgraphs are:_

1. \((3-WL)\) _if_ \(G_{v}^{r}\) _planar_
2. \((k-WL)\) _if_ \(tw^{1}(G_{v}^{r})\leq k\)__
3. \(((3k+4)-WL)\) _if_ \(rankwidth(G_{v}^{r})\leq k\)__
4. \(((4k+3)-WL)\) _if_ \(Euler-genus(G_{v}^{r})\leq k\)__

Proof.: Consider the subgraph induced by the vertex \(v\) and its \(r\)-hop neighborhood in \(G\), say \(G_{v}^{r}\), and the subgraph induced by the vertex \(u\) and its \(r\)-hop neighborhood in \(H,\) say \(H_{u}^{r}.\) Suppose both structures belong to _good_ graph classes. Now, we run corresponding \(k\) based on the local substructure as mentioned in the theorem. If the color histogram of the stable color matches for both the graphs. This implies that both the graphs are isomorphic. Thus, the number of subgraphs and induced subgraphs in both of the substructures are also the same.

Also, we run respective \(k-\)WL on a colored graph, where vertices at a distance \(i\) from \(v\) is colored \(i\). So, it is at least as expressive as running \(k-\)WL on an uncolored graph. We can also show that it is strictly more expressive in distinguishing non-isomorphic graphs. Thus, all the \(k-\)WL mentioned corresponding to _good_ graph classes are sufficient for counting the number of patterns appearing as subgraphs and induced subgraphs. 

**Corollary 1**.: _If \(G_{v}^{r}\) is amenable, for all \(v\in V(G),\) then Local \(1-\)WL outputs the exact count of the patterns appearing as subgraph and induced subgraph._

**Corollary 2**.: _Running \(1-\)WL guarantees the exact number of subgraphs and induced subgraphs of all patterns of radius one, when the maximum degree of the host graph is bounded by 5._

_Similarly, if the maximum degree of the host graph is bounded by \(15\), then running \(2-\)WL is sufficient to count subgraphs and induced subgraphs of all patterns with a dominating vertex._

### Counting Induced Subgraphs

The following lemma shows that we can easily aggregate the local counts of the pattern \(H\) appearing as an induced subgraph to get the count of \(H\) over the entire graph.

**Lemma 3**.: \[IndCount(H,G)=\frac{\sum_{v\in V(G)}IndCount_{(u,v)}(H,G_{v}^{r})}{| Orbit_{H}(u)|}\] (1)

Proof.: Suppose a pattern \(H\) in \(G\) appears as an induced subgraph. So, an injective homomorphism from \(V(H)\) to \(V(G)\) exists, such that it preserves the edges. We fix one subgraph and find the number of mappings possible. Suppose one of the mappings maps \(u_{i}\) to \(v_{j}\) where \(j\in|V(H)|\). Now, we can see that the number of mappings of \(u_{i}\)(key vertex) is the same as the size of the orbit of \(u_{i}\) in \(H.\) This proves the claim that every induced subgraph has been counted, the size of the orbit many times. 

Assume that we want to count the number of occurrences of pattern \(H\) in \(G\) as (an induced) subgraph. Let \(u\) be the key vertex of \(H\) and \(r\) be the radius of \(H\).

**Lemma 4**.: _It is sufficient to look at the \(r-\)hop neighborhood of \(v_{i}\) to compute \(Count_{(u,v)}(H,G)\) or \(IndCount_{(u,v)}(H,G)\)._

Proof.: Suppose a subgraph exists in \(G\) that is isomorphic to \(H\) or isomorphic to some supergraph of \(H\) with an equal number of vertices, where \(u\) is mapped to \(v_{i}\). The homomorphism between the graphs preserves edge relations. Consider the shortest path between \(u\) and any arbitrary vertex \(u_{i}\) in \(H\). The edges are preserved in the homomorphic image of \(H\). Therefore, the shortest distance from \(f(u)\) to any vertex \(f(u_{i})\) in \(G\) is less than equal to \(r\). So, it is sufficient to look at the \(r\)-hop neighborhood of \(v_{i}\) in \(G\). 

From the above two lemmas, we can conclude the following theorem:

**Theorem 5**.: _It is sufficient to compute \(IndCount_{(u,v)}(H,G_{v}^{r}),\) for \(i\in[n],\) where \(G_{v}^{r}\) is induced subgraph of \(r-\)hop neighborhood vector of \(v.\)_
The following theorem gives a direct comparison with the \(S_{k}\) model Papp and Wattenhofer (2022), where each node has an attribute which is the count of induced subgraphs of size at most \(k\).

**Theorem 6**.: _Local \(k-\)WL can count all induced subgraphs of size at most \((k+2)\)._

Proof.: Suppose, if possible \(G\) and \(H\) are Local \(k-\)WL equivalent and \(|P|\leq(k+2),\) where \(P\) is the pattern to be counted. We will choose one vertex \(v\) as a key vertex. Now, we want to count \(P-v\) locally. Assume that the distance from \(v\) to any vertex is \({}^{\prime}r^{\prime}\). So, we take the \(r\)-hop neighborhood of every vertex in \(G\) and \(H,\) respectively. It is easy to see that the number of induced subgraphs or subgraphs of size \(k\) is the same locally if they are \(k-\)WL equivalent since we do an initial coloring of \(k\)-tuples based on isomorphism type. Now, suppose \(P^{\prime}=P-v\) is of size \((k+1).\)

Let \(P_{i}=P^{\prime}-v_{i},\) for \(i\in[k+1].\) It is possible that there may exist two pairs of subgraphs that are isomorphic. In that case, we remove such graphs. Let \(P_{1},P_{2},\ldots,P_{l}\) be all pairwise non-isomorphic graphs. \(k-\)WL would initially color the vertices according to isomorphism type. So, the subgraph count of \(P_{i}\) is the same in any two \(k-\)WL equivalent graphs. Let \(V(P_{i})=(u_{1},u_{2},\ldots u_{k}).\) We see the refined color after one iteration in equation 3. Now, we can observe that by looking at the first coordinate of the color tuples in a multiset, we can determine the adjacency of \(u\) with \((u_{2},u_{3},\ldots u_{k}).\) Similarly, after seeing the second coordinate of the color tuples in the multiset, we can determine the adjacency of \(u\) with \((u_{1},u_{2},\ldots u_{k}).\)

Consider, \(\forall u\in V(G),\)\(P_{i}\cup\{u\}=H\) will give the count of induced subgraph \(P^{\prime}.\) Thus, if \(G\) and \(H\) are \(k-\)WL equivalent, then the size of each color class after the first iteration will be same.

Now, for each \(P^{\prime}\) with \(v\) will form \(P\) if it has exactly \(|N_{P}(v)|\) many vertices of color \(1\). Also, as mentioned earlier that \(k-\)WL is equivalent to \(C^{k+1}\) logic, and we have to add unary logic stating that the color of neighbor to be \(1\). The \(k-\)WL and \(C^{k+1}\) are equivalent, so we have to add the unary relation putting the condition of the required colors. Again, using Lemma 3 we can say that running \(k-\)WL locally can count all patterns of size \((k+2)\) appearing as an induced subgraph. 

We can see the corollary below in which we mention the set of patterns shown in Chen et al. (2020).

**Corollary 3**.: _Local \(2-\)WL on the subgraphs induced by neighborhood of each vertex can count each pattern appearing as induced subgraphs as well as subgraphs of (a) 3-star (b) triangle (c) tailed triangle (d) chordal cycle (e) attributed triangle._

Based on the above results, we now present the algorithm 4 for counting patterns appearing as an induced subgraph in \(G\) using the localized algorithm. The function \(IndCount_{u,v}(H,G_{v}^{r})\) takes as input the pattern \(H\), the attributed version of \(G_{v}^{r}\), and returns the induced subgraph count of \(H\) in \(G_{v}^{r},\) where \(u\in H\) is mapped to \(v\in G_{v}^{r}\). Notice that the function \(IndCount_{u,v}(H,G_{v}^{r})\) is a predictor that we learn using training data.

```
1:Given \(G,H\).
2:Find \(r=radius(H)\) and let \(u\in H\) be a corresponding center vertex.
3:for each vertex \(v\) in V(G) do
4: Extract subgraph \(G_{v}^{r}\).
5: Find suitable \(k\), which will give an exact count based on the local substructure.
6: Run Local+Layer \(k-\)WL on \(G_{v}^{r}\).
7: Calculate \(IndCount_{u,v}(H,G_{v}^{r^{v}}).\)
8:endfor
9: return \(\frac{\sum_{v\in V(G)}IndCount_{(u,v)}(H,G_{v}^{r})}{|Orbit_{H}(u)|}\)
```

**Algorithm 4** Counting induced subgraph H in G

The running time and space requirement for Algorithm 4 is dependent on the value of \(k\) and \(r\). We can make informed choices for the values of \(k\) and \(r\). Notice that the value of \(k\) is chosen based on the local substructure. Also, the value of \(r\) is the radius of \(H\). Suppose the local substructure is simple (planar, bounded treewidth, bounded rankwidth Theorem 4). In that case, \(k-\)WL, for small values of \(k,\) is sufficient for counting induced subgraph \(H\). Otherwise, we have to run \((|H|-2)-\)WL in the worst case.
### Deciding k based on the pattern for counting subgraphs

For any pattern \(H\), it turns out that the number of subgraph isomorphisms from \(H\) to a host graph \(G\) is simply a linear combination of all possible graph homomorphisms from \(H^{\prime}\) to \(G\) (\(Hom(H^{\prime},G)\) is the number of homomorphisms from \(H^{\prime}\) to \(G\)) where \(H^{\prime}\) is the set of all homomorphic images of \(H\). That is, there exists constants \(\alpha_{H^{\prime}}\in\mathbb{Q}\) such that:

\[Count(H,G)=\sum_{H^{\prime}}\alpha_{H^{\prime}}Hom(H^{\prime},G) \tag{2}\]

where \(H^{\prime}\) ranges over all graphs in \(H^{\prime}\). This equation has been used to count subgraphs by many authors (Please refer Alon et al. (1997); Curticapean et al. (2017)).

**Theorem 7**.: _Cai et al. (1992); Dell et al. (2018) For all \(k\geq 1\) and for all graphs \(G\) and \(H\), the following are equivalent:_

1. \(HOM(F,G)=HOM(F,H)\) _for all graph_ \(F\) _such that_ \(tw(F)\leq k\)_._
2. \(k-\)_WL does not distinguish_ \(G\) _and_ \(H\) _and_
3. _Graph_ \(G\) _and_ \(H\) _are_ \(C^{k+1}\) _equivalent_1_._ Footnote 1: Counting logic with (k+1) variables

Using Equation (2) and Theorem 7, we arrive at the following theorem:

**Theorem 8**.: _Let \(G_{1}\) and \(G_{2}\) be \(k-\)WL equivalent and \(htw(H)\leq k\). Then subgraph count of \(H\) in \(G_{1}\) and \(G_{2}\) are the same._

**Lemma 5**.: \[Count(H,G)=\frac{\sum_{v\in V(G)}Count_{(u,v)}(H,G_{v}^{r})}{|Orbit_{H}(u)|}\] (3)

Proof.: Suppose \(H\) appears as subgraph in \(G\). Then, there must exist an injective function matching \(u\in V(H)\) to some \(v\in V(G)\). Thus, counting locally, we can easily see that every subgraph would be counted. Now to prove Equation (3), it is sufficient to show that for a given subgraph it is counted exactly \(|Orbit_{H}(u)|\) many times. Note that two subgraphs are same if and only if their vertex sets and edge sets are same. We fix the vertex set and edge set in \(G\) which is isomorphic to \(H\). Now, consider an automorphism of \(H\) which maps \(u\) to one of the vertices \(u^{\prime}\) in its orbit. Note that we can easily find the updated isomorphic function that maps \(u^{\prime}\) to \(v\). Now, the number of choices of such \(u^{\prime}\) is exactly \(|Orbit_{H}(u)|\). Thus, the same subgraph is counted at least \(|Orbit_{H}(u)|\) many times. Suppose \(x\in V(H)\) is a vertex such that \(x\notin Orbit_{H}(u)\). Considering the fixed vertex set and edge set, if we can find an isomorphism, then it is a contradiction to the assumption that \(x\notin Orbit_{H}(u)\). Thus, the same subgraph is counted exactly \(|Orbit_{H}(u)|\) many times. 

Using Theorem 8 and Lemma 5, one can easily see that for counting pattern \(H\) as a subgraph, it is sufficient to run Local \(k-\)WL on the local substructure and count the subgraph locally.

**Theorem 9**.: _Local \(k-\)WL can exactly count any subgraph \(H\) if \(htw(H-v)\leq k\)._

The upper bound on the choice of \(k\) for running \(k-\)WL can be improved from the default \(|H|-2\) bound that we used for the induced subgraph count. The value of \(k\) is now upper bound by \(htw(H)\). Hence, we pick the minimum \(k\) based on the local substructure of \(G\) as well as the hereditary treewidth of pattern \(H\) for computing the subgraph count of \(H\) in \(G\). The algorithm for counting the subgraph is similar to the induced subgraph.

**Corollary 4**.: _Local \(1-\)WL can exactly count the number of patterns \(P\) or \(P-v\) appearing as a subgraph, when \(P\) or \(P-v,\) with a dominating vertex \(v\), is \(K_{1,s}\) and \(2K_{2}\)._

Proof.: In a star, \(K_{1,s}\), all the leaves are mutually independent. By the definition of homomorphism, the edges are preserved in homomorphic images. So, the only possibility of a homomorphic image of the star is getting another star with less number of leaves. Note that the star is a tree, and its treewidth is one. Also, for \(2K_{2}\), the homomorphic image is either itself or the star. So, the treewidth of all its homomorphic images is \(1\).
**Corollary 5**.: _Local \(1-\)WL can exactly count the number of \(C_{4}\) appearing as a subgraph._

Proof.: Choosing any vertex as a key vertex, we can see that \(H-v\) is \(K_{1,2}\). Also, the orbit size is \(4\). So, we can directly use Lemma 5 to compute the count of the \(C_{4}\) in the graph locally, and then sum it over all the vertices and divide it by \(4\). 

**Corollary 6**.: _Local \(1-\)WL can exactly count patterns appearing as subgraphs of (a) 3-star (b) triangle (c) tailed triangle (d) chordal cycle (e)attributed triangle and patterns appearing as induced subgraphs of (a) triangle and (c) attributed triangle._

Proof.: For subgraph counting, we can see that for all of the \(5\) patterns, there exists a vertex \(v\) such that \(htw(P-v)=1\). One can note that the attributed triangle can also be handled using Corollary 1. Since every pattern has a dominating vertex, running \(1-\)WL on the subgraph induced on the neighborhood is sufficient. Now, we only have to argue for the patterns appearing as induced subgraphs. Note that the induced subgraph count of the triangle and the attributed triangle is same as the subgraph count of the triangle and attributed triangle. 

Note that all the subgraph or induced subgraph counting can be easily extended to attributed subgraph or attributed-induced subgraph counting (graph motif). We will be given a coloured graph as an input, and we will incorporate those colours and apply a similar technique as described above to get the subgraph count.

**Corollary 7**.: _If \(C(G)=C(H),\) where \(C(.)\) is the color histogram, then \(Count(P,G)=Count(P,H)\) where \(P\) is the attributed subgraph._

In particular, for counting the number of triangles, we can see that it is enough to count the number of edges in the subgraph induced on the neighbourhood of the vertices. Thus, Local \(1-\)WL can give the exact count of the number of triangles. For more details, please see 6.4. The running time of \(1-\)WL depends on the number of iterations, \(h\). In general, it takes \(O((n+m)\log n)\) time, where \(m\) is the number of edges, whereas when we look at it in terms of iteration number it requires \(O(nh)\) time.

**Lemma 6**.: _It requires \(O(n)\) time to guarantee the count of patterns which can be written using \(2\)-variable with a counting quantifier where the depth of the quantifier is constant._

For more details, please see the equivalence between the number of iterations and quantifier depth in Theorem 2. A list of patterns that can be counted as subgraph and induced subgraphs using local \(1-\)WL and local \(2-\)WL are mentioned in Table 1. Also, the patterns including 3-star, triangle, chordal \(4\)-cycle (chordal \(C_{4}\)), and attributed triangle have been studied in Chen et al. (2020) and have been shown that it cannot be counted by \(1-\)WL.

### Algorithms for subgraph counting

#### Triangle counting in the host graph

We describe an algorithm for counting the number of triangles in a given host graph \(G\) in Algorithm 5. Note that counting the number of triangles is the same as counting the number of edges in the

\begin{table}
\begin{tabular}{|c|c|c|c|c|c|} \hline
**Restriction on \(G_{v}^{r}\)** & **k** & **Patterns, \(H\)** & **Induced** & **Subgraph** & **Reference** \\ \hline \(G_{v}^{r}\) is amenable & 1 & All & ✓ & ✓ & Corollary 1 \\ \hline Max degree \(\leq\) 5 & 1 & Patterns with a dominating vertex & ✓ & ✓ & Corollary 2 \\ \hline Max degree \(\leq\) 15 & 2 & Pattern with a dominating vertex & ✓ & ✓ & Corollary 2 \\ \hline No restriction & 2 & \begin{tabular}{c} 3-star, triangle, tailed triangle, chordal cycle, \\ attributed triangle \\ \end{tabular} & ✓ & ✓ & Corollary 3 \\ \hline No restriction & 1 & \begin{tabular}{c} Either \(H\) or \(H-v\) is \(K_{1,s}\) or \(2K_{2}\), where \(v\) is the \\ dominating vertex \\ \end{tabular} & ✓ & ✓ & Corollary 4 \\ \hline No restriction & 1 & \begin{tabular}{c} \(C_{4}\) \\ \(3\)-star, tailed triangle, chordal cycle \\ \end{tabular} & ✓ & ✓ & Corollary 5 \\ \hline No restriction & 1 & 
\begin{tabular}{c} triangle, attributed triangle \\ \end{tabular} & ✓ & ✓ & Corollary 6 \\ \hline \end{tabular}
\end{table}
Table 1: List of all the patterns that can be counted exactly(as a subgraph or induced subgraph), given \(G\), using Local \(k-\)WL, for different \(k\).
subgraph induced by \(N_{G}(v)\). It is well known that two \(1-\)WL equivalent graphs have the same number of edges. This ensures that if we run \(1-\)WL on the induced subgraphs in the neighborhood of \(v\), taking color as a feature, we can guarantee the count of the triangles. On the other hand, we can see that running \(1\)- WL on graph \(G\) will not guarantee the count of triangles. Running \(1-\)WL on the entire graph takes \(O(n+m)\log(n)\) and \(O(n)\) space, where \(m\) is the number of edges. Thus, running \(1-\)WL locally in the neighborhood is more space and time efficient. Note that the running time is also dependent on the number of iterations, \(h\). Running \(1-\)WL for \(h-\) iteration requires \(O(nh)\) time. The quantifier depth of counting logic with \((k+1)\) variables is equivalent to the number of iterations of \(k-\)WL ( See Theorem 2). For the case of triangle counting, we just need to count the number of edges, which can be done by running just one iteration of \(1-\)WL. So, the time required is \(O(deg(v))\) for each \(v\). This can be done in parallel for each vertex.

```
1:Let \(G\) be the host graph.
2:\(num\_edges=0\)
3:for each vertex \(v\) in \(V(G)\)do
4: Find the induced subgraph on \(N_{G}(v)\)
5: Find the number of edges in the induced subgraph on \(N_{G}(v)\)
6: Add it to \(num\_edges\)
7:endfor
8:\(c\) = \(num\_edges/3\)
9:Output: The number of triangles in graph \(G\) is \(c\).
```

**Algorithm 5** Counting the number of triangles

#### Counting subgraph of radius one

We begin by explaining the procedure for counting the number of subgraphs having a dominating vertex (radius one). For this purpose, we fix a dominating vertex \(u\). If a subgraph exists, then the dominating vertex must be mapped to some vertex. We iteratively map the dominating vertex to each vertex in the host graph and count the number of patterns in the neighborhood of the dominating vertex.

Here we present an algorithm for counting patterns of radius one Algorithm 6. Note that running \(k-\)WL on the entire graph takes \(O(k^{2}\cdot n^{k+1}\log n)\) time and \(O(n^{k})\) space, whereas when we run locally, it requires less time and space. Suppose we run only on the neighborhood of each vertex. Then, it requires \(\sum_{v\in V(G)}(deg(v))^{k+1}\log(deg(v))\) and space \(O(max_{i}(deg(v_{i}))^{k}+n)\). More specifically, suppose the given graph is \(r\)-regular. Then it requires \(O(r^{k+1}\log(r)n)\) time and \(O(r^{k}+n)\) space. Therefore, if the graph is sparse, then we can implement Local \(k-\)WL for a larger value of \(k\). We can see that running \(k-\)WL locally is not dependent on the size of the graph exponentially. However, it is not feasible to run \(k-\)WL on the entire graph for a larger value of \(k\).

```
1:Let \(H\) be a pattern having a dominating vertex and \(G\) be the host graph.
2:for each vertex \(v\) in \(V(G)\)do
3: Find the induced subgraph on \(N_{G}(v)\)
4:if\(\text{degree}(v)+1<|V(H)|\)then
5: skip this iteration
6:endif
7: run \(k-\)WL on the induced subgraph on \(N_{G}(v)\)
8: Calculate \(Count_{u,v}(H,G_{v}^{r})\)
9:endfor
10:return\(\frac{\sum_{v\in V(G)}Count_{u,v}(H,G_{v}^{r})}{|Orbit_{H}(u)|}\)
```

**Algorithm 6** Counting the number of patterns of radius one

#### Counting subgraphs of radius r

Here, in Algorithm 7, we describe how to count the number of subgraphs of radius \(r\). We iterate over all the vertices and take the \(r\)-hop neighborhood around that vertex, say \(v\), and choose a suitable \(k\) according to the structure of the pattern that can guarantee the count of subgraphs in the local substructure.
## 7 Fragmentation

Here, we discuss the _Fragmentation_ technique that is different from the localized methods we have seen so far. From Table 1, we have seen that Local \(k-\)WL (or ((Local + Layer) \(k-\)WL)) is sufficient for getting an exact count for the patterns given in the table. Given a pattern \(P,\) that is more complicated than the patterns in Table 1, we fragment \(P\) into simpler patterns such that their exact count is known. In the subgraph GNN proposed earlier, look into subgraph of the host graph. We have seen that this technique is scalable on large graphs. Also, we have seen that subgraph GNN is more expressive and efficient than traditional GNN. So, we tried to explore the expressibility when the pattern is also fragmented into smaller subpatterns.

The fragmentation method involves fragmenting the pattern, \(P\), into smaller subpatterns and counting these subpatterns to get the count of the \(P\) in the host graph. As described in Section 6, the value of \(k\) depends on the size of the pattern (induced subgraph) and its structure (subgraph count). Thus, even though the \(htw(P)\) may be large, if we divide it into subpatterns, then \(k\) required to guarantee the count would be reduced. Thus, it provides more expressiveness for smaller \(k\) in order to count the patterns which cannot be counted if we directly apply Local \(k-\)WL. Thus, given a pattern we apply the same fragmentation on \(G_{v}^{r}.\) Thus, the number of occurrences of \(H\) in \(G_{v}^{r}\) can be computed by combining the counts of the simpler patterns. Instead of training a GNN for counting \(H,\) we can design GNNs for learning the easier tasks (i.e., for counting the simpler models) and combine the outputs of those models. It should be noted that the fragmentation into smaller subgraphs depends on the structure of the pattern \(H.\)

We demonstrate this technique for counting induced tailed triangles in Figure 2. As seen in the figure, the tailed triangle pattern can be fragmented into two parts : the pendant vertex as the key vertex, and an attributed triangle. The colors assigned to the nodes of the attributed triangle depend on the distance of the nodes from the key node. Thus, the task of counting tailed triangles reduces to counting attributed triangles, as all the vertices at level 1 are connected to the root.

Suppose the task is to count the number of chordal cycles appearing as induced subgraphs. If we pick the vertex of degree three as the key vertex, then it is enough to search the neighbourhood of \(v\) in the host graph. Now, in \(N_{G}(v),\) if we count the number of \(2-stars,\) then it gives the count of chordal cycle appearing as subgraph. If we eliminate the appearance of \(K_{4}\), then it would give the
exact count of chordal cycles appearing as induced subgraphs. In that case, we count the number of triangles in \(N_{G}(v)\), which gives the exact count of \(K_{4}\).

Using the fragmentation technique, we show that just \(1-\)WL is sufficient for getting exact counts of induced subgraphs of certain sizes.

**Idea of the fragmentation algorithm 8:**

Given a graph \(G\) and a pattern \(P\), we first fix a vertex \(u\in V(P)\) as the key vertex. Now, assume that the radius of the pattern is \(r\). Thus, for counting \(P\) locally, it is sufficient to take the \(r\)-hop neighbourhood for each vertex \(v\) of \(G\), say \(G_{v}^{r}\), as has been shown in Lemma 3 and Lemma 5. Also, we have proved above that doing count locally is sufficient for both subgraph and induced subgraph. Now, we fragment pattern \(P\) into smaller subpatterns, say \(P_{1},P_{2},\ldots P_{l}\).

Based on the structure of \(P\), we consider the subgraphs of \(G_{v}^{r}\) where the subpattern \(P_{i}\) is required to be counted. For all subpattern \(P_{i}\) in \(P\), we make a list of subgraphs \(G_{v}^{r}(1),G_{v}^{r}(2),\ldots G_{v}^{r}(t)\) of \(G_{v}^{r}\) where \(P_{i}\) needs to be counted. We aggregate these lists into a dictionary, \(\mathcal{L}\), with \(P_{i}\) as the keys. It should be noted that the decomposition of \(P\) into \(P_{i}\)s is such that their counts can be calculated. That is, we have learnt models \(M_{i},\) corresponding to each \(P_{i}\), which counts the number of subpatterns \(P_{i}\). The array \(c\) stores the count of \(P_{i}\)'s in each subgraph of \(G_{v}^{r}\). Now, for each vertex, we use appropriate functions \(\alpha\) and \(\beta\) to combine the counts in \(c\) to get the count of \(P\) in \(G_{v}^{r}\). Finally, the function \(\gamma\) finds the normalizing factor to get the actual count of the pattern in the \(G\).

```
1:Let \(G\) be the host graph and \(P\) be the list of patterns,
2:\(\mathcal{L}\) be the dictionary of subgraph rules associated with the subpattern \(P_{i},\)
3:\(M=\{M_{1},\ldots,M_{l}\}\) the list of learnt models for counting \(P_{i}\)'s, where \(l=|P|\)
4:\(a\leftarrow\) Zero array of size \(|V(G)|\)
5:for each vertex \(v\) in \(V(G)\)do
6: Extract \(G_{v}^{r}\)
7:\(b\leftarrow\) Zero array of size \(l\)
8:for each pattern \(P_{i}\) in \(\mathcal{L}\)do
9:\(c\leftarrow\) Zero array of size \(s\), where \(s=|\mathcal{L}(P_{i})|\)
10:for each rule \(k\) in \(\mathcal{L}(P_{i})\)do
11: Extract \(G_{v}^{r}(k)\)
12:\(c[k]\) = \(M_{i}(G_{v}^{r}(k))\)
13:endfor
14:\(b[i]\) = \(\alpha(c)\)
15:endfor
16:\(a[v]\) = \(\beta(b)\)
17:endfor
18:\(Count(P,G)\) = \(\gamma(a)\)
19:return\(Count(P,G)\)
```

**Algorithm 8** Fragmentation Algorithm

**Theorem 10**.: _Using the fragmentation method, we can count induced tailed triangle, chordal \(C_{4}\) and \(3-star\), by running Local \(1-\)WL._

Proof.: For tailed triangle, we fix the pendant vertex as the key vertex (refer to Figure 2). Now, we have to look for the count of triangles such that exactly one of the node of triangle is adjacent to the key vertex. We find induced subgraph of \(2\)-hop neighborhood for each key vertex. Now, we color the vertices at distance \(i\) by color \(i\). Then, the problem of counting the number of tailed triangles reduces to counting the number of colored triangles in the colored graph such that one node of triangle is colored \(1\) and remaining two nodes are colored \(2\). We can find the count of colored triangles using \(1-\)WL on the induced subgraph by Corollary 1. This number of count of colored triangle is same

Figure 2: Fragmentation for counting tailed triangles.
as \(IndCount_{(u,v}(tailed-triangle,G_{v}^{r})\). Now, using Lemma 3, we can say that fragmentation technique can count tailed triangles appearing as induced subgraphs using \(1-\)WL.

Consider the pattern, chordal \(C_{4}\). We have proved that \(1-\)WL can count the number of subgraphs of chordal \(C_{4}\). So, to count the number of induced subgraphs of chordal \(C_{4}\), we only have to eliminate the count of \(K_{4}\). When we fix one vertex of degree \(3\) as key vertex, we can easily compute the count of \(K_{1,2}\) in the neighborhood. Now, we have to eliminate all three tuples appearing as triangles in the neighborhood of the key vertex. We can easily count the number of triangles in the neighborhood of each vertex. This gives the exact count of chordal \(C_{4}\) appearing as subgraph in the local structure. Using Lemma 3, we can find \(IndCount(ChordalC_{4},G)\).

Consider the pattern, \(3-star\). Here, we choose any of the pendant vertex as key vertex. Now, we have to compute \(K_{1,2}\), where one center vertex of the star is connected to the key vertex. We can easily count the number of colored \(K_{1,2}\) in 2-hop neighborhood of the key vertex. However, a triangle can be also be included in this count. So, we have to eliminate the \(3\) tuples forming a triangle. Again, using the approach discussed above, we can count the number of colored triangles and this will output the exact count of colored induced \(K_{1,2}\). Again, using lemma 3, we can find \(IndCount(3-star,G)\). 

**Theorem 11**.: _Using the fragmentation technique we can count all patterns appearing as induced subgraphs of size \(4\) by just running Local \(1-\)WL._

Proof.: In Table 2, we describe how the fragmentation technique can be leveraged to count all the induced subgraphs of size \(4\). This shows that for \(k=1\), the fragmentation technique is more expressive than \(S_{k+3}\). Also, we can enlist more graphs where the larger pattern can be seen as the union of fragments of smaller patterns. Using this, we can see that it covers all the graphs that were mentioned in Chen et al. (2020). One can see that all the formulae use the function that can be computed by \(1-\)WL. The number of vertices and the number of edges can easily be computed after doing one iteration of \(1-\)WL. Also, the degree sequence can be computed after running \(1-\)WL for one iteration. All other functions(formulae) are just the linear combination of the functions computed earlier, the number of vertices or the number of edges. 

In the structures shown in the table 2 below, we have highlighted the key vertex by "light green" color and other vertices by "black color".

\begin{table}
\begin{tabular}{|c|c|c|} \hline Vertices & Structure & Formula \\ \hline G1 & \(\circ\) & \(\bullet\) & \(\binom{n}{2}-|E|\) \\ \hline G2 & \(\circ\) & \(|E|\) \\ \hline G3 & \(\bullet\) & \(\bullet\) & \(\frac{\sum_{v}IndCount_{(u,v)}(G_{1},G-\mathcal{G}[N_{G}[v]])}{3}\) \\ \hline G4 & \(\circ\) & \(\sum_{v}IndCount(G_{2},G-G[N_{G}[v]])\) \\ \hline \end{tabular}
\end{table}
Table 2: Fragmentation for patterns for size of at most 4.
\begin{tabular}{|c|c|c|} \hline G5 & & \(\frac{\sum_{v}IndCount_{(u,v)}(G_{2},G[N_{G}(v)])}{3}\) \\ \hline G6 & & \(\sum_{v}\binom{degree(v)}{2}-|E(N_{G}(v))|\) \\ \hline G7 & & \(\frac{\sum_{v}IndCount_{(u,v)}(G_{3},G-G[N_{G}(v)]}{4}\) \\ \hline G8 & & \(\frac{\sum_{v}IndCount_{(u,v)}(G_{5},G[N_{G}[v]))}{4}\) \\ \hline G9 & & \(\frac{\sum_{v}IndCount_{(u,v)}(G_{4},G-G[N_{G}[v]])}{2}\) \\ \hline G10 & & \(\frac{\sum_{v}(IndCount(G_{6},N_{G}(V))-IndCount(G_{5},N_{G}(v)))}{2}\) \\ \hline G11 & & \(\sum_{v}(IndCount(G_{6},G-G[N_{G}[v]])\) \\ \hline G12 & & \(\sum_{v}Count(attribute*-triangle,G[N_{G}(v)\cup N_{G}(N_{G}(v))])\) \\ \hline \end{tabular}
## 8 Theoretical Comparison of Graph Neural Networks

In this section we give a comparison of the time and expressiveness between the GNN hierarchies proposed in Papp and Wattenhofer (2022) and our methods. From Theorem 3, it is clear that \(k-\)WL is less expressive than Local \(k-\)WL. Also, we have shown that the space and time required by Local \(k-\)WL are less compared to \(k-\)WL.

\(S_{k}\) is a model in which a vertex is given an attribute based on the number of connected induced subgraphs of size at most \(k\), the key vertex. Even though it searches locally, the number of non-isomorphic graphs may be too many for a small value of \(k\). Suppose the radius of the induced subgraph is \(r\); then it has to search up to the \(r\)-hop neighborhood. Using brute force would require \(O(n_{1}^{k})\)-time to count the number of induced subgraphs of size \(k\), for each individual induced subgraph. To improve the time complexity, it either stores the previous computation, which requires a lot of space or further recomputes from scratch. Thus, it would require \(O(t_{k}\times n_{1}^{k})\), where \(t_{k}\) is the number of distinct graphs upto isomorphism of at most \(k\)-vertices. Using Theorem 6, one can easily see that running \(k-\)WL locally is more expressive than \(S_{k+2}\).

The \(N_{k}\) model has a preprocessing step in which it takes the \(k\)-hop neighborhood around vertex \(v\), and gives attributes based on the isomorphism type. In a dense graph, it is not feasible to solve the isomorphism problem in general, as the size of the induced subgraph may be some function of \(n\). Also, using the best known algorithm for graph isomorphism by Babai (2016), the time required is \(O(n_{1}^{O(\log n_{1})})\). However, running Local \(k-\)WL would require \(O(n_{1}^{k})\). Also, there are rare examples of graphs that are \(3-\)WL equivalent and non-isomorphic. So, if we run \(3-\)WL locally, then most of times expressive power matches with \(N_{k}\).
The \(M_{k}\) model deletes a vertex \(v\) and then runs \(1-WL\). Papp and Wattenhofer [2022a] proposed that instead of deleting the vertices, \(k\) vertices are marked in the local neighborhood and showed that it is more expressive than deletion. It identifies \(k\) set of vertices in the local \(r\)-hop neighborhood of the graph. It would require \(O(n_{1}^{(k+2)}\log(n_{1}))\) time as it has \(O(n_{1}^{k})\) many possibilities of choosing \(k\) many vertices. It requires \(O(n^{2}\log n)\) time to run \(1-\)WL. The same time is required for Local \((k+1)-\)WL. That's why we compare \(M_{k-1}\) with Local \(k-\)WL in Table 3. Also, it is known that with any \(l\) markings and running \(k-\)WL is less expressive than running \((k+l)-\)WL on the graph Furer [2017]. So, if we plug in the value, we can see that running Local \(k-\)WL is more expressive than doing \(l\) marking and running \(1-\)WL. One can get an intuition by comparing with the \((k+1)\) bijective pebble game. If we mark the vertices, then the marking pebbles get fixed and give less power to spoiler. However, just running \(k-\)WL the spoiler is free to move all the pebbles. We present a simple proof that Local \(k-\)WL is at least as expressive as \(M_{k-1}\).

**Theorem 12**.: _Local \(k-\)WL is atleast as expressive as \(M_{k-1}\)._

Proof.: Let \(G_{v}^{r}\) and \(G_{u}^{r}\) be the induced subgraphs around the \(r\)-hop neighborhood for vertices \(v\) and \(u\), respectively. Let \(M_{k}\) distinguish \(G_{v}^{r}\) and \(G_{u}^{r}\). We claim that Local \(k-\)WL can also distinguish the graph \(G_{v}^{r}\) and \(G_{u}^{r}\). To prove our claim, we use the pebble bijective game. \(G_{v}^{r}\) is distinguished because there exists a tuple \((v_{1},v_{2},....v_{k-1})\) such that marking these vertices and running \(1-\)WL on the graph gives stabilised coloring of the vertices that does not match with that of \(G_{u}^{r}\). Now, consider two games. One game corresponds to \(1-\)WL and another to Local \(k-\)WL. For the first \((k-1)\) moves, the Spoiler chooses to play and places pebbles at \((v_{1},v_{2},....v_{k-1})\). After that, in both games, there are two pebbles and the position of both the games are same. Let \(S_{1}\) and \(D_{1}\) be the spoiler and Duplicator in the \((k+1)\) bijective pebble game, and \(S_{2}\) and \(D_{2}\) be the spoiler and Duplicator in \(2\) bijective pebble game. \(S_{1}\) will follow the strategy of \(S_{2}\) and \(D_{2}\) follows the strategy of \(D_{1}\). We prove by induction on the number of rounds. Our induction hypothesis is that the position of games in both the games is the same and if \(S_{2}\) wins, then \(S_{1}\) also wins.

_Base case :_ Duplicator \(D_{1}\) proposes a bijection. \(D_{2}\) will propose the same bijection. Now, \(S_{2}\) places a pebble on some vertex \(v\). Similarly, \(S_{1}\) will also place a pebble at \(v\). Note that the position of the game is the same, and if \(S_{2}\) wins, then \(S_{1}\) also wins.

Now, using the induction hypothesis, assume that the position of both the games is the same and \(S_{2}\) has not won till round \(i\).

Now, consider the game at round \((i+1)\). If \(S_{2}\) decides to play / remove a pebble, then \(S_{1}\) will do the same. If \(S_{2}\) decides to play a pebble, then \(S_{1}\) also decides to play a pebble. So, \(D_{1}\) proposes a bijective function. \(D_{2}\) proposes the same bijective function. Now, \(S_{2}\) places pebble at some vertex \(u\), then \(S_{1}\) also places pebble at \(u\). Thus, the position of both the game is the same and if \(S_{2}\) wins, then \(S_{1}\) will also win. 

## 9 Model

Using Lemma 3 for induced subgraph counting and Lemma 5 for subgraph counting, we present the _InSigGNN_ model, which is shown in Figure 4. We have also designed a separate model, _InsideOutGNN_, for certain cases (Figure 3).

\begin{table}
\begin{tabular}{|c|c|c|c|c|c|} \hline  & **Local k -WL** & **(Local+Layer) k -WL** & **k- WL** & **S k** & **M\_k-1** \\ \hline Expressiveness & - & - & Less & Less than & Less \\  & & - & & Local \((k-2)-\)WL & Less \\ \hline Time & \(O(n\times n_{1}^{k+1})\) & \(O((n_{2})^{k+1}\times rn)\) & \(O(n^{k+1})\) & \(O(t_{k}n_{1}^{t}n)\) & \(O(n_{1}^{t+1}n)\) \\ \hline \end{tabular}
\end{table}
Table 3: Here, \(n:\) number of nodes in the graph, \(n_{1}:\) max number of nodes in a \(r\)-hop neighbourhood of a vertex, \(n_{2}:\) maximum number of nodes in any two consecutive layers for a particular vertex, over all the vertices of the graph, and \(t_{k}:\) number of distinct graphs upto isomorphism of at most \(k\)-vertices. The first row compares the expressiveness of the models, and the second row compares the time complexity, with respect t0 our models.
### Model Description

We conducted the experiments using two different architectures, _InsideOutGNN_ and _InSigGNN_.

#### 9.1.1 InsideOutGNN Model

In the InsideOut model (Figure 3), we take a graph as our input. We construct subgraphs with each node as the root node. For different tasks, we create the subgraphs in a different manner. For example, for counting triangles, we create the \(1\)-hop induced subgraph. These subgraphs are then taken as input for the Internal GNN part of our model. For our experiments, we used GINConv layers Xu et al. (2019). The Internal GNN outputs the embeddings of the nodes present in the subgraph. We then pass this embeddings through a Global Add pool layer which is then treated as the embedding for the root node which was used to create the subgraph. Using this embedding, we predict the local count by passing the embedding through a linear transformation. This local count is then used to train the Internal GNN model.

We take a union of all the subgraphs. For the embeddings of the nodes of this union of subgraphs, we use the embeddings learned in the Internal GNN part of the model. Using these embeddings, we predict the global count of the substructure using a GIN Convolutional layer in the External GNN model.

The motivation to split the model into two separate parts is to force the model to learn the local counts. If the local counts are predicted well, then we can easily count the global counts, as the global count is just a linear function of the local counts.

#### 9.1.2 InSigGNN

In the InSig Model, the Internal GNN part is similar to InsideOut Model. The model architecture is shown in Figure 4. In this model, we do not transfer the embeddings learned in the Internal GNN part of the model. We use the local counts only, sum it up, and pass it through a linear transformation. The weights learned in the Internal GNN model are based only on local counts. The external linear transformation is learned based on the external count.

### Model Usage

We know that the global counts of the substructure is just a linear function of the local counts. Substructures such as \(2-Stars\) and \(3-Stars\) depend on the local substructures. Therefore, for counting such substructures, we use the InsideOut Model, which uses a GIN convolutional layer in the external part of the Model.

Substructures, such as triangles, don't depend on the subgraph created with respect to the root nodes. It just depends on the number of edges in the subgraphs. We use a linear transformation on the summation of the local counts to predict the global count. Therefore, for substructures such as

Figure 3: Schematic of the InsideOutGNN Model
triangles and chordal cycles, we use linear transformation as those structures are not dependent on the subgraph.

### Hyperparameters

We use two GIN Convolutional layers for the Internal GNN model. As there can be more than one length path in the subgraph, 2 convolutional layers are beneficial to capture the information well. In InsideOut Model, we use two GIN convolutional layers in the external part of the model.

We use a learning rate of 0.0001 and a batch size of 1. We also varied the experiments with different hidden dimensions for the node embedding and found the best results when we used 512 as a hidden dimension. The experiments are conducted in Nvidia A100 40GB GPU.

## 10 Experimental Observations

We also experimented with the fragmentation technique. The details of these models and the experimental details are described in Section 9. We used the random graphs dataset prepared in Chen et al. (2020). We report our experiments' Mean Absolute Error (MAE) in Table 4. We compare our results with those reported in Zhao et al. (2022).

It can be observed that our model significantly outperforms the baseline. This is due to the incorporation of the counts of the patterns in the local substructures, which leads to better learning of the internal GNN (in both of our proposed models). The patterns such as \(3\)-star and \(2\)-star require some knowledge of the overall position of the root node \(v\) in \(G_{v}^{1}\). Thus, the InsideOutGNN model performs better for these patterns, as the external GNN takes the global structure of \(G\) into account. However, counts of patterns such as triangles can be computed by counting the number of edges at a \(1\)-hop neighbourhood, \(G_{v}^{1}\). So, the InSigGNN model, which learns the size of the orbit of the triangle, performs better. The fragmentation technique for each pattern is different and is discussed in detail in Section 91.

Footnote 1: The dataset and the code are present in the GitHub repository [Link].

\begin{table}
\begin{tabular}{|l|l l l|l l|l l|} \hline  & \multicolumn{4}{c|}{**Without Fragmentation**} & \multicolumn{2}{c|}{**Fragmentation**} \\ \hline \multirow{2}{*}{_Models_} & \multicolumn{2}{c|}{_Triangle_} & \multicolumn{1}{l|}{_3-stars_} & \multicolumn{1}{l|}{_2-stars_} & \multicolumn{1}{l|}{_C4_} & \multicolumn{1}{l|}{_Chordal_} & \multicolumn{1}{l|}{\(K_{4}\)} & \multicolumn{1}{l|}{_Chordal_} \\ \hline Zhao et al. (2022) & 8.90E-03 & 1.48E-02 & NA & **9.00E-03** & NA & NA & NA & NA \\ \hline InsideOutGNN & 3.30E-03 & **2.80E-04** & **4.10E-04** & 4.40E-02 & 1.06E-02 & – & **9.14E-05** \\ \hline InSigGNN & **6.00E-04** & 2.00E-02 & 8.30E-03 & 3.53E-02 & **3.80E-04** & **4.85E-05** & 2.30E-02 \\ \hline \end{tabular}
\end{table}
Table 4: MAE for the subgraph count of different patterns. The results for \(2-stars\), \(chordal\)\(C_{4}\) and \(K_{4}\) are not available in Zhao et al. (2022) and are marked as NA.

Figure 4: Schematic of the InSigGNN model
Conclusion

In this work, we progressed toward a more precise characterization of the localized versions of WL algorithms. We showed how the Local-\(k-\)WL lies between the global \(k\) and \(k+1-\)WL in terms of expressiveness. We also developed strategies to make the Local-\(k-\)WL algorithm more efficient by introducing techniques such as layered WL, recursive WL, and pattern fragmentation. The hope is that such generalizations of the WL algorithm will lead to a finer subdivision of the WL hierarchy as well as more efficient and expressive graph neural networks.
