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 be an undirected multigraph with edges, where a self-loop adds 2 to the degree of its vertex. For a multigraph , a bridge of is an edge with such that and are in different components of ; a self-loop is never a bridge.
NIL is correct. By Euler's theorem, 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 be the start chosen at line L2. If has two odd vertices, is one of them and is the other; otherwise . Each pass of the loop removes one edge, so the loop runs at most times and the algorithm stops. We show that at the test of line L4:
- (a) if has an edge, every edge of lies in the component of that contains ;
- (b) the odd vertices of are if , and there are none if .
Initially. and . Line L1 gives (a), and (b) holds by the choice of and .
Lemma 1 (one bridge at most). Assume (a) and (b), and . Then at most one edge at is a bridge of . In particular, line L6 finds an edge.
Proof. Let be a bridge of at , and let be the component of that contains ; it does not contain . The degrees in of the vertices of are their degrees in , except for , whose degree is one less. Suppose . Then by (b) every vertex of has even degree in , because the only odd vertices of are and , which are outside . So in the vertex is the only odd vertex of . But is a graph on its own, and the number of its odd vertices is even by the handshaking lemma. This is a contradiction, so .
Now let and be two different bridges at , with far components and as above. If some vertex were in both, there would be a walk from to in and from to in , both avoiding , because . Then and are joined by a walk that avoids , so and lie on a cycle through , and is not a bridge. So and are disjoint, but both contain , a contradiction.
If then either has at least two edges of , 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.
Lemma 2 (the invariants are kept). If (a) and (b) hold at line L4 and , they hold again after lines L5 to L8.
Proof. Let be the edge removed, , and the new current vertex.
For (b): removing lowers the degrees of and by 1 each, or the degree of by 2 if is a loop, so the parities of and flip if , and nothing changes if . If , (b) is the same statement for . If : when , the odd set becomes , which is if and empty if ; when , the empty odd set becomes . In each case it is the set stated in (b) for the new current vertex .
For (a): if was chosen at line L6, it is not a bridge, so has the same components as on the vertices that keep edges, and all edges of are in the component of , which contains . If was chosen at line L5, was the only edge at , so has no edge in . Every edge of was joined to in by a walk, by (a), and the walk enters only through , so the part of it after the last use of is a walk in from to . So all edges of are in the component of .
Lemma 3 (all edges are used). When the loop stops, has no edges.
Proof. The loop stops when . Then the component of in is with no edge, and by (a) has no edge at all.
Theorem. If has an Eulerian trail, FLEURY returns one, with the edges in and the vertices in ; otherwise it returns NIL.
Proof. The NIL case is shown above. Otherwise, each pass appends to an edge of at the current vertex , removes it from , and moves to its other end, so is a walk in that uses no edge twice. By Lemmas 1 and 2 each pass is well defined, and by Lemma 3 the walk uses all edges when it stops. So it is an Eulerian trail.