As part of a research project studying neural architectures, we needed an algorithm that could identify isomorphic computational graphs
, the building blocks of neural networks. In our context, computational graphs represent operations performed on arbitrary tensors where the vertices are operations and the edges are tensors. To generalize this problem, we represent a computational graph as a colored directed acyclic graph where the colors represent unique operations. Depending on the graph representation (e.g., adjacency matrix plus a color per vertex), multiple representations may encode the same computational graph (Figure1).
In this report, we describe a novel graph invariant for computational graphs and how we used it to generate all distinct computational graphs up to isomorphism for small graphs. While this invariant cannot perfectly distinguish all pairs of non-isomorphic computational graphs, we suggest that it may be useful as a heuristic for comparing graphs.
A computational graph on vertices and colors is a directed acyclic graph where each vertex is assigned an arbitrary color, and every vertex lies on a path between two designated vertices. Formally, it is a tuple , where:
is the number of vertices;
is the number of colors;
is a set of directed edges such that ;
is a function assigning a color to each vertex; and
Note that there is no restriction that adjacent vertices have different colors.
The goal is to count the number of distinct computational graphs up to isomorphism, where isomorphism is defined as follows.
Computational graphs and on vertices and colors are isomorphic if there exists a bijection such that:
adjacency is preserved: ; and
coloring is preserved: .
3 Related Approaches
In the context of general graph isomorphism, a graph invariant is a property of a graph such that if two graphs are isomorphic, they have the same value of that property (the converse is not necessarily true). The Weisfeiler–Lehman algorithm  uses an iterative coloring approach to come up with a canonical coloring on a graph, though subsequent work  shows that the algorithm can fail for some graphs. It is unknown whether graph isomorphism can be solved in polynomial time .
This paper deals with a more constrained problem than general graph isomorphism, where the graphs are directed acyclic graphs and there are colorings assigned to the vertices. Nonetheless, the algorithm is partially inspired by the iterative nature of the Weisfeiler–Lehman algorithm.
OEIS  provides a few sequences which are close to what we are looking for:
A000088: Number of graphs on unlabeled nodes. This series does not consider coloring and considers all graphs rather than just directed acyclic graphs.
A003024: Number of acyclic digraphs with labeled nodes. This series treats all vertices as uniquely labeled rather than individual colored and also counts disconnected graphs.
A057500: Number of connected labeled graphs with edges and nodes. This series does not use directed edges and does not consider coloring.
A240955: Number of -colored labeled digraphs with vertices. In this series, colored refers to vertices which cannot be colored the same color as its neighbors, which is different than the notion of coloring used here.
Pólya–Redfield counting can be used to count the number of colorings on undirected graphs  but it does not provide a way to quickly identify if two directed colored graphs are isomorphic.
4 Iterative Graph Hashing Algorithm
In this section, we describe an algorithm for generating a novel graph invariant for computational graphs.
At a high-level, the key idea is to iteratively apply isomorphism-invariant operations to the graph in a way takes into account the graph structure as well as the coloring.
Algorithm 1 provides the pseudo-code for the graph hashing algorithm:
Specifically, hash returns a fixed-length hash. Our implementation uses the 128-bit MD5 hash algorithm which we found sufficient for our use-case. The sort function performs a lexicographical sort of the hash outputs. We repeat the algorithm up to the number of vertices iterations (line 7) but we suspect that it may be sufficient to iterate up to the diameter of the graph. In our implementation, we represent as an adjacency matrix along with a list of colors of length equal to the number of vertices. The in-degree and out-degree functions are implemented as summations across the columns or rows of the matrix.
4.1 Proof of graph invariance
To show that this algorithm computes a graph invariant, we must show that any two isomorphic any computational graphs output the same hash. Consider two graphs and that are isomorphic with isomorphism . Suppose that we run Algorithm 1 on and . We will use and to refer to the values of the hashes in the executions of the algorithm on and , respectively. We will say that the hashes are consistent if they respect the isomorphism : that is, if for each vertex .
Choose any vertex , and let . Then the initial hash of in is equal to the initial hash of in (after line 6), because the two vertices have the same adjacency and coloring, by Definition 2. Thus:
The initial hashes and are consistent (after line 6).
In each iteration of the outer for-loop (line 7), the hashes are updated. Suppose, at the start of an iteration, that the hashes are consistent. Again, choose any , and let . If , then by the isomorphism condition. By the consistency assumption, . Since and (line 10), then the multisets (ignoring order) represented by and are the same. Likewise, is the same as (line 11) ignoring order. Because sorting ensures that the list orderings are identical, and . We also have (because and are consistent), so it follows that (line 14) since the hash function is operating on the identical triplets. Thus:
If the hashes are consistent at the start of an iteration (line 7), then they are also consistent at the end of that iteration (after line 14).
By induction, Lemma 4.1 and Lemma 4.2 show that the hashes are consistent throughout the full loop. Because the final hashes and are consistent, they are permutations of each other, so their sorted forms are the same, and thus the final hashed results are identical. Therefore, Algorithm 1 computes a graph invariant. ∎
5 Graph Enumeration Procedure
Given the graph invariant, we can proceed to generate all computational graphs, up to isomorphism. Using the canonical ordering, we treat the first vertex (no in-neighbors) as the “input” vertex and the last vertex (no out-neighbors) as the “output” vertex.
We observe that vertices not on a directed path from to in a colored directed acyclic graph can be pruned to yield a valid computational graph.
If we generate the directed acyclic graphs in increasing number of vertices order, then any graph that needs to be pruned has been already generated at a previous iteration and can be immediately skipped.
This algorithm also provides a canonical computational graph (i.e., the first one that is observed) for each unique hash which represents the equivalence class of computational graphs induced by the Algorithm 1.
For our neural network use-case, we needed to generate all graphs up to 7 vertices, 9 edges, and 3 colors. Furthermore, the first vertex and the last vertex are specially colored and distinct from each other and the other 3 colors (they represent the input and output tensors of the network).
We verified that all graphs with the same hash generated in Algorithm 2 were unique up to isomorphism by running an expensive procedure which enumerates all possible permutations to confirm that any graph with a duplicate hash (line 11) is isomorphic to the canonical computational graph. The definition of graph invariant implies that graphs with different hashes are non-isomorphic.
Thus for our constrained use-case, Algorithm 1 can exactly identify if two computational graphs are isomorphic or not.
7 Adversarial graphs
An adversarial example to the identifiability of the algorithm consists of two non-isomorphic computational graphs which hash to the same value. One such example can be seen in Figure 2 using 10 vertices and 16 edges. The counterexample holds also long as vertices 2, 3, 4, 5 are the same color and likewise for vertices 6, 7, 8, 9. The two graphs are non-isomorphic by inspection but Algorithm 1 fails to distinguish between the two. This is because vertices 2, 3, 4, 5 all start with the same initial hash due to having the same degree and each iteration maintains this equivalence because the in and out neighbors share the same colors (and likewise for vertices 6, 7, 8, 9).
An infinite number of similar adversarial graphs can be constructed from pairs of directed non-isomorphic bipartite graphs where all edges point from one partition to the other and the degree of all vertices within each partition is the same.
8 Future Work
Modifying the algorithm to deal with cases like the counterexample above is the first direction for future work.
In addition to the counter example discussed above, another possible problem is hash collision. A possible solution is to replace the hash function with string concatenation, which would cause the iterative “hashes” to grow exponentially in length at each iteration. This eliminates the possibility of hash collision, and the proof of graph invariance still holds.
We would like to thank Chris Jones for suggesting Weisfeiler–Lehman color refinement and finding a counterexample, Esteban Real for reviewing the code implementation of the algorithm, and William Chargin for reviewing the notation and proof.
-  L. Babai and L. Kucera. Canonical labelling of graphs in linear average time. In 20th Annual Symposium on Foundations of Computer Science (sfcs 1979), pages 39–46, Oct 1979.
-  László Babai. Graph isomorphism in quasipolynomial time. CoRR, abs/1512.03547, 2015.
-  Robert W Robinson. Enumeration of colored graphs. Journal of Combinatorial Theory, 4(2):181–190, 1968.
-  N. J. A. Sloane. The on-line encyclopedia of integer sequences. https://oeis.org.
-  Boris Weisfeiler and Andrei A. Lehman. A reduction of a graph to a canonical form and an algebra arising during this reduction. Nauchno-Technicheskaya Informatsiya, 2(9):12–16, 1968. (in Russian).