Documentation

CslibFrontier.Algorithms.Lean.Euler.Circuit

Euler circuits in finite multigraphs #

A finite multigraph is given by a list H : List (α × α) of edges; parallel edges and self-loops are allowed, and an edge (a, b) may be traversed in either direction. Its edges are compared up to orientation through undirected : α × α → Sym2 α. A walk is a list of vertices and uses the edges between consecutive entries (Mathlib's List.consecutivePairs). An Euler circuit of H from u (IsEulerCircuit H u w) is a walk w from u back to u whose edges are those of H, each used exactly once, in either direction.

Euler's theorem (exists_isEulerCircuit_iff): H has an Euler circuit from u exactly when every vertex has even degree (degree, where a self-loop counts twice) and every edge is connected to u by edges of H. Sufficiency is constructive: eulerCircuit H u runs Hierholzer's algorithm, which walks greedily from u along unused edges (walkFrom) until it returns, then repeatedly splices in a closed walk from a visited vertex that still has unused edges (splice, eulerLoop).

Model #

Vertices form any type with decidable equality. The degree of x counts the endpoints of edges of H equal to x. Connectivity is phrased with Relation.EqvGen of the relation "(a, b) is an edge of H", analogous to Cslib.Algorithms.Lean.TSP.eqvGen_kruskalEdges_completeEdges. Vertices without edges are irrelevant: the condition only concerns endpoints of edges, and the empty multigraph has the Euler circuit [u]. Compared with the textbook statement for connected graphs ([KV18], Section 2.4), the model thus generalizes in three harmless ways: connectivity is required of edges only, the empty multigraph is allowed, and so are self-loops. Mathlib's SimpleGraph.Walk.IsEulerian proves the necessity of even degrees for simple graphs; the list model here admits the multigraphs that Christofides' algorithm builds (a spanning tree together with a matching may repeat an edge).

Main definitions #

Main results #

References #

Walks and their edges #

The edges of a walk w are the pairs of its consecutive entries, Mathlib's w.consecutivePairs (w.zip w.tail).

def Cslib.Algorithms.Lean.Euler.undirected {α : Type u_1} (e : α × α) :
Sym2 α

The unordered edge {a, b} underlying the ordered pair (a, b).

@[simp]
theorem Cslib.Algorithms.Lean.Euler.undirected_mk {α : Type u_1} (a b : α) :

Splitting a walk at a vertex x splits its edges. This generalizes Cslib.Orthogonal.consecutivePairs_join, which joins routes of points at a common endpoint.

theorem Cslib.Algorithms.Lean.Euler.eqvGen_of_mem_cons {α : Type u_1} {r : α → α → Prop} {a : α} {l : List α} :
(∀ e ∈ (a :: l).consecutivePairs, Relation.EqvGen r e.1 e.2) → ∀ y ∈ a :: l, Relation.EqvGen r a y

A walk whose steps are related by EqvGen r relates its first vertex to all of its vertices.

theorem Cslib.Algorithms.Lean.Euler.consecutivePairs_append_append {α : Type u_1} {A c B : List α} {x : α} (hc1 : c.head? = some x) (hc2 : c.getLast? = some x) :

Replacing one occurrence of x by a closed walk c from x adds the edges of c.

theorem Cslib.Algorithms.Lean.Euler.head?_append_append {α : Type u_1} {A c B : List α} {x : α} (hc1 : c.head? = some x) :
(A ++ c ++ B).head? = (A ++ x :: B).head?
theorem Cslib.Algorithms.Lean.Euler.getLast?_append_append {α : Type u_1} {A c B : List α} {x : α} (hc2 : c.getLast? = some x) :
(A ++ c ++ B).getLast? = (A ++ x :: B).getLast?

Euler circuits #

structure Cslib.Algorithms.Lean.Euler.IsEulerCircuit {α : Type u_1} (H : List (α × α)) (u : α) (w : List α) :

w is an Euler circuit of the multigraph H from u: a walk from u back to u that uses every edge of H exactly once, in either direction.

theorem Cslib.Algorithms.Lean.Euler.eqvGen_of_isEulerCircuit {α : Type u_1} {H : List (α × α)} {u : α} {w : List α} (h : IsEulerCircuit H u w) (e : α × α) :
e ∈ H → Relation.EqvGen (fun (a b : α) => (a, b) ∈ H) e.1 u

Euler's theorem, necessity of connectivity: every edge of a multigraph with an Euler circuit from u is connected to u.

theorem Cslib.Algorithms.Lean.Euler.mem_of_isEulerCircuit {α : Type u_1} {H : List (α × α)} {u : α} {w : List α} (h : IsEulerCircuit H u w) {x : α} (hx : Relation.EqvGen (fun (a b : α) => (a, b) ∈ H) x u) :
x ∈ w

An Euler circuit from u visits every vertex connected to u by edges of H.

theorem Cslib.Algorithms.Lean.Euler.sum_map_consecutivePairs_eq {α : Type u_1} {β : Type u_2} [AddCommMonoid β] {δ : α → α → β} (hsymm : ∀ (a b : α), δ a b = δ b a) {H : List (α × α)} {u : α} {w : List α} (h : IsEulerCircuit H u w) :
(List.map (fun (e : α × α) => δ e.1 e.2) w.consecutivePairs).sum = (List.map (fun (e : α × α) => δ e.1 e.2) H).sum

For a symmetric weight δ, an Euler circuit of H weighs as much as H.

Degrees #

def Cslib.Algorithms.Lean.Euler.ends {α : Type u_1} [DecidableEq α] (x : α) (e : α × α) :

The number of endpoints of e equal to x: 2 for a self-loop at x.

def Cslib.Algorithms.Lean.Euler.degree {α : Type u_1} [DecidableEq α] (H : List (α × α)) (x : α) :

The degree of x in the multigraph with edge list H; a self-loop at x counts twice.

@[simp]
theorem Cslib.Algorithms.Lean.Euler.degree_nil {α : Type u_1} [DecidableEq α] (x : α) :
degree [] x = 0
@[simp]
theorem Cslib.Algorithms.Lean.Euler.degree_cons {α : Type u_1} [DecidableEq α] (e : α × α) (H : List (α × α)) (x : α) :
degree (e :: H) x = ends x e + degree H x
theorem Cslib.Algorithms.Lean.Euler.degree_append {α : Type u_1} [DecidableEq α] (H K : List (α × α)) (x : α) :
degree (H ++ K) x = degree H x + degree K x
theorem Cslib.Algorithms.Lean.Euler.degree_eq_of_perm {α : Type u_1} [DecidableEq α] {H K : List (α × α)} (h : (List.map undirected H).Perm (List.map undirected K)) (x : α) :
degree H x = degree K x

The degree depends only on the edges up to orientation.

theorem Cslib.Algorithms.Lean.Euler.degree_consecutivePairs_cons {α : Type u_1} [DecidableEq α] (a : α) (l : List α) (x : α) :
((degree (a :: l).consecutivePairs x + if x = a then 1 else 0) + if x = (a :: l).getLast ⋯ then 1 else 0) = 2 * List.count x (a :: l)

Twice the number of visits to x counts the edges of the walk at x, plus one for each end of the walk at x.

theorem Cslib.Algorithms.Lean.Euler.even_degree_of_isEulerCircuit {α : Type u_1} [DecidableEq α] {H : List (α × α)} {u : α} {w : List α} (h : IsEulerCircuit H u w) (x : α) :
Even (degree H x)

Euler's theorem, necessity of even degrees: a multigraph with an Euler circuit has only vertices of even degree.

Greedy walks #

def Cslib.Algorithms.Lean.Euler.touches {α : Type u_1} [DecidableEq α] (x : α) (e : α × α) :

Whether x is an endpoint of e.

def Cslib.Algorithms.Lean.Euler.other {α : Type u_1} [DecidableEq α] (x : α) (e : α × α) :
α

The endpoint of e other than x (the second one when x is the first).

theorem Cslib.Algorithms.Lean.Euler.ends_of_touches {α : Type u_1} [DecidableEq α] {x : α} {e : α × α} (h : touches x e = true) (y : α) :
ends y e = (if y = x then 1 else 0) + if y = other x e then 1 else 0
def Cslib.Algorithms.Lean.Euler.walkFrom {α : Type u_1} [DecidableEq α] :
ℕ → List (α × α) → α → List α × List (α × α)

Follow unused edges of H greedily from u, for at most fuel steps. Returns the walk and the edges it did not use.

theorem Cslib.Algorithms.Lean.Euler.length_walkFrom_snd_le {α : Type u_1} [DecidableEq α] (fuel : ℕ) (H : List (α × α)) (u : α) :
(walkFrom fuel H u).2.length ≤ H.length
theorem Cslib.Algorithms.Lean.Euler.length_walkFrom_snd_lt {α : Type u_1} [DecidableEq α] {fuel : ℕ} (hfuel : 0 < fuel) {H : List (α × α)} {u : α} {e : α × α} (he : e ∈ H) (hte : touches u e = true) :
(walkFrom fuel H u).2.length < H.length

A greedy walk from a vertex with an unused edge uses at least one edge.

theorem Cslib.Algorithms.Lean.Euler.walkFrom_spec {α : Type u_1} [DecidableEq α] (fuel : ℕ) (H : List (α × α)) (u t : α) (hfuel : H.length ≤ fuel) (hpar : ∀ (y : α), ((degree H y + if y = u then 1 else 0) + if y = t then 1 else 0) % 2 = 0) :
(walkFrom fuel H u).1.head? = some u ∧ (walkFrom fuel H u).1.getLast? = some t ∧ (List.map undirected H).Perm (List.map undirected ((walkFrom fuel H u).1.consecutivePairs ++ (walkFrom fuel H u).2)) ∧ ∀ (y : α), degree (walkFrom fuel H u).2 y % 2 = 0

The trail lemma. Suppose that u and t are the only vertices of odd degree in H, or that all degrees are even and u = t. Then the greedy walk from u ends at t, it and the unused edges together are the edges of H, and the unused edges have even degrees.

Splicing closed walks #

def Cslib.Algorithms.Lean.Euler.splice {α : Type u_1} [DecidableEq α] (x : α) (c : List α) :
List α → List α

Splice c into w in place of the first occurrence of x.

theorem Cslib.Algorithms.Lean.Euler.exists_splice {α : Type u_1} [DecidableEq α] {x : α} (c : List α) {w : List α} (hx : x ∈ w) :
∃ (A : List α) (B : List α), w = A ++ x :: B ∧ splice x c w = A ++ c ++ B
theorem Cslib.Algorithms.Lean.Euler.eq_nil_of_forall_not_touches {α : Type u_1} [DecidableEq α] {H R : List (α × α)} {w : List α} {u : α} (hu : u ∈ w) (hconn : ∀ e ∈ H, Relation.EqvGen (fun (a b : α) => (a, b) ∈ H) e.1 u) (hperm : (List.map undirected H).Perm (List.map undirected (w.consecutivePairs ++ R))) (hnt : ∀ x ∈ w, ∀ e ∈ R, touches x e = false) :
R = []

If the edges of H are connected to a vertex of w, and the edges of w together with R are those of H, then some vertex of w touches an edge of R, unless R is empty.

def Cslib.Algorithms.Lean.Euler.eulerLoop {α : Type u_1} [DecidableEq α] :
ℕ → List α → List (α × α) → List α

Splice closed walks from vertices of w that still touch an edge of R, for at most fuel rounds.

theorem Cslib.Algorithms.Lean.Euler.eulerLoop_spec {α : Type u_1} [DecidableEq α] {H : List (α × α)} {u : α} (hconn : ∀ e ∈ H, Relation.EqvGen (fun (a b : α) => (a, b) ∈ H) e.1 u) (fuel : ℕ) (w : List α) (R : List (α × α)) :
w.head? = some u → w.getLast? = some u → (List.map undirected H).Perm (List.map undirected (w.consecutivePairs ++ R)) → (∀ (y : α), degree R y % 2 = 0) → R.length ≤ fuel → IsEulerCircuit H u (eulerLoop fuel w R)

Hierholzer's algorithm and Euler's theorem #

def Cslib.Algorithms.Lean.Euler.eulerCircuit {α : Type u_1} [DecidableEq α] (H : List (α × α)) (u : α) :
List α

Hierholzer's algorithm: walk greedily from u until stuck, then splice in closed walks from visited vertices with unused edges.

theorem Cslib.Algorithms.Lean.Euler.isEulerCircuit_eulerCircuit {α : Type u_1} [DecidableEq α] {H : List (α × α)} {u : α} (heven : ∀ (x : α), Even (degree H x)) (hconn : ∀ e ∈ H, Relation.EqvGen (fun (a b : α) => (a, b) ∈ H) e.1 u) :

Euler's theorem, sufficiency ([Hie73]): if every degree in H is even and every edge of H is connected to u, then Hierholzer's algorithm returns an Euler circuit of H from u.

theorem Cslib.Algorithms.Lean.Euler.exists_isEulerCircuit_iff {α : Type u_1} [DecidableEq α] {H : List (α × α)} {u : α} :
(∃ (w : List α), IsEulerCircuit H u w) ↔ (∀ (x : α), Even (degree H x)) ∧ ∀ e ∈ H, Relation.EqvGen (fun (a b : α) => (a, b) ∈ H) e.1 u

Euler's theorem for finite multigraphs ([Hie73]; [KV18], Section 2.4): H has an Euler circuit from u exactly when every degree is even and every edge is connected to u.