Graph Isomorphism
Two drawings can look completely different and still show the same graph: only who is connected to whom matters, not where the dots are. Isomorphic graphs have the same structure up to renaming the vertices. Deciding isomorphism is a famous problem: easy to check given the renaming, but no polynomial-time algorithm is known for finding it in general.
Definition
Graphs and are isomorphic if there is a bijection such that
The bijection is an isomorphism: it maps edges to edges and non-edges to non-edges.
Invariants: proving graphs are different
To show two graphs are not isomorphic, find a property preserved by isomorphisms (an invariant) on which they differ:
- number of vertices and edges,
- the degree sequence,
- the number of triangles (or of cycles of each length),
- being connected, being bipartite,
- the spectrum (eigenvalues) of the adjacency matrix.
Matching invariants do not prove isomorphism: the 6-cycle and two disjoint triangles both have 6 vertices, 6 edges and every degree equal to 2, but one is connected and the other is not.
from itertools import combinations
import numpy as np
def invariants(n, E):
adj = {v: set() for v in range(n)}
for u, v in E:
adj[u].add(v); adj[v].add(u)
degs = sorted((len(adj[v]) for v in range(n)), reverse=True)
triangles = sum(1 for a, b, c in combinations(range(n), 3) if b in adj[a] and c in adj[a] and c in adj[b])
seen, stack = {0}, [0]
while stack:
for w in adj[stack.pop()]:
if w not in seen:
seen.add(w); stack.append(w)
A = np.zeros((n, n))
for u, v in E:
A[u, v] = A[v, u] = 1
eig = np.round(np.sort(np.linalg.eigvalsh(A)), 3) + 0.0
return degs, triangles, len(seen) == n, eig
C6 = [(i, (i + 1) % 6) for i in range(6)]
two_triangles = [(0, 1), (1, 2), (2, 0), (3, 4), (4, 5), (5, 3)]
for name, E in [("C6", C6), ("two triangles", two_triangles)]:
d, t, conn, eig = invariants(6, E)
print(f"{name:13s} degrees={d} triangles={t} connected={conn} eigenvalues={eig}")C6 degrees=[2, 2, 2, 2, 2, 2] triangles=0 connected=True eigenvalues=[-2. -1. -1. 1. 1. 2.]
two triangles degrees=[2, 2, 2, 2, 2, 2] triangles=2 connected=False eigenvalues=[-1. -1. -1. -1. 2. 2.]
Finding an isomorphism
For small graphs, try all bijections. Invariants prune the search: a vertex of degree 3 can only map to a vertex of degree 3. Real tools (nauty, the algorithm in NetworkX's is_isomorphic) refine vertex classes by degree, neighbour degrees and so on, and are fast in practice. In 2015 László Babai found a quasi-polynomial algorithm, , the best known in theory.
from itertools import permutations
def isomorphism(n, E1, E2):
S1 = {frozenset(e) for e in E1}
S2 = {frozenset(e) for e in E2}
if len(S1) != len(S2):
return None
for perm in permutations(range(n)):
if all(frozenset((perm[u], perm[v])) in S2 for u, v in E1):
return perm
return None
# a 5-cycle drawn as a pentagon, and as a pentagram (star)
pentagon = [(0, 1), (1, 2), (2, 3), (3, 4), (4, 0)]
pentagram = [(0, 2), (2, 4), (4, 1), (1, 3), (3, 0)]
f = isomorphism(5, pentagon, pentagram)
print("isomorphism pentagon -> pentagram:", f)
print("edges map to:", [tuple(sorted((f[u], f[v]))) for u, v in pentagon])isomorphism pentagon -> pentagram: (0, 2, 4, 1, 3)
edges map to: [(0, 2), (2, 4), (1, 4), (1, 3), (0, 3)]
The star is just the pentagon visited in a different order.
Counting labelled graphs
There are graphs on the vertex set (each possible edge is in or out). Many of them are isomorphic, so the number of unlabelled graphs is smaller: for there are 64 labelled graphs but only 11 up to isomorphism. Each isomorphism class contains at most labelled graphs, which gives the lower bound on the number of classes.
In software
Chemistry databases detect duplicate molecules by graph isomorphism (with canonical forms such as canonical SMILES). Compilers and query optimizers look for subgraph patterns, a harder relative (subgraph isomorphism is NP-complete). Graph neural networks are designed to give the same output for isomorphic inputs, which limits what they can distinguish (the Weisfeiler–Lehman test).
Exercises
Exercise 1: Same shape?
Graph has edges ; graph has edges . Are they isomorphic? Start with the degree sequences.
Answer
Yes, both have degree sequence and the map is an isomorphism.
Solution
In , and have degree 3 (they are joined by the diagonal); in , and have degree 3. Both graphs are a 4-cycle plus one diagonal. Mapping to sends , , , , : every edge goes to an edge.
Exercise 2: Not isomorphic
Show that a path on 4 vertices and a star with 3 leaves (one centre joined to 3 others) are not isomorphic.
Answer
Degree sequences differ: versus .
Solution
Both have 4 vertices and 3 edges, but an isomorphism preserves degrees, and the star has a vertex of degree 3 while the path has none.
Exercise 3: Count classes for (code)
Group all 64 labelled graphs on 4 vertices into isomorphism classes.
Answer
11 classes.
Solution
Use a canonical form: the lexicographically smallest edge list over all relabellings.
from itertools import combinations, permutations
pairs = list(combinations(range(4), 2))
canon = set()
for mask in range(2 ** len(pairs)):
E = [pairs[i] for i in range(len(pairs)) if mask >> i & 1]
forms = [tuple(sorted(tuple(sorted((p[u], p[v]))) for u, v in E)) for p in permutations(range(4))]
canon.add(min(forms))
print(len(canon))11
Test yourself
An isomorphism between two graphs is…
- any function between their vertex sets
- a bijection of vertices that maps edges exactly to edges
- a drawing of the graph
- a spanning tree
Two graphs have the same degree sequence. What follows?
- They are isomorphic
- They are not isomorphic
- Nothing definite: they may or may not be isomorphic
- They are both connected
How many labelled simple graphs are there on 5 vertices?