Constant Amortized Time Enumeration of Eulerian trails

In this paper, we consider enumeration problems for edge-distinct and vertex-distinct Eulerian trails. Here, two Eulerian trails are \emph{edge-distinct} if the edge sequences are not identical, and they are \emph{vertex-distinct} if the vertex sequences are not identical. As the main result, we propose optimal enumeration algorithms for both problems, that is, these algorithm runs in $\mathcal{O}(N)$ total time, where $N$ is the number of solutions. Our algorithms are based on the reverse search technique introduced by [Avis and Fukuda, DAM 1996], and the push out amortization technique introduced by [Uno, WADS 2015].


Introduction
An Eulerian trail in a graph is a trail that visits all edges exactly once. Finding a Eulerian trail is a classical problem in graph theory. A famous Seven Bridges of Königsberg problem solved by Leonhard Euler in 1736 is one of a well-known application. Eulerian trails have many other applications such as CMOS circuit design [5] bioinformatics [11,12], and automaton theory [9].
Deciding for the existence of a Eulerian trail can be done in time polynomial [8]. However, the counting problem of edge-distinct Eulerian trails is #P-complete [4] for general undirected graphs, although for directed graphs, Aardenne and Brujin [1] proposed the BEST algorithm whose running time is polynomial time. Here, two Eulerian trails are edge-distinct if the edge sequences of them are different. Similarly, two Eulerian trails are vertex-distinct if the vertex sequences of them are different. If a graph is simple, the set of edgedistinct Eulerian trails and that of vertex-distinct Eulerian trails are equivalent. Thus, counting Eulerian trails is intractable for general cases unless P = #P. Recently, Conte et al. [6] give an algorithm that answers whether a graph contains at least z Eulerian trails in time polynomial in m and z, where m is the number of edges in the graph.
In contrast to counting problems, enumeration problems ask to output all the solutions without duplicates. Especially, enumeration problems for subgraphs satisfying some constraints have been widely studied, such as spanning trees [14], st-paths [3,7], cycles [3], maximal cliques [16,17], and many others [19]. In this paper, we focus on enumeration problems for Eulerian trails. As mentioned in above, because the counting version is intractable, the enumeration problem is also intractable with respect to the size of inputs. Hence, in this paper, we aim to develop efficient enumeration algorithms with respect to the size of both inputs and outputs. Such algorithms are called output-sensitive algorithms. In particular, an enumeration algorithm A runs in polynomial amortized time if the total running time of A is O(poly(n)N ) time, where n is the size of inputs and N is the number of solutions. That is, A outputs solutions in O(poly(n)) time per each on average. In this contexts, the ultimate goal is to develop O(N ) time enumeration algorithm, that is poly(n) ∈ O(1), and such optimal algorithms have been obtained [13,18]. Under this evaluation, Kikuchi [10] proposed an O(mN ) time algorithm for simple general graphs, where m is the number of edges in an input graph. However, the existence of a constant amortized time enumeration algorithm for Eulerian trails is open.
In this paper, we propose optimal enumeration algorithms for edge-distinct Eulerian trails and vertex-distinct Eulerian trails based on the reverse search technique [2]. Intuitively speaking, an enumeration algorithm based on the reverse search technique enumerates solutions by traversing on a tree-shaped search space, called the family tree. Each node on the tree corresponds to a prefix of a solution called a partial solution and each leaf corresponds to some solution. The edge set of the tree is defined by the parent-child relation between nodes. in particular, a partial solution P is the parent of a partial solution P ′ if P ′ is obtained by adding one edge to P . Although our algorithm is quite simple, with a sophisticated analysis [18] and contracting operations for graphs, we achieve constant amortized time per solution.

Preliminaries
An undirected graph G = (V, E) is a pair of a vertex set V and an edge set E ⊆ V × V . Note that (u, v) ∈ E if and only if (v, u) ∈ E for each pair of vertices u, v. Graphs may contain self loops and parallel edges. For each vertex v, the neighborhood N (v, µ v ) is a multiset of the underlying set N (v), where N (v) is the set of vertices adjacent to v and µ v : ) and edges in π are mutually distinct. Note that some vertex appears more than once in a trail. In particular, a trail π is a path if v i = v j in π for i = j. A circuit is a trail such that the first vertex and the last vertex are equal. A trail π is an Eulerian trail if π contains all the edges in G. G is Eulerian if G has at least one Eulerian trail. It is known that G is Eulerian if and only if either every vertex has even degree or exactly two vertices have odd degree. In what follows, we assume that input graphs are Eulerian. We define E(π) = (e 1 , . . . , e ℓ−1 ) as a subsequence of π containing all the edges in π, and similarly, V (π) = (v 1 , . . . , v ℓ ) as a subsequence of π containing all the vertices in π. Let π 1 and π 2 be two Eulerian trails. We say π 1 is edge-distinct from π 2 if E(π 1 ) = E(π 2 ) and we say Assume that every Eulerian trail starts from s and ends with t. A trail P is a partial Eulerian trail in G if P starts from s and there is an Eulerian trail that contains P as a prefix. Especially, if E(G) \ P = ∅, then P is called a proper partial Eulerian trail. An edge e is said to be addible if P + e is also a partial Eulerian trail. We denote by t(P ) the end vertex of a partial Eulerian trail P such that t(P ) = s. For each partial Eulerian trail P , we denote by That is, P G is the set of edges which we have to add to P for constructing a solution. Now, we formalize our problems as follows.
Problem 1 (Edge-distinct Eulerian trail enumeration). Given a graph G, output all the edge-distinct Eulerian trails in G without duplicate.
Problem 2 (Vertex-distinct Eulerian trail enumeration). Given a graph G, output all the vertex-distinct Eulerian trails in G without duplicate.

An algorithm for edge-distinct Eulerian trails
In this section, we propose our enumeration algorithm for edge-distinct Eulerian trails based on the reverse search technique proposed by Avis and Fukuda [2]. We remark that Eulerian trails in this section are considered as a sequence of edges. We enumerate solutions by traversing a directed rooted tree, called a family tree T . The tree consists of nodes P and edges E. Each node X in P is associated with a partial Eulerian trail P and a graph G. Let P (X) be the associated partial Eulerian trail of X. If no confusion arises, we identify a node with its associated a partial Eulerian trail. The root of T is the trail R consisting of s. For two partial Eulerian trails P 1 and P 2 , there is a directed edge (P 1 , P 2 ) in E if P 1 ⊂ P 2 and |P 2 \ P 1 | = 1. We say that P 1 is the parent of P 2 and P 2 is a child of P 1 . Note that a partial Euler path may have more than one children. Let C(P ) be the set of child partial Eulerian trails of P . From the definition of the parent-child relation, for any partial Eulerian trail P , there is a unique path from P to the root on T obtained by recursively removing an edge (u, t(P )) in P . Hence, for any node P in T , there is a path from R to P in T .
Let Γ G (P ) := ∂ G (t(P )) ∩ P G be the candidates of addible edges e to P without violating the parent-child relation. To traverse all the children of a partial Eulerian trail P , generating partial Eulerian trails P + e is enough if e ∈ Γ G (P ) satisfies the condition given in the next lemma.
Lemma 3. Let P be a partial Eulerian trail in G and e be an edge in P G . Then, P + e is a child of P if and only if Γ G (P ) = {e} or e ∈ Γ G (P ) is not a bridge.
Proof. Suppose that P + e is a child of P . Assume that |Γ G (P )| > 1 and e is a bridge. This implies that G − (P + e) is disconnected, and thus, this contradicts the assumption that P + e is a partial Eulerian trail.

Algorithm 1: Proposed enumeration algorithm for Eulerian trails
Find bridges in P G by Tarjan's algorithm; If Γ G (P ) contains exactly one edge, then the lemma clearly holds. Suppose that |Γ G (P )| > 1. From the definition of a partial Eulerian trail, Γ G (P ) contains at most one bridge. Hence, Γ G (P ) contains a non-bridge edge. Now, we give our proposed enumeration algorithm as follows. The algorithm starts with the root partial Eulerian trail R. Then, for each edge e = (s, u) in Γ G (R), the algorithm generates a child partial Eulerian trail (e) of R if e meets the condition by Lemma 3. The algorithm recursively generates descendant nodes of R by adding edges in the candidate set by depth-first traversal on T . If the algorithm finds an Eulerian trail, then the algorithm outputs it as a solution. The correctness of the algorithm is clear from the construction. Hence, we obtain the next theorem.

Amortized analysis: achieving constant amortized time
The goal of this section is to develop an optimal enumeration algorithm constant amortized time per solution based on the algorithm given in the previous section. We summarize our algorithm in Algorithm 1. We first consider the time complexity for generating all the children of a partial Eulerian trail P . Let m P = |P G |. Finding all bridges in P G consumes O(m P ) time by Tarjan's algorithm [15]. Thus, by using these bridges, we can find edges satisfying the condition in Lemma 3 in O(m P ). In addition, P G can be computed in constant time by just removing f from P ′ G . Hence, the following lemma holds. time. In this paper, we employ the push out amortization technique as the trick introduced by Uno [18]. We first introduce some terminology. Let P be a partial Eulerian trail. The phrase "the algorithm generates P " indicates that the algorithm computes P and P G at line 7 and line 8 of Algorithm 1, respectively. We denote by T (P ) of P the exact number of computational steps for generating P . That is, T (P ) does not include the time complexity of descendants of P . Let T (P ) be the sum of the computation time of generating children of P .
Roughly speaking, the push out amortization technique gives a sufficient condition for proving that an enumeration algorithm runs in O(T * ) per solution on average, where T * is the maximum time complexity among leaves. This sufficient condition is called the push out condition defined as follows.
Note that we slightly modify the original condition given in [18] to fit our purpose. The intuition behind this condition is that if the computation time of each node is smaller than the sum of that of descendants, then the whole computation time is dominated by the computation time of descendants, that is, the computation time of leaves. In our case, T * = O(1) because m P on a leaf is constant. If each partial Eulerian trail has at least two children, then the condition clearly holds. However, if there is a partial Eulerian trail P has at most one child, then the condition may not hold because for the child P ′ of P , P G is larger than P ′ G and thus T (P ) can be smaller than T (P ). Hence, we modify P G by contracting vertices and edges so that each non-leaf partial Eulerian trail P has at least two children. This contraction of a graph is performed by applying the follow rules. Let v be a vertex in P G .

Contraction Rule 1
If v is a pendant, then remove v.

Contraction Rule 2
If v is incident to exactly two distinct edges (u, v), (v, w) and t(P ) = v, then remove v and add edge (u, w) to P G .
See Figures 1 and 2. Note that when Contraction Rule 1 is applied to t, the last vertex of Eulerian trails becomes the unique neighbor of t in P G . We also note that when applying Contraction Rule 2, u = w may hold, that is, a loop on u may be generated. After recursively applying these operations, we say G is contracted if neither Contraction Rule 1 nor Contraction Rule 2 can not be applicable to G. Clearly, there is a surjection from partial Eulerian trails in G to ones in the contracted graph of G. The next lemma shows the time complexity for obtaining contracted graphs.
Lemma 7. Let G be a graph, P be a partial Eulerian trail in G such that there is the parent P ′ satisfying P = P ′ + (u, v). Assume that the contracted graph of P ′ G is already obtained. Then, the contraction of P G can be done in constant time.
Proof. The degree of vertices other than u and v is same in P ′ G and P G . Note that each vertex in a contracted graph has degree at least three. Thus, Contraction Rule 1 is not applicable. Moreover, Contraction Rule 2 can be applied to u or v in P G . Hence, by checking the degree of u and v, the contraction can be done in constant time.

Lemma 8. Each vertex in a contracted graph is incident to at least two nonbridge edges.
Proof. Let G be a contracted graph such that G has an Eulerian trail. Because of Contraction Rule 1 and Contraction Rule 2, all vertices are incident to at least three edges. Suppose that a vertex v incident to two distinct bridges b 1 and b 2 . Otherwise, the lemma holds. Let B i be a connected component of G − v such that b i ∈ B i for i = 1, 2. Then, there are two classes of Eulerian trails the one contains paths first visit B 1 and next visit B 2 , and the other first visits B 2 and next B 1 . Because G is Eulerian, v must be incident to at least two edges for visiting all the edges in G \ (B 1 ∪ B 2 ). Hence, the statement holds.
Because detecting bridges can be done in O(m P ) time [15], the time complexity of each iteration is O(m P ). In addition, the difference between the number of edges in P G and that in Q G of a child Q of P is constant, and each proper partial Eulerian trail has at least two children by Lemma 8. Hence, the proposed algorithm satisfies the push out condition and runs in constant amortized time per solution without outputting.
Next, we consider the output format of our algorithm. If we simply output all the solutions naïvely, then total time complexity is O(mN ). Hence, to achieve constant amortized time enumeration, we modify our algorithm so that it only outputs the difference between two consecutive solutions. Outputting only the difference is a well known technique for reducing the total time complexity in enumeration field [14,16] During the execution of the algorithm, the algorithm maintains two working memories W and I. These are implemented by doubly-linked lists. W contains the current partial Eulerian trail and I contains the information of contractions. When the algorithm finds an edge e such that P + e is a child of P , then the algorithm appends e to W . In addition, the algorithm also appends an information ξ to I, where ξ represents how the algorithm contracts a graph. ξ forms a set of tuples each of which contains what is added and removed from G, and which rule is used. Then, the algorithm prints +(e, ξ). We can easily see that the length of each output is constant. When the algorithm backtracks from P ′ , then the algorithm removes e from W and ξ from I, and prints −(e, ξ). When the algorithm reaches a leaf, the algorithm prints # to indicate that a solution is stored in W . By using the information stored in the working memory, we can get the corresponding Eulerian trail if necessary. Moreover, it is enough to allocate O(m) space to each working memory.
Lemmas 3 and 8 imply that each partial Eulerian trail has either zero or at least two children. This yields the following corollary. Remind that partial Eulerian trails corresponds to nodes on T . Thus, by Corollary 9, the total length of outputs is O(N ) and the next theorem holds.

Algorithm for vertex-distinct Eulerian trails
In this section, we focus on an enumeration of vertex-distinct Eulerian trails. In a way similar to the previous section, we develop an enumeration algorithm based on the reverse search technique. We first some terminologies. A pair (u, v) of vertices is good if there are two parallel edges e 1 and e 2 between u and v such that the corresponding trail of e 1 in the original graph differs from that of e 2 . Otherwise, we say (u, v) is bad. Note that we can check whether (u, v) is good or bad in constant time.
The main difference from the edge-distinct problem is that adding parallel edges between a bad pair to a current partial Eulerian trail may generate duplicate solutions. Note that two parallel edges between a good pair may yield two children if these parallel edges is generated by Contraction Rule 2. This is because an added edge by Contraction Rule 2 carries two or more edges. To ensure that each proper partial Eulerian trail has at least two children, that is, each vertex v has at least two distinct edges, we slightly modify an input graph of each recursive call as follows.

Contraction Rule 3
If v has exactly two neighbors u and w such that µ v (u) = 1, (v, w) is bad, u and w are distinct from v, and v has no loops, then remove µ v (w) − 1 edges between v and w. Then, add a vertex v ′ and µ v (w) − 1 edges between v ′ and w.
Contraction Rule 4 Remove incident edges of a vertex v if v has no loops, v has exactly one neighbor u such that (u, v) is bad and µ v (u) ≥ 3. Then, add ⌊ µv (u) 2 ⌋ loops to u. In addition, add an edge See Figures 3 and 4. Note that after performing Contraction Rule 3, the number of edges in the resultant graph G ′ and the original graph G are same, and Contraction Rule 2 is applicable to v in G ′ . In addition, after performing Contraction Rule 4, Contraction Rule 1 may be applicable. When Contraction Rule 3 is applicable to G, there are (a) at least one solution containing (u, v, w) or (w, v, u) and (b) at least one solutions containing (w, v, w). This implies that if t(P ) = v and Contraction Rule 3 is applicable, then we can generate two partial Eulerian trails which are extended by (a) and (b). In addition, we can easily obtain a one-to-one corresponding between solutions in G and G ′ , which G ′ is obtained by applying Contraction Rule 3 or Contraction Rule 4. However, applying Contraction Rule 3 and Contraction Rule 4 may make G drastically small.
then P has two children. However, after adding an edge incident to v i to P , v j has at most one child for j = 2, . . . , ℓ − 1. If an input graph forms this sequence, then after contracting by Contraction Rule 3 and Contraction Rule 1, the size of the resultant graph becomes constant. This shrinking prevents the algorithm from satisfying the push out condition. To avoid such a situation, we introduce another rule.

Multibirth Rule
Let v be a vertex adjacent exactly two distinct neighbors u, w such that (1) t(P ) = v, (2) v has no loops, (3) µ v (u) = µ v (w) = 2, and (4) both (u, v) and (v, w) are bad. Then make a copy G 1 of an input graph G, and remove v from G 1 and add two edges between u and w. In addition, if v is not a cut in G, then make an additional copy G 2 of an input graph G, and remove v from G 2 and add self loops (u, u) and (w, w).
This rule comes from the following observation: each solution in G contains either (1) two (u, v, w) or (2) (u, v, u) and (w, v, w). Note that if v is a cut, then all the solutions contain the former subtrails. Moreover, the set of solutions in G 1 and G 2 are disjoint. Hence, if there is such a vertex v, then the algorithm replace a child Q of P with two children Q 1 and Q 2 whose associated graphs are G 1 and G 2 , respectively.
In this section, we say that a graph G is contracted if Contraction Rule 1 to Contraction Rule 4 can not be applicable to G \ P . If a graph is contracted, then we show the key lemmas in this section.
Lemma 11. Let G be a contracted graph and P be a partial Eulerian trail in G. Suppose that P ′ G of the parent P ′ of P is already obtained. Then, contracting P G can be done in constant time. Proof. Let e = (u, v) be an edge such that P = P ′ + e. Firstly, removing e from P ′ G can be done in constant time. We consider that which rule can be applied to P G . Because P ′ G is contracted, we have a case analysis with respect to the multiplicity µ v of edges on P ′ G and the number of neighbors of v as follows; Case A: v has at least three distinct neighbors u, w 1 , w 2 If µ v (u) > 1 or more than three distinct neighbors, then no rules can be applicable to v. We assume that v has exactly three distinct neighbors u, w 1 , w 2 such that µ v (u) = 1.
We can not apply any rules to w 1 and w 2 after adding e to P ′ .
A.2: µ v (w 1 ) = 1 and µ v (w 2 ) ≥ 1 We first apply Contraction Rule 3 and Contraction Rule 2 to v. Then, possibly, we also apply Contraction Rule 1, Contraction Rule 2, and Contraction Rule 4 to the copy of v. If w 2 is incident to w 3 such that µ w2 (w 3 ) = 1, then Contraction Rule 2 can be applied to w 2 . However, no more rule can be applied to neither w 1 nor x.
Case B: v has exactly two distinct neighbors u, w If µ v (u) = 3 and µ v (w) = 2, then this case is the same as case A.1. Otherwise, we can not apply any rules to v and w after adding e to P ′ .
This case is the same as case A.2 after adding e to P ′ .
In this case, Contraction Rule 3 can be applied to v on P ′ G . Because P ′ G is contracted, this derives a contradiction. Thus, this case does not happen.
We also have the same cases for u. Thus, from the above observation, the number of applying contracting rules is constant. Moreover, each rule can be proceeded in constant time. Therefore, the lemma holds.
Let P ′ = P + e be the parent of P . Assume that P G is obtained by removing e and repeatedly applying Contraction Rule 1 to Contraction Rule 4 to P ′ G . Assume that P ′ G has no vertex to which Multibirth Rule can be applied. This yields the number of vertices to which Multibirth Rule is applicable in P G is constant. Moreover, when making children of P , we avoid to generate all the whole copies of P G by applying Multibirth Rule to reduce the amount of space usage. In stead of whole copying, we locally modify v that is a target of Multibirth Rule. Hence, the following clearly holds.
Lemma 12. Let G be a graph and P be a partial Eulerian trail in G. Suppose that P G is contracted. Then, children of P that are generated by applying Multibirth Rule to P G can be obtained in constant time.
From the above discussion, we can obtain the following key lemma.
Lemma 13. A proper partial Eulerian trail P of a contracted graph G has at least two children.
Proof. Because G is contracted, each vertex has at least two distinct neighbors. If t(P ) is incident to two bridges, then this contradicts with the definition of a partial Eulerian trail. Thus, t(P ) is incident to at most one bridge. In addition, if t(P ) is not incident to a bridge, then the lemma clearly holds.
Suppose that t(P ) is incident to a bridge e = (t(P ), u). If t(P ) is adjacent to exactly two vertices, then this implies that we can apply Contraction Rule 3. Hence, t(P ) is adjacent to more than two distinct vertices and the statement holds.
By Lemmas 11 to 13, each non-leaf node P on the family tree made by the algorithm has at least two children and can be done in O(m P ) time, where m P is the number of edges in the contracted graph P G . Remind that the computation time is dominated by detecting bridges on P G . Thus, by the same discussion in the previous section, the main theorem in this section is established.