Algorithm Search⌘K

Proofs: Fleury's algorithm

These are the human proofs of Fleury's algorithm, at full length.

Fleury's algorithm finds an Eulerian trail when one exists full

This proof shows: correctness, termination.

Let GG be an undirected multigraph with m≥1m \ge 1 edges, where a self-loop adds 2 to the degree of its vertex. For a multigraph HH, a bridge of HH is an edge e=uve = uv with u≠vu \ne v such that uu and vv are in different components of H−eH - e; a self-loop is never a bridge.

NIL is correct. By Euler's theorem, GG has an Eulerian trail if and only if its edges lie in one component and it has at most two odd vertices. By the handshaking lemma the number of odd vertices is even, so it is 0 or 2. Line L1 returns NIL exactly when this condition fails. From now on the condition holds.

Setup. Let ss be the start chosen at line L2. If GG has two odd vertices, ss is one of them and tt is the other; otherwise t=st = s. Each pass of the loop removes one edge, so the loop runs at most mm times and the algorithm stops. We show that at the test of line L4:

  • (a) if HH has an edge, every edge of HH lies in the component of HH that contains uu;
  • (b) the odd vertices of HH are {u,t}\{u, t\} if u≠tu \ne t, and there are none if u=tu = t.

Initially. H=GH = G and u=su = s. Line L1 gives (a), and (b) holds by the choice of ss and tt.

Lemma 1 (one bridge at most). Assume (a) and (b), and deg⁡H(u)≥2\deg_H(u) \ge 2. Then at most one edge at uu is a bridge of HH. In particular, line L6 finds an edge.

Proof. Let e=uve = uv be a bridge of HH at uu, and let CC be the component of H−eH - e that contains vv; it does not contain uu. The degrees in H−eH - e of the vertices of CC are their degrees in HH, except for vv, whose degree is one less. Suppose t∉Ct \notin C. Then by (b) every vertex of CC has even degree in HH, because the only odd vertices of HH are uu and tt, which are outside CC. So in H−eH - e the vertex vv is the only odd vertex of CC. But CC is a graph on its own, and the number of its odd vertices is even by the handshaking lemma. This is a contradiction, so t∈Ct \in C.

Now let e1=uv1e_1 = uv_1 and e2=uv2e_2 = uv_2 be two different bridges at uu, with far components C1C_1 and C2C_2 as above. If some vertex xx were in both, there would be a walk from v1v_1 to xx in C1C_1 and from xx to v2v_2 in C2C_2, both avoiding uu, because u∉C1∪C2u \notin C_1 \cup C_2. Then v1v_1 and v2v_2 are joined by a walk that avoids uu, so e1e_1 and e2e_2 lie on a cycle through uu, and e1e_1 is not a bridge. So C1C_1 and C2C_2 are disjoint, but both contain tt, a contradiction.

If deg⁡H(u)≥2\deg_H(u) \ge 2 then either uu has at least two edges of HH, or one self-loop. In the first case at most one of them is a bridge, and in the second the loop is not a bridge. Either way line L6 finds an edge. □\square

Lemma 2 (the invariants are kept). If (a) and (b) hold at line L4 and deg⁡H(u)>0\deg_H(u) > 0, they hold again after lines L5 to L8.

Proof. Let e=uve = uv be the edge removed, H′=H−eH' = H - e, and vv the new current vertex.

For (b): removing ee lowers the degrees of uu and vv by 1 each, or the degree of uu by 2 if ee is a loop, so the parities of uu and vv flip if u≠vu \ne v, and nothing changes if u=vu = v. If u=vu = v, (b) is the same statement for H′H'. If u≠vu \ne v: when u≠tu \ne t, the odd set {u,t}\{u, t\} becomes {t}△{v}\{t\} \triangle \{v\}, which is {v,t}\{v, t\} if v≠tv \ne t and empty if v=tv = t; when u=tu = t, the empty odd set becomes {u,v}={t,v}\{u, v\} = \{t, v\}. In each case it is the set stated in (b) for the new current vertex vv.

For (a): if ee was chosen at line L6, it is not a bridge, so H′H' has the same components as HH on the vertices that keep edges, and all edges of H′H' are in the component of uu, which contains vv. If ee was chosen at line L5, ee was the only edge at uu, so uu has no edge in H′H'. Every edge ff of H′H' was joined to uu in HH by a walk, by (a), and the walk enters uu only through ee, so the part of it after the last use of ee is a walk in H′H' from vv to ff. So all edges of H′H' are in the component of vv. □\square

Lemma 3 (all edges are used). When the loop stops, HH has no edges.

Proof. The loop stops when deg⁡H(u)=0\deg_H(u) = 0. Then the component of uu in HH is {u}\{u\} with no edge, and by (a) HH has no edge at all. □\square

Theorem. If GG has an Eulerian trail, FLEURY returns one, with the edges in TT and the vertices in WW; otherwise it returns NIL.

Proof. The NIL case is shown above. Otherwise, each pass appends to TT an edge of HH at the current vertex uu, removes it from HH, and moves to its other end, so (W,T)(W, T) is a walk in GG that uses no edge twice. By Lemmas 1 and 2 each pass is well defined, and by Lemma 3 the walk uses all mm edges when it stops. So it is an Eulerian trail. ■\blacksquare

Back to Fleury's algorithm