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 #
Cslib.Algorithms.Lean.Euler.undirected: an ordered pair as an unordered edge.Cslib.Algorithms.Lean.Euler.IsEulerCircuit: a closed walk using each edge once.Cslib.Algorithms.Lean.Euler.degree: the degree of a vertex in an edge list.Cslib.Algorithms.Lean.Euler.walkFrom: the greedy walk along unused edges.Cslib.Algorithms.Lean.Euler.splice: splice a closed walk into a walk at a vertex.Cslib.Algorithms.Lean.Euler.eulerLoop: repeatedly splice in closed walks.Cslib.Algorithms.Lean.Euler.eulerCircuit: Hierholzer's algorithm.
Main results #
Cslib.Algorithms.Lean.Euler.walkFrom_spec: the trail lemma. Whenuandtare the only vertices of odd degree (or all degrees are even andu = t), the greedy walk fromuends att, and the unused edges have even degrees.Cslib.Algorithms.Lean.Euler.isEulerCircuit_eulerCircuit: Hierholzer's algorithm returns an Euler circuit of every connected multigraph with even degrees.Cslib.Algorithms.Lean.Euler.even_degree_of_isEulerCircuit,Cslib.Algorithms.Lean.Euler.eqvGen_of_isEulerCircuit: the converse conditions.Cslib.Algorithms.Lean.Euler.exists_isEulerCircuit_iff: Euler's theorem.Cslib.Algorithms.Lean.Euler.mem_of_isEulerCircuit: an Euler circuit visits every vertex connected to its start.Cslib.Algorithms.Lean.Euler.sum_map_consecutivePairs_eq: for a symmetric weight, an Euler circuit weighs as much as its multigraph.
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).
The unordered edge {a, b} underlying the ordered pair (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.
A walk whose steps are related by EqvGen r relates its first vertex to all of its
vertices.
Euler circuits #
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.
The walk starts at
u.The walk ends at
u.The walk uses the edges of
H, each once, in either direction.
Euler's theorem, necessity of connectivity: every edge of a multigraph with an Euler
circuit from u is connected to u.
An Euler circuit from u visits every vertex connected to u by edges of H.
For a symmetric weight δ, an Euler circuit of H weighs as much as H.
Degrees #
The number of endpoints of e equal to x: 2 for a self-loop at x.
The degree of x in the multigraph with edge list H; a self-loop at x counts twice.
The degree depends only on the edges up to orientation.
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.
Euler's theorem, necessity of even degrees: a multigraph with an Euler circuit has only vertices of even degree.
Greedy walks #
Whether x is an endpoint of e.
The endpoint of e other than x (the second one when x is the first).
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 #
Splice c into w in place of the first occurrence of x.
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.
Splice closed walks from vertices of w that still touch an edge of R, for at most fuel
rounds.
Hierholzer's algorithm and Euler's theorem #
Hierholzer's algorithm: walk greedily from u until stuck, then splice in closed walks
from visited vertices with unused edges.
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.
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.