learngraphtheory.org

Guest

Sign in to keep your graphs and progress.

Cover of the book Graph Theory Through Problems

Practice book

Graph Theory Through Problems

300 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.

Available inKindlePaperback

13 chapters

Also by the authorGraph Algorithms for Coding Interviews100 solved interview problems with visual explanations and Python solutions.View on Amazon
Algorithm Selection

Select an algorithm and generate steps to begin visualization

Weisfeiler-Lehman Test

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

Time: O((n + m) log n) per round
Space: O(n + m)
Use Case: Graph isomorphism, chemistry, graph neural networks
Showing the example graph for this algorithm.
Algorithm Execution

Select an algorithm and generate steps to begin visualization

About Weisfeiler-Lehman Test (Colour Refinement)

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.

How it works

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.

Applications

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.

Pseudocode

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 ISOMORPHIC

Each 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.

Worked example, step by step

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.

  1. Round 0. Every vertex has colour 1. The counts agree: six vertices of colour 1 on each side.
  2. Round 1 signatures. Each signature is 1 followed by one 1 per neighbour, so it just records the degree: "1 | 1" for the leaves, "1 | 1,1" for the path vertices, "1 | 1,1,1" for B and R.
  3. Round 1 colours. Three colours: 1 for degree 1, 2 for degree 2, 3 for degree 3. Each graph has three, two and one of them, so the degree sequences do not tell the trees apart.
  4. Round 2 signatures. Now the neighbours' degrees show. In G the leaf E hangs off a degree-2 vertex ("1 | 2"), while A and F hang off B ("1 | 3"). In H both P and T hang off degree-2 vertices ("1 | 2") and only U off R.
  5. Counts differ. The signature "1 | 2" becomes colour 1. G has one vertex of colour 1 (E), H has two (P and T). The graphs cannot be isomorphic.

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.

Complexity, and where it comes from

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).

When to use Weisfeiler-Lehman Test (Colour Refinement), and when not to

WL is a fast filter, not a complete test. Here is how it relates to the other tools.

AlternativePrefer it whenCost
Degree sequenceA first check, equal to one WL round. Cheap, but weaker.O(n log n)
k-dimensional WLColours 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

Common pitfalls

  • Reading equal counts as "isomorphic". Equal counts only mean WL found no difference. Regular graphs of the same degree and size all look alike to it, for example a hexagon and two separate triangles.
  • Numbering colours separately in each graph. The colours must be assigned on both graphs together, from one shared list of signatures. Numbering each graph on its own makes the counts meaningless.
  • Dropping the vertex's own colour from the signature. The signature must start with the old colour. Without it, classes can merge again between rounds and the refinement is no longer guaranteed to stop or to be correct.

Frequently asked questions

What does the Weisfeiler-Lehman test prove?
If the colour counts differ in some round, the graphs are certainly not isomorphic. If they never differ, the test is inconclusive: the graphs may or may not be isomorphic.
For which graphs is WL exact?
For trees, and for almost all random graphs. It fails on regular graphs, where every vertex looks the same at every distance.
What does WL have to do with graph neural networks?
A message-passing GNN updates each vertex from its own state and its neighbours' states, exactly like a WL round. Xu et al. and Morris et al. (2019) showed such networks can distinguish at most the graphs that WL distinguishes.
How many rounds does WL need?
At most n, since each round that continues splits a class. In practice a few rounds suffice: on the example two rounds settle it.

Related algorithms: Breadth-First Search, Graph Coloring, Chordality Check, Bipartite Check, PageRank

An example graph: you can change it, or start blank.

Zoom Controls

47%
Nodes: 12
Edges: 10