TExcelTAD
Home › Discrete Mathematics › Graph Isomorphism

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 G1=(V1,E1)G_1 = (V_1, E_1) and G2=(V2,E2)G_2 = (V_2, E_2) are isomorphic if there is a bijection f:V1→V2f: V_1 \to V_2 such that

{u,v}∈E1  ⟺  {f(u),f(v)}∈E2\{u, v\} \in E_1 \iff \{f(u), f(v)\} \in E_2

The bijection ff 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 C6C_6 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.

python · try it yourself
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 n!n! 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, 2O((log⁡n)c)2^{O((\log n)^c)}, the best known in theory.

python · try it yourself
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 2(n2)2^{\binom{n}{2}} graphs on the vertex set {1,…,n}\{1, \ldots, n\} (each possible edge is in or out). Many of them are isomorphic, so the number of unlabelled graphs is smaller: for n=4n = 4 there are 64 labelled graphs but only 11 up to isomorphism. Each isomorphism class contains at most n!n! labelled graphs, which gives the lower bound 2(n2)/n!2^{\binom{n}{2}} / n! 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 GG has edges ab,bc,cd,da,acab, bc, cd, da, ac; graph HH has edges pq,qr,rs,sp,prpq, qr, rs, sp, pr. Are they isomorphic? Start with the degree sequences.

Answer

Yes, both have degree sequence (3,3,2,2)(3, 3, 2, 2) and the map a↦p,b↦q,c↦r,d↦sa \mapsto p, b \mapsto q, c \mapsto r, d \mapsto s is an isomorphism.

Solution

In GG, aa and cc have degree 3 (they are joined by the diagonal); in HH, pp and rr have degree 3. Both graphs are a 4-cycle plus one diagonal. Mapping a,b,c,da, b, c, d to p,q,r,sp, q, r, s sends ab→pqab \to pq, bc→qrbc \to qr, cd→rscd \to rs, da→spda \to sp, ac→prac \to pr: 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: (2,2,1,1)(2, 2, 1, 1) versus (3,1,1,1)(3, 1, 1, 1).

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 n=4n = 4 (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 4!=244! = 24 relabellings.

python · try it yourself
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…

  1. any function between their vertex sets
  2. a bijection of vertices that maps edges exactly to edges
  3. a drawing of the graph
  4. a spanning tree

Two graphs have the same degree sequence. What follows?

  1. They are isomorphic
  2. They are not isomorphic
  3. Nothing definite: they may or may not be isomorphic
  4. They are both connected

How many labelled simple graphs are there on 5 vertices?

  1. 252^5
  2. 2102^{10}
  3. 5!5!
  4. 10210^2