Euler tour

Definitions
An Euler tour (or Eulerian circuit, or Eulerian cycle) in an undirected graph is a cycle (a closed path), which uses each edge exactly once. If such a cycle exists, the graph is called Eulerian.

An Eulerian path (or Eulerian trail) in an undirected graph is a path, which uses each edge exactly once, but its starting and ending vertices are different.

For directed graphs, definitions are similar: path has to be replaced with directed path and cycle with directed cycle.

Existence conditions
An undirected graph has an Euler tour, iff it is connected and each vertex has an even degree.

An undirected graph has an Eulerian path, iff it is connected, and each vertex, except exactly two vertices, has an even degree. The two vertices of odd degree have to be the endpoints of the path.

A directed graph has an Euler tour, iff it is strongly-connected and every vertex has equal in degree and out degree.

A directed graph has an Eulerian path from vertex $$u$$ to $$v$$, iff the graph is strongly connected, every vertex except $$u$$ and $$v$$ has equal in degree and out degree, and $$\deg^+(u)=\deg^-(u)+1$$, $$\deg^+(v)=\deg^-(v)-1$$,

Note: these criterions assume that the graph has no isolated points, i.e. vertices which are not part of any edge. If the graph contains such vertices, you should ignore them when you are checking graph's connectivity.

Fleury's algorithm
Fleury's algorithm is a straightforward algorithm for finding Eulerian paths/tours. It proceeds by repeatedly removing edges from the graph in such way, that the graph remains Eulerian. A version of the algorithm, which finds Euler tour in undirected graphs follows.

Start with any vertex of non-zero degree. Choose any edge leaving this vertex, which is not a bridge (i.e. its removal will not disconnect the graph into two or more disjoint connected components). If there is no such edge, stop. Otherwise, append the edge to the Euler tour, remove it from the graph, and repeat the process starting with the other endpoint of this edge.

Though the algorithm is quite simple, it is not often used, because it needs to identify bridges in the graph (which is not a trivial thing to code.) Slightly more sophisticated, but easily implementable algorithm is presented below.

Cycle finding algorithm
This algorithm is based on the following observation: if C is any cycle in a Eulerian graph, then after removing the edges of C, the remaining connected components will also be Eulerian graphs.

The algorithm consists in finding a cycle in the graph, removing its edges and repeating this steps with each remaining connected component. It has a very compact code with recursion:

tour is a list

find_tour(u): for each edge e=(u,v) in E:		remove e from E		find_tour(v) prepend u to tour

to find the tour, clear stack 'tour' and call find_tour(u), where u is any vertex with non-zero degree

This algorithm can also be used to find Eulerian paths: simply connect the path's endpoints by a dummy edge, and find Euler tour.

Generalizations
In some practical situations, it is desirable to find a path, which visits all edges of a graph, when the graph does not have an Euler tour. In this case some of the edges will have to be visited more than once. If the graph is weighted, and a minimum-cost path is sought, then this is what is known as a Chinese postman problem.