AN OPTIMAL XP ALGORITHM FOR HAMILTONIAN CYCLE ON GRAPHS OF BOUNDED CLIQUE-WIDTH

BENJAMIN BERGOUGNOUX, MAMADOU MOUSTAPHA KANTÉ, AND O-JOUNG KWON

Abstract. In this paper, we prove that, given a clique-width k-expression of an n-vertex graph, Hamiltonian Cycle can be solved in time nO(k). This improves the naive algorithm that runs 2 in time nO(k ) by Espelage et al. (WG 2001), and it also matches with the lower bound result by Fomin et al. that, unless the Exponential Time Hypothesis fails, there is no algorithm running in time no(k) (SIAM. J. Computing 2014). We present a technique of representative sets using two-edge colored on k vertices. The essential idea is that, for a two-edge colored , the existence of an Eulerian trail that uses edges with different colors alternately can be determined by two information: the number of colored edges incident with each vertex, and the connectedness of the multigraph. With this idea, we avoid the bottleneck of the naive algorithm, which stores all the possible multigraphs on k vertices with at most n edges.

1. Introduction Tree-width is one of the most well-studied graph parameters in the graph algorithm community, due to its numerous structural and algorithmic properties (see the survey [4]). A celebrated al- gorithmic meta-theorem by Courcelle [8] states that every graph problem expressible in monadic second-order logic (MSO2) can be decided in linear time on graphs of bounded tree-width. Among the problems expressible in MSO2, there are some well-known NP-hard problems such as Minimum Dominating Set, Hamiltonian Cycle, and Graph Coloring. Despite the broad interest on tree-width, only sparse graphs can have bounded tree-width. But, on many classes, some NP-hard problems admit polynomial-time algorithms, and many of these algorithms can be explained by the boundedness of their clique-width. Clique-width is a graph parameter that originally emerges from the theory of graph grammars [10] and the terminology was first introduced by Courcelle and Olariu [13] (see also the book [9]). Clique-width is defined in terms of the following graph operations: (1) addition of a single labeled vertex, (2) addition of all possible edges between vertices labeled i and those labeled j, (3) renaming of labels, and (4) taking the disjoint union of two labeled graphs. The clique-width of a graph is the minimum number of labels needed to construct it. An expression constructing a graph with at arXiv:1702.06095v3 [cs.DS] 9 Jun 2019 most k labels is called a (clique-width) k-expression. The modeling power of clique-width is strictly stronger than the modeling power of tree-width. In other words, if a graph class has bounded tree-width, then it has bounded clique-width, but the converse is not true. Computing the clique-width of a graph is a problem which has received significant attention over the last decade. Fellows et al. [17] showed that computing clique-width is NP-hard. For a fixed k, the best known approximation algorithm is due to Hliněný and Oum [24]; it computes in time O(f(k) · n3) a (2k+1 − 1)-expression for an n-vertex graph of clique-width at most k.

Date: June 11, 2019. An extended abstract appeared in Algorithms and Data Structures, WADS 2017 [3]. B. Bergougnoux and M.M. Kanté are supported by French Agency for Research under the GraphEN project (ANR-15-CE40-0009). O. Kwon is supported by the European Research Council (ERC) under the European Union’s Horizon 2020 research and innova- tion programme (ERC consolidator grant DISTRUCT, agreement No. 648527), and also supported by the National Research Foundation of Korea (NRF) grant funded by the Ministry of Education (No. NRF-2018R1D1A1B07050294). 1 Courcelle, Makowsky, and Rotics [11] extended the meta-theorem of Courcelle [8] to graphs of bounded clique-width at a cost of a smaller set of problems. More precisely, they showed that every problem expressible in monadic second order logic with formula that does not use edge set quantifications (called MSO1) can be decided in time O(f(k) · n) in any n-vertex graph of clique- width k, provided that a clique-width k-expression of it is given. For some MSO1 problems, clique-width and tree-width have sensibly the same behavior. Indeed, O(k) O(1) many problems expressible in MSO1 that admit 2 ·n -time algorithms parameterized by tree- width have been shown to admit 2O(k) · nO(1)-time algorithms, when a clique-width k-expression is given. These include famous problems like Minimum Dominating Set and Minimum Vertex Cover [7, 23, 28], or even their connected variants and Feedback Vertex Set [2, 5, 14]. On the other hand, several classical problems, such as Max-Cut, Edge Dominating Set (EDS), Graph Coloring (GC), and Hamiltonian Cycle (HC), are not expressible in MSO1. These problems are known to be FPT parameterized by tree-width thanks to Courcelle’s theorem or some variants of this latter theorem [1, 6, 12]. They are also known to admit XP algorithms parameterized by clique-width [16, 20, 25]. A natural question is whether these problems admit algorithms with running time f(k)·nO(1) given a k-expression of the input graph. Fomin, Golovach, Lokshtanov, and Saurabh [19] proved the W[1]- hardness of EDS, GC, and HC with clique-width as parameter, which implies that these problems do not admit algorithms with running time f(k) · nO(1), for any function f, unless W[1] = FPT. In 2014, the same authors [20] have proved that Max-Cut and EDS admit nO(k)-time algorithms, and that they do not admit f(k) · no(k)-time algorithms unless ETH fails. In the conclusion of [20], the authors state that HC does not admit f(k)·no(k)-time algorithm under ETH (they gave the proof in [21]) and they left an open question of finding an algorithm with running time f(k) · nO(k). At the time, the best known running time parameterized by clique-width for HC and GC were respectively nO(k2) [16] and nO(2k) [25]. Recently, Fomin et al. [21] provided a lower bound of f(k) · n2o(k) for GC. Our Contribution and Approach. In this paper, we prove that there exists an algorithm solving Hamiltonian Cycle in time nO(k), when a clique-width k-expression is given. A Hamiltonian cycle of a graph G is a cycle containing all the vertices of G. The problem Hamiltonian Cycle asks, given a graph G, if G contains a Hamiltonian cycle. Specifically, we prove the following. Theorem 1.1. There exists an algorithm that, given an n-vertex graph G and a k-expression of G, 2k+5 2k(log (k)+1) 3 solves Hamiltonian Cycle in time O(n · 2 2 · k log2(nk)). Our algorithm is a dynamic programming one whose steps are based on the k-labeled graphs H arising in the k-expression of G. Observe that the edges of a Hamiltonian cycle of G which belong to E(H) induce either a Hamiltonian cycle or a collection of vertex-disjoint paths in G covering H. Consequently, we define a partial solution as a set of edges of H which induces a collection of paths (potentially empty) covering H. As in [16], with each partial solution P, we associate an auxiliary multigraph such that its vertices correspond to the labels of H and each edge {i, j} corresponds to a maximal path induced by P with -vertices labeled i and j. Since H is a k-labeled graph arising in a k-expression of G, we have that two vertices x and y with the same label in H have the same neighborhood in G − E(H) (the graph G without the edges of H). It follows that the endpoints of a path in a partial solution are not important and what matters are the labels of these endpoints. As a result, two partial solutions with the same auxiliary multigraph are equivalent, i.e., if one is contained in a Hamiltonian cycle, then the other also. From these observations, one easily deduces the nO(k2)-time algorithm, due to Espelage, Gurski, and Wanke [16], because there are at most n possible paths between two label classes and thus there are at most nO(k2) possible auxiliary graphs. 2 To obtain our nO(k)-time algorithm, we refine the above equivalence relation. We define that two partial solutions are equivalent if their auxiliary graphs have the connected components on same vertex sets, and the paths they induce have the same number of endpoints labeled i, for each label i. The motivation of this equivalence relation can be described as follows. Suppose that we have a partial solution P and a set of edges Q ⊆ E(G) \ E(H) so that P ∪ Q forms a Hamiltonian cycle, and we consider to make an auxiliary graph of Q, and identify with the one for P. To distinguish edges obtained from P or Q, we color edges by red if one came from P and by blue otherwise. Then following the Hamiltonian cycle, we can find an Eulerian trail of this merged auxiliary graph that uses edges of distinct colors alternately. But then if P0 is equivalent to P, then one can observe that if we replace the red part with the auxiliary graph of P0, then it also has such an Eulerian trail, and we can show that P0 can also be completed to a Hamiltonian cycle. So, in the algorithm, for each equivalence class, we store one partial solution. We define this equivalence relation formally in Section 3. Since, the number of partitions of a k-size set is at most kk and the number of paths induced by a partial solution is always bounded by n, the number of non-equivalent partial solutions is then bounded by (2n)k ·kk (the maximum of an auxiliary multigraph is at most 2n because a is counted as two edges). The running time of our algorithm follows from the maximum number of non-equivalent partial solutions. The main effort in the algorithm consists then in updating the equivalence classes of this equivalence relation in terms of operations based on the clique-width operations. Overview. In Section 2, we give basic definitions and notations concerning (multi)graphs and clique-width. Our notions of partial solutions and of auxiliary multigraphs are given in Section 3. In Section 4, we prove the equivalence between the existence of Hamiltonian cycles in the input graph and the existence of red-blue Eulerian trails in auxiliary multigraphs, and deduce that it is enough to store (2n)k · kk partial solutions at each step of our dynamic programming algorithm. In Section 5, we show how to obtain from the results of Section 4 an nO(k)-time algorithm for Hamiltonian Cycle. In Section 6, we give some intuitions for solving in time nO(k) the problems Directed Hamiltonian Cycle given a k-expression. We end up with some concluding remarks and open questions in Section 7.

2. Preliminaries The size of a set V is denoted by |V |, and we write [V ]2 to denote the set of all subsets of V of size 2. We denote by N the set of non-negative integers. For two sets A and B, we let ( ∅ if A = ∅ or B = ∅, A ⊗ B := {X ∪ Y | X ∈ A ∧ Y ∈ B} otherwise.

For a positive integer n, let [n] := {1, 2, . . . , n}. Graph. We essentially follow Diestel’s book [15] for our graph terminology, but we deal only with finite graphs. We distinguish graphs and multigraphs, and for graphs we do not allow to have multiple edges or loops, while we allow them in multigraphs. The vertex set of a graph G is denoted by V (G) and its edge set is denoted by E(G) ⊆ [V (G)]2. We write xy to denote an edge {x, y}. Let G be a graph. For X ⊆ V (G), we denote by G[X] the subgraph of G induced by X. For F ⊆ E(G), we write G|F for the subgraph (V (G),F ) and G−F for the subgraph (V (G),E(G)\F ). For an edge e of G, we simply write G−e for G−{e}. The degree of a vertex x, denoted by degG(x), is the number of edges incident with x. The set of vertices adjacent to a vertex v is denoted by NG(x).

3 Multigraph. A multigraph is essentially a graph, but we allow multiple edges, i.e., edges incident with the same set of vertices. Formally, a multigraph G is a pair (V (G),E(G)) of disjoint sets, also 2 called sets of vertices and edges, respectively, together with a map multG : E(G) → V (G)∪[V (G)] , which maps every edge to one or two vertices, still called its endpoints. The degree of a vertex x in a multigraph G, is defined analogously as in graphs, except that each loop is counted twice, and similarly for other notions. If there are exactly k edges e such that multG(e) = {x, y} (or multG(e) = {x}), then we denote these distinct edges by {x, y}1,..., {x, y}k (or {x}1,..., {x}k). For two multigraphs G and H on the same vertex set {v1, . . . , vk} and with disjoint edge sets, we denote by G ] H the multigraph with vertex set {v1, . . . , vk}, edge set E(G) ∪ E(H), and ( multG(e) if e ∈ E(G), multG]H (e) := multH (e) otherwise. If the edges of G and H are colored, then this operation preserves the colors of the edges. Walk. A walk of a graph is a sequence of vertices and edges, starting and ending at some vertices, called end-vertices, where for every consecutive pair of a vertex x and an edge e, x is incident with e. The vertices of a walk which are not end-vertices are called internal vertices. A trail of a graph is a walk where each edge is used at most once. A walk (or a trail) is closed if its two end-vertices are the same. Moreover, when the edges of a graph are colored red or blue, we say that a walk W = (v1, e1, . . . , vr−1, er−1, vr) is a red-blue walk, if, for every i ∈ {1, . . . , r − 2}, the colors of ei and ei+1 are different and the colors of e1 and er−1 are different, when the walk is closed. We adapt all the notations to multigraphs as well. 0 0 0 0 0 For two walks W1 = (v1, e1, . . . , e`−1, v`) and W2 = (v1, e1, . . . , er−1, vr) such that v` = v1, the 0 0 0 concatenation of W1 and W2, denoted by W1 − W2, is the walk (v1, e1, . . . , e`−1, v`, e1, . . . , er−1, vr). A path of a graph is a walk where each vertex is used at most once. A cycle of a graph is a closed walk where each vertex other than the end-vertices is used at most once. An (closed) Eulerian trail in a graph G is a closed trail containing all the edges of G. In particular, if the edges of a graph are colored red or blue, then a red-blue Eulerian trail is an Eulerian trail that is a red-blue walk.

Clique-width. A k-labeled graph is a pair (G, labG) of a graph G and a function labG from V (G) −1 to [k], called the labeling function. We denote by labG (i) the set of vertices in G with label i. The notion of clique-width is defined by Courcelle and Olariu [13] and is based on the following operations: • creating a graph, denoted by i(x), with a single vertex x labeled with i ∈ [k]; • for a labeled graph G and distinct labels i, j ∈ [k], relabeling the vertices of G with label i to j, denoted by ρi→j(G); • for a labeled graph G and distinct labels i, j ∈ [k], adding all the non-existent edges between vertices with label i and vertices with label j, denoted by ηi,j(G); • taking the disjoint union of two labeled graphs G and H, denoted by G ⊕ H, with ( labG(v) if x ∈ V (G), labG⊕H (v) := labH (v) otherwise. A clique-width k-expression, or shortly a k-expression, is a finite well-formed term built with the four operations above and using at most k labels. Each k-expression φ evaluates into a k-labeled graph (val(φ), labval(φ)). The clique-width of a graph G, denoted by cw(G), is the minimum k such that G is isomorphic to val(φ) for some k-expression φ. We can assume without loss of generality that any k-expression defining a graph G uses O(n) disjoint union operations and O(nk2) unary operations [13]. It is worth noticing, from the recursive definition of k-expressions, that one can compute in time linear in |φ| the labeling function labval(φ) of val(φ), and hence we will always assume that it is given. 4 For example, the cycle abcdea of length 5 can be constructed using the 3-expression represented as a tree-structure in Figure 1.

η1,3

ρ3→2 3(e)

η2,3

η1,2 η1,3

⊕ ⊕

1(a) 2(b) 3(c) 1(d)

Figure 1. An irredundant 3-expression of C5.

The set of subexpressions of a k-expression φ, denoted by Sub(φ), is defined by the following induction:  {φ} if φ := i(x) with i ∈ [k],  Sub(φ) := {φ} ∪ Sub(φ0) ∪ Sub(φ?) if φ = φ0 ⊕ φ?,  0 0 {φ} ∪ Sub(φ ) if φ = f(φ ) with f ∈ {ρi→j, ηi,j | i, j ∈ [k]}. 0 We say that a k-labeled graph (H, labH ) arises in a k-expression φ if H = val(φ ) and labH = labval(φ0), for some φ0 ∈ Sub(φ). 0 A k-expression φ is called irredundant if, for every subexpression ηi,j(φ ) of φ, there are no edges in val(φ0) between the vertices labeled i and the vertices labeled j. Courcelle and Olariu [13] proved that given a clique-width k-expression, it can be transformed into an irredundant k-expression in linear time. The following useful properties of an irredundant k-expression will be used in Section 4. Lemma 2.1. Let H be a k-labeled graph arising in an irredundant k-expression φ of a graph G. For all u, v ∈ V (H) with labH (u) = i and labH (v) = j, we have the following. (1) If i = j, then NG(u) \ V (H) = NG(v) \ V (H). (2) If uv ∈ E(G) \ E(H), then i 6= j and, for all x, y ∈ V (H) with labH (x) = i and labH (y) = j, we have xy ∈ E(G) \ E(H). Proof. (1) Assume that i = j. Let φ0 be the subexpression of φ such that H = val(φ0). As u and v have the same label in H, in every subexpression of φ having φ0 as a subexpression, u and v have the same label. Since edges are added only through the operation ηa,b, we conclude that NG(u) ∩ (V (G) \ V (H)) = NG(v) ∩ (V (G) \ V (H)).

(2) Assume that uv ∈ E(G) \ E(H). Then, we have i 6= j because the operation ηa,b adds edges only between vertices with distinct labels. Let φ0 be the minimal (size wise) subexpression of φ 0 0 ? ? such that uv ∈ E(val(φ )). It follows that φ = ηa,b(φ ), with φ ∈ Sub(φ), labval(φ0)(u) = a and ? labval(φ0)(v) = b. Let D := val(φ ). Observe that we have V (H) ⊆ V (D) and E(H) ⊆ E(D). Moreover, all vertices labeled i in H are labeled a in D and those labeled j in H are labeled b in D. Since φ is irredundant, there are no edges in D between a vertex labeled a and one labeled b. −1 −1 Thus, for all vertices x ∈ labH (i) and y ∈ labH (j), we have xy∈ / E(H) and xy ∈ E(G).  5 3. Partial solutions and auxiliary graphs

Let G be a graph and (H, labH ) be a k-labeled graph such that H is a subgraph of G. A partial solution of H is a set of edges P ⊆ E(H) such that H|P is a union of vertex-disjoint paths, i.e., H|P is acyclic and, for every vertex v ∈ V (H), the degree of v in H|P is at most two. We denote by Π(H) the set of all partial solutions of H. We say that a path P in H|P is maximal 0 if the degree of its end-vertices in H|P is at most one; in other words, there is no path P in H|P 0 such that V (P ) ( V (P ). Observe that an isolated vertex in H|P is considered as a maximal path. A complement solution of H is a subset Q of E(G) \ E(H) such that G|Q is a union of vertex- disjoint paths with end-vertices in V (H); in particular, for every vertex v in V (G) \ V (H), the degree of v in G|Q is two. We denote by Π(H) the set of all complement solutions of H. A path P in G with at least 2 vertices is an H-path if the end-vertices of P are in V (H) and the internal vertices of P are in V (G)\V (H). By definition, isolated vertices in V (H) are not H-paths. Observe that, for a complement solution Q, we can decompose each maximal path Q of G|Q with at least 2 vertices into H-paths (not necessarily one). Examples of a partial solution and a complement solution are given in Figure 2. Note that if G has a Hamiltonian cycle C and E(C) 6⊆ E(H), then E(C) ∩ E(H) is a partial solution and E(C)∩(E(G)\E(H)) is a complement solution. We say that a partial solution P and a complement solution Q form a Hamiltonian cycle if (V (G), P ∪ Q) is a cycle containing all the vertices of G.

H −1 −1 −1 −1 −1 labH (1) labH (2) labH (3) labH (4) labH (5)

Figure 2. Examples of a partial solution P (solid lines) and a complement solution Q (dashed lines) forming a Hamiltonian cycle. Observe that H|P contains 5 maximal paths and G|Q contains 5 H-paths (and only 4 maximal paths).

Auxiliary Multigraph. For P ∈ Π(H) ∪ Π(H) and i, j ∈ [k], we define `ij and `i as follows.

• If P is a partial solution of H, then `ij is the number of maximal paths in H|P with end- vertices labeled i and j, and `i is the number of maximal paths in H|P with both end-vertices labeled i. • If P is a complement solution of H, then `ij is the number of H-paths in G|P with end- vertices labeled i and j, and `i is the number of H-paths in G|P with both end-vertices labeled i.

Now, we define the auxiliary multigraph of P, denoted by auxH (P), as the multigraph with vertex set {v1, . . . , vk} and edge set [ [ {{vi, vj}1,..., {vi, vj}`ij } ∪ {{vi}1,..., {vi}`i }. i,j∈[k] i∈[k] i6=j 6 Moreover, if P is a partial solution of H, then we color all the edges of auxH (P) in red, and if P is a complement solution, then we color the edges of auxH (P) in blue. An example of an auxiliary multigraph is given in Figure 3.

v1 v2 v3 v4 v5

Figure 3. The union G1 ] G2 of auxiliary multigraphs G1 and G2 associated with the partial solution (solid lines) and the complement solution (dashed lines) repre- sented in Figure 2.

4. Relations between Hamiltonian cycles and Eulerian trails Let G be an n-vertex graph and φ be an irredundant k-expression of G. Let H be a k-labeled graph arising in the k-expression φ. Observe that H is a subgraph of G (disregarding the labels). This section is dedicated to prove the properties of the following relation between partial solutions of H based on the degree sequence and the connected components of their auxiliary multigraphs.

Definition 4.1. Let P1, P2 ∈ Π(H). We write P1 'P2 if auxH (P1) and auxH (P2) have the same set of connected components and for each vertex v in {v , . . . , v }, deg (v) = deg (v). 1 k auxH (P1) auxH (P2) Observe that ' is an equivalence relation. For a set A ⊆ Π(H), we define reduceH (A) as the operation which returns a set containing one element of each equivalence class of A/ '.

The main idea of our algorithm is to call reduceH at each step of our dynamic programming algorithm in order to keep the size of a set of partial solutions manipulated small, i.e., bounded by nO(k). The running time of our algorithm follows mostly from the following lemma. k k(log (k)+1) Lemma 4.2. For every A ⊆ Π(H), we have |reduceH (A)| ≤ n · 2 2 and we can moreover 2 compute reduceH (A) in time O(|A| · nk log2(nk)). k k(log (k)+1) Proof. To prove that reduceH (A) ≤ n ·2 2 , it is enough to bound the number of equivalence classes of Π(H)/ '. We claim that, for every P ∈ Π(H), we have P deg (v ) ≤ 2|V (H)|. First observe i∈[k] auxH (P) i that P deg (v ) = 2|V (H)| when P = ∅, since each isolated vertex in H gives a loop in i∈[k] auxH (P) i |P auxH (P). Moreover, when P contains an edge, removing an edge from a partial solution P of H increases P deg (v ) by two; indeed, this edge removal splits a maximal path of H into i∈[k] auxH (P) i |P two maximal paths. Therefore, any partial solution P satisfies that P deg (v ) ≤ 2|V (H)|; i∈[k] auxH (P) i in particular each vertex of auxH (P) has degree at most 2|V (H)|. As auxH (P) contains k vertices, k k we deduce that there are at most (2|V (H)|) ≤ (2n) possible degree sequences for auxH (P). k log k Since the number of partitions of {v1, . . . , vk} is bounded by 2 2 . We conclude that ' parti- tions Π(H) into at most nk · 2k(log2 k+1) equivalences classes. It remains to prove that we can compute reduceH (A) in time O(|A| · nk log2(nk)). First observe that, for every P ∈ Π(H), we can compute auxH (P) in time O(nk). Moreover, we can also com- pute the degree sequence of auxH (P) and the connected components of auxH (P) in time O(nk). 7 Thus, by using the right data structures, we can decide whether P1 'P2 in time O(nk). Fur- thermore, by using a self-balancing binary search tree, we can compute reduceH (A) in time O(|A| · nk log2(|reduceH (A)|)). Since log2(|reduceH (A)|) ≤ k log2(2nk), we conclude that reduceH (A) is 2 computable in time O(|A| · nk log2(nk)).  The rest of this section is dedicated to prove that, for a set of partial solutions A of H, the set reduceH (A) is equivalent to A, i.e., if A contains a partial solution that forms a Hamiltonian cycle with a complement solution, then reduceH (A) also. Our results are based on a kind of equivalence between Hamiltonian cycles and red-blue Eulerian trails. The following observation is one direction of this equivalence.

Lemma 4.3. If P ∈ Π(H) and Q ∈ Π(H) form a Hamiltonian cycle, then the multigraph auxH (P)] auxH (Q) admits a red-blue Eulerian trail.

Proof. Suppose that P ∈ Π(H) and Q ∈ Π(H) form a Hamiltonian cycle C. Let M := auxH (P) ] auxH (Q). From the definitions of a partial solution and of a complement solution, there is a sequence (P1,Q1,...,P`,Q`) of paths in P and Q such that

• P1,P2,...,P` are all the maximal paths in H|P , • Q1,Q2,...,Q` are all the H-paths in G|Q, • P1,Q1,...,P`,Q` appear in C in this order, • for each x ∈ [`], the first end-vertex of Px is the last end-vertex of Qx−1 and the last end-vertex of Px is the first end-vertex of Qx (indices are considered modulo `).

Observe that each maximal path Px in H|P with end-vertices labeled i and j is associated with a red edge in M, say ex with multM (ex) = {vi, vj} if i 6= j or multM (ex) = {vi} if i = j such that the edges e1, . . . , e` are pairwise distinct and E(auxH (P)) = {e1, . . . , e`}. Similarly, each H- path Qy of G|Q with end-vertices labeled i and j is associated with a blue edge fy in M with multM (fy) = {vi, vj} if i 6= j or multM (fy) = {vi} if i = j such that the edges f1, . . . , f` are pairwise distinct and E(auxH (Q)) = {f1, . . . , f`}. It is not difficult to see that (e1, f1, . . . , e`, f`) is a red-blue Eulerian trail of auxH (P) ] auxH (Q).  Next, we prove the other direction. We use the properties of an irredundant k-expression described in Lemma 2.1.

Lemma 4.4. Let P ∈ Π(H). If there exists a complement solution Q of H such that auxH (P) ] ? ? auxH (Q) admits a red-blue Eulerian trail, then there exists Q ∈ Π(H) such that P and Q form a Hamiltonian cycle.

Proof. Let T = (va1 , r1, vc1 , b1, va2 , r2, vc2 , ··· , va` , r`, vc` , b`, va1 ) be a red-blue Eulerian trail of auxH (P) ] auxH (Q) with r1, . . . , r` ∈ E(auxH (P)) and b1, . . . , b` ∈ E(auxH (Q)). In the follow- ing, the indexes are modulo `. For each i ∈ [`], we associate ri with a maximal path Pi of H|P with end-vertices labeled ai and ci and we associate bi with an H-path Qi of G|Q with end-vertices labeled ci and ai+1, such that P1,...,P`,Q1,...,Q` are all pairwise distinct. ? For every i ∈ [`], we construct from Qi an H-path Qi of G by doing the following. Let u, v be respectively the last end-vertex of Pi and the first end-vertex of Pi+1. Observe that u and the first vertex of Qi are labeled ci, and v and the last vertex of Qi are labeled ai+1. We distinguish two cases:

• Suppose that Qi = (x, xy, y), i.e., Qi uses only one edge. Since Q is a complement solution of H, we have xy ∈ E(G) \ E(H). By Lemma 2.1, we have uv ∈ E(G) \ E(H). In this case, ? we define Qi = (u, uv, v). 8 • Assume now that Qi = (x, xy, y, . . . , w, wz, z) with w, y ∈ V (G) \ V (H) (possibly, w = y). Since x and u have the same label in H, by Lemma 2.1, we have NG(x) \ V (H) = NG(u) \ V (H). Hence, u is also adjacent to y. Symmetrically, we have v is adjacent to w. In this ? case, we define Qi = (u, uy, y, . . . , w, wv, v), i.e., the path with the same internal vertices as Qi and with end-vertices u and v. ? In both cases, we end up with a path Qi that uses the same internal vertices as Qi and whose end-vertices are the last vertex of Pi and the first vertex of Pi+1. We conclude that

? ? P1 − Q1 − · · · − P` − Q` is a Hamiltonian cycle. ? ? ? ? Let Q be the set of edges used by the paths Q1,...,Q` . By construction, we have Q ⊆ E(G) \ E(H), and thus Q? ∈ Π(H). Observe that, for every i ∈ [`], the labels of the end-vertices of ? ? Qi are the same as those of Qi. Consequently, we have auxH (Q ) = auxH (Q). 

It is well known that a connected multigraph contains an Eulerian trail if and only if every vertex has even degree. As an extension, Kotzig [26] proved that a connected two-edge colored graph (without loops and multiple edges) contains a red-blue Eulerian trail if and only if each vertex is incident with the same number of edges for both colors. This result can be easily generalized to multigraphs by replacing red edge with a path of length 3 whose colors are red, blue, red in the order, and replacing blue edge with a path of length 3 whose colors are blue, red, blue in the order. For the completeness of our paper, we add its proof. Let G be a multigraph whose edges are colored red or blue, and let R and B be respectively the set of red and blue edges. For a vertex v ∈ V (G), we let rdegG(v) and bdegG(v) be respectively the degree of v in G|R and G|B. Recall that a loop is counted twice in the degree of a vertex. Lemma 4.5 (Kotzig [26]). Let G be a connected multigraph whose edges are colored red or blue. Then G has a red-blue Eulerian trail if and only if, for every vertex v, bdegG(v) = rdegG(v). Proof. One can easily check that if G has a red-blue Eulerian trail, then for every vertex v, bdegG(v) = rdegG(v). Indeed, if T = (v1, e1, v2, . . . , v`, e`, v1) is a red-blue Eulerian trail, then, for 2 ≤ i ≤ `, ei−1 and ei have different colors, and e1 and e` have different colors, we can conclude that the blue edges incident with a vertex v are in 1-to-1 correspondence with the red edges incident with v (by counting twice the loops). Let us now prove the other direction. Let T := (v1, e1, v2, e2, ··· , vi, ei, vi+1) be a longest red- blue trail. We may assume that e1 is colored red. First observe that v1 = vi+1. Otherwise, bdegT (v1) + 1 = rdegT (v1) and thus, there is a blue edge in E(G) \ E(T ) incident with v1. So, we can extend T by adding this edge, a contradiction. Thus, v1 = vi+1. Next we show that ei is colored blue. Suppose ei is colored red. Then bdegT (v1) + 2 = rdegT (v1) and thus, there is a blue edge in E(G) \ E(T ) incident with v1. So, we can extend T by adding this edge, a contradiction. Thus, ei is colored red, and it implies that T is a closed red-blue trail. It means that T can be considered as a closed red-blue trail starting from any vertex of T and following the same order or the reverse order of T . Now, we show that V (G) = V (T ). Suppose V (G) \ V (T ) is non-empty. Since G is connected, there is an edge vw with v ∈ V (T ) and w ∈ V (G) \ V (T ). If vw is a red edge, then starting from this edge and following T from a blue edge incident with v, we can find a red-blue trail longer than T , a contradiction. The same argument holds when vw is a blue edge. Therefore, we have that V (G) = V (T ). By a similar argument, one can show that E(G) = E(T ); if there is an edge vw in E(G) \ E(T ), we can extend T by putting vw at the beginning. So, E(G) = E(T ). We conclude that T is a red-blue Eulerian trail, as required.  9 In order to prove the correctness of our algorithm, we need the following relation between subsets of partial solutions.

Definition 4.6. Let A and B be two subsets of Π(H). We write A .H B if, for every multigraph M whose edges are colored blue, whenever there exists P1 ∈ B such that auxH (P1) ]M admits a red-blue Eulerian trail, there exists P2 ∈ A such that auxH (P2)]M admits a red-blue Eulerian trail. The main idea of our algorithm for Hamiltonian Cycle, is to compute, for every k-labeled graph H arising in φ, a set A ⊆ Π(H) of small size such that A .H Π(H). Indeed, by Lemmas 4.3 and 4.4, A .H Π(H) implies that if there exist P ∈ Π(H) and Q ∈ Π(H) such that P and Q form a Hamiltonian cycle, then there exist P? ∈ A and Q? ∈ Π(H) such that P? and Q? form a Hamiltonian cycle. The following lemma is the key of our algorithm.

Lemma 4.7. Let A ⊆ Π(H). Then reduceH (A) .H A.

Proof. Let P ∈ A and M be a multigraph whose edges are colored blue such that auxH (P) ]M ? admits a red-blue Eulerian trail. By definition, reduceH (A) contains a partial solution P such that ? P'P . As auxH (P) ]M contains a red-blue Eulerian trail, by Lemma 4.5, we have that • auxH (P) ]M is connected and • for every i ∈ [k], deg (v ) = deg (v ). auxH (P) i M i ? ? Since auxH (P) has the same set of connected components as auxH (P ), the multigraph auxH (P ) ] M is also connected. Moreover, for every i ∈ [k], we have

deg (v ) = deg ? (v ) = deg (v ). auxH (P) i auxH (P ) i M i ? By Lemma 4.5, we conclude that auxH (P ) ]M admits a red-blue Eulerian trail. Thus, for every P ∈ A and multigraph M with blue edges such that auxH (P) ]M admits a ? ? red-blue Eulerian trail, there exists P ∈ reduceH (A) such that auxH (P ) ]M admits a red-blue Eulerian trail. Hence, we have reduceH (A) .H A. 

Lemma 4.8. Let A, B ⊆ Π(H). If A .H B, then reduceH (A) .H B.

Proof. One easily checks that .H is a transitive relation. Now, assuming that A .H B, we have reduceH (A) . B because reduceH (A) .H A by Lemma 4.7. 

5. Hamiltonian Cycle problem In this section, we present our algorithm solving Hamiltonian Cycle. Our algorithm computes recursively, for every k-labeled graph H arising in the k-expression of G, a set AH such that k k(log (k)+1) AH .H Π(H) and |AH | ≤ n · 2 2 . In order to prove the correctness of our algorithm, we need the following lemmas which prove that the operations we use to compute sets of partial solutions preserve the relation .H .

Lemma 5.1. Let H = ρi→j(D). If AD .D Π(D), then AD .H Π(H). Proof. First, observe that H has the same set of vertices and edges as D. Thus, we have Π(H) = Π(D) and Π(H) = Π(D). Suppose that AD .D Π(D). Let P ∈ Π(H) and M be a multigraph whose edges are colored blue such that auxH (P) ]M contains a red-blue Eulerian trail T . To prove the lemma, it is sufficient to prove that there exists ? ? P ∈ AD such that auxH (P ) ]M contains a red-blue Eulerian trail. Let f be a bijective function such that

• for every edge e of auxD(P) with endpoints v` and vi, for some `, f(e) is an edge of auxH (P) with endpoints v` and vj, and • for every loop e with endpoint vi, f(e) is a loop of auxH (P) with endpoint vj. 10 By construction of auxD(P) and auxH (P), such a function exists. We construct the multigraph M0 from M and T by successively doing the following:

• For every edge e of auxD(P) with endpoints v` and vi, take the subwalk W = (v`, f(e), vj, ea, va) 0 of T . Replace ea in M by an edge ea with endpoints vi and va. • For every loop e with endpoint vi in auxD(P), take the subwalk W = (va, ea, vj, f(e), vj, eb, vb) 0 0 of T . Replace ea (respectively eb) in M by an edge ea (resp. eb) with endpoints va and vi (resp. vi and vb). 0 By construction, one can construct from T a red-blue Eulerian trail of auxD(P) ]M . Since ? ? 0 AD .D Π(D), there exists P ∈ AD such that auxD(P ) ]M contains a red-blue Eulerian trail. ? 0 Observe that auxH (P) (respectively M) is obtained from auxD(P ) (resp. M ) by replacing each ? 0 edge associated with {vi, vk} or {vi} in auxD(P ) (resp. M ) with an edge associated with {vj, vk} ? or {vj} respectively. We conclude that auxH (P ) ]M admits a red-blue Eulerian trail.  Lemma 5.2. Let H = D ⊕ F . If AD .D Π(D) and AF .F Π(F ), then (AD ⊗ AF ) .H Π(H). Proof. Observe that V (D) and V (F ) are disjoint. Consequently, we have Π(H) = Π(D) ⊗ Π(F ), and, for all PD ∈ Π(D) and PF ∈ Π(F ), we have auxH (PD ∪PF ) = auxH (PD)]auxH (PF ). Suppose that AD .D Π(D) and AF .F Π(F ). Let PD ∈ Π(D) and PF ∈ Π(F ), and let M be a multigraph whose edges are colored blue such that there exists a red-blue Eulerian trail T in auxH (PD ∪ PF ) ]M. It is sufficient to prove that ? ? ? ? there exist PD ∈ AD and PF ∈ AF such that auxH (PD ∪ PF ) ]M admits a red-blue Eulerian trail. ? ? We begin by proving that there exists PD ∈ AD such that auxH (PD ∪ PF ) ]M contains a red- 0 blue Eulerian trail. In order to do so, we construct from auxH (PD ∪ PF ) ]M a multigraph M by successively repeating the following: take a maximal sub-walk W of T which uses alternately blue edges and red edges from auxH (PF ) ]M, remove these edges and add a blue edge between the two end-vertices of W . 0 0 0 0 By construction of M , for every PD ∈ Π(D), if auxD(PD) ]M admits a red-blue Eulerian 0 trail, then auxH (PD ∪ PF ) ]M contains a red-blue Eulerian trail also. We also deduce from this 0 construction that the multigraph auxD(PD) ]M contains a red-blue Eulerian trail. As AD .D ? ? 0 Π(D), there exists PD such that auxD(PD) ]M contains a red-blue Eulerian trail. We conclude ? that auxH (PD ∪ PF ) ]M contains also a red-blue Eulerian trail. ? ? ? Symmetrically, we can prove that there exists PF ∈ AF such that auxH (PD ∪ PF ) ]M contains a red-blue Eulerian trail. This proves the lemma. 

For two k-labeled subgraphs H and D arising in the k-expression of G such that H = ηi,j(D), we H denote by Ei,j the set of edges whose endpoints are labeled i and j, i.e., {uv ∈ E(H) | labH (v) = 0 i ∧ labH (v) = j}. For P ∈ Π(H), we denote by P + (i, j) the set of all partial solutions P of Π(H) H obtained by the union of P and an edge uv in Ei,j. Observe that u and v must be the endpoints of two distinct maximal paths of H|P . We extend this notation to sets of partial solutions; for every S A ⊆ Π(H), we denote by A + (i, j), the set P∈A(P + (i, j)). It is worth noting that Π(D) ⊆ Π(H) and thus the operator +(i, j) is well defined for a partial solution in Π(D). Moreover, for every A ⊆ Π(D) and integer t ≥ 0, we define the set At as follows ( t A if t = 0, A := t−1 reduceH (A + (i, j)) otherwise. t H Observe that each set P in A is a partial solution of H and |P ∩ Ei,j| = t. H 0 Lemma 5.3. Let H = ηi,j(D) such that E(D) ∩ Ei,j = ∅. If AD .D Π(D), then we have AD ∪ · · · ∪ n AD .H Π(H). Proof. Suppose that AD .D Π(D). We begin by proving the following claim. 11 Claim 5.3.1. Let A, B ⊆ Π(H). If A .H B, then A + (i, j) .H B + (i, j). Proof. Suppose that A .H B. Let P ∈ B + (i, j) and M be a multigraph with blue edges such that H 0 auxH (P) ]M admits a red-blue Eulerian trail. Take xy ∈ Ei,j such that P := P − xy belongs to −1 −1 0 B and x ∈ labH (i) and y ∈ labH (j). Let M be the multigraph obtained by adding a blue edge f with endpoints vi and vj to M. 0 0 We claim that the multigraph auxH (P ) ]M admits a red-blue Eulerian trail. Note that there is a path P ∈ P containing the edge xy, and when we remove xy from P, we divide P into two maximal subpaths, say P1 and P2. Without loss of generality, we may assume that P1 contains 0 0 x and P2 contains y. Let x and y be the other end-vertices of P1 and P2, respectively, and let 0 0 0 0 0 i := labH (x ) and j := labH (y ). Note that auxH (P ) can be obtained from auxH (P) by removing an edge e associated with {vi0 , vj0 } and adding two edges e1 and e2 associated with {vi0 , vi} and 0 0 {vj, vj0 } respectively. So, we can obtain a red-blue Eulerian trail of auxH (P ) ]M from a red-blue Eulerian trail of auxH (P) ]M by replacing (vi0 , e, vj0 ) with the sequence (vi0 , e1, vi, f, vj, e2, vj0 ) where f is the blue edge we add to M to obtain M0. It implies the claim. ? ? 0 Now, since A .H B, there exists P ∈ A such that auxH (P ) ]M admits a red-blue Eulerian trail T . Let W be the subwalk of T such that W = (va, ea, vi, f, vj, eb, vb). Take two maximal paths P1 and P2 in H|P? such that the end-vertices of P1 (respectively P2) are labeled a and i (resp. b ? H and j). Let Pb be the partial solution of H obtained from P by adding the edge in Ei,j between the end-vertex of P1 labeled i and the end-vertex of P2 labeled j. By construction, we have Pb ∈ A+(i, j) and auxH (Pb) ]M admits a red-blue Eulerian trail. We conclude that A + (i, j) .H B + (i, j).  Let Π(D) + t(i, j) be the set of partial solutions of H obtained by applying t times the operation +(i, j) on Π(D). Since every partial solution of H is obtained from the union of a partial solution H S of D and a subset of Ei,j of size at most n, we have Π(H) = 0≤t≤n(Π(D) + t(i, j)). 0 Since V (D) = V (H) and E(D) ⊆ E(H), we have AD = AD .H Π(D)+0(i, j). Let ` ∈ {1, . . . , n} `−1 `−1 and suppose that AD .H Π(D) + (` − 1)(i, j). From Claim 5.3.1, we have AD + (i, j) .H ` `−1 Π(D) + `(i, j). By Lemma 4.8, we deduce that AD = reduce(AD + (i, j)) .H Π(D) + `(i, j). ` Thus, by recurrence, for every 0 ≤ ` ≤ n, we have AD .H Π(D) + `(i, j). We conclude that 0 n AD ∪ · · · ∪ AD .H Π(H).  We are now ready to prove the main theorem of this paper. Theorem 5.4. There exists an algorithm that, given an n-vertex graph G and a k-expression φ of 2k+5 2k(log (k)+1) 3 G, solves Hamiltonian Cycle in time O(n · 2 2 · k · log2(nk)). Proof. Since every k-expression can be transformed into an irredundant k-expression in linear time, we may assume that φ is an irredundant k-expression. We do a bottom-up traversal of the k-expression and at each k-labeled graph H arising in φ, k k(log(k)+1) we compute a set AH ⊆ Π(H) such that |AH | ≤ n 2 and AH .H Π(H), by doing the following:

• If H = i(v), then we have Π(H) = {∅} because E(H) = ∅. In this case, we set AH := {∅}. Obviously, we have AH .H Π(H). • If H = ρi,j(D), then we set AH := AD. • If H = D ⊕ F , then we set AH := reduceH (AD ⊗ AF ). 0 n • If H = ηi,j(D), then we set AH := reduceH (AD ∪ · · · ∪ AD). For the last three cases, we deduce, by induction, from Lemma 4.8 and Lemmas 5.1–5.3 that AH .H k k(log(k)+1) Π(H). Moreover by the use of the function reduceH , by Lemma 4.2, we have |AH | ≤ n ·2 . We now explain how our algorithm decides whether G admits a Hamiltonian cycle. We claim that G has a Hamiltonian cycle if and only if there exist two k-labeled graphs H and D arising in φ 12 with V (H) = V (G) and H = ηi,j(D), and there exists P ∈ AD such that, for every ` ∈ [k] \{i, j}, we have deg (v ) = 0 and deg (v ) = deg (v ) > 0. auxD(P) ` auxH (P) i auxH (P) j First suppose that G contains a Hamiltonian cycle C and take the k-labeled graph H arising in φ such that E(C) ⊆ E(H) and |E(H)| is minimal. Note that the operations of taking the disjoint union of two graphs or relabeling cannot create a Hamiltonian cycle. Thus, by minimality, we have H = ηi,j(D) such that • D is a k-labeled graph arising in φ and i, j ∈ [k], • E(C) 6⊆ E(D). H H It follows that E(C) \ E(D) ⊆ Ei,j. Let P := E(C) ∩ E(D) and Q := E(C) ∩ Ei,j. Observe that P ∈ Π(D) and Q ∈ Π(D). By Lemma 4.3, the multigraph auxD(P) ] auxD(Q) contains a ? ? red-blue Eulerian trail. Since AD .D Π(D), there exists P ∈ AD such that auxD(P ) ] auxD(Q) H contains a red-blue Eulerian trail. As Q ⊆ Ei,j, we deduce that, for every ` ∈ [k] \{i, j}, we have deg ? (v ) = 0 and deg ? (v ) = deg ? (v ). auxD(P ) ` auxH (P ) i auxH (P ) j For the other direction, suppose that the latter condition holds. Let Q be the graph on the vertex set V (G) such that it contains exactly deg (v ) many edges between the set of vertices labeled auxH (P) i i and the set of vertices labeled j. Observe that aux (Q) consists of deg (v ) many edges H auxH (P) i between vi and vj. Therefore, by Lemma 4.5, auxH (P) ] auxH (Q) admits a red-blue Eulerian trail. Then, by Lemma 4.4, there exists Q? ∈ Π(H) such that P and Q? form a Hamiltonian cycle, as required.

Running time. Let H be a k-labeled graph arising in φ. Observe that if H = i(v) or H = ρi→j(D), then we compute AH in time O(1). 2 By Lemma 4.2, for every A ⊆ Π(H), we can compute reduceH (A) in time O(|A| · nk log2(nk)). Observe that, for every k-labeled graph D arising in φ and such that AD is computed before AH , k k(log (k)+1) we have |AD| ≤ n · 2 2 . It follows that: 2k 2k(log (k)+1) • If H = D ⊕ F , then we have |AD ⊗ AF | ≤ n · 2 2 . Thus, we can compute AH := reduceH (AD ⊗ AF ) in time

2k+1 2k(log2(k)+1) 2 O(n · 2 · k log2(nk)).

• If H = ηi,j(D), then we can compute AH in time

k+4 k(log2(k)+1) 2 O(n · 2 · k log2(nk)). First observe that, for every partial solution P of H, we have |P + (i, j)| ≤ n2 and we can compute the set P +(i, j) in time O(n2). Moreover, by Lemma 4.2, for every ` ∈ {0, . . . , n− ` k k(log2(k)+1) ` k+2 k(log2(k)+1) 1}, we have |AD| ≤ n ·2 and thus, we deduce that |AD+(i, j)| ≤ n ·2 `+1 k+3 k(log2(k)+1) 2 and that AD can be computed in time O(n · 2 · k log2(nk)). Thus, we can 1 n k+4 k(log(k)+1) 2 compute the sets AD,..., AD in time O(n · 2 · k log2(nk)). The running time 0 n k+1 2k(log2(k)+1) to compute AH from AD follows from |AD ∪ · · · ∪ AD| ≤ n · 2 . Since φ uses at most O(n) disjoint union operations and O(nk2) unary operations, we deduce that the total running time of our algorithm is

2k+5 2k(log2(k)+1) 4 O(n · 2 · k log2(nk)).  6. Directed Hamiltonian cycle In this section, we explain how to adapt our approach for directed graphs. A k-labeled digraph is a pair (G, labG) of a digraph G and a function labG from V (G) to [k]. Directed clique-width is also defined in [10], and it is based on the four operations, where the three operations of creating a digraph, taking the disjoint union of two labeled digraphs, and relabeling a digraph are the same, and the operation of adding edges is replaced with the following: 13 • For a labeled digraph G and distinct labels i, j ∈ [k], add all the non-existent arcs from vertices with label i to vertices with label j (so we do not add arcs of both directions altogether). A directed clique-width k-expression and directed clique-width are defined in the same way. A directed Hamiltonian cycle of a digraph G is a directed cycle containing all the vertices of G. The Directed Hamiltonian Cycle problem asks, for a given digraph G, whether G contains a directed Hamiltonian cycle or not. With a similar approach, we can show the following. Theorem 6.1. There exists an algorithm that, given an n-vertex digraph G and a directed clique- width k-expression of G, solves Directed Hamiltonian Cycle in time nO(k). For Directed Hamiltonian Cycle, auxiliary graphs should be directed graphs. We define partial solutions and auxiliary multigraphs similarly, at the exception that a directed maximal path (resp. H-path) from a vertex labeled i to a vertex labeled j in a partial solution (resp. complement solution) corresponds to an arc (vi, vj) in its auxiliary multigraph. Let G be an n-vertex and φ be a directed irredundant k-expression of G. Similar to the proof of Theorem 5.4, for every k-labeled graph H arising in φ, we recursively compute a set AH of small size such that AH represents Π(H) which is the set of all partial solutions of H. It is not hard to see that Lemmas 4.3 and 4.4 hold also in the directed case. That is, we have an equivalence between directed Hamiltonian cycle in graphs and directed red-blue Eulerian trail in multigraphs. Thus, to adapt our ideas for undirected graphs, we only need to characterize the directed multigraphs which admit a red-blue Eulerian trail. Fleischner [18, Theorem VI.17] gave such a characterization for directed graphs without loops and multiple arcs, but the proof can easily be adapted for directed multigraphs. Let M be a directed multigraph whose arcs are colored red or blue, and let R and B be respectively ∗ ∗ its set of red and blue arcs. We denote by M the digraph derived from M with V (M ) := {v1, v2 | ∗ v ∈ V (M)} and E(M ) := {(v1, w2) | (v, w) ∈ B} ∪ {(v2, w1) | (v, w) ∈ R}. For a digraph G and a + − vertex v of G, we denote by degG(v) and degG(v), respectively, the outdegree and indegree of v in G. Lemma 6.2 (Fleischner [18]). Let M be a directed multigraph whose arcs are colored red or blue. The following are equivalent. (1) M has a red-blue Eulerian trail. (2) M ∗ has an Eulerian trail. (3) The underlying undirected graph of M ∗ has at most one connected component containing an ∗ + − edge, and, for each vertex v of M , degM ∗ (v) = degM ∗ (v). ∗ + − In (3), the condition that “for each vertex v of M , degM ∗ (v) = degM ∗ (v)” can be translated to that, for each vertex v of M, the number of blue incoming arcs is the same as the number of red outgoing arcs, and the number of red incoming arcs is the same as the number of blue outgoing arcs. However, an important point is that instead of having that the underlying undirected graph of M ∗ has at most one connected component containing an edge, the condition that “the underlying graph of M is connected” or “M is strongly connected” is not sufficient, because the connectedness of M ∗ depends on the colors of arcs. We give an example. Let G be a graph on {x, y, z} with red arcs (x, y), (y, z) and blue arcs (z, y), (y, x). Even though G is strongly connected, it does not have a red-blue Eulerian trail, and one can check that M ∗ has two connected components containing an edge. To decide whether the underlying undirected graph of M ∗ has at most one connected component containing an edge, multiple arcs are useless. So, it is enough to keep one partial solution P for each degree sequence in auxH (P) and for each set {multauxH (P)(e) | e ∈ E(auxH (P))}. 14 Let ' be the equivalence relation on Π(H) such that P1 'P2 if the following are satisfied: • for every pair (v, w) of vertices in {v1, . . . , vk}, there exists an arc from v to w in auxH (P1) if and only if there exists one in auxH (P2), + + − • for every vertex v in {v1, . . . , vk}, deg (v) = deg (v) and deg (v) = auxH (P1) auxH (P2) auxH (P1) deg− (v). auxH (P2) From Lemma 6.2 and the definition of M ∗, we deduce the following fact.

Fact 6.3. Let P1, P2 ∈ Π(H). If P1 'P2, then P1 is part of a directed Hamiltonian cycle in G if and only if P2 is part of a directed Hamiltonian cycle in G. From the definition of ', one can show that |Π(H)/ ' | ≤ n2k · 2k2 . Thus we can follow the lines of the proof for undirected graphs, and easily deduce that one can solve Directed Hamiltonian Cycle in time nO(k), where k is the directed clique-width of the given digraph.

7. Conclusion We have proved that, given a k-expression, one can solve Hamiltonian Cycle in time nO(k), and also prove a similar variant for directed graphs. One major open question related to clique-width is to determine whether it can be approximated within a constant factor. One can bypass this long-standing open problem by using a related parameter called rank-width. This parameter was introduced by Oum and Seymour [27] and admits an efficient algorithm to approximate it within a constant factor. Moreover, the clique-width of a graph is always bigger than its rank-width. On the other hand, rank-width is harder to manipulate than clique-width. To the best of our knowledge, there is no optimal algorithm known for basic problems such as Vertex Cover and Dominating Set, where the best algorithms run in time 2O(k2) · nO(1) [7] and the best lower bounds state that we can not solve these problems in time 2o(k) · nO(1) unless ETH fails. Improving these algorithms or these lower bounds would be the natural way of continuing the works done on clique-width. Recently, Bergougnoux and Kanté [2] proved that the Max Cut problem is solvable in time nO(k) where k is the clique-width of the input graph without assuming that the graph is given with a k-expression. For doing so, they used a related parameter called Q-rank-width and the notion of d-neighbor equivalence. It would be interesting to know whether the same approach can be used for the Hamiltonian Cycle problem. We conclude with one explicit question. A digraph D is an out-tree if D is an oriented tree (an undirected tree with orientations on edges) with only one vertex of indegree zero (called the root). The vertices of out-degree zero are called leaves of D. The Min Leaf Out-Branching problem asks for a given digraph D and an integer `, whether there is a spanning out-tree of D with at most ` leaves. This problem generalizes (and also Hamiltonian Cycle) by taking ` = 1. O(k) Ganian, Hliněný, and Obdržálek [22] showed that there is an n2 -time algorithm for solving Min Leaf Out-Branching problem, when a clique-width k-expression of a digraph D is given. We ask whether it is possible to drop down the exponential blow-up from 2O(k) to O(k). Acknowledgement. The authors would like to thank the anonymous reviewer for pointing out the previous mistake on red-blue Eulerian trails for directed graphs and for indicating proper citations.

References [1] S. Arnborg, J. Lagergren, and D. Seese. Easy problems for tree-decomposable graphs. Journal of Algorithms, 12(2):308–340, 1991. [2] B. Bergougnoux and M. M. Kanté. More applications of the d-neighbor equivalence: acyclic and connectivity constraints. CoRR, abs/1805.11275, 2018. 15 [3] B. Bergougnoux, M. M. Kanté, and O. Kwon. An optimal XP algorithm for Hamiltonian cycle on graphs of bounded clique-width. In Algorithms and Data Structures - 15th International Symposium, WADS 2017, St. John’s, NL, Canada, July 31 - August 2, 2017, Proceedings, pages 121–132, 2017. [4] H. Bodlaender. A partial k-arboretum of graphs with bounded . Theor. Comput. Sci., 209(1-2):1–45, 1998. [5] H. . Bodlaender, M. Cygan, S. Kratsch, and J. Nederlof. Deterministic single exponential time algorithms for connectivity problems parameterized by treewidth. Inform. and Comput., 243:86–111, 2015. [6] R. B. Borie, R. G. Parker, and C. A. Tovey. Automatic generation of linear-time algorithms from predicate calculus descriptions of problems on recursively constructed graph families. Algorithmica, 7(5&6):555–581, 1992. [7] B.-M. Bui-Xuan, J. A. Telle, and M. Vatshelle. Fast dynamic programming for locally checkable vertex subset and vertex partitioning problems. Theoret. Comput. Sci., 511:66–76, 2013. [8] B. Courcelle. The monadic second-order logic of graphs IV: definability properties of equational graphs. Annals of Pure and Applied Logic, 49(3):193–255, 1990. [9] B. Courcelle and J. Engelfriet. Graph structure and monadic second-order logic, volume 138 of Encyclopedia of Mathematics and its Applications. Cambridge University Press, Cambridge, 2012. A language-theoretic approach, With a foreword by Maurice Nivat. [10] B. Courcelle, J. Engelfriet, and G. Rozenberg. Handle-rewriting hypergraph grammars. Journal of Computer and System Sciences, 46(2):218–270, 1993. [11] B. Courcelle, J. A. Makowsky, and U. Rotics. Linear time solvable optimization problems on graphs of bounded clique-width. Theory Comput. Syst., 33(2):125–150, 2000. [12] B. Courcelle and M. Mosbah. Monadic second-order evaluations on tree-decomposable graphs. Theor. Comput. Sci., 109(1&2):49–82, 1993. [13] B. Courcelle and S. Olariu. Upper bounds to the clique width of graphs. Discrete Applied Mathematics, 101(1- 3):77–114, 2000. [14] M. Cygan, J. Nederlof, M. Pilipczuk, M. Pilipczuk, J. M. M. van Rooij, and J. O. Wojtaszczyk. Solving connec- tivity problems parameterized by treewidth in single exponential time (extended abstract). In 2011 IEEE 52nd Annual Symposium on Foundations of Computer Science—FOCS 2011, pages 150–159. IEEE Computer Soc., Los Alamitos, CA, 2011. [15] R. Diestel. . Number 173 in Graduate Texts in Mathematics. Springer, third edition, 2005. [16] W. Espelage, F. Gurski, and E. Wanke. How to solve NP-hard graph problems on clique-width bounded graphs in polynomial time. In Graph-theoretic concepts in computer science (Boltenhagen, 2001), volume 2204 of Lecture Notes in Comput. Sci., pages 117–128. Springer, Berlin, 2001. [17] M. R. Fellows, F. A. Rosamond, U. Rotics, and S. Szeider. Clique-width is NP-complete. SIAM J. Discrete Math., 23(2):909–939, 2009. [18] H. Fleischner. Eulerian graphs and related topics, volume 1. Elsevier, 1990. [19] F. V. Fomin, P. A. Golovach, D. Lokshtanov, and S. Saurabh. Intractability of clique-width parameterizations. SIAM J. Comput., 39(5):1941–1956, 2010. [20] F. V. Fomin, P. A. Golovach, D. Lokshtanov, and S. Saurabh. Almost optimal lower bounds for problems parameterized by clique-width. SIAM J. Comput., 43(5):1541–1563, 2014. [21] F. V. Fomin, P. A. Golovach, D. Lokshtanov, S. Saurabh, and M. Zehavi. Clique-width III: hamiltonian cycle and the odd case of graph coloring. ACM Trans. Algorithms, 15(1):9:1–9:27, 2019. [22] R. Ganian, P. Hliněný, and J. Obdržálek. Clique-width: when hard does not mean impossible. In 28th Interna- tional Symposium on Theoretical Aspects of Computer Science, volume 9 of LIPIcs. Leibniz Int. Proc. Inform., pages 404–415. Schloss Dagstuhl. Leibniz-Zent. Inform., Wadern, 2011. [23] F. Gurski. A comparison of two approaches for polynomial time algorithms computing basic graph parameters. CoRR, abs/0806.4073, 2008. [24] P. Hliněný and S. Oum. Finding branch-decompositions and rank-decompositions. SIAM J. Comput., 38(3):1012– 1032, 2008. [25] D. Kobler and U. Rotics. Edge dominating set and colorings on graphs with fixed clique-width. Discrete Applied Mathematics, 126(2-3):197–221, 2003. [26] A. Kotzig. Moves without forbidden transitions in a graph. Matematick`yčasopis, 18(1):76–80, 1968. [27] S. Oum and P. Seymour. Approximating clique-width and branch-width. J. Combin. Theory Ser. B, 96(4):514– 528, 2006. [28] J. M. M. van Rooij, H. L. Bodlaender, and P. Rossmanith. Dynamic programming on tree decompositions using generalised fast subset convolution. In Algorithms - ESA 2009, 17th Annual European Symposium, Copenhagen, Denmark, September 7-9, 2009. Proceedings, pages 566–577, 2009.

16 Université Clermont Auvergne, LIMOS, CNRS, Aubière, France E-mail address: [email protected]

E-mail address: [email protected]

Department of Mathematics, Incheon National University, Incheon, South Korea E-mail address: [email protected]

17