Documentation

CslibFrontier.Foundations.Combinatorics.SimpleGraph.ColorRefinement

Color refinement #

Color refinement, also called the 1-dimensional Weisfeiler–Leman algorithm or naive vertex classification, refines a vertex coloring χ₀ of a graph G round by round: in each round, a vertex is recolored by the pair of its old color and the multiset of the old colors of its neighbors. Two vertices that have the same color so far get different colors in the next round when, for some color c, they have different numbers of neighbors of color c.

The colors of round n live in a type RefinementColor α n that depends only on the type α of initial colors and on n, not on the graph. Colors of different graphs are therefore directly comparable, which is the canonical color naming behind the color refinement isomorphism test.

A coloring χ is equitable (SimpleGraph.IsEquitable) when vertices of the same color have the same multiset of neighbor colors, that is, when one round of refinement does not split any color class. For partitions, given as setoids, this is the classical notion of an equitable partition (SimpleGraph.IsEquitablePartition): any two vertices of a block have the same number of neighbors in every block. This is unrelated to Mathlib's Finpartition.IsEquipartition, which asks for blocks of almost equal sizes.

A coloring is compared with others through its partition into color classes, Setoid.ker χ, and Cslib.SetPartition.blocks counts the classes. The partitions of successive rounds refine each other, and the number of classes increases strictly until the partition stops changing. So on a finite graph whose initial coloring has k₀ classes, the partition is stable after |V| - k₀ rounds. The coloring after |V| rounds, SimpleGraph.stableColoring, is equitable, and its partition is the coarsest equitable partition refining the initial one.

A graph isomorphism transports the colors of every round, so isomorphic graphs have the same color histogram (multiset of vertex colors) in every round. This is the soundness of the color refinement isomorphism test. The test is incomplete: two d-regular graphs with the same number of vertices have the same histograms in every round. Running color refinement on the disjoint union shows that agreement in round |V| + |W| implies agreement in every round.

The graphs are assumed LocallyFinite, which is what neighbor multisets need. Mathlib provides this instance for a disjoint union G ⊕g H but no decidable adjacency instance.

Main definitions #

Main results #

Implementation notes #

[GKMS14] describe color refinement on graphs in words at the start of Section 1 (Introduction). In its paragraph "Colour Refinement on Matrices" they work with the color refinement of a matrix A ∈ ℝ^{V×W}, a weighted bipartite graph whose two sides are the rows V and the columns W, from the trivial partition. They state stabilization within |V| + |W| rounds, the number of vertices of that one bipartite graph, and that the result is the coarsest equitable partition. A 0-1 matrix gives the bipartite graph B_A; according to the source, general weighted directed graphs correspond to square matrices, but the source presents only the bipartite case. The present file treats simple graphs with an arbitrary initial coloring directly. The bound of |V| - k₀ rounds is the standard counting argument. In SimpleGraph.forall_colorHistogram_eq_iff, V and W are the vertex types of two graphs, so its |V| + |W| is the order of their disjoint union and is unrelated to the source's bound. That lemma and the regular-graph example are standard.

References #

def SimpleGraph.RefinementColor (α : Type u_1) :
ℕ → Type u_1

The colors of round n of color refinement started from colors in α: a color of round n + 1 is a color of round n together with a multiset of colors of round n.

@[instance_reducible]

Colors of every round have decidable equality when the initial colors do.

theorem Multiset.map_eq_map_of_ker_le {α : Type u_3} {β : Type u_4} {γ : Type u_5} {f : α → β} {g : α → γ} (h : Setoid.ker f ≤ Setoid.ker g) {s t : Multiset α} (hst : map f s = map f t) :
map g s = map g t

If g is constant on the fibers of f, then two multisets with the same image under f have the same image under g.

def SimpleGraph.refineColoring {V : Type u_1} {β : Type u_4} (G : SimpleGraph V) [G.LocallyFinite] (χ : V → β) (v : V) :
β × Multiset β

One round of color refinement: the color of v together with the multiset of the colors of its neighbors.

def SimpleGraph.refinementColoring {V : Type u_1} {α : Type u_3} (G : SimpleGraph V) [G.LocallyFinite] (χ₀ : V → α) (n : ℕ) :
V → RefinementColor α n

The coloring after n rounds of color refinement started from χ₀.

def SimpleGraph.IsEquitable {V : Type u_1} {β : Type u_4} (G : SimpleGraph V) [G.LocallyFinite] (χ : V → β) :

A coloring is equitable when vertices of the same color have the same multiset of neighbor colors.

A partition of the vertices, given as a setoid, is equitable when any two vertices of the same block have the same number of neighbors in every block. The numbers are counted in ℕ∞, so the definition also makes sense when neighborhoods are infinite.

def SimpleGraph.stableColoring {V : Type u_1} {α : Type u_3} [Fintype V] (G : SimpleGraph V) [G.LocallyFinite] (χ₀ : V → α) :

The coloring after |V| rounds of color refinement started from χ₀. By SimpleGraph.ker_refinementColoring_eq, its partition is the stable partition.

def SimpleGraph.colorHistogram {V : Type u_1} {α : Type u_3} [Fintype V] (G : SimpleGraph V) [G.LocallyFinite] (χ₀ : V → α) (n : ℕ) :

The color histogram of round n: the multiset of the colors of the vertices after n rounds.

One round #

theorem SimpleGraph.refineColoring_apply {V : Type u_1} {β : Type u_4} {G : SimpleGraph V} [G.LocallyFinite] (χ : V → β) (v : V) :
theorem SimpleGraph.refinementColoring_zero {V : Type u_1} {α : Type u_3} {G : SimpleGraph V} [G.LocallyFinite] {χ₀ : V → α} :
G.refinementColoring χ₀ 0 = χ₀
theorem SimpleGraph.refinementColoring_succ {V : Type u_1} {α : Type u_3} {G : SimpleGraph V} [G.LocallyFinite] {χ₀ : V → α} (n : ℕ) :
theorem SimpleGraph.stableColoring_eq {V : Type u_1} {α : Type u_3} {G : SimpleGraph V} [G.LocallyFinite] {χ₀ : V → α} [Fintype V] :
theorem SimpleGraph.colorHistogram_eq {V : Type u_1} {α : Type u_3} {G : SimpleGraph V} [G.LocallyFinite] {χ₀ : V → α} [Fintype V] (n : ℕ) :
theorem SimpleGraph.colorHistogram_zero_const {V : Type u_1} {α : Type u_3} {G : SimpleGraph V} [G.LocallyFinite] [Fintype V] (c : α) :
G.colorHistogram (fun (x : V) => c) 0 = Multiset.replicate (Fintype.card V) c
theorem SimpleGraph.colorHistogram_zero_const_eq_iff {V : Type u_1} {W : Type u_2} {α : Type u_3} {G : SimpleGraph V} [G.LocallyFinite] [Fintype V] [Fintype W] {H : SimpleGraph W} [H.LocallyFinite] (c : α) :
G.colorHistogram (fun (x : V) => c) 0 = H.colorHistogram (fun (x : W) => c) 0 ↔ Fintype.card V = Fintype.card W
theorem SimpleGraph.mem_colorHistogram {V : Type u_1} {α : Type u_3} {G : SimpleGraph V} [G.LocallyFinite] {χ₀ : V → α} [Fintype V] {n : ℕ} {c : RefinementColor α n} :
c ∈ G.colorHistogram χ₀ n ↔ ∃ (v : V), G.refinementColoring χ₀ n v = c
theorem SimpleGraph.isEquitable_iff {V : Type u_1} {β : Type u_4} {G : SimpleGraph V} [G.LocallyFinite] {χ : V → β} :
G.IsEquitable χ ↔ ∀ ⦃v w : V⦄, χ v = χ w → Multiset.map χ (G.neighborFinset v).val = Multiset.map χ (G.neighborFinset w).val
theorem SimpleGraph.refineColoring_eq_refineColoring_iff {V : Type u_1} {W : Type u_2} {β : Type u_4} {G : SimpleGraph V} [G.LocallyFinite] {H : SimpleGraph W} [H.LocallyFinite] {χ : V → β} {ψ : W → β} {v : V} {w : W} :
theorem SimpleGraph.card_refineColoring_snd {V : Type u_1} {β : Type u_4} {G : SimpleGraph V} [G.LocallyFinite] (χ : V → β) (v : V) :
(G.refineColoring χ v).2.card = G.degree v

The multiset part of a refined color has as many elements as the vertex has neighbors.

theorem SimpleGraph.ker_refineColoring_le {V : Type u_1} {β : Type u_4} {G : SimpleGraph V} [G.LocallyFinite] (χ : V → β) :

A round of refinement refines the partition.

theorem SimpleGraph.ker_refineColoring_mono {V : Type u_1} {β : Type u_4} {γ : Type u_5} {G : SimpleGraph V} [G.LocallyFinite] {χ : V → β} {ψ : V → γ} (h : Setoid.ker χ ≤ Setoid.ker ψ) :

A round of refinement is monotone in the partition.

theorem SimpleGraph.isEquitable_iff_ker_le {V : Type u_1} {β : Type u_4} {G : SimpleGraph V} [G.LocallyFinite] {χ : V → β} :

A coloring is equitable exactly when a round of refinement does not split its classes.

theorem SimpleGraph.isEquitable_congr_ker {V : Type u_1} {β : Type u_4} {γ : Type u_5} {G : SimpleGraph V} [G.LocallyFinite] {χ : V → β} {ψ : V → γ} (h : Setoid.ker χ = Setoid.ker ψ) :

Being equitable depends only on the partition into color classes.

theorem SimpleGraph.isEquitable_const_iff {V : Type u_1} {β : Type u_4} {G : SimpleGraph V} [G.LocallyFinite] (b : β) :
(G.IsEquitable fun (x : V) => b) ↔ ∀ (v w : V), G.degree v = G.degree w

A constant coloring is equitable exactly when all vertices have the same degree.

theorem SimpleGraph.isEquitable_iff_card_filter {V : Type u_1} {β : Type u_4} {G : SimpleGraph V} [G.LocallyFinite] [DecidableEq β] {χ : V → β} :
G.IsEquitable χ ↔ ∀ ⦃v w : V⦄, χ v = χ w → ∀ (c : β), {x ∈ G.neighborFinset v | χ x = c}.card = {x ∈ G.neighborFinset w | χ x = c}.card

A coloring is equitable exactly when vertices of the same color have the same number of neighbors of each color.

A partition is equitable exactly when its quotient map is an equitable coloring.

theorem SimpleGraph.isEquitablePartition_ker_iff {V : Type u_1} {β : Type u_4} {G : SimpleGraph V} [G.LocallyFinite] {χ : V → β} :

The partition into the color classes of χ is equitable exactly when χ is.

Refinement and stabilization #

theorem SimpleGraph.ker_refinementColoring_succ_le {V : Type u_1} {α : Type u_3} {G : SimpleGraph V} [G.LocallyFinite] {χ₀ : V → α} (n : ℕ) :

Each round refines the partition of the previous round.

theorem SimpleGraph.antitone_ker_refinementColoring {V : Type u_1} {α : Type u_3} {G : SimpleGraph V} [G.LocallyFinite] {χ₀ : V → α} :
Antitone fun (n : ℕ) => Setoid.ker (G.refinementColoring χ₀ n)
theorem SimpleGraph.ker_refinementColoring_le_ker {V : Type u_1} {α : Type u_3} {G : SimpleGraph V} [G.LocallyFinite] {χ₀ : V → α} (n : ℕ) :

Every round refines the initial coloring.

theorem SimpleGraph.isEquitable_refinementColoring_iff {V : Type u_1} {α : Type u_3} {G : SimpleGraph V} [G.LocallyFinite] {χ₀ : V → α} {n : ℕ} :

The coloring of round n is equitable exactly when round n + 1 has the same partition.

theorem SimpleGraph.ker_refinementColoring_eq_of_succ_eq {V : Type u_1} {α : Type u_3} {G : SimpleGraph V} [G.LocallyFinite] {χ₀ : V → α} {n : ℕ} (h : Setoid.ker (G.refinementColoring χ₀ (n + 1)) = Setoid.ker (G.refinementColoring χ₀ n)) {m : ℕ} (hm : n ≤ m) :

Once a round does not change the partition, no later round does.

theorem SimpleGraph.IsEquitable.ker_le_ker_refinementColoring {V : Type u_1} {α : Type u_3} {β : Type u_4} {G : SimpleGraph V} [G.LocallyFinite] {χ₀ : V → α} {ψ : V → β} (hψ : G.IsEquitable ψ) (h : Setoid.ker ψ ≤ Setoid.ker χ₀) (n : ℕ) :

An equitable partition refining the initial coloring refines the coloring of every round.

The number of color classes does not decrease from one round to the next.

The number of color classes increases strictly as long as the partition changes.

Stabilization: if the initial coloring has k₀ classes, then the partition does not change after round |V| - k₀.

theorem SimpleGraph.ker_stableColoring {V : Type u_1} {α : Type u_3} {G : SimpleGraph V} [G.LocallyFinite] {χ₀ : V → α} [Fintype V] {m : ℕ} (hm : Fintype.card V ≤ m) :

From round |V| on, the partition is that of the stable coloring.

theorem SimpleGraph.isEquitable_stableColoring {V : Type u_1} {α : Type u_3} {G : SimpleGraph V} [G.LocallyFinite] {χ₀ : V → α} [Fintype V] :

The stable coloring is equitable.

theorem SimpleGraph.ker_stableColoring_le {V : Type u_1} {α : Type u_3} {G : SimpleGraph V} [G.LocallyFinite] {χ₀ : V → α} [Fintype V] :

The stable coloring refines the initial coloring.

theorem SimpleGraph.IsEquitable.ker_le_ker_stableColoring {V : Type u_1} {α : Type u_3} {β : Type u_4} {G : SimpleGraph V} [G.LocallyFinite] {χ₀ : V → α} [Fintype V] {ψ : V → β} (hψ : G.IsEquitable ψ) (h : Setoid.ker ψ ≤ Setoid.ker χ₀) :

Every equitable coloring refining the initial coloring refines the stable coloring.

theorem SimpleGraph.isGreatest_ker_stableColoring {V : Type u_1} {α : Type u_3} {G : SimpleGraph V} [G.LocallyFinite] {χ₀ : V → α} [Fintype V] :

Coarsest equitable partition: the partition of the stable coloring is the coarsest equitable partition refining the initial coloring.

Isomorphism invariance and soundness #

theorem SimpleGraph.Iso.neighborFinset_apply {V : Type u_1} {W : Type u_2} {G : SimpleGraph V} [G.LocallyFinite] {H : SimpleGraph W} [H.LocallyFinite] (e : G ≃g H) (v : V) :

An isomorphism maps the neighbors of v onto the neighbors of its image.

theorem SimpleGraph.refinementColoring_apply_of_neighborFinset_map {V : Type u_1} {W : Type u_2} {α : Type u_3} {G : SimpleGraph V} [G.LocallyFinite] {H : SimpleGraph W} [H.LocallyFinite] (f : V ↪ W) (hf : ∀ (v : V), H.neighborFinset (f v) = Finset.map f (G.neighborFinset v)) {χ₀ : V → α} {ψ₀ : W → α} (h₀ : ∀ (v : V), ψ₀ (f v) = χ₀ v) (n : ℕ) (v : V) :
H.refinementColoring ψ₀ n (f v) = G.refinementColoring χ₀ n v

An embedding that maps the neighbors of every v onto the neighbors of its image transports every round of color refinement.

theorem SimpleGraph.Iso.refinementColoring_apply {V : Type u_1} {W : Type u_2} {α : Type u_3} {G : SimpleGraph V} [G.LocallyFinite] {H : SimpleGraph W} [H.LocallyFinite] (e : G ≃g H) {χ₀ : V → α} {ψ₀ : W → α} (h₀ : ∀ (v : V), ψ₀ (e v) = χ₀ v) (n : ℕ) (v : V) :
H.refinementColoring ψ₀ n (e v) = G.refinementColoring χ₀ n v

An isomorphism of colored graphs preserves the colors of every round.

theorem SimpleGraph.Iso.colorHistogram_eq {V : Type u_1} {W : Type u_2} {α : Type u_3} {G : SimpleGraph V} [G.LocallyFinite] {H : SimpleGraph W} [H.LocallyFinite] [Fintype V] [Fintype W] (e : G ≃g H) {χ₀ : V → α} {ψ₀ : W → α} (h₀ : ∀ (v : V), ψ₀ (e v) = χ₀ v) (n : ℕ) :
H.colorHistogram ψ₀ n = G.colorHistogram χ₀ n

Isomorphic colored graphs have the same color histogram in every round.

theorem SimpleGraph.not_exists_iso_of_colorHistogram_ne {V : Type u_1} {W : Type u_2} {α : Type u_3} {G : SimpleGraph V} [G.LocallyFinite] {H : SimpleGraph W} [H.LocallyFinite] [Fintype V] [Fintype W] {χ₀ : V → α} {ψ₀ : W → α} {n : ℕ} (h : G.colorHistogram χ₀ n ≠ H.colorHistogram ψ₀ n) :
¬∃ (e : G ≃g H), ∀ (v : V), ψ₀ (e v) = χ₀ v

Soundness of the color refinement test for colored graphs: if the color histograms differ in some round, then no isomorphism preserves the initial colors.

theorem SimpleGraph.isEmpty_iso_of_colorHistogram_ne {V : Type u_1} {W : Type u_2} {G : SimpleGraph V} [G.LocallyFinite] {H : SimpleGraph W} [H.LocallyFinite] [Fintype V] [Fintype W] {n : ℕ} (h : G.colorHistogram (fun (x : V) => ()) n ≠ H.colorHistogram (fun (x : W) => ()) n) :

Soundness of the color refinement test: if the color histograms from the constant coloring differ in some round, then the graphs are not isomorphic.

Disjoint unions #

theorem SimpleGraph.refinementColoring_sum_inl {V : Type u_1} {W : Type u_2} {α : Type u_3} {G : SimpleGraph V} [G.LocallyFinite] {H : SimpleGraph W} [(G ⊕g H).LocallyFinite] (χ₀ : V ⊕ W → α) (n : ℕ) (v : V) :

On the left summand, color refinement of a disjoint union is color refinement of G.

theorem SimpleGraph.refinementColoring_sum_inr {V : Type u_1} {W : Type u_2} {α : Type u_3} {G : SimpleGraph V} {H : SimpleGraph W} [H.LocallyFinite] [(G ⊕g H).LocallyFinite] (χ₀ : V ⊕ W → α) (n : ℕ) (w : W) :

On the right summand, color refinement of a disjoint union is color refinement of H.

theorem SimpleGraph.colorHistogram_sum {V : Type u_1} {W : Type u_2} {α : Type u_3} {G : SimpleGraph V} [G.LocallyFinite] {H : SimpleGraph W} [H.LocallyFinite] [(G ⊕g H).LocallyFinite] [Fintype V] [Fintype W] (χ₀ : V ⊕ W → α) (n : ℕ) :
(G ⊕g H).colorHistogram χ₀ n = G.colorHistogram (χ₀ ∘ Sum.inl) n + H.colorHistogram (χ₀ ∘ Sum.inr) n

The histogram of a disjoint union is the sum of the histograms.

Earlier rounds and completeness of round |V| + |W| #

Forget the last k rounds of a color of round n + k.

theorem SimpleGraph.truncate_refinementColoring {V : Type u_1} {α : Type u_3} {G : SimpleGraph V} [G.LocallyFinite] (χ₀ : V → α) (n k : ℕ) (v : V) :

The color of round n + k determines the color of round n.

theorem SimpleGraph.refinementColoring_eq_of_le {V : Type u_1} {W : Type u_2} {α : Type u_3} {G : SimpleGraph V} [G.LocallyFinite] {H : SimpleGraph W} [H.LocallyFinite] {χ₀ : V → α} {ψ₀ : W → α} {m n : ℕ} (hnm : n ≤ m) {v : V} {w : W} (h : G.refinementColoring χ₀ m v = H.refinementColoring ψ₀ m w) :
G.refinementColoring χ₀ n v = H.refinementColoring ψ₀ n w

Colors that agree in some round agree in every earlier round.

theorem SimpleGraph.colorHistogram_map_truncate {V : Type u_1} {α : Type u_3} {G : SimpleGraph V} [G.LocallyFinite] [Fintype V] (χ₀ : V → α) (n k : ℕ) :

The histogram of round n + k determines the histogram of round n.

theorem SimpleGraph.colorHistogram_eq_of_le {V : Type u_1} {W : Type u_2} {α : Type u_3} {G : SimpleGraph V} [G.LocallyFinite] [Fintype V] [Fintype W] {H : SimpleGraph W} [H.LocallyFinite] {χ₀ : V → α} {ψ₀ : W → α} {m n : ℕ} (hnm : n ≤ m) (h : G.colorHistogram χ₀ m = H.colorHistogram ψ₀ m) :
G.colorHistogram χ₀ n = H.colorHistogram ψ₀ n

Histograms that agree in some round agree in every earlier round.

theorem SimpleGraph.forall_refinementColoring_eq_iff {V : Type u_1} {W : Type u_2} {α : Type u_3} [Fintype V] [Fintype W] {G : SimpleGraph V} [G.LocallyFinite] {H : SimpleGraph W} [H.LocallyFinite] {χ₀ : V → α} {ψ₀ : W → α} {v : V} {w : W} :

Color refinement distinguishes two vertices of two graphs in some round exactly when it distinguishes them in round |V| + |W|, the order of the disjoint union.

theorem SimpleGraph.forall_colorHistogram_eq_iff {V : Type u_1} {W : Type u_2} {α : Type u_3} [Fintype V] [Fintype W] {G : SimpleGraph V} [G.LocallyFinite] {H : SimpleGraph W} [H.LocallyFinite] {χ₀ : V → α} {ψ₀ : W → α} :

Color refinement distinguishes two graphs in some round exactly when it distinguishes them in round |V| + |W|. The proof runs color refinement on the disjoint union, whose partition is stable from round |V| + |W| on.

Regular graphs #

theorem SimpleGraph.IsRegularOfDegree.colorHistogram_eq {V : Type u_1} {W : Type u_2} [Fintype V] [Fintype W] {G : SimpleGraph V} [G.LocallyFinite] {H : SimpleGraph W} [H.LocallyFinite] {d : ℕ} (hG : G.IsRegularOfDegree d) (hH : H.IsRegularOfDegree d) (hcard : Fintype.card V = Fintype.card W) (n : ℕ) :
G.colorHistogram (fun (x : V) => ()) n = H.colorHistogram (fun (x : W) => ()) n

Incompleteness of the color refinement test: two d-regular graphs with the same number of vertices have the same color histograms in every round.