Fleury's algorithm '''Fleury's algorithm''' is an elegant but inefficient algorithm that dates to 1883. Consider a graph known to have all edges in the same component and at most two vertices of odd degree. The algorithm starts at a vertex of odd degree, or, if the graph has none, it starts with an arbitrarily chosen vertex. At each step it chooses the next edge in the path to be one whose deletion would not disconnect the graph, unless there is no such edge, in which case it picks the remaining edge left at the current vertex. It then moves to the other endpoint of that edge and deletes the edge. At the end of the algorithm there are no edges left, and the sequence from which the edges were chosen forms an Eulerian cycle if the graph has no vertices of odd degree, or an Eulerian trail if there are exactly two vertices of odd degree. While the
graph traversal in Fleury's algorithm is linear in the number of edges, i.e. O(|E|), we also need to factor in the complexity of detecting
bridges. If we are to re-run
Tarjan's linear time
bridge-finding algorithm after the removal of every edge, Fleury's algorithm will have a time complexity of O(|E|^2). A dynamic bridge-finding algorithm of allows this to be improved to O(|E| \cdot \log^3 |E| \cdot \log \log |E|), but this is still significantly slower than alternative algorithms.
Hierholzer's algorithm Hierholzer's 1873 paper provides a different method for finding Euler cycles that is more efficient than Fleury's algorithm: • Choose any starting vertex
v, and follow a trail of edges from that vertex until returning to
v. It is not possible to get stuck at any vertex other than
v, because the even degree of all vertices ensures that, when the trail enters another vertex
w there must be an unused edge leaving
w. The tour formed in this way is a closed tour, but may not cover all the vertices and edges of the initial graph. • As long as there exists a vertex
u that belongs to the current tour but that has adjacent edges not part of the tour, start another trail from
u, following unused edges until returning to
u, and join the tour formed in this way to the previous tour. • Since we assume the original graph is
connected, repeating the previous step will exhaust all edges of the graph. By using a data structure such as a
doubly linked list to maintain the set of unused edges incident to each vertex, to maintain the list of vertices on the current tour that have unused edges, and to maintain the tour itself, the individual operations of the algorithm (finding unused edges exiting each vertex, finding a new starting vertex for a tour, and connecting two tours that share a vertex) may be performed in constant time each, so the overall algorithm takes
linear time, O(|E|). This algorithm may also be implemented with a
deque. Because it is only possible to get stuck when the deque represents a closed tour, one should rotate the deque by removing edges from the tail and adding them to the head until unstuck, and then continue until all edges are accounted for. This also takes linear time, as the number of rotations performed is never larger than |E| (intuitively, any "bad" edges are moved to the head, while fresh edges are added to the tail) ==Counting Eulerian circuits==