Guest
Sign in to keep your graphs and progress.

Practice book
Graph Theory Through Problems300 Exercises with Hints, Worked Algorithms and Complete Solutions
By Mohammed Islam Hadjoudj
Learn graph theory by solving problems: 300 exercises, from routine checks to challenging proofs, each with a complete solution.
Also by the authorGraph Algorithms for Coding Interviews100 solved interview problems with visual explanations and Python solutions.View on AmazonSelect an algorithm and generate steps to begin visualization
Are these two graphs the same?
Recolours every vertex by its colour and its neighbours' colours, round by round, and compares the colour counts of two graphs to test isomorphism
Select an algorithm and generate steps to begin visualization
Two graphs are isomorphic when they are the same graph drawn differently: some renaming of the vertices turns one into the other. Deciding this is surprisingly hard in general, and no polynomial algorithm is known. The Weisfeiler-Lehman test, also called colour refinement or 1-WL, is the classic quick test. It gives every vertex a colour that summarises what the vertex looks like from inside its graph, refines these colours round by round, and compares how many vertices of each colour the two graphs have. Different counts prove that the graphs are not isomorphic. Equal counts mean only that WL cannot tell them apart. The method goes back to Boris Weisfeiler and Andrei Lehman in 1968.
All vertices start with the same colour. In each round, every vertex writes down a signature: its own colour followed by the sorted list of its neighbours' colours. Vertices with the same signature get the same new colour, and different signatures get different colours, numbered the same way in both graphs so the colours can be compared. After the first round the colour is the degree, after the second it also reflects the neighbours' degrees, and so on. After each round the algorithm counts each colour in both graphs: if a count differs, the graphs are not isomorphic and it stops. When a round no longer splits any colour, the refinement is stable and the answer is "possibly isomorphic". At most n rounds are needed, each taking O((n + m) log n) with sorting. The visualizer shows each vertex's signature as a badge, colours the classes and keeps a table of the counts in G and H.
Colour refinement is the first step of practical isomorphism tools such as nauty and Traces, which split vertices into WL classes before searching for a mapping. It identifies almost all random graphs and decides isomorphism exactly for trees. In chemistry it compares molecules and finds equivalent atoms. In machine learning, the WL kernel compares graphs by their colour histograms, and graph neural networks are known to be at most as powerful as the WL test at telling graphs apart, which makes it the standard yardstick for their expressiveness. Its known blind spots are just as instructive: regular graphs of the same degree, such as a hexagon and two triangles, all look alike to it.
Colour refinement on both graphs at once, so the colour numbers mean the same thing in G and in H.
WL(G, H):
colour[v] = 1 for every vertex of G and H
repeat:
for each vertex v:
sig[v] = (colour[v], sorted colours of the neighbours)
number the distinct signatures 1, 2, 3...
colour[v] = number of sig[v]
if some colour is counted differently in G and in H:
return NOT ISOMORPHIC
until the number of colours stops growing
return POSSIBLY ISOMORPHICEach new colour includes the old one, so the colouring only ever splits classes. When a round splits nothing, no later round will either, and the refinement is stable.
Compare the two trees this page loads.
Example graph: G (top): the path A-B-C-D-E with F attached to B. H (below): the path P-Q-R-S-T with U attached to R. Both have six vertices, five edges and the degree sequence 3, 2, 2, 1, 1, 1.
Not isomorphic, proven in round 2. The difference is where the branch sits: next to the end of the path in G, in the middle in H. Round 1 alone, which is the degree sequence, could not see it.
Time: O((n + m) log n) per round, at most n rounds · Space: O(n + m)
A round builds a signature for every vertex, whose length is its degree, so all signatures together have length O(n + m). Sorting them to number the distinct ones costs O((n + m) log n). Each round that continues splits at least one class, and there can be at most n classes, so there are at most n rounds. Smarter partition refinement (in the style of Hopcroft's DFA minimisation) brings the whole computation to O((n + m) log n).
WL is a fast filter, not a complete test. Here is how it relates to the other tools.
| Alternative | Prefer it when | Cost |
|---|---|---|
| Degree sequence | A first check, equal to one WL round. Cheap, but weaker. | O(n log n) |
| k-dimensional WL | Colours k-tuples of vertices instead of single vertices. Distinguishes more graphs (2-WL separates the hexagon from two triangles). | O(n^(k+1) log n) |
| Backtracking search (nauty, VF2) | A definite yes or no with an explicit isomorphism. Uses WL classes to prune the search. | exponential worst case, fast in practice |
Read the full article: Graph Isomorphism: When Are Two Graphs the Same?
Read the full article: Graph Neural Networks: A Practical Introduction
Related algorithms: Breadth-First Search, Graph Coloring, Chordality Check, Bipartite Check, PageRank