 # 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 edge-distinct if the edge sequences are not identical, and they are 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 𝒪(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].

## Authors

##### This week in AI

Get the week's most popular data science and artificial intelligence research sent straight to your inbox every Saturday.

## 1 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  bioinformatics [11, 12], and automaton theory .

Deciding for the existence of a Eulerian trail can be done in time polynomial . However, the counting problem of edge-distinct Eulerian trails is #¶-complete  for general undirected graphs, although for directed graphs, Aardenne and Brujin  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 edge-distinct Eulerian trails and that of vertex-distinct Eulerian trails are equivalent. Thus, counting Eulerian trails is intractable for general cases unless . Recently, Conte et al.  give an algorithm that answers whether a graph contains at least Eulerian trails in time polynomial in and , where 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 , -paths [7, 3], cycles , maximal cliques [16, 17], and many others . 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 runs in polynomial amortized time if the total running time of is time, where is the size of inputs and is the number of solutions. That is, outputs solutions in time per each on average. In this contexts, the ultimate goal is to develop time enumeration algorithm, that is , and such optimal algorithms have been obtained [18, 13]. Under this evaluation, Kikuchi  proposed an time algorithm for simple general graphs, where 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 . 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 is the parent of a partial solution if is obtained by adding one edge to . Although our algorithm is quite simple, with a sophisticated analysis  and contracting operations for graphs, we achieve constant amortized time per solution.

## 2 Preliminaries

An undirected graph is a pair of a vertex set and an edge set . Note that if and only if for each pair of vertices . Graphs may contain self loops and parallel edges. For each vertex , the neighborhood is a multiset of the underlying set , where is the set of vertices adjacent to and is a function from to a positive integer. That is, represents the number of distinct edges in a graph. The degree of a vertex is defined as . A vertex is pendant if . Let be the set of edges incident to .

A sequence is a trail if for each , 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 in for . 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 . is Eulerian if has at least one Eulerian trail. It is known that

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

as a subsequence of containing all the edges in , and similarly, as a subsequence of containing all the vertices in . Let and be two Eulerian trails. We say is edge-distinct from if and we say is vertex-distinct from if . A sequence is a prefix of if for some .

Assume that every Eulerian trail starts from and ends with . A trail is a partial Eulerian trail in if starts from and there is an Eulerian trail that contains as a prefix. Especially, if , then is called a proper partial Eulerian trail. An edge is said to be addible if is also a partial Eulerian trail. We denote by the end vertex of a partial Eulerian trail such that . For each partial Eulerian trail , we denote by such that a vertex is contained in if there is an edge in such that is incident to it. That is, is the set of edges which we have to add to for constructing a solution. Now, we formalize our problems as follows.

###### Problem 1 (Edge-distinct Eulerian trail enumeration).

Given a graph , output all the edge-distinct Eulerian trails in without duplicate.

###### Problem 2 (Vertex-distinct Eulerian trail enumeration).

Given a graph , output all the vertex-distinct Eulerian trails in without duplicate.

## 3 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 . 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 . The tree consists of nodes and edges . Each node in is associated with a partial Eulerian trail and a graph . Let be the associated partial Eulerian trail of . If no confusion arises, we identify a node with its associated a partial Eulerian trail. The root of is the trail consisting of . For two partial Eulerian trails and , there is a directed edge in if and . We say that is the parent of and is a child of . Note that a partial Euler path may have more than one children. Let be the set of child partial Eulerian trails of . From the definition of the parent-child relation, for any partial Eulerian trail , there is a unique path from to the root on obtained by recursively removing an edge in . Hence, for any node in , there is a path from to in .

Let be the candidates of addible edges to without violating the parent-child relation. To traverse all the children of a partial Eulerian trail , generating partial Eulerian trails is enough if satisfies the condition given in the next lemma.

###### Lemma 3.

Let be a partial Eulerian trail in and be an edge in . Then, is a child of if and only if or is not a bridge.

###### Proof.

Suppose that is a child of . Assume that and is a bridge. This implies that is disconnected, and thus, this contradicts the assumption that is a partial Eulerian trail.

If contains exactly one edge, then the lemma clearly holds. Suppose that . From the definition of a partial Eulerian trail, contains at most one bridge. Hence, contains a non-bridge edge. ∎

Now, we give our proposed enumeration algorithm as follows. The algorithm starts with the root partial Eulerian trail . Then, for each edge in , the algorithm generates a child partial Eulerian trail of if meets the condition by Lemma 3. The algorithm recursively generates descendant nodes of by adding edges in the candidate set by depth-first traversal on . 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.

###### Theorem 4.

Algorithm 1 outputs all the edge-distinct Eulerian trails in a given graph once and only once.

### 3.1 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 . Let . Finding all bridges in consumes time by Tarjan’s algorithm . Thus, by using these bridges, we can find edges satisfying the condition in Lemma 3 in . In addition, can be computed in constant time by just removing from . Hence, the following lemma holds.

###### Lemma 5.

Each recursive call at Algorithm 1 of Algorithm 1 makes all the child recursive calls in time.

From Lemma 5, we need some trick to achieve constant amortized time enumeration since recursive calls near to the root recursive call needs time. In this paper, we employ the push out amortization technique as the trick introduced by Uno .

We first introduce some terminology. Let be a partial Eulerian trail. The phrase “the algorithm generates indicates that the algorithm computes and at Algorithm 1 and Algorithm 1 of Algorithm 1, respectively. We denote by of the exact number of computational steps for generating . That is, does not include the time complexity of descendants of . Let be the sum of the computation time of generating children of .

Roughly speaking, the push out amortization technique gives a sufficient condition for proving that an enumeration algorithm runs in per solution on average, where is the maximum time complexity among leaves. This sufficient condition is called the push out condition defined as follows.

###### Definition 6.

Let be a partial Eulerian trail. We call the following equation the push out condition for our problem: For some two constants and ,

 ¯¯¯¯T(P)≥αT(P)−β(|C(P)|+1)T∗.

Note that we slightly modify the original condition given in  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, because 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 has at most one child, then the condition may not hold because for the child of , is larger than and thus can be smaller than . Hence, we modify by contracting vertices and edges so that each non-leaf partial Eulerian trail has at least two children. This contraction of a graph is performed by applying the follow rules. Let be a vertex in .

Contraction Rule 1

If is a pendant, then remove .

Contraction Rule 2

If is incident to exactly two distinct edges and , then remove and add edge to .

See Figures 2 and 1. Note that when Contraction Rule 1 is applied to , the last vertex of Eulerian trails becomes the unique neighbor of in . We also note that when applying Contraction Rule 2, may hold, that is, a loop on may be generated. After recursively applying these operations, we say is contracted if neither Contraction Rule 1 nor Contraction Rule 2 can not be applicable to . Clearly, there is a surjection from partial Eulerian trails in to ones in the contracted graph of . The next lemma shows the time complexity for obtaining contracted graphs.

###### Lemma 7.

Let be a graph, be a partial Eulerian trail in such that there is the parent satisfying . Assume that the contracted graph of is already obtained. Then, the contraction of can be done in constant time.

###### Proof.

The degree of vertices other than and is same in and . 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 or in . Hence, by checking the degree of and , the contraction can be done in constant time. ∎

###### Lemma 8.

Each vertex in a contracted graph is incident to at least two non-bridge edges.

###### Proof.

Let be a contracted graph such that 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 incident to two distinct bridges and . Otherwise, the lemma holds. Let be a connected component of such that for . Then, there are two classes of Eulerian trails the one contains paths first visit and next visit , and the other first visits and next . Because is Eulerian, must be incident to at least two edges for visiting all the edges in . Hence, the statement holds. ∎

Because detecting bridges can be done in time , the time complexity of each iteration is . In addition, the difference between the number of edges in and that in of a child of 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 . 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 [16, 14]

During the execution of the algorithm, the algorithm maintains two working memories and . These are implemented by doubly-linked lists. contains the current partial Eulerian trail and contains the information of contractions. When the algorithm finds an edge such that is a child of , then the algorithm appends to . In addition, the algorithm also appends an information to , where represents how the algorithm contracts a graph. forms a set of tuples each of which contains what is added and removed from , and which rule is used. Then, the algorithm prints . We can easily see that the length of each output is constant. When the algorithm backtracks from , then the algorithm removes from and from , and prints . When the algorithm reaches a leaf, the algorithm prints to indicate that a solution is stored in . By using the information stored in the working memory, we can get the corresponding Eulerian trail if necessary. Moreover, it is enough to allocate space to each working memory.

Lemmas 8 and 3 imply that each partial Eulerian trail has either zero or at least two children. This yields the following corollary.

###### Corollary 9.

The number of partial Eulerian trails generated by the algorithm is .

Remind that partial Eulerian trails corresponds to nodes on . Thus, by Corollary 9, the total length of outputs is and the next theorem holds.

###### Theorem 10.

One can enumerate all the Eulerian trails in a graph in constant amortized time per solution with linear space and preprocess time in the size of the graph.

## 4 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 of vertices is good if there are two parallel edges and between and such that the corresponding trail of in the original graph differs from that of . Otherwise, we say is bad. Note that we can check whether 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 has at least two distinct edges, we slightly modify an input graph of each recursive call as follows.

Contraction Rule 3

If has exactly two neighbors and such that , is bad, and are distinct from , and has no loops, then remove edges between and . Then, add a vertex and edges between and .

Contraction Rule 4

Remove incident edges of a vertex if has no loops, has exactly one neighbor such that is bad and . Then, add loops to . In addition, add an edge if is odd.

See Figures 4 and 3. Note that after performing Contraction Rule 3, the number of edges in the resultant graph and the original graph are same, and Contraction Rule 2 is applicable to in . In addition, after performing Contraction Rule 4, Contraction Rule 1 may be applicable. When Contraction Rule 3 is applicable to , there are (a) at least one solution containing or and (b) at least one solutions containing . This implies that if 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 and , which is obtained by applying Contraction Rule 3 or Contraction Rule 4.

However, applying Contraction Rule 3 and Contraction Rule 4 may make drastically small. Let be a vertex sequence such that for each , and . For each , if , then has two children. However, after adding an edge incident to to , has at most one child for . 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 be a vertex adjacent exactly two distinct neighbors such that (1) , (2) has no loops, (3) , and (4) both and are bad. Then make a copy of an input graph , and remove from and add two edges between and . In addition, if is not a cut in , then make an additional copy of an input graph , and remove from and add self loops and .

This rule comes from the following observation: each solution in contains either (1) two or (2) and . Note that if is a cut, then all the solutions contain the former subtrails. Moreover, the set of solutions in and are disjoint. Hence, if there is such a vertex , then the algorithm replace a child of with two children and whose associated graphs are and , respectively.

In this section, we say that a graph is contracted if Contraction Rule 1 to Contraction Rule 4 can not be applicable to . If a graph is contracted, then we show the key lemmas in this section.

###### Lemma 11.

Let be a contracted graph and be a partial Eulerian trail in . Suppose that of the parent of is already obtained. Then, contracting can be done in constant time.

###### Proof.

Let be an edge such that . Firstly, removing from can be done in constant time. We consider that which rule can be applied to . Because is contracted, we have a case analysis with respect to the multiplicity of edges on and the number of neighbors of as follows;

Case A: has at least three distinct neighbors

If or more than three distinct neighbors, then no rules can be applicable to . We assume that has exactly three distinct neighbors such that .

A.1: or

We can not apply any rules to and after adding to .

A.2: and

We first apply Contraction Rule 3 and Contraction Rule 2 to . Then, possibly, we also apply Contraction Rule 1, Contraction Rule 2, and Contraction Rule 4 to the copy of . If is incident to such that , then Contraction Rule 2 can be applied to . However, no more rule can be applied to neither nor .

Case B: has exactly two distinct neighbors

B.1:

If and , then this case is the same as case A.1. Otherwise, we can not apply any rules to and after adding to .

B.2:

This case is the same as case A.2 after adding to .

B.3:

In this case, Contraction Rule 3 can be applied to on . Because is contracted, this derives a contradiction. Thus, this case does not happen.

We also have the same cases for . 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 be the parent of . Assume that is obtained by removing and repeatedly applying Contraction Rule 1 to Contraction Rule 4 to . Assume that has no vertex to which Multibirth Rule can be applied. This yields the number of vertices to which Multibirth Rule is applicable in is constant. Moreover, when making children of , we avoid to generate all the whole copies of by applying Multibirth Rule to reduce the amount of space usage. In stead of whole copying, we locally modify that is a target of Multibirth Rule. Hence, the following clearly holds.

###### Lemma 12.

Let be a graph and be a partial Eulerian trail in . Suppose that is contracted. Then, children of that are generated by applying Multibirth Rule to can be obtained in constant time.

From the above discussion, we can obtain the following key lemma.

###### Lemma 13.

A proper partial Eulerian trail of a contracted graph has at least two children.

###### Proof.

Because is contracted, each vertex has at least two distinct neighbors. If is incident to two bridges, then this contradicts with the definition of a partial Eulerian trail. Thus, is incident to at most one bridge. In addition, if is not incident to a bridge, then the lemma clearly holds.

Suppose that is incident to a bridge . If is adjacent to exactly two vertices, then this implies that we can apply Contraction Rule 3. Hence, is adjacent to more than two distinct vertices and the statement holds. ∎

By Lemmas 12, 13 and 11, each non-leaf node on the family tree made by the algorithm has at least two children and can be done in time, where is the number of edges in the contracted graph . Remind that the computation time is dominated by detecting bridges on . Thus, by the same discussion in the previous section, the main theorem in this section is established.

###### Theorem 14.

All vertex-distinct Eulerian trails can be enumerated in constant amortized time per solution with linear time preprocessing and linear space.

## Acknowledgement

This work was partially supported by JST CREST Grant Number JPMJCR18K3 and JSPS KAKENHI Grant Numbers 19K20350, JP19J10761, and JP20H05793, Japan.

## References

•  T. Aardenne-Ehrenfest, van and N.G. Bruijn, de (1951) Circuits and trees in oriented linear graphs. Simon Stevin : Wis- en Natuurkundig Tijdschrift 28, pp. 203–217. External Links: ISSN 0037-5454 Cited by: §1.
•  D. Avis and K. Fukuda (1996) Reverse search for enumeration. Discrete Applied Mathematics 65 (1), pp. 21 – 46. Note: First International Colloquium on Graphs and Optimization Cited by: §1, §3.
•  E. Birmelé, R. Ferreira, R. Grossi, A. Marino, N. Pisanti, R. Rizzi, and G. Sacomoto (2012) Optimal listing of cycles and st-paths in undirected graphs. In SODA 2012: the 24th Annual ACM-SIAM Symposium on Discrete Algorithms, pp. 1884–1896. Cited by: §1.
•  G. R. Brightwell and P. Winkler (2005) Counting eulerian circuits is #p-complete. In Proceedings of the Seventh Workshop on Algorithm Engineering and Experiments and the Second Workshop on Analytic Algorithmics and Combinatorics, ALENEX /ANALCO 2005, Vancouver, BC, Canada, 22 January 2005, C. Demetrescu, R. Sedgewick, and R. Tamassia (Eds.), pp. 259–262. External Links: Link Cited by: §1.
•  Z. Chen, X. He, and C. Huang (2002) Finding double euler trails of planar graphs in linear time. SIAM Journal on Computing 31 (4), pp. 1255–1285. Cited by: §1.
•  A. Conte, R. Grossi, G. Loukides, N. Pisanti, S. P. Pissis, and G. Punzi (2020) Fast Assessment of Eulerian Trails. In Fourth International Workshop on Enumeration Problems and Applications, Cited by: §1.
•  R. Ferreira, R. Grossi, R. Rizzi, G. Sacomoto, and S. Marie-France (2014) Amortized -delay algorithm for listing chordless cycles in undirected graphs. In ESA 2014: the 22th Annual European Symposium on Algorithms, Lecture Notes in Computer Science, Vol. 8737, pp. 418–429. Cited by: §1.
•  C. Hierholzer and C. Wiener (1873) Ueber die Möglichkeit, einen Linienzug ohne Wiederholung und ohne Unterbrechung zu umfahren. Mathematische Annalen 6 (1), pp. 30–32. External Links: Document, ISSN 0025-5831, Link Cited by: §1.
•  J. Kari (2003) Synchronizing finite automata on eulerian digraphs. Theoretical Computer Science 295 (1), pp. 223 – 232. Cited by: §1.
•  Y. Kikuchi (2010) Enumerating All Eulerian Trails (in Japanese). Technical report Technical Report Vol.2010-AL-131 No.7, IPSJ. Cited by: §1.
•  C. Kingsford, M. C. Schatz, and M. Pop (2010-01) Assembly complexity of prokaryotic genomes using short reads. BMC Bioinformatics 11 (1). Cited by: §1.
•  K. Roy (2007) Optimum gate ordering of CMOS logic gates using euler path approach: some insights and explanations. Journal of Computing and Information Technology 15 (1), pp. 85. Cited by: §1.
•  N. Schweikardt, L. Segoufin, and A. Vigny (2018) Enumeration for fo queries over nowhere dense graphs. In Proceedings of the 37th ACM SIGMOD-SIGACT-SIGAI Symposium on Principles of Database Systems, SIGMOD/PODS ’18, New York, NY, USA, pp. 151–163. External Links: ISBN 9781450347068 Cited by: §1.
•  A. Shioura, A. Tamura, and T. Uno (1997) An optimal algorithm for scanning all spanning trees of undirected graphs. SIAM Journal on Computing 26 (3), pp. 678–692. Cited by: §1, §3.1.
•  R. Tarjan (1974) A note on finding the bridges of a graph. Information Processing Letters 2 (6), pp. 160 – 161. External Links: ISSN 0020-0190, Document, Link Cited by: §3.1, §3.1.
•  E. Tomita, A. Tanaka, and H. Takahashi (2006) The worst-case time complexity for generating all maximal cliques and computational experiments. Theoretical Computer Science 363 (1), pp. 28 – 42. Note: Computing and Combinatorics External Links: ISSN 0304-3975 Cited by: §1, §3.1.
•  S. Tsukiyama, M. Ide, H. Ariyoshi, and I. Shirakawa (1977) A new algorithm for generating all the maximal independent sets. SIAM Journal on Computing 6 (3), pp. 505–517. Cited by: §1.
•  T. Uno (2015) Constant time enumeration by amortization. In Proc. of WADS 2015, pp. 593–605. Cited by: §1, §1, §3.1, §3.1.
•  K. Wasa (2016) Enumeration of enumeration algorithms. Note: arXiv:1605.05102 External Links: 1605.05102 Cited by: §1.