Listing Acyclic Orientations of Graphs with Single and Multiple Sources Alessio Conte, Roberto Grossi, Andrea Marino, Romeo Rizzi

To cite this version:

Alessio Conte, Roberto Grossi, Andrea Marino, Romeo Rizzi. Listing Acyclic Orientations of Graphs with Single and Multiple Sources. LATIN 2016: Theoretical Informatics - 12th Latin American Sym- posium, Apr 2016, Ensenada, Mexico. pp.319-333, ￿10.1007/978-3-662-49529-2_24￿. ￿hal-01388470￿

HAL Id: hal-01388470 https://hal.inria.fr/hal-01388470 Submitted on 30 May 2017

HAL is a multi-disciplinary open access L’archive ouverte pluridisciplinaire HAL, est archive for the deposit and dissemination of sci- destinée au dépôt et à la diffusion de documents entific research documents, whether they are pub- scientifiques de niveau recherche, publiés ou non, lished or not. The documents may come from émanant des établissements d’enseignement et de teaching and research institutions in France or recherche français ou étrangers, des laboratoires abroad, or from public or private research centers. publics ou privés. Listing Acyclic Orientations of Graphs with Single and Multiple Sources

Alessio Conte1, Roberto Grossi1, Andrea Marino1, and Romeo Rizzi2

1 Università di Pisa and Erable, Inria, conte,grossi,[email protected]? 2 Università di Verona, [email protected]

Abstract. We study enumeration problems for the acyclic orientations of an undirected graph with n nodes and m edges, where each edge must be assigned a direction so that the resulting directed graph is acyclic. When the acyclic orientations have single or multiple sources specified as input along with the graph, our algorithm is the first one to provide guaranteed bounds, giving new bounds with a delay of O(m · n) time per solution and O(n2) working space. When no sources are specified, our algorithm improves over previous work by reducing the delay to O(m), and is the first one with linear delay.

1 Introduction

Acyclic orientations of graphs are related to several basic problems in graph −→ theory. An orientation of an undirected graph G is the directed graph G whose −→ arcs are obtained assigning a direction to each edge in G. The orientation G is −→ acyclic when it does not contain cycles, and a node s is a source in G if it has indegree zero. For instance, consider the graphs in Figure 1. The directed graph in (b) is an acyclic orientation for the undirected graph in (a). In particular, since the orientation has only one source (v9), it is called a single source acyclic orientation. Starting from the observation that each acyclic orientation corresponds to a partial order for the underlying graph, Iriarte [7] investigates which orientations maximize the number of linear extensions of the corresponding poset. Alon and Tarsi [1] look for special orientations to give bounds on the size of the maximum independent set or the chromatic number. Gallai, Roy, and Vitaver indepen- dently describe a well-known result stating that every orientation of a graph with chromatic number k contains a simple directed path with k vertices [6, 12, 17]. There are further problems that can be addressed by looking at acyclic orientations. For instance, Benson et al. [4] show that there exists a bijection between the set of the so-called superstable configurations of a graph and the set of its acyclic orientations with a unique source.

? This work has been partially supported by the Italian Ministry of Education, Uni- versity, and Research (MIUR) under PRIN 2012C4E3KT national research project AMANDA — Algorithmics for MAssive and Networked DAta. v6 v6 v3 v9 v3 v9

v8 v8

v2 v2 v5 v7 v5 v7

v4 v4 v1 v1 (a) (b)

Fig. 1. An undirected connected graph without self-loops (a) and one of its acyclic orientations (b).

Counting how many acyclic orientations can be found in a graph is a funda- mental problem in combinatorics, dating back to the 70s or earlier [16]. Linial [10] proves that this problem is #P-complete. Stanley [15] shows how the number of acyclic orientations can be computed by using the (a special case of Tutte’s polynomial). Another approach that concerns the number of acyclic orientations is the acyclic orientation game. Alon and Tuza [2] inquire about the amount of oriented edges needed to define a unique orientation of G, and find this number to be almost surely Θ(|V | log |V |) in Erdős–Rényi random graphs. Pikhurko [11] shows that the number of these edges in the worst case is 1 2 no greater than ( 4 + o(1))|V | for general graphs.

Problems addressed. Our paper investigates new algorithms for enumerat- ing patterns for this interesting problem in , given an undirected connected graph G(V,E) with n nodes and m edges. single source acyclic orientations ( ): Given a node s ∈ V , enumerate −→ ssao all the acyclic orientations G of G, such that s is the only source. single source acyclic orientations (weak ): Given a set of nodes S ⊆ −→ssao V , enumerate all the acyclic orientations G of G such that there is exactly one source x and x ∈ S. multiple source acyclic orientations (strong ): Given a set of nodes −→msao S ⊆ V , enumerate all the acyclic orientations G of G such that all the nodes in S are the only sources.3 multiple source acyclic orientations (weak ): Given a set of nodes msao−→ S ⊆ V , enumerate all the acyclic orientations G of G such that if x is a source then x ∈ S.4 −→ acyclic orientations (ao): Enumerate all the acyclic orientations G of G.

We will show that these problems can be reduced to ssao, with a one-to-one correspondence between their solutions. Many other variants with constraints on the number or choice of sources can be reduced to ssao as well: the ones 3 These orientations are possible if and only if S is an independent set. 4 Not all nodes in S must be sources, but there cannot be sources in V \ S.

2 we present are some of the most representative ones. We analyze the cost of an enumeration algorithm for ssao, weak ssao, strong msao, weak msao, and ao in terms of its delay cost, which is a well-known measure of performance for enumeration algorithms corresponding to the worst-case time between any two consecutively enumerated solutions (e.g. [8]). We are interested in algorithms with guaranteed delay and space.

Previous work. We are not aware of any provably good bounds for problems ssao, weak ssao, strong msao and weak msao. Johnson’s backtracking algo- rithm [9] for ssao has been presented over 30 years ago to solve problems on network reliability. However its complexity is not given and is hard to estimate, as it is based on a backtracking approach with dead ends. In his paper presenting an algorithm for ao, Squire [14] writes that he has been unable to efficiently implement Johnson’s approach because of its dead ends. Squire’s algorithm for ao uses Gray codes and has an amortized cost of O(n) per solution, but its delay can be O(n3) time for a solution. The algorithm by Barbosa and Szwarcfiter [3] solves ao with an amortized time complexity of O(n + m) per solution, delay O(n · m). The algorithm builds the oriented graph incrementally by iteratively adding the nodes to an empty directed graph. It is worth observing that by replacing each edge with a double arc and applying any algorithm for maximal feedback arc set enumeration, one can obtain all the acyclic orientations of G. State of the art approaches for the latter problem guarantee a delay Ω(n3) as shown by Schwikowski and Speckenmeyer [13]. All the techniques above for ao, including the one in [14], do not to extend smoothly to ssao, weak ssao, strong msao, and weak msao. In a previous work [5], we studied the related problem of enumerating the cyclic orientations of an undirected graph, but the proposed techniques cannot be reused for the problems in this paper.

Our results. Our contribution is the design of the first enumeration algorithms with guaranteed bounds for ssao, weak ssao, strong msao, and weak msao: the complexity is O(m · n) delay per solution using O(n2) space. For ao, we also show how to obtain O(m) delay, improving the delay of [3, 13, 14], but we do not improve the amortized cost of O(n) in [14]. We therefore focus on ssao in the first part of the paper, and then show an optimization that holds for the case of ao. We guarantee that, at any given partial solution, the extensions of the partial solution will enumerate new acyclic orientations. To this aim, we solve several non-trivial issues.

– We use a recursive approach where each call surely leads to a solution. For ssao this is achieved also by using a suitable ordering of the nodes. – We quickly identify the next recursion calls within the claimed time delay. – We do a careful analysis of the recursion , and show how to check effi- ciently for node reachability during recursion. – In the case of ao we exploit the fact that the recursion tree does not contain unary nodes.

3 The paper is organized as follows. We give the necessary definitions and terminology in Section 2. We then discuss how to solve ssao in Section 3 and further reduce the delay for ao in Section 4. In Section 5 we show how weak ssao, strong msao, weak msao, and ao reduce to ssao. We draw some conclusions in Section 6.

2 Preliminaries

Given an undirected graph G(V,E) with n nodes and m edges, an orientation of −→ −→ −→ G is the directed graph G(V, E ) where for any pair {u, v} ∈ E either (u, v) ∈ E −→ −→ −→ or (v, u) ∈ E . We call E an orientation of E. We say that the orientation G is acyclic when it does not contain cycles. For the sake of clarity, in the following we will call edges the unordered pairs {x, y} (undirected graph), while we will call arcs the two possible orientations (x, y) and (y, x) (directed graphs). We assume wlog that G is connected and does not contain self-loops. Given an undirected graph G(V,E), let v1, . . . , vn ∈ V be an ordering of the nodes of G. We define V≤i as the set {v1, . . . , vi}, N(vi) = {x : {vi, x} ∈ E} as the set of neighbors of the node vi, and N≤i(v) as the set N(v) ∩ V≤i. For Pn brevity, N<(vj) means N≤j−1(vj). Clearly we have j=1 |N<(vj)| = m. Starting from an empty directed graph, for increasing values of i = 1, 2, . . . , n, our algorithms add vi to the current graph and recursively exploit all the possible ways of directing the edges {v , x} with x ∈ N (v ), called direction assignments: −→ i < i a direction assignment Z for vi is an orientation of the set of edges {{vi, x} : x ∈ N<(vi)}. We refer to the following special assignments as

Xi = {(x, vi): x ∈ N<(vi)}

Yi = {(vi, x): x ∈ N<(vi)} −→ −→ We denote by G 0 the starting empty directed graph, and by G i the graph whose last added node is vi, with 1 ≤ i ≤ n.

3 Single Source Acyclic Orientations (ssao)

Given a graph G and a node s, this section describes how to enumerate its acyclic −→ −→ orientations G such that s is the unique source in G. Starting from an empty −→ −→ graph G , our algorithm adds v to G for i = 1, 2, . . . , n. For the edges in 0 i i−1 −→ N (v ), it exploits all the suitable direction assignments Z : each assignment < i −→ gives a certain G , on which it recurses. Every time it adds the last vertex v in i −→ −→ n a recursive call, it outputs the corresponding G n as a new solution G. The above simple scheme can lead to dead ends in its recursive calls, where −→ −→ −→ partial orientations G i cannot be extended to reach G n, i.e. an acyclic G whose only source is s. We prevent this situation by examining the nodes of G in a suitable order that allows us to exploit the following notions. Definition 1 (full node). Given an ordering v , . . . , v of the nodes in G, a −→ 1 n node vj (1 ≤ j ≤ i) is full in G i if N≤i(vj) = N(vj).

4 v6 v3 v9

v8

v2 v5 v7

v4 v1

Fig. 2. A partial acyclic orientation (thick edges). To add v7 to the partial orientation we exploit valid direction assignments for undirected edges {v4, v7}, {v5, v7}, {v6, v7}.

−→ −→ Definition 2 (valid direction assignment). Given G (V , E ), the di- −→ i−1 ≤i−1 rection assignment Z is valid if −→ −→ −→ – G i(V≤i, E ∪ Z ) is acyclic,−→ and −→ −→ – any vj 6= s that is full in G i(V≤i, E ∪ Z ) is not a source, for 1 ≤ j ≤ i. −→ The rationale is the following. When dealing with G , full nodes are the ones −→ i whose edges in G have been all already assigned in G . This means that if a full −→ i −→ node is a source in G it will be a source also in any extension of G , i.e. in the −→ i i final orientation G. A valid direction assignment imposes that we do not create cycles and each full node (except s) is not a source. We will deal with orientations −→ of G i that are the outcome of a sequence of valid direction assignments: such orientations will be referred to as partial acyclic orientations of Gi. Consider the graph in Figure 1 (a). We we want acyclic orientations whose only source is s = v processing the nodes in the order v , . . . , v . In partic- 9 1 −→ 9 ular, consider the situation in Figure 2. We have the graph G 6 (thick edges) and we have to add the vertex v7, deciding how to orient the edges to the vertices in N (v ), namely {v , v }, {v , v }, {v , v }, to obtain all the possible −→ < i 4 7 5 7 −→ 6 7 G 7. Notice that v1, v2, and v3 are full in G 6 since the edges in their whole neighborhood have been already oriented: hence since they are not source in −→ G , they will be not sources in the final orientation. The direction assignment 6 −→ {(v , v ), (v , v ), (v , v )} is valid since the full nodes in the corresponding G , 7 4 5 7 6 7 −→ 7 namely v1, v2, v3, v4 are not sources, and G 7 is acyclic. On the other hand, {(v , v ), (v , v ), (v , v )} is not a valid direction assignment since v is a source 4 7 5 7 6 7 −→ 4 full in the corresponding G and it will be source in any final orientation ex- −→ 7 tending G . Notice that, since v , and v are not in N (v ), v can be source −→ 7 7 −→ 9 < 6 6 −→ in G 6 and can remain source also in G 7, while it should be not a source in G 9. Hence, exploiting just the valid direction assignments means taking care that no is created and no full node is source. In order to efficiently find valid direction assignments, our algorithm uses an ordering of the nodes v1, . . . , vn that satisfies the conditions below.

Definition 3. An ordering of the nodes v1, . . . , vn is good if

– vn = s, and – N<(vi) ( N(vi), for 1 ≤ i < n.

5 Algorithm 1: single-source-acyclic −→ −→ Input: Graph G(V,E), a partial acyclic orientation G (V , E ), integer i −→ i−1−→ ≤i−1 Output: Acyclic orientations of G containing G i−1(V≤i, E ) with source s −→ if i > n then output G; return ; Execute Algorithm 2; −→ −→ for any valid direction assignment−→ Z for−→vi starting−→ from Z = Yi do single-source-acyclic(G, G i(V≤i, E ∪ Z ), i + 1);

The first condition in Definition 3 says that s should be the last node as it is the only source. The second condition says that there is at least one unassigned −→ incident edge for each v , when adding the latter to G . Dead ends can be i −→ i−1 avoided in this way: when adding v to G , we have at least one solution −→ i i−1 extending G i−1 in which vi is not a source. Indeed the following property holds. −→ Property 1. For any partial acyclic orientation G , there is always an acyclic −→ i −→ orientation G for G that has unique source s and includes G i.

Proof. For any j > i, consider the valid direction assignment Y , i.e. {(v , x): −→ j j x ∈ N (v )} obtaining G. These direction assignments cannot create cycles in −→ < j G j and the only final source is s. ut

Referring to the graph in Figure 1 (a), if s = v , the order induced by −→ 9 v1, . . . , v9 is a good order. Considering G 6 in Figure 2, according to Property 1, Y7 corresponds to {(v7, v6), (v7, v5), (v7, v4)}, Y8 corresponds to {(v8, v7)}, while Y9 corresponds to {(v9, v8), (v9, v6)}. These direction assignments are valid and lead to the acyclic orientation in Figure 1 (b) whose only source is indeed v9. A good ordering for G and s can be found in linear time by performing a DFS from s and considering its nodes in postorder. Observe that this is a good order according to our definition: node s is the last node and, for each node, its parent in the DFS tree appears after it in the order. Our recursive algorithm is shown in Algorithm 1, where the good ordering −→ of the nodes is employed. The initial call is single-source-acyclic(G, G 0, 1). The algorithm recursively exploits all the possible ways of expanding the cur- −→ rent partial solution G i−1 by iterating over all the valid direction assignments, starting from Yi, which is surely valid. The latter assignments are generated by a recursive computation. The general picture of our solution can be seen as follows: we have the primary recursion tree to generate all the wanted cyclic orientations (Algorithm 1), where each node has associated a secondary recur- sion tree to generate locally all the valid direction assignments (Algorithm 2). A naive implementation would simply consider each of the 2|N<(vi)| direction assignments checking whether it is valid or not (e.g. using a DFS in O(m) time). Instead, the following section introduces an efficient method that allows us to iterate through valid direction assignments only.

6 Algorithm 2: Returning valid direction assignments −→ −→ Input: Graph G(V,E), a partial acyclic orientation G (V , E ), node v −→ i−1 ≤i−1 i Output: Valid direction assignments Z −→ −→ F ← set of full nodes in G that are sources and not full in G ; −→i i i−1 Z ← {(vi, y): y ∈ Fi}; Let x , . . . , x be the nodes in N (v ) \ F ; 1 k −→ −→ < i i Execute Generate (G, G, vi, Z, 1, ∅, ∅). −→ −→ −→ Procedure Generate (G(V,E), G (V , E ), v , W , j, R, B) −→ i−1 ≤i−1 i if j > k then add W to the output list; return ; −→ −→ −→ Update B as the set of nodes leading to v in G (V , E ∪ W ); −→ −→i i ≤i if x 6∈ B then Generate (G, G , v , W ∪ {(v , x )}, j + 1,R,B); j i−1 i i j−→ −→ −→ Update R as the set of nodes reachable from v in G (V , E ∪ W ); −→ −→ i i ≤i if xj 6∈ R then Generate (G, G i−1, vi, W ∪ {(xj , vi)}, j + 1,R,B);

3.1 Iterating over valid direction assignments −→ Given G i−1 and node vi, we show how to iterate over valid direction assign- ments: our approach is shown in Algorithm 2. By definition, each valid direction −→ assignment W of the edges in {{vi, x} : x ∈ N<(vi)} should guarantee that we are not creating a cycle and no new full node becomes a source. −→ −→ Let F be the set of nodes which are: (a) full in G , (b) sources in G , i −→ i i−1 (c) not full in G . Note that F ⊆ N (v ). All the valid direction assignments i−1 i < i −→ should guarantee that nodes in F are not sources in G : this can be easily done i −→ i by adding the arcs {(vi, x): x ∈ Fi} to W . Observe that this is mandatory for the orientations of the corresponding edges because otherwise a node in F would −→ −→ −→ −→ i become a source in the final orientation G. Also, G i(V≤i, E ∪ W ) is acyclic. After that, we have to decide the orientation of the remaining edges. This part relies on procedure Generate in Algorithm 2. In particular, we have to assign a direction to the edges {vi, u} for each node u in N<(vi) \ Fi and check if they do not create cycles. We take these nodes in arbitrary order as x1, . . . , xk and, for increasing values of j = 1, 2, . . . , k, we do the following: if the arc e ∈ −→ −→ −→ {(v , x ), (x , v )} does not create a cycle in G (V , E ∪W ), proceed recursively i −→j −→j i i ≤i with W = W ∪ {e}. For a given vi, the reachability tests above for xj (j = 1, 2, . . . , k) can be performed with O(k) (forward and backward) DFS traversals, requiring overall O(k · m) time. Since k is bounded by the degree of vi and can be O(n) in the worst case, we propose a solution that reduces the cost from O(k · m) to O(m) time. It truncates the DFSes using sets B and R to avoid visiting the nodes in these sets. Since the partially built graph is acyclic, B and R are disjoint, and a node can belong to either one of them or none of them. Below we provide an analysis based on coloring the nodes of B and R showing that the overall time required by these tests for each valid direction assignment is O(m).

Lemma 1. Algorithm 2 returns valid direction assignments with delay O(m).

7 Proof. The arcs directed to nodes in Fi are unchanged for all the valid direction assignments for vi and can be computed in O(m) time at the beginning. When exploring the possible orientations of edges {vi, xj}, for j = 1, 2, . . . , k, each time we have to decide whether (v , x ) or (x , v ) creates a cycle or not −→ −→ −→ i j j i when added to G i(V≤i, E ∪ W ). To this aim we color incrementally the nodes: all the nodes R reachable from vi are red; all the nodes B that can lead to vi are black; the remaining nodes are uncolored. Initially, all the nodes are uncolored, −→ −→ −→ are R and B are empty. Since G i(V≤i, E ∪ W ) is acyclic any node has just one color or is uncolored. We now show that the sum of the costs to update the colors to produce a solution (valid direction assignment) is O(m). Since each leaf in the secondary recursion tree induced by Generate corresponds to a distinct solution, we should bound the sum of the costs along the k + 1 nodes from the root to that leaf. Specifically, the delay is upper bounded by the sum of the costs along two paths: the leaf-to-root path of the current solution and the root-to-next-leaf path for the next solution (actually only the latter for the first solution). Observe that the former cost is always O(|N<(vi)|). We prove that the sum of the costs from the root to a leaf in the secondary recursion tree induced by Generate is bounded by O(m). When j = 1, the red colors are assigned with a forward traversal and the −→ −→ −→ black colors are assigned with a backward traversal in the graph G (V , E ∪W ). −→ i ≤i When j > 1, while adding the arc (vi, xj) to W we have only to make the traversed uncolored nodes red: since the forward traversal is rooted at vi, we continue the traversal avoiding to visit red nodes. (No black node can be reached, −→ otherwise x would be black also and thus G cyclic). On the other hand, when j −→ i adding the arc (xj, vi) to W we have only to make the traversed uncolored nodes black: once again, this corresponds to continue the backward traversal rooted in vi avoiding to visit black nodes (no red node can be reached). Since this process traverses each arc at most once for any 1 ≤ j ≤ k, the sum of the costs of a root to leaf path in the secondary recursion tree induced by the Generate procedure is O(m). ut

Remark 1. After the last valid direction assignment has been returned, Algo- rithm 2 recognizes that there are no more valid direction assignments, using time O(m).

Lemma 2. Referring to Algorithm 1, the following holds. 1. All the acyclic orientations of G whose unique source is s are output. 2. Only the acyclic orientations of G whose unique source is s are output. 3. There are no duplicates.

Proof. We prove the three statements separately. 1. Given a good order of the nodes, we show that any single source acyclic −→ −→ orientation G can be expressed as a sequence of direction assignments Z i for v , for increasing values of i. Consider the following process: for decreasing i −→ −→ values of j remove vj from G, and set Z j equal to the current outgoing arcs

8 −→ −→ −→ from u in G. The sequence of sets Z ,..., Z will lead the algorithm to the −→ 1 n −→ discovery of G. Note that each direction assignment Z is valid otherwise −→ j we have a cycle or a source different from s in G. 2. Each solution is acyclic, since each time we add a node vi and a valid direc- tion assignment we do not introduce a cycle by definition of valid direction −→ assignment. We have to show that s is the unique source; any v full in G , −→ j −→ −→i with j ≤ i < n, is not a source in G : hence it is not a source in G = G. i −→n Indeed, for the good ordering definition, each node v not full in G , with −→ j i j ≤ i, has a neighbor in V \ V : if it is a source in G , when considering its ≤i i −→ last neighbor vz in the good order, it will not be a source anymore in G z. 3. Given any two solutions, looking at the primary recursion tree induced by Algorithm 1, they differ at least for the valid direction assignments branching in their least common ancestor. ut

Theorem 1. ssao can be solved with delay O(n · m) and space O(n2). Proof. We exploit the properties of the primary recursion tree induced by Algo- rithm 1. First of all, notice that each internal node has at least one child because of Property 1. This means that all the leaves correspond to a solution. Moreover, observe that all the leaves are at the same depth n, which is the height of the recursion tree. The first solution is clearly returned in time O(n · m), which is the height times the cost to get the first valid direction assignment for vi. This is bounded by O(m) time by applying Lemma 1. For any two consecutive solutions, the delay is bounded by the sum of the costs along a leaf-to-root path and the root-to-next-leaf path. The former is bounded by O(n · m): indeed the height of the tree is O(n) and each time we return we spend O(m) to recognize that no more valid direction assignments are possible, as highlighted by Remark 1. The latter is still bounded by O(n · m), applying n times Lemma 1. The space is bounded by O(n2), that is the space occupancy of a root-to-leaf path in the primary recursion. Indeed, in this path we have to maintain O(n) times the status of the Generate iterator, whose total space is bounded by O(n). ut

4 Acyclic orientations (ao)

This section deals with the problem of enumerating all the acyclic orientations of an undirected graph G. The general scheme remains the same as discussed in Section 3. Differently from before, we have no restriction about the possi- ble sources when adding vi. Hence we redefine the concept of valid direction assignment as follows. −→ −→ Definition 4 (valid direction assignment). Given G (V , E ), a direc- −→ −→ −→ −→ i−1 ≤i−1 tion assignment Z is valid if G i(V≤i, E ∪ Z ) is acyclic. −→ Referring to Figure 2, when adding v7 to G 6, this means that also the direc- tion assignment (v4, v7), (v5, v7), (v6, v7) is valid.

9 Algorithm 3: acyclic −→ −→ Input: Graph G(V,E), partial acyclic orientation G (V , E ), integer i −→ i−1 ≤i−→−1 Output: Acyclic orientations of G containing G i−1(V≤i−1, E ) −→ if i > n then output G n; return ; Execute Algorithm 4; −→ −→ −→ for any valid direction−→ assignment−→ −→ Z for vi starting from Z = Xi to Z = Yi do acyclic(G, G i(V≤i, E ∪ Z ), i + 1);

Moreover, another difference from the previous section is that we do not need the good order (Definition 3). Namely for any order of the nodes we can prove that the following property holds.

Property 2. For any vi there are always at least two valid direction assignments, i.e. Xi = {(x, vi): x ∈ N<(vi)} and Yi = {(vi, x): x ∈ N<(vi)}. −→ −→ Proof. While adding vi to G i−1, adding the arcs of Xi to G i−1 or adding the arcs of Y does not create a cycle. Indeed, inductively the following facts hold. −→ i −→ G 0 does not contains a cycle. Assuming that G i−1 does not contain a cycle, any cycle should involve v . Since in the two orientations above v is source or i −→i target, adopting one of these direction assignments cannot make G i cyclic. ut

Property 2 simply states that in Figure 2, both {(v4, v7), (v5, v7), (v6, v7)} and {(v , v ), (v , v ), (v , v )} are valid direction assignments when adding v −→ 7 4 7 5 −→7 6 7 to G . This is because G is acyclic and both of the new direction assignments 6 6 −→ cannot create cycles in the corresponding G 7. The actual scheme is summarized in Algorithm 3, whose starting call is −→ −→ −→ (G, G , 1). At step i, given the acyclic orientation G (V , E ), each acyclic 0 −→ −→ i−1 ≤−→i−1 −→ recursive call is of the kind (G, G , i + 1) where G (V , E ∪ Z ) is ob- acyclic i −→ i ≤i −→ tained by adding a valid direction assignment Z . By Property 2, Z = X and −→ i Z = Yi are always taken. The corresponding two recursive calls are done respec- tively at the beginning and at the end of the procedure. All the other possible valid direction assignments (if any) are explored in the other calls of the for cycle. When i = n + 1, all the nodes have been added and all the edges have −→ been assigned a direction. In this case G n is an acyclic orientation to be output. Lemma 3. Referring to Algorithm 3, the following holds. 1. All the acyclic orientations of G are output. 2. Just the acyclic orientations of G are output. 3. There are no duplicates.

Proof. Similar to the proof of Lemma 2. ut

We introduce a method that, at running time, allows us to iterate just on valid direction assignments: in particular, this method gets the first valid direction assignment Xi in O(|N<(vi)|) time and the remaining ones with delay O(m), one

10 Algorithm 4: Returning valid direction assignments −→ −→ Input: Graph G(V,E), partial acyclic orientation G (V , E ), node v −→ i−1 ≤i−1 i Output: Valid direction assignments Z Let x , . . . , x be the nodes of N (v ); 1 k −→ < i Execute Generate (G, G, vi, ∅, 1, ∅, ∅). −→ −→ −→ Procedure Generate (G(V,E), G (V , E ), v , W , j, R, B) −→ i−1 ≤i−1 i if j > k then add W to the output list; return ; −→ if W ∩ Yi 6= ∅ then −→ −→ −→ update R as the nodes reachable from vi in G i(V≤i, E ∪ W ) −→ −→ if x 6∈ R then Generate (G, G , v , W ∪ {(x , v )}, j + 1,R,B); −→j i−1 i j i if W ∩ Xi 6= ∅ then −→ −→ −→ update B as the nodes leading to vi in G i(V≤i, E ∪ W ) −→ −→ if xj 6∈ B then Generate (G, G i−1, vi, W ∪ {(vi, xj )}, j + 1,R,B);

after the other. The point is that we spend O(m) time and get a new solution. Here it is crucial that Yi is the last valid direction assignment for this purpose as it indicates when stopping the search for valid direction assignments. The motivation for this choice is given by the following lemma. −→ Lemma 4. When iterating over valid direction assignments Z , suppose that the first assignment Xi is returned in O(|N<(vi)|) time and the delay between any two consecutive valid direction assignments is O(m). Then Algorithm 3 has delay O(m).

Proof. In the primary recursion tree, the internal nodes have at least two chil- dren, the leaves are all the solutions, and their depth is n. The solution in the first leaf is obtained by using always Xi sets, each one corresponding to a node Pn vi, and the cost is i=1 O(|N<(vi)|) = O(m), using the hypothesis of the lemma. The delay between two consecutive solutions is upper bounded by the sum of the costs of the recursive calls to go from a leaf to the next leaf in preorder. Now let S and T be the solutions in two consecutive leaves. The paths from the root to S and to T share the prefix until a recursive call Rj, corresponding to −→ 0 0 the recursion while adding vj to G j−1, for some 1 ≤ j ≤ n − 1. Let S and T be Rj’s children that are respectively the ancestors of S and T . Note that the 0 0 path from S to S is made of Yi branches while the path from T to T is made 0 of Xi branches (j + 1 ≤ i ≤ n). The cost from S to S is O(n − j) as we do not need to check for further valid direction assignments after each Yi. The cost from S0 to T 0 is O(m) by hypothesis as they are two consecutive valid direction 0 Pn assignments for vj. The cost from T to T is O( i=j+1 |N<(vi)|) = O(m) by hypothesis on the costs to get each Xi. ut

In the next section we will provide a way of iterating over valid direction assignments fitting the hypothesis of Lemma 4.

11 4.1 Iterating over valid direction assignments −→ Given the node vi and the current acyclic directed graph G i−1, this section describes how to get all the valid direction assignments (i.e. such that adding −→ −→ one of them and v to G , we obtain a G which is still acyclic). i i−1 i −→ Algorithm 4 extends the current partial valid direction assignment W for vi: for each edge in {v , x} such that x ∈ N (v ), it adds the arc (v , x) or the arc −→ i < i i (x, vi) to W whether the partial direction assignment is still valid, i.e. no cycles are created. It explores all of these extensions. −→ −→ More formally, given G (V , E ) and v , we consider the nodes x , . . . , x i−1 ≤i i −→ 1 k in N<(vi) (where |N<(vi)| = k) one after the other. Initially, let W be an empty set. For increasing values of j, with 1 ≤ j ≤ k, we do the following. If the arc −→ −→ −→ e ∈ {(v , x ), (x , v )} does not create a cycle in G (V , E ∪ W ), this arc can i j j i −→ i−1 ≤i −→ −→ be added to the ongoing solution W , exploring recursively the case W = W ∪{e}. −→ −→ Let R and B be respectively the set of nodes reachable from v in G (V , E ∪ −→ −→ −→ −→ i i ≤i W ) and the set of nodes leading to v in G (V , E ∪W ). Adding the arc (v , x ) −→ i i ≤i i j to W creates a cycle if and only if x 6∈ B. Analogously, adding the arc (x , v ) to −→ j j i W creates a cycle if and only if xj 6∈ R. The scheme of the iterator is summarized by Algorithm 4. Notice that the first valid direction assignment produced is Xi and the last valid direction assignment is Yi, as required by Algorithm 3. Moreover observe that the update of R and B is respectively not required when −→ −→ W ∩ Y = ∅ and W ∩ X = ∅, since these conditions means respectively that the i i −→ −→ −→ outdegree and the indegree of vi in G i(V≤i, E ∪ W ) is zero. We remark that there are no dead ends. Indeed, if xj ∈ R then xj 6∈ B, mean- ing that even if the first call is skipped the second one is performed. Similarly xj ∈ B implies xj 6∈ R. This means that each call produces at least another call unless the direction assignment is completed, that is, each call returns at least one solution.

Lemma 5. Algorithm 4 returns the first valid direction assignment in time O(|N<(vi)|), and the remaining ones with delay O(m).

Proof. Recall that the direction assignment Xi is always returned first while Yi is returned last. For increasing values of i, adding the arcs of Xi (respectively Yi) does not create cycles: in this case no check is needed. In particular Xi is returned in time O(|N (v )|) since the update of R is not needed and never performed in < i −→ −→ −→ −→ Algorithm 4 (because W ∩ Y = ∅, i.e. the outdegree of v in G (V , E ∪ W ) is −→ i −→ i i ≤i zero). Checking W ∩ Y 6= ∅ and W ∩ X 6= ∅ can be done in constant time: these i −→ i −→ −→ are the out- and in-degree of x in G(V , E ∪ W ) that can be updated while −→ j ≤i updating W . The time is hence dominated by the cost to update R and B. As in Lemma 1, this can be done by growing R and B continuing the same forward and backward traversals from vi: since each arc is traversed at most once, the overall time to update R and B is O(m). ut

By combining Lemma 4 and Lemma 5, we obtain the following result. Theorem 2. Problem ao can be solved with delay O(m) and space O(n2).

12 5 Reducing to Single Source Acyclic Orientations

We show that the problems mentioned in Section 1 can be reduced to ssao. It is easy to see that weak ssao can be solved simply by enumerating all the ssaos in G with source s for each s ∈ S. It is worth observing that for each s there is at least a solution, meaning that the size of S does not influence the delay of weak ssao. Let us consider weak msao. To solve it, we create a dummy node s, and 0 connect it to every node in S. More formally, we build G (V ∪ {s},E ∪ Es), where Es = {{s, x} : x ∈ S}. Any weak msao of G can be transformed into 0 a ssao of G if we add s and all edges in Es (oriented away from s): s is a source and all nodes in S are no longer sources since they can be reached from s, hence s is the single source. Note that the orientation is still acyclic as s is a source and cannot be part of a cycle. The opposite is true as well: any ssao of G0 can be transformed into a weak msao of G by removing s and the edges in Es. This process only removes edges incident to nodes in S, hence only nodes in S possibly become sources. Clearly the orientation is still acyclic as removing nodes and edges cannot create cycles. Finally, consider strong msao. To solve it, we simply collapse all nodes of 00 S into one node s. More formally, we generate G (V ∪ {s}\ S, E ∪ Es), where Es = {{s, x} : ∃ y ∈ S with {y, x} ∈ E}. As s and all nodes in S must be sources, all of their incident edges must be oriented away from them in all acyclic orientations, while the rest of the graph is exactly the same for both cases. Clearly, any ssao for G00 induces a strong msao of G that can be obtained by removing s and Es and re-integrating S and the edges between S and V \ S (oriented away from S). Similarly, removing S (and the edges between S and V \S) and integrating s and Es (with edges oriented away from s), creates a ssao for G00: there is an edge from s to any node in V \S that was previously connected with S, hence these nodes cannot be sources; all other nodes in V \ S were not connected to S and hence their in-degrees and out-degrees are unchanged. As for ao, it can be obtained from weak msao by setting S = V . A direct reduction to ssao is described in [14], although the paper does not provide an algorithm for ssao. By Theorem 1, observing that the above transformations requires O(m) time, we can conclude the following result. Theorem 3. Problems ssao, weak ssao, strong msao, and weak msao can be solved with delay O(m · n) and space O(n2).

6 Conclusions

In this paper we have shown the first enumeration algorithms with guaranteed bounds for ssao, weak ssao, strong msao, and weak msao, whose delay is O(m · n) time and O(n2) space. The delay reduces to O(m) in the case of ao, improving prior work. It would be interesting to reduce the delay of the former problems to O(m) as well.

13 References

1. N. Alon and M. Tarsi. Colorings and orientations of graphs. Combinatorica, 12(2):125–134, 1992. 2. N. Alon and Z. Tuza. The acyclic orientation game on random graphs. Random Structures & Algorithms, 6(2-3):261–268, 1995. 3. V. C. Barbosa and J. L. Szwarcfiter. Generating all the acyclic orientations of an undirected graph. Information Processing Letters, 72(1):71 – 74, 1999. 4. B. Benson, D. Chakrabarty, and P. Tetali. G-parking functions, acyclic orientations and spanning trees. Discrete Mathematics, 310(8):1340–1353, 2010. 5. A. Conte, R. Grossi, A. Marino, and R. Rizzi. Enumerating cyclic orientations of a graph. IWOCA, 26th International Workshop on Combinatorial Algorithms, 2015. To appear. 6. P. Erdős, G. Katona, and B. J. M. Társulat. Theory of Graphs: Proceedings of the Colloquium Held at Tihany, Hungary, September 1966. Academic Press, 1968. 7. B. Iriarte. Graph orientations and linear extensions. DMTCS Proceedings, pages 945–956, 2014. 8. D. S. Johnson, C. H. Papadimitriou, and M. Yannakakis. On generating all maxi- mal independent sets. Inf. Process. Lett., 27(3):119–123, 1988. 9. R. Johnson. Network reliability and acyclic orientations. Networks, 14(4):489–505, 1984. 10. N. Linial. Hard enumeration problems in geometry and combinatorics. SIAM Journal on Algebraic Discrete Methods, 7(2):331–335, 1986. 11. O. Pikhurko. Finding an unknown acyclic orientation of a given graph. Combina- torics, Probability and Computing, 19:121–131, 1 2010. 12. B. Roy. Nombre chromatique et plus longs chemins d’un graphe. Revue française d’informatique et de recherche opérationnelle, 1(5):129–132, 1967. 13. B. Schwikowski and E. Speckenmeyer. On enumerating all minimal solutions of feedback problems. Discrete Applied Mathematics, 117(1):253–265, 2002. 14. M. B. Squire. Generating the acyclic orientations of a graph. Journal of Algorithms, 26(2):275 – 290, 1998. 15. R. Stanley. Acyclic orientations of graphs. In I. Gessel and G.-C. Rota, edi- tors, Classic Papers in Combinatorics, Modern Birkhäuser Classics, pages 453–460. Birkhäuser Boston, 1987. 16. R. P. Stanley. What Is Enumerative Combinatorics? Springer, 1986. 17. L. Vitaver. Determination of minimal coloring of vertices of a graph by means of boolean powers of the incidence matrix. In Dokl. Akad. Nauk SSSR, volume 147, page 728, 1962.

14