Search

Search papers and lessons

Graph Isomorphism

Level 2 · 11 min read

Two graphs are isomorphic when a bijection between their vertices keeps every edge and non-edge, so they have the same shape even if they are drawn differently.

On this page

  • Colour = topic mix
  • Discrete math
  • ReadReady to readNot yet

TL;DR

Two graphs are isomorphic when you can rename the vertices of one so that it becomes exactly the other. The renaming is a bijection between the two vertex sets, and it must keep every connection and every missing connection. Isomorphic graphs are the same structure, even when their drawings look different.

Builds on: Graph, Bijective Function · Leads to: Weisfeiler-Lehman Test, graph neural networks

Where it's used

Chemistry. A molecule is drawn as a graph, with atoms as vertices and bonds as edges. Two diagrams that differ only in layout describe the same molecule when they are isomorphic. Chemists comparing a new compound against a database need exactly this test to decide whether the compound is already known.

Checking that two networks have the same shape. A transport planner may receive two maps of the same bus network, drawn by two agencies with different stop labels and different layouts. An isomorphism between the two maps shows which stop on one corresponds to which stop on the other, and that the routes match.

Graph neural networks. A graph neural network should give the same answer for a graph whatever order its vertices are listed in. The tests used to study the power of these networks ask when two graphs are different, and a widely used one, the Weisfeiler-Lehman test, is a fast but incomplete attempt at the isomorphism question.

How it works

Take two graphs with four vertices each. Graph GG has vertices 1,2,3,41, 2, 3, 4 and edges {1,2}\{1,2\}, {2,3}\{2,3\}, {3,4}\{3,4\}, {4,1}\{4,1\}. Graph HH has vertices a,b,c,da, b, c, d and edges {a,c}\{a,c\}, {c,b}\{c,b\}, {b,d}\{b,d\}, {d,a}\{d,a\}. Both are a loop of four vertices, but the labels are different. We ask whether they are the same shape.

  1. Propose a renaming. Let φ\varphi send 1↦a1 \mapsto a, 2↦c2 \mapsto c, 3↦b3 \mapsto b and 4↦d4 \mapsto d. This is a bijection: the four outputs are distinct and cover a,b,c,da, b, c, d.
  2. Send each edge of GG through the renaming. The edge {1,2}\{1,2\} becomes {a,c}\{a,c\}, which is an edge of HH. The edge {2,3}\{2,3\} becomes {c,b}\{c,b\}, also an edge of HH.
  3. Continue with the other two. The edge {3,4}\{3,4\} becomes {b,d}\{b,d\}, and {4,1}\{4,1\} becomes {d,a}\{d,a\}. Both are edges of HH.
  4. Check the reverse direction. GG has four edges and all four land on edges of HH. HH also has exactly four edges, so every edge of HH is the image of an edge of GG. Nothing extra is present on either side.
  5. Because the renaming is a bijection and it maps the edges of GG exactly onto the edges of HH, the two graphs are isomorphic. The renaming φ\varphi is the witness.

The renaming can be drawn as a picture. Both diagrams below are the same loop of four vertices.

flowchart LR
  subgraph G
    v1((1)) --- v2((2))
    v2 --- v3((3))
    v3 --- v4((4))
    v4 --- v1
  end
flowchart LR
  subgraph H
    va((a)) --- vc((c))
    vc --- vb((b))
    vb --- vd((d))
    vd --- va
  end

Run the same check on a graph that is not isomorphic to these. A triangle has three vertices, each joined to the other two, so it has three edges. A path with three vertices in a row has only two edges. No renaming of the vertices can turn three edges into two, so the triangle and the path are not isomorphic. The edge count is a quick test: if the counts differ, stop.

The math

A graph GG has a vertex set VV and an edge set EE, where each edge is a pair {u,v}\{u, v\} of distinct vertices. A graph HH has vertex set WW and edge set FF.

Definition: Graph isomorphism

An isomorphism from GG to HH is a bijection φ:V→W\varphi : V \to W such that for every pair of distinct vertices u,vu, v in VV,

{u,v}∈E  ⟺  {φ(u),φ(v)}∈F.\{u, v\} \in E \iff \{\varphi(u), \varphi(v)\} \in F.

The graphs GG and HH are isomorphic, written G≅HG \cong H, when such a φ\varphi exists. The symbol   ⟺  \iff means "if and only if". In words: uu and vv are joined in GG exactly when their images are joined in HH. The condition works in both directions, so a bijection that keeps only the edges and not the non-edges is not enough.

Step 1 in symbols

For the example, φ(1)=a\varphi(1) = a, φ(2)=c\varphi(2) = c, φ(3)=b\varphi(3) = b and φ(4)=d\varphi(4) = d. The four outputs a,c,b,da, c, b, d are distinct and exhaust WW, so φ\varphi is a bijection.

Step 2 in symbols

For each edge of GG, apply φ\varphi to both ends:

{1,2}↦{a,c},{2,3}↦{c,b},{3,4}↦{b,d},{4,1}↦{d,a}.\{1,2\} \mapsto \{a,c\}, \qquad \{2,3\} \mapsto \{c,b\}, \qquad \{3,4\} \mapsto \{b,d\}, \qquad \{4,1\} \mapsto \{d,a\}.

Each image is in FF. This gives the forward direction of the definition for these pairs.

Step 4 in symbols

Both edge sets have four elements. The map from the edges of GG to the edges of HH that the renaming induces is injective, because φ\varphi is a bijection, and the four images are four distinct edges of FF. Since FF has only four edges, they are all of FF. So the condition holds in the reverse direction too.

A necessary condition

Any isomorphism preserves the number of edges, and it preserves the number of edges at each vertex. If φ(u)=w\varphi(u) = w, then uu and ww have the same degree, where the degree of a vertex is the number of edges touching it. Degrees give a fast way to reject a pair: if the sorted list of degrees differs, the graphs are not isomorphic. Matching degrees do not prove isomorphism, as the common mistakes section shows.

Worked examples

Example
1. Two paths on three vertices

Graph GG is the path 1−2−31 - 2 - 3, with edges {1,2}\{1,2\} and {2,3}\{2,3\}. Graph HH is the path x−z−yx - z - y, with edges {x,z}\{x,z\} and {z,y}\{z,y\}. Find an isomorphism.

  1. Both graphs have three vertices and two edges, so try a bijection that sends the middle vertex to the middle vertex: φ(2)=z\varphi(2) = z.
  2. Send the ends to the ends: φ(1)=x\varphi(1) = x and φ(3)=y\varphi(3) = y. This is a bijection onto {x,z,y}\{x, z, y\}.
  3. Apply φ\varphi to the edges of GG. The edge {1,2}\{1,2\} becomes {x,z}\{x,z\}, an edge of HH. The edge {2,3}\{2,3\} becomes {z,y}\{z,y\}, also an edge of HH.
  4. The pair {1,3}\{1,3\} is not an edge of GG, and its image {x,y}\{x,y\} is not an edge of HH. So the non-edges match as well.

Interpretation: a path is a path however you name its vertices. The only thing that matters is which vertex sits in the middle.

Example
2. The loop of four vertices

Use the two graphs from the walkthrough: GG with edges {1,2}\{1,2\}, {2,3}\{2,3\}, {3,4}\{3,4\}, {4,1}\{4,1\}, and HH with edges {a,c}\{a,c\}, {c,b}\{c,b\}, {b,d}\{b,d\}, {d,a}\{d,a\}.

  1. Count the edges: GG has four, and so does HH.
  2. Check degrees. In GG, every vertex touches exactly two edges. In HH, every vertex also touches exactly two edges. The degree lists match.
  3. Use the bijection φ\varphi from the walkthrough, which sends 1↦a1 \mapsto a, 2↦c2 \mapsto c, 3↦b3 \mapsto b, 4↦d4 \mapsto d.
  4. Each of the four edges of GG maps to an edge of HH, and the four images are all of HH's edges.
  5. The non-edges also match. In GG, the pair {1,3}\{1,3\} is a non-edge, and its image {a,b}\{a,b\} is also a non-edge in HH.

Interpretation: both graphs are the cycle of length four, so the relabelling works. The check on non-edges in step 5 is the part people forget.

Example
3. Two bus maps with different labels

Map AA has a central stop HH joined to PP, QQ and RR, and a route P−QP - Q. Map BB has a central stop KK joined to XX, YY and ZZ, and a route Y−ZY - Z. The two agencies drew them differently. Are they the same network?

  1. List the edges of AA: {H,P}\{H,P\}, {H,Q}\{H,Q\}, {H,R}\{H,R\} and {P,Q}\{P,Q\}. That is four edges.
  2. List the edges of BB: {K,X}\{K,X\}, {K,Y}\{K,Y\}, {K,Z}\{K,Z\} and {Y,Z}\{Y,Z\}. That is also four edges.
  3. Match the hubs and the degree-two stops. Let φ(H)=K\varphi(H) = K, φ(P)=Y\varphi(P) = Y, φ(Q)=Z\varphi(Q) = Z and φ(R)=X\varphi(R) = X. This is a bijection.
  4. Apply φ\varphi to the edges of AA. {H,P}\{H,P\} becomes {K,Y}\{K,Y\}, {H,Q}\{H,Q\} becomes {K,Z}\{K,Z\}, {H,R}\{H,R\} becomes {K,X}\{K,X\}, and {P,Q}\{P,Q\} becomes {Y,Z}\{Y,Z\}. Those are exactly the four edges of BB.
  5. The two maps are isomorphic. Every route on one map corresponds to exactly one route on the other.

Try it: a third map CC has four stops in a row, W−X−Y−ZW - X - Y - Z, with three routes. Is CC isomorphic to AA? Answer: no. CC has three edges and AA has four. No bijection can keep the edges when the counts differ, so the test fails at the first step.

Common mistakes and tradeoffs

Same number of vertices and edges means isomorphic. It is tempting to think that equal counts settle the question. In fact they do not. Take two disjoint triangles and a single six-vertex loop. Both have six vertices and six edges, and every vertex in both has degree two. But the two triangles fall apart into two pieces, while the loop is one piece. A bijection cannot join pieces that do not match, so the graphs are not isomorphic. Matching counts are a necessary condition, not a sufficient one.

A different drawing means a different graph. It is tempting to judge graphs by how they look on the page. In fact the drawing carries no information: moving a vertex, straightening a curved edge or swapping labels does not change the graph. Two drawings that look different may be isomorphic, and two drawings that look alike may not be.

Keeping only the edges is enough. It is tempting to check only that each edge of GG maps to an edge of HH. In fact the definition is an if-and-only-if, so non-edges must match too. Without the reverse check, a graph with fewer edges could appear to map into a graph with more.

When to use it. Use an isomorphism test when the labels are arbitrary and you care only about structure, such as comparing molecules or networks, or deduplicating a list of graphs. Use the quick tests first: the vertex count, the edge count and the sorted degree list reject most non-matches at once.

Alternatives. No general method is known to decide isomorphism in polynomial time for all graphs, and the problem is not known to be NP-complete. In practice, programs use invariants that are fast to compute and stronger than degree, such as the Weisfeiler-Lehman test. A test that says "not isomorphic" is always correct when it fires. A test that says "maybe isomorphic" is only a suggestion, and a full search is needed for a proof.

History

As graph theory developed, isomorphism became the standard notion of "same structure" for graphs, and the bijection in its definition is the same idea of exact pairing used for sets.

Isomorphism is also a hard computational question. Once a bijection is given it can be checked quickly, but no polynomial-time algorithm for all graphs is known. In 2015 László Babai announced an algorithm that runs in quasi-polynomial time, which is much faster than a brute-force search over all bijections but slower than polynomial time.

Recap

  • A Graph is a set of vertices joined by edges.
  • An isomorphism between two graphs is a bijection between their vertex sets that keeps every edge and every non-edge.
  • Equal vertex counts, edge counts and degree lists are necessary for isomorphism, but they do not prove it.
  • To show two graphs are isomorphic, give the bijection and check every edge in both directions.
  • Stronger tests such as the Weisfeiler-Lehman Test can often reject a pair quickly, and they are the bridge to the question of what graph neural networks can and cannot tell apart.

Check yourself

QuestionAre the paths 1−2−3−41 - 2 - 3 - 4 and a−c−b−da - c - b - d isomorphic? The first has edges {1,2}\{1,2\}, {2,3}\{2,3\}, {3,4}\{3,4\}; the second has edges {a,c}\{a,c\}, {c,b}\{c,b\}, {b,d}\{b,d\}.Show answer

Yes. Use the map 1↦a1 \mapsto a, 2↦c2 \mapsto c, 3↦b3 \mapsto b, 4↦d4 \mapsto d. It is a bijection. The three edges of the first path map to {a,c}\{a,c\}, {c,b}\{c,b\} and {b,d}\{b,d\}, which are exactly the edges of the second. Both paths have three edges, so no edge is left out on either side.

QuestionA graph GG has five vertices and six edges. Can a graph HH with five vertices and five edges be isomorphic to GG? Give the reason in one sentence.Show answer

No. An isomorphism is a bijection that maps edges to edges and non-edges to non-edges, so it preserves the number of edges. Six edges cannot correspond to five.

QuestionGive the degree of each vertex of the triangle on {p,q,r}\{p, q, r\} and of the path x−y−zx - y - z, and explain whether the degree lists settle whether the two graphs are isomorphic.Show answer

In the triangle, each vertex touches two edges, so the degrees are 2,2,22, 2, 2. In the path, xx and zz have degree 11 and yy has degree 22, so the degrees are 1,2,11, 2, 1. The lists differ, so the graphs are not isomorphic. Here the degree test was enough, but in general matching degree lists do not prove isomorphism.

QuestionA chemist draws two diagrams of a molecule with the atoms labelled differently and the diagrams laid out differently. Explain how an isomorphism between the two diagrams shows that they describe the same molecule.Show answer

Each diagram is a graph whose vertices are atoms and whose edges are bonds. An isomorphism is a bijection from the atoms of one diagram to the atoms of the other that sends every bond to a bond and every non-bond to a non-bond. Such a bijection shows that each atom in one diagram corresponds to exactly one atom in the other, with the same bonds between them. The layout and labels do not matter, so the two diagrams describe the same connections.

Sources