The Isomorphism Question
Two graphs are isomorphic when one can be relabeled to become the other: there is a bijection between vertices that preserves adjacency and, when present, vertex or edge labels. The difficulty is that vertex names carry no structural meaning. A useful test must describe vertices in a way that survives renaming.
Intuition: Neighborhood Fingerprints
At first, every unlabeled vertex looks identical. Ask each vertex what kinds of neighbors it has, replace its color with that answer, and repeat. A vertex's color gradually summarizes a larger neighborhood around it—without choosing an ordering for those neighbors.
The one-dimensional Weisfeiler–Leman algorithm, or 1-WL, maintains a partition of the vertices into color classes. Same color means “not yet distinguished by this test.” Different colors certify different rooted local structures at the current depth.
Initial color
Use one shared color for an unlabeled graph, or compress meaningful input labels/features.
Color class
Vertices that currently have the same structural summary belong to one class.
Compression
Map equal signatures to equal compact IDs and unequal signatures to unequal IDs.
Colors as Local Structural Summaries
At round $t+1$, vertex $v$ combines its old color with the multiset of colors on its neighbors:
- Read every vertex's colors from round $t$; do not update in place.
- Sort or count the neighbor colors so the signature is independent of adjacency-list order.
- Include the vertex's own old color, ensuring refinement may split a class but never merge two old classes.
- Assign the same new integer to identical signatures using one shared compression map.
- Stop when no color class splits.
Why a Multiset, Not a Set?
Multiplicity carries degree information. A vertex with neighbor colors {teal, teal, blue} must differ from one with {teal, blue}. A plain set would erase the repeated teal neighbor and weaken the test immediately.
Worked Refinement Trace
Consider edges $A-B$, $B-C$, $C-D$, and $C-E$. All vertices begin navy. Round one separates vertices by degree. Round two notices that leaf $A$ touches the degree-two vertex $B$, while leaves $D$ and $E$ touch the degree-three vertex $C$.
| Vertex | Round 1 clue | Round 2 neighbor-color clue | Stable class |
|---|---|---|---|
| $A$ | degree 1 | touches the degree-2 class | $\{A\}$ |
| $B$ | degree 2 | touches leaf and degree-3 classes | $\{B\}$ |
| $C$ | degree 3 | touches degree-2 and two leaf vertices | $\{C\}$ |
| $D,E$ | degree 1 | each touches the degree-3 class | $\{D,E\}$ |
How to Compare Two Graphs Correctly
Color numbers have meaning only inside the compression system that created them. Refining two graphs independently and comparing only sorted class sizes is unsafe: two graphs can have classes of the same sizes produced by different signatures.
- Reject immediately if vertex counts or compatible input-label histograms differ.
- Form the disjoint union of the two graphs—no cross edges—and refine both components with one shared signature-to-color map.
- After every round, compare the color histogram in the first component with the histogram in the second.
- If a histogram differs, return definitely non-isomorphic. If refinement stabilizes with equal histograms, return inconclusive, not “isomorphic.”
One-Sided Verdict
Different synchronized color histograms are a proof of non-isomorphism. Equal histograms only mean that 1-WL has found no contradiction.
A Visual Failure Case
A six-cycle $C_6$ and two disjoint triangles $2C_3$ are plainly non-isomorphic—one graph is connected and the other is not. Yet every vertex has degree two and sees two neighbors of the same color in every round. Starting from one unlabeled color, 1-WL never splits either graph.
Soundness and Stabilization
Soundness follows by induction. An isomorphism preserves initial labels. If corresponding vertices have equal colors at round $t$, the isomorphism bijects their neighbors and therefore preserves the multiset of neighbor colors. Their next signatures—and compressed colors—remain equal. Thus an isomorphic pair can never develop different synchronized histograms.
Termination follows from refinement monotonicity. Including the old color means old classes never merge. Every non-stable round splits at least one class and increases the number of classes, which is at most $V$. The final partition is equitable: vertices in one class have the same number of neighbors in every color class.
What the Proof Does Not Say
It proves that histogram mismatch is impossible for isomorphic graphs. It does not prove the converse. The six-cycle example has equal histograms but no isomorphism between the graphs.
Connection to Message-Passing GNNs
A typical message-passing layer aggregates neighbor representations with a permutation-invariant operation, then updates each vertex from its previous representation and the aggregate. That mirrors the 1-WL signature rule.
Under the standard message-passing model, such GNNs are at most as discriminative as 1-WL. With sufficiently expressive injective aggregation and update functions, architectures can match 1-WL's distinguishing power. Weaker aggregators may lose information—for example, a mean can erase multiplicity—so “bounded by 1-WL” does not mean every GNN automatically reaches the bound.
Higher-Dimensional WL
$k$-WL colors ordered $k$-tuples of vertices rather than individual vertices. Tuple relationships expose interactions that 1-WL cannot see, and increasing $k$ yields a stronger hierarchy of tests. The cost also rises sharply: there are $V^k$ tuples before refinement overhead, so higher-dimensional methods trade tractability for expressive power.
Beyond the 1-WL Ceiling
Practical models can add positional encodings, unique identifiers, subgraph features, higher-order states, or global mechanisms. Those additions change the information available to the model; they are not counterexamples to the 1-WL bound for plain local message passing.
Implementation
The safest comparison implementation refines the disjoint union of the two input graphs. That guarantees equal signatures in either component receive the same color ID. The routine below returns the full refinement history for inspection and uses a three-valued comparison result: different or inconclusive.
Using the Other Implementations for Comparison
The C++ and Java tabs show the single-graph refinement core. To compare graphs, offset the second graph's vertex indices, concatenate the adjacency lists into a disjoint union, run refine once, and compare the two component histograms—exactly as the Python tab does.
Complexity and Alternatives
There are at most $V-1$ strict splitting rounds. In the straightforward code above, sorting neighbor colors costs $\sum_v O(d(v)\log d(v))$ per round, bounded by $O(E\log V)$. Including signature construction and compression gives the conservative bound
That bound describes the readable implementation, not the best possible color-refinement engine. Counting small integer colors and refining partitions with worklists avoids repeatedly sorting every neighborhood; established partition-refinement implementations can compute the stable coloring in $O((V+E)\log V)$ time.
| Method | Answer | Strength | Typical cost intuition |
|---|---|---|---|
| 1-WL | different / inconclusive | local color refinement | fast; near-linear refinement is possible |
| Exact isomorphism | yes / no | complete decision | more involved; use after filtering |
| $k$-WL | different / inconclusive | stronger tuple refinement | state space begins at $V^k$ |
| Message-passing GNN | learned representation | at most 1-WL in the plain model | depends on layers, width, and edges |
Where 1-WL Is Useful
Designing More Expressive Graph Neural Networks
Researchers use 1-WL as a clean baseline for asking which graph pairs a message-passing architecture could distinguish in principle. Higher-order states, positional features, subgraph counts, and global mechanisms can then be evaluated by the extra distinctions they enable.
- Isomorphism prefilter: discard pairs proved different before invoking an exact algorithm.
- Graph indexing: use refinement-derived summaries as inexpensive search keys, while remembering they are not unique canonical identifiers.
- Feature engineering: count refined labels to summarize recurring local structures in graph classification pipelines.
- Model diagnostics: build controlled graph pairs that expose what a GNN's aggregation rule preserves or loses.
Common Pitfalls and a Reliable Checklist
| Pitfall | Why it fails | Reliable practice |
|---|---|---|
| Refining graphs independently | color IDs do not share meaning | refine the disjoint union |
| Comparing only class sizes | equal sizes may encode different signatures | compare synchronized color histograms |
| Using a neighbor set | erases multiplicity and degree | use a sorted multiset or counts |
| Updating colors in place | makes a round depend on vertex order | compute all signatures from the old array |
| Dropping the old color | may merge previously distinct classes | include $(c_t(v),\ldots)$ in every signature |
| Declaring equality after a match | 1-WL has false negatives for non-isomorphism | report “inconclusive” |
Implementation Checklist
Validate adjacency indices; preserve multiplicities if parallel edges matter; decide how self-loops contribute; encode incoming and outgoing multisets separately for directed graphs; align input-label semantics; and never expose process-randomized hash values as persistent color IDs.
Exercises
- Trace: reproduce the three rounds in the first diagram, writing the exact signature of each vertex before compression.
- Failure case: run the Python comparison on $C_6$ and $2C_3$. Explain why every vertex receives the same signature forever even though connectedness differs.
- Relabeling: randomly permute the indices of a graph. Verify that synchronized histograms stay equal even if the numeric color IDs or vertex order look different.
- Directed extension: replace one neighbor multiset with separate incoming- and outgoing-color multisets. Which orientation patterns become distinguishable?
- GNN connection: identify the 1-WL counterparts of
AGGandUPDATE. Give an example in which mean aggregation loses multiplicity information.
Historical Note and Takeaway
The method stems from work by Boris Weisfeiler and Andrei Leman in the late 1960s. Decades later, color refinement supplied the right abstraction for characterizing the expressive limits of neighborhood-aggregation graph neural networks.
The Mental Model to Keep
1-WL repeatedly replaces each vertex's color with a lossless code for “my previous color plus the multiset of my neighbors' colors.” A mismatch is a certificate of non-isomorphism; a match is only the end of this test's evidence. That one-sided logic is the key to using the algorithm—and its connection to GNNs—correctly.