1 Introduction

For the well known Steiner tree problem there are efficient algorithms on special graph classes like series-parallel graphs (Wald and Colbourn 1983), outerplanar graphs (Wald and Colbourn 1982) and graphs of bounded tree-width (Bodlaender et al. 2015; Chimani et al. 2012). Some authors consider the class Steiner tree problem (CSP), which is a generalization of the Steiner tree problem where the vertices are partitioned into classes of terminals (Reich and Widmayer 1990). The unit-weight version of CSP is linear time solvable on co-graphs (Westbrook and Yan 1995).

The Steiner path problem is a special restriction of the Steiner tree problem in which the required terminal vertices lie on one path with minimal cost. It can also be seen as a generalization of the Hamiltonian path problem, since if we choose each vertex as a terminal vertex and if we have uniform edge weights, the Steiner path problem is equal to the Hamiltonian path problem. In Abu-Affash et al. (2014) they observed the Euclidean bottleneck Steiner path problem. Moreover, in Moharana et al. (2013) there was given a linear time solution for the Steiner path problem on trees.

A Steiner tree always exists within connected graphs. Nevertheless, it is not always possible to find a Steiner path, which motivates us to look at Steiner path cover problems. This Steiner path cover problem was already considered in Custic and Lendl (2021) on interval graphs.

In this article we investigate the directed Steiner path cover problem for which we give the following definition. Let G be a directed graph with vertex set V(G) and edge set E(G) and let \(T \subseteq V(G)\) be a set of terminal vertices. Further, let \(c: E(G) \rightarrow {\mathbb R}^{\ge 0}\) be a function that assigns a weight to each edge. A directed Steiner path cover for G is a set of vertex-disjoint simple directed paths P in G that consists of all terminal vertices T and possibly some additional non-terminal (Steiner) vertices of \(V(G) - T\). We define the size of a directed Steiner path cover as the number of its paths, i.e. the size is |P|, while the cost is defined as the sum of weights of the edges used in the paths in a directed Steiner path cover of minimum size.

  • Name:   Directed Steiner Path Cover

  • Instance:   A directed graph G, a set of terminal vertices \(T \subseteq V(G)\), and edge weights \(c: E(G) \rightarrow {\mathbb R}^{\ge 0}\).

  • Task:   Find a directed Steiner path cover P of minimum size for G that minimizes \(\sum _{p\in P}\sum _{e\in p} c(e)\).

Minimizing only the sum of the weights of the edges is not reasonable since this sum is minimized if we take each terminal vertex on its own as a path of length 0. So we primary require that the number of paths is minimal.

The directed Steiner path cover problem is NP-hard since it is a generalization of the directed Hamiltonian path problem. This gives a motivation for a restriction of the problem to special graph classes. We observe digraphs of a very natural graph class, namely directed co-graphs, which are defined as follows. Directed co-graphs (complement reducible graphs) are generated starting with the single vertex graph by applying disjoint union, order composition and series composition (Bechet et al. 1997). There exists another characterization of directed co-graphs by the exclusion of eight forbidden induced sub-digraphs, see Crespelle and Paul (2006, Fig. 2).

Further, directed co-graphs are precisely the digraphs of directed NLC-width 1 and they are a proper subset of the digraphs of directed clique-width at most 2 (Bang-Jensen and Gutin 2018; Gurski et al. 2016). This graph class is also interesting from an algorithmic point of view. Several hard graph problems are polynomial time solvable using dynamic programming along the tree structure of the input graph, see Bang-Jensen and Maddaloni (2014), Gurski (2017), Gurski et al. (2019), Gurski et al. (2019), Gurski and Rehs (2018), Gurski et al. (2021). Additionally, there is a practical application as directed co-graphs can be used for the reconstruction of the evolutionary history of genes or species using genomic sequence data, see Hellmuth et al. (2017), Nojgaard et al. (2018).

For graphs where all edges have the same weight, the above definition of the directed Steiner path cover problem results in a minimum number of Steiner vertices. Graphs without edge weights can be considered as a special case of graphs with unit-edge weights. Since edge weights do not occur in co-graphs, we use the following problem definition.

  • Name:   Unit-Edge-Weight Directed Steiner Path Cover

  • Instance:   A directed graph G and a set of terminal vertices \(T \subseteq V(G)\).

  • Task:   Find a directed Steiner path cover of minimum size for G such that the number of Steiner vertices is minimal.

In the following we present how to compute the value of a directed Steiner path cover of minimum size and cost for the disjoint union, order composition and series composition of two directed co-graphs in linear time from the corresponding values of the involved directed co-graphs. For this purpose, we give a definition of a useful normal form for directed Steiner path covers in digraphs which are defined by the order composition or series composition of two directed co-graphs. Moreover, we show a linear time algorithm which builds a directed Steiner path cover of minimum size and cost for a directed co-graph.

The (unit-weight) directed Steiner tree problem is \(\text {MSO}_1\)-definable by Ganian et al. (2014, Proposition 4.14). But this does not hold for the directed Steiner path (cover) problem, since it is a generalization of the directed Hamiltonian path problem which is not \(\text {MSO}_1\)-definable. Thus it is not possible to obtain our solutions for the directed Steiner path (cover) problem using the fact that directed co-graphs have bounded directed clique-width (Gurski et al. 2016) and the result from Ganian et al. (2014) stating that all \(\text {MSO}_1\)-definable digraph problems are fixed parameter tractable for the parameter directed clique-width. Since the Hamiltonian cycle problem is \(\text{ W }[1]\)-hard parameterized by clique-width (Fomin et al. 2010) it even follows that Hamiltonian path problem and thus the (directed) Steiner path (cover) problem are \(\text{ W }[1]\)-hard when parameterized by (directed) clique-width.

We also give binary integer programs for the directed Hamiltonian path problem and for the directed Steiner path (cover) problem. These integer programs can be used to minimize change-over times in pick-and-place machines used in electronic industry. The problem of minimizing change-over times is introduced in Sect. 4.

  • Name:   Directed Steiner Path

  • Instance:   A directed graph G, a set \(T \subset V(G)\) of terminal vertices, and a function \(c: E(G) \rightarrow {\mathbb R}^{\ge 0}\) that assigns to each edge some weight.

  • Task:   Find a directed Steiner path p in graph G that minimizes \(\sum _{e \in p} c(e)\).

2 Preliminaries

We consider the directed Steiner path cover problem on directed co-graphs, so we first recall the definition of directed co-graphs and we give a normal form of directed Steiner path covers.

2.1 Directed co-graphs

The class of directed co-graphs was introduced by Bechet et al. in (1997).

Definition 1

The class of directed co-graphs is recursively defined as follows.

  1. (i)

    Every digraph on a single vertex \((\{v\},\emptyset )\), denoted by \(\bullet _v\), is a directed co-graph.

  2. (ii)

    If A, B are vertex-disjoint directed co-graphs, then

    1. (a)

      the disjoint union \(A \oplus B\), which is defined as the digraph with vertex set \(V(A) \cup V(B)\) and edge set \(E(A) \cup E(B)\),

    2. (b)

      the order composition \(A \oslash B\), defined by their disjoint union plus all possible edges only directed from V(A) to V(B), and

    3. (c)

      the series composition \(A \otimes B\), defined by their disjoint union plus all possible edges between V(A) and V(B) in both directions,

    are directed co-graphs.

An expression which uses these operations is called a directed co-expression. The recursive generation of a directed co-graph can be described by a tree structure, called directed co-tree. The vertices of the digraph are represented by the leaves of the directed co-tree, while the inner vertices of the directed co-tree correspond to the operations applied on the subgraphs of G defined by the respective subtrees. Constructing a directed co-tree for a directed co-graph is possible in linear time, see Crespelle and Paul (2006).

For the characterization of directed co-graphs by excluding eight forbidden induced sub-digraphs, see Crespelle and Paul (2006), Fig. 2 or Table 1.

Table 1 The eight forbidden induced sub-digraphs for directed co-graphs (see Crespelle and Paul 2006)

In the following we give a definition of a normal form for directed Steiner path covers in directed co-graphs which are defined by the order composition or series composition of two directed co-graphs.

2.2 Normal form for directed steiner path covers

Let G be a directed co-graph and let \(T \subseteq V(G)\) be a set of terminal vertices. Further, let C be a directed Steiner path cover for G with respect to T, while \(\mathfrak {s}(C)\) denotes the number of Steiner vertices in the paths of C.

Lemma 1

Let C be a directed Steiner path cover for some directed co-graph \(G = A \oslash B\) or \(G = A \otimes B\) with respect to a set \(T \subseteq V(G)\) of terminal vertices. Then, there is a directed Steiner path cover \(C'\) with respect to T that does not contain paths p and \(p'\) satisfying one of the structures (1)-(4), such that \(|C| \ge |C'|\) and \(\mathfrak {s}(C) \ge \mathfrak {s}(C')\) applies. Let \(q_1,\ldots ,q_4\) denote sub-paths which may be empty.

  1. 1.

    \(p=(x, q_1)\) or \(p=(q_1,x)\) where \(x \not \in T\). Comment: No path starts or ends with a Steiner vertex.

  2. 2.

    \(p=(q_1, u, x, v, q_2)\) where \(u \in V(A)\), \(v \in V(B)\), and \(x \not \in T\). Comment: On a path, the neighbors uv of a Steiner vertex x are both contained in the same digraph.

  3. 3.

    \(p=(q_1,x)\), \(p'=(u, q_2)\), where \(x \in V(A)\), \(u \in V(B)\), \(p \ne p'\). Comment: No path p ends in A, if there is a path \(p' \ne p\) that starts in B.

  4. 4.

    \(p=(\ldots , x, u, v, y, \ldots )\) where \(u,v \not \in T\). Comment: The paths contain no edge between two Steiner vertices.

If \(G = A \otimes B\), then the cover \(C'\) also does not contain paths satisfying structures (5)-(8).

  1. 5.

    \(p=(x, q_1)\), \(p'=(u,q_2)\), where \(x \in V(A)\), \(u \in V(B)\), \(p \ne p'\). Comment: All paths start in the same digraph.

  2. 6.

    \(p=(q_1, x, y, q_2)\), \(p'=(q_3, u, v, q_4)\) where \(x,y \in V(A)\), \(u, v \in V(B)\). Comment: The cover \(C'\) contains edges of only one of the digraphs.

  3. 7.

    \(p=(x, q_1)\), \(p'=(q_2, u, y, v, q_3)\), where \(x,y \in V(A)\), \(u, v \in V(B)\), and \(y \not \in T\). Comment: If a path starts in A then there is no Steiner vertex in A with two neighbors on the path in B.

  4. 8.

    \(p=(x, q_1)\), \(p'=(q_2, u, v, q_3)\), where \(x \in V(A)\) and \(u, v \in V(B)\). Comment: If a path starts in A, then no edge of B is contained in the cover.

Proof

  1. 1.

    If x is removed from p we get a cover with one Steiner vertex less than C.

  2. 2.

    If x is removed from p, we get a cover with one Steiner vertex less than C.

  3. 3.

    We combine the paths to only one path \((q_1, x, u, q_2)\) and we get a cover with one path less than C.

  4. 4.

    Since G is a directed co-graph, the underlying undirected graph is a co-graph such that the path cannot include a \(P_4\), i.e. a simple path of 4 vertices, as induced subgraph. Thus, there must be at least one additional arc. If such an additional arc would shorten the Steiner path by skipping u, v or both then we remove u or v or both and take the shortcut for getting a cover \(C'\). Additional arcs that do not shorten the path would create a forbidden induced subgraph from Table 1 which is not possible. For details see Table 2.

  5. 5.

    The new paths are \(q_1\) and \((x,u.q_2)\). The cover \(C'\) is as good as C.

  6. 6.

    If \(p \ne p'\), then \((q_1, x, v, q_4)\) and \((q_3, u, y, q_2)\) are the paths in cover \(C'\), see Fig. 1.

    Table 2 The leftmost column shows a graph with underlying undirected \(P_4\) and at least one additional arc that do not shorten the path. The other columns shows the forbidden subgraphs that are contained in the leftmost graph depending on the edges of the \(P_4\)

    If \(p=p'\), then we have to distinguish whether \((u,v)\in q_1\), \((u,v)\in q_2\), \((x,y)\in q_3\), or \((x,y)\in q_4\). We show how to handle the first case, the other three cases are similar. Let \(p=(q_3,u,v,q_5,b,a,q_6,x,y,q_2)\), where \(b\in V(B)\) and \(a\in V(A)\). Then the new path in cover \(C'\) is \((q_3,u,a,q_6,x,v,q_5,b,y,q_2)\). Such vertices a and b must exist because \(v \in V(B)\) and \(x \in V(A)\), possibly it holds \(a=x\) or \(b=v\). In any case cover \(C'\) is as good as C, see Fig. 2.

  7. 7.

    If \(p \ne p'\), then \(q_1\) and \((q_2,u,x,v,q_3)\) are the new paths in cover \(C'\). If \(p=p'\), i.e. \(q_1 = (q'_2,u,y,v,q_3)\), where \(q'_2\) is obtained from \(q_2\) by removing x, then \((q'_2,u,x,v,q_3)\) is the new path in cover \(C'\). The cover \(C'\) is as good as C, see Fig. 3. If \(p \ne p'\), then the edge (ab) is missing in Fig. 3.

  8. 8.

    If \(p \ne p'\), then \(q_1\) and \((q_2,u,x,v,q_3)\) are the new paths in cover \(C'\). If \(p=p'\), i.e. \(q_1 = (q'_2,u,v,q_3)\), where \(q'_2\) is obtained from \(q_2\) by removing x, then \((q'_2,u,x,v,q_3)\) is the new path in cover \(C'\). The cover \(C'\) is as good as C, see Fig. 4. If \(p \ne p'\), then the edge (ab) is missing in Fig. 4.

Operations 1, 2, 4 and 7 reduce the number of Steiner vertices by one, the remaining operations 3, 5 and 6 do not change the number of Steiner vertices. Therefore, operations 1, 2, 4 and 7 can only be executed at most \(|V - (T_A \cup T_B)|\) times.

Operation 6 reduces the number of paths by one, the remaining operations do not increase the number of paths. Therefore operation 6 can be executed at most \(\max \{|T_A|, |T_B|\}\) times.

Let us now consider those edges on a path that connect vertices of A and vertices of B. The maximum number of those edges is \(|V(A)| + |V(B)| - 1\). Operation 7 can remove two such edges, operations 3 and 5 can add two such edges. Since the other operations 1, 2, 4 and 6 do not reduce the number of edges, operations 3 and 5 can be used at most \((|V(A)| + |V(B)| - 1) / 2 + |V - (T_A \cup T_B)|\) times. \(\square \)

The hypothesis of Lemma 1 is symmetric in A and B and thus, the statement of Lemma 1 is also valid for co-graphs \(G = A \otimes B\) if we switch A and B.

Fig. 1
figure 1

Substitution in the proof of Lemma 1(6.) for \(p \ne p'\)

Fig. 2
figure 2

Substitution in the proof of Lemma 1(6.) for \(p=p'\)

Fig. 3
figure 3

Substitution in the proof of Lemma 1(7.)

Fig. 4
figure 4

Substitution in the proof of Lemma 1(8.)

Definition 2

A directed Steiner path cover C for some directed co-graph \(G = A \oslash B\) or \(G = A \otimes B\) is said to be in normal form if none of the operations described in the proof of Lemma 1 is applicable.

Now we assume that a directed Steiner path cover for some directed co-graph \(G = A \oslash B\) or \(G = A \otimes B\) is always in normal form, since the operations of the proof of Lemma 1 do not increase the number of paths or Steiner vertices of a cover. Lemma 1 implies the following theorem.

Theorem 1

For each directed co-graph \(G = A \otimes B\) and set of terminal vertices \(T \subseteq V(G)\) any directed Steiner path cover C in normal form with respect to T does not contain an edge of digraph A, and no path in C starts or ends in digraph A if \(|T_A| \le |T_B|\).

Proof

(by contradiction) Assume, the Steiner path cover C contains an edge of digraph A. Then by Lemma 1(5), all paths starts in digraph A. By Lemma 1(4), it holds that no Steiner vertex v of V(A) is contained in C, where the neighbors of v are both of digraph B. By Lemma 1 (1), (2), and (5), it holds that all vertices of V(B) from C are connected with a terminal vertex of V(A), thus \(|T_A| > |T_B|\).

figure h

Second, we have to show that no path in C starts or ends in digraph A. Assume on the contrary, that there is one path that starts in A. By Lemma 1(6), it holds that all paths start in A. Continuing as in the first case this leads to a contradiction. \(\square \)

Remark 1

For each directed co-graph \(G = A \oslash B\) and set of terminal vertices \(T \subseteq V(G)\) any directed Steiner path cover C in normal form with respect to T it holds that each path that starts in A either remains in A or it crosses over to B and remains in B. Each path that reaches a vertex of B has to stay in B since no edge from a vertex in B to a vertex in A exists.

3 Algorithms for the directed Steiner path cover problem

3.1 Computing the optimal number of paths

Let G be a directed co-graph and \(T \subseteq V(G)\) be a set of terminal vertices. We define p(GT) as the minimum number of paths within a Steiner path cover for G with respect to T. Further, let s(GT) be the minimum number of Steiner vertices in a directed Steiner path cover of size p(GT) with respect to T. If it is clear from the context, we do not specify set T.

Lemma 2

Let A and B be two vertex-disjoint directed co-graphs and let \(T_A \subseteq V(A)\) and \(T_B \subseteq V(B)\) be two sets of terminal vertices. The following equations apply.

  1. 1.

    \(p(\bullet _v, \emptyset )=0\) and \(p(\bullet _v, \{v\})=1\)

  2. 2.

    \(p(A \oplus B, T_A \cup T_B) = p(A, T_A) + p(B, T_B)\)

  3. 3.

    \(p(A \otimes B, \emptyset ) = 0\)

  4. 4.

    \(p(A \otimes B, T_A \cup T_B) = \max \{1, p(B, T_B) - |V(A)|\}\) if \(1 \le |T_B|\) and \(|T_A| \le |T_B|\)

  5. 5.

    \(p(A \otimes B, T_A \cup T_B) = \max \{1, p(A, T_A) - |V(B)|\}\) if \(1 \le |T_A|\) and \(|T_A| > |T_B|\)

  6. 6.

    \(p(A \oslash B, T_A \cup T_B) = p(A, T_A)\) if \(p(A) > p(B)\)

  7. 7.

    \(p(A \oslash B, T_A \cup T_B) = p(B, T_B)\) if \(p(A) \le p(B)\)

Proof

1. - 3. Obviously holds.

  1. 4.

    We show \(p(A \otimes B) \ge \max \{1,p(B)-|V(A)|\}\) by an indirect proof. We assume that in a directed Steiner path cover C for \(A \otimes B\) there are less than \( \max \{1,p(B) - |V(A)|\}\) paths. Removing all vertices of A from all paths in C leads to a directed Steiner path cover of size \(|C| + |V(A)| < p(B)\) for B.

    figure i

    We now show that \(p(A \otimes B) \le \max \{1, p(B) - |V(A)|\}\) applies. We can use any vertex of A to combine two paths of the cover of B to one path, as the series composition of A and B creates every possible directed edge between A and B. If there exists more terminal vertices in \(T_A\) than there are paths in the cover of B, i.e. \(p(B) < |T_A|\), we split paths of B and reconnect them with terminal vertices of \(T_A\), which is always possible since \(|T_A| \le |T_B|\).

  2. 5.

    Similar to 4.

  3. 6.

    We show that \(p(A \oslash B) \le p(A)\) holds. Consider that it is possible to append any path of A by any path of B, see Lemma 1(3). As this creates no edge between B and A, we cannot extend a path of B with a path of A. We show by an indirect proof that \(p(A \oslash B) \ge p(A)\) applies. We assume that a directed Steiner path cover C for \(A \oslash B\) contains less than p(A) paths. By removing all vertices of B from all paths in C we get a Steiner path cover of size \(|C| < p(A)\).

    figure j
  4. 6.

    Similar to 6. \(\square \)

3.2 Computing the optimal number of Steiner vertices

Remark 2

For two vertex-disjoint directed co-graphs A, B and two sets of terminal vertices \(T_A \subseteq V(A)\), \(T_B \subseteq V(B)\) it holds that \(s(A \oplus B, T_A \cup T_B) = s(A, T_A) + s(B, T_B)\), as the disjoint union does not create new edges.

Remark 3

Let \(G=A \oslash B\) be a directed co-graph, and let C be a directed Steiner path cover of G such that \(p=(q_1,u_1,x,q_2,v_1)\) is a path in A, \(p_1=(u_2, q_3)\) and \(p_2=(v_2,q_4)\) are paths in B and all paths are vertex-disjoint paths in C, where \(x \not \in T\), \(u_1,u_2,v_1,v_2 \in T\), and \(q_1,\ldots ,q_4\) are sub-paths. Now, we split p at vertex x into two paths and combine them with \(p_1\) and \(p_2\) to obtain \((q_1,u_1,u_2,q_3)\) and \((q_2,v_1,v_2,q_4)\) as new paths. On this way we get a Steiner path cover without increasing the cost. If we switch A and B, we obtain \((u_2,q_3,q_1,u_1)\) and \((v_2,q_4,q_2,v_1)\) as new paths such that the statement holds as well.

We conclude the central lemma of our work. The proof is done by induction on the structure of the co-graph.

Lemma 3

Let G be a directed co-graph and C a directed Steiner path cover for G with respect to a set \(T \subseteq V(G)\) of terminal vertices. Then, it holds that \(p(G) + s(G) \le |C| + \mathfrak {s}(C)\).

Proof

(by induction) Obviously, the statement is valid for directed co-graphs with only one vertex. We now assume that the statement is valid for directed co-graphs of n vertices. Let A and B are vertex-disjoint directed co-graphs with at most n vertices each.

Disjoint union: Let \(G = A \oplus B\) be a directed co-graph with more than n vertices. By Lemma 2, and Remark 2, we know that \(p(A \oplus B) +s(A \oplus B) = p(A) + p(B) + s(A) + s(B)\). By the induction hypothesis, \(p(A) + s(A) \le |C_{|A}| + \mathfrak {s}(C_{|A})\) and \(p(B) + s(B) \le |C_{|B}| + \mathfrak {s}(C_{|B})\) apply, where \(C_{|A}\) denotes the cover C restricted to digraph A, i.e. the cover which results if we remove all vertices of B from C. This yields to the statement of the lemma.

$$\begin{aligned} p(A \oplus B) + s(A \oplus B) \le |C_{|A}| + \mathfrak {s}(C_{|A}) + |C_{|B}| + \mathfrak {s}(C_{|B}) = |C| + \mathfrak {s}(C) \end{aligned}$$

Series composition: Let \(G = A \otimes B\) be a directed co-graph on more than n vertices and without loss of generality it holds that \(|T_A| \le |T_B|\).

  1. 1.

    Let X(A) denote the vertices of A used in cover C, and let D be the cover for B that results by removing the vertices of X(A) from the cover C. By induction hypothesis, we get \(p(B) + s(B) \le |D| + \mathfrak {s}(D)\).

  2. 2.

    Let nt(X(A)) be the number of non-terminal vertices of X(A). By Theorem 1 we get that \(\mathfrak {s}(C) = \mathfrak {s}(D) + nt(X(A))\) and \(|C| = |D| - |T_A| - nt(X(A))\). Consequently, we come to \(|C| + \mathfrak {s}(C) = |D| + \mathfrak {s}(D) - |T_A|\).

Combining these results together we get:

$$\begin{aligned} p(B) + s(B) - |T_A| \le |D| + \mathfrak {s}(D) - |T_A| = |C| + \mathfrak {s}(C) \end{aligned}$$

To prove the statement of the lemma, we start with considering the case \(p(B) - 1 \le |V(A)|\). Then, we get \(p(A \otimes B) = 1\). If \(|T_A| \ge p(B) - 1\), then \(d := |T_A| - (p(B)-1)\) many Steiner vertices from B, are replaced by terminal vertices from A, if they are available. Otherwise, if \(|T_A| < p(B) - 1\), we get \(-d = (p(B) - 1) - |T_A|\) many Steiner vertices from A with which we combine the paths. Consequently, it holds that \(s(A \otimes B) \le \max \{0,s(B) - d\}\) as the number of Steiner vertices in an optimal cover is at most the number of Steiner vertices in a certain cover. Thus, since \(p(A \otimes B) = 1\) we obtain for \(s(B)\ge d\):

figure k

If \(s(B)< d\), then all Steiner vertices of B are replaced by terminal vertices of A and as we have \(|T_A|\le |T_B|\), some of the paths of B are reconnected by the remaining terminal vertices of A. Consequently, we get \(p(A \otimes B) + s(A \otimes B)=1\le |C| + \mathfrak {s}(C)\).

Now, we come to the case where \(p(B) - 1 > |V(A)|\), i.e. it is not possible to combine all paths in an optimal cover for B with vertices of A. By Lemma 2, we know that \(p(A \otimes B) = \max \{1, p(B) - |V(A)|\}\). Consequently, for \(p(A \otimes B) > 1\) it follows:

$$\begin{aligned} p(A \otimes B) + s(A \otimes B)\le & {} p(B) - |V(A)| + s(B) + nt(A) \\= & {} p(B) + s(B) - |T_A| ~ \le ~ |C| + \mathfrak {s}(C) \end{aligned}$$

Since the non-terminal vertices of A are now used to combine paths of the cover, the non-terminal vertices of A become Steiner vertices.

Order composition: Let \(G = A \oslash B\) be a directed co-graph with more than n vertices. By the induction hypothesis, we get \(p(A) + s(A) \le |C_{|A}| + \mathfrak {s}(C_{|A})\) and \(p(B) + s(B) \le |C_{|B}| + \mathfrak {s}(C_{|B})\).

Let us first consider the case \(p(A) > p(B)\). By Lemma 2 it holds \(p(A \oslash B) = p(A)\). We can append any path of A by any path of B, and by Remark 3 it holds that for every path that there is more in A than in B, a Steiner vertex of B can be removed. Additionally, as an optimal cover has at most as many Steiner vertices as a concrete cover, we get \(s(A \oslash B) \le \mathfrak {s}(C_{|A}) + \mathfrak {s}(C_{|B}) - \min \{\mathfrak {s}(C_{|B}), |C_{|A}| - |C_{|B}|\}\). If we sum up both equations we come to

$$\begin{aligned} p(A \oslash B) + s(A \oslash B) \le p(A) + \mathfrak {s}(C_{|A}) + \mathfrak {s}(C_{|B}) - \min \{\mathfrak {s}(C_{|B}), |C_{|A}| - |C_{|B}|\} \end{aligned}$$

If \(\mathfrak {s}(C_{|B}) \ge |C_{|A}| - |C_{|B}|\) applies, and as \(\mathfrak {s}(C) = \mathfrak {s}(C_{|A}) + \mathfrak {s}(C_{|B})\) applies, we get

$$\begin{aligned} p(A \oslash B) + s(A \oslash B) \le p(A) + \mathfrak {s}(C) - |C_{|A}| + |C_{|B}|. \end{aligned}$$

The statement would be shown if \(p(A) - |C_{|A}| + |C_{|B}| \le |C|\) applied. It holds \(p(A) \le |C_{|A}|\) since an optimal cover has at most as many paths as a concrete cover, and it holds \(|C_{|B}| \le |C|\), since \(|C| = \max \{|C_{|A}|, |C_{|B}|\}\) by Remark 1. We sum up these equations which leads to \(p(A) + |C_{|B}| \le |C_{|A}| + |C|\). This is equivalent to \(p(A) - |C_{|A}| + |C_{|B}| \le |C|\), such that \(p(A \oslash B) + s(A \oslash B) \le |C| + \mathfrak {s}(C)\) has been shown.

If \(\mathfrak {s}(C_{|B}) < |C_{|A}| - |C_{|B}|\), then we get \(p(A \oslash B) + s(A \oslash B) \le p(A) + \mathfrak {s}(C_{|A})\). It is left to show that \(p(A) + \mathfrak {s}(C_{|A}) \le |C| + \mathfrak {s}(C)\) applies. Since an optimal cover has at most as many paths as a concrete cover, it holds \(p(A) \le |C_{|A}|\). Further, we know that \(|C_{|A}| \le |C|\) since \(|C| = \max \{|C_{|A}|, |C_{|B}|\}\) by Remark 1. Moreover, it holds \(\mathfrak {s}(C_{|A}) \le \mathfrak {s}(C)\), as a part can at most be as big as the whole.

We can show the other case \(p(A) \le p(B)\) on a similar way. \(\square \)

To see why Lemma 3 is crucial for the rest of this work, consider the directed graph B of Fig. 5 that is not a directed co-graph. Terminal vertices \(T_A=\{f,g\}\) and \(T_B=\{a,c,e,u,w,y\}\) are shown as squares. In the left part of the figure a Steiner path cover \(C_\ell = \{(a,b,c,d,e), (u,v,w,x,y)\}\) for graph B is shown with \(|C_\ell | = 2\) and \(\mathfrak {s}(C_\ell ) = 4\) which is optimal. In the right part of the figure a Steiner path cover \(C_r = \{(a,b,c,w,x,y), (e), (u)\}\) for B is shown with \(|C_r| = 3\) and \(\mathfrak {s}(C_r) = 2\).

Fig. 5
figure 5

Small example that shows the contrapositive of the statement of Lemma 3 in a graph B that is not directed co-graph

The right cover can be extended to an optimal cover for \(A \otimes B\) if the vertices of A are used to combine the path: \(\{(u,f,a,b,c,w,x,y,g,e)\}\) is an optimal cover for \(A \otimes B\) with only one path and 2 Steiner vertices. The left Steiner path cover can not be extended to an optimal cover for \(A \otimes B\). For example, we can split path (abcde) at vertex b into two paths (a) and (cde) and reconnect them by a vertex of A and get (afcde). The other vertex of A must be used to combine the remaining two paths to (afcdeguvwxy) which results in a cover for \(A \otimes B\) that consists of one path but 3 Steiner vertices. For graph B the statement of Lemma 3 is not satisfied: \(p(B) + s(B) = 2 + 4 = 6 > |C_r| + \mathfrak {s}(C_r) = 3 + 2 = 5\)

In the proof of Lemma 4 we use the statement of Lemma 3 to show that optimal solutions for directed co-graphs A and B can be combined to an optimal solution for \(A \oslash B\) and \(A \otimes B\).

Remark 4

Let G be a directed co-graph and let C be a directed Steiner path cover for G with respect to some set of terminal vertices \(T \subseteq V(G)\). Then \(\mathfrak {s}(C) \ge s(G)\) holds only if \(|C| = p(G)\). If \(|C| > p(G)\) then \(\mathfrak {s}(C)\) might be smaller than s(G).

Lemma 4

Let A and B be two vertex-disjoint digraphs, and let \(T_A \subseteq V(A)\), \(T_B \subseteq V(A)\) be sets of terminal vertices. Then the following equations applies:

  1. 1.

    \(s(\bullet _v, \emptyset )=0\) and \(s(\bullet _v, \{v\})=0\)

  2. 2.

    \(s(A \oplus B, T_A \cup T_B) = s(A, T_A) + s(B, T_B)\)

  3. 3.

    \(s(A \otimes B) = \max \{0, s(B) + p(B) - p(A \otimes B) - |T_A|\}\) if \(|T_A| \le |T_B|\)

  4. 4.

    \(s(A \otimes B) = \max \{0,s(A) + p(A) - p(A \otimes B) - |T_B|\}\) if \(|T_A| > |T_B|\)

  5. 5.

    \(s(A \oslash B) = s(A) + s(B)\) if \(p(A) = p(B)\)

  6. 6.

    \(s(A \oslash B) = s(A) + s(B) - \min \{s(A), p(B)-p(A)\}\) if \(p(A) < p(B)\)

  7. 7.

    \(s(A \oslash B) = s(A) + s(B) - \min \{s(B), p(A)-p(B)\}\) if \(p(A) > p(B)\)

Proof

  1. 1.

    Obvious.

  2. 2.

    See Remark 2

  3. 3.

    At first we show \(s(A \otimes B) \le \max \{0, s(B) + p(B) - p(A \otimes B) - |T_A|\}\). Lemma 3 implies that \(s(A \otimes B) + p(A \otimes B) \le \mathfrak {s}(C) + |C|\) holds for any cover C for directed co-graph \(A \otimes B\) and any set of terminal vertices T. We consider the cover C for \(A \otimes B\) which is obtained by an optimal cover D for B in the following way: Take the terminal vertices of A to either combine paths of D or to remove a Steiner vertex of D by replacing \(v \not \in T\) by a terminal vertex of A in a path as \((\ldots , u, v, w, \ldots ) \in D\), where \(u,w \in T\). If \(|T_A|\ge s(B)+p(B)\), then we can combine all paths of D while all Steiner vertices can be removed by terminal vertices of A. Since \(|T_A|\le |T_B|\) holds, we can split some of the paths and reconnected them by the remaining terminal vertices of A. Consequently, we get \(\mathfrak {s}(C) + |C|=1\) and \(s(A \otimes B)=0\). Otherwise, if \(|T_A|< s(B)+p(B)\), then we have \(\mathfrak {s}(C) + |C| = s(B) + p(B) - |T_A|\), and by Lemma 3, the statement follows.

    $$\begin{aligned} \begin{array}{crcl} &{} s(A \otimes B) + p(A \otimes B) &{} \le &{} s(B) + p(B) - |T_A| ~ = ~ \mathfrak {s}(C) + |C| \\ \iff &{} s(A \otimes B) &{} \le &{} s(B) + p(B) - p(A \otimes B) - |T_A| \end{array} \end{aligned}$$

    We prove now that \(s(A \otimes B) \ge \max \{0, s(B) + p(B) - p(A \otimes B) - |T_A|\}\). By X(A) we denote the vertices of V(A) that are part of the paths in an optimal cover C for \(A \otimes B\). Let D be the cover for B which we get by removing the vertices of X(A) from C. Then, by Theorem 1 we come to the following:

    $$\begin{aligned} \begin{array}{crcl} &{} |X(A)| = nt(X(A)) + |T_A| &{} = &{} |D| - p(A \otimes B) \\ \iff &{} nt(X(A)) &{} = &{} |D| - p(A \otimes B) - |T_A| \end{array} \end{aligned}$$

    Thus, we get:

    $$\begin{aligned} \begin{array}{rcl} s(A \otimes B) - nt(X(A)) ~ = ~ \mathfrak {s}(D) &{} = &{} s(A \otimes B) - |D| + p(A \otimes B) + |T_A| \\ \iff ~~~ s(A \otimes B) &{} = &{} \mathfrak {s}(D) + |D| - p(A \otimes B) - |T_A| \\ \Rightarrow ~~~~~ s(A \otimes B) &{} \ge &{} s(B) + p(B) - p(A \otimes B) - |T_A| \end{array} \end{aligned}$$

    The implication follows, as by Lemma 3 it holds \(\mathfrak {s}(D) + |D| \ge s(B) +p(B)\).

  4. 4.

    Can be shown similar to the previous item.

  5. 5.

    To show that \(s(A \oslash B) \le s(A) + s(B)\) holds, we consider optimal covers C and D for A and B. We then build a cover E for \(A \oslash B\) in such a way that any path of C is appended by a path of D, see Lemma 1(3). Since \(|E| = p(A \oslash B)\) applies, \(s(A \oslash B) \le \mathfrak {s}(E) = \mathfrak {s}(C) + \mathfrak {s}(D) = s(A) + s(B)\) follows. This is because an optimal cover has at most as many Steiner vertices as a concrete cover. To show that \(s(A \oslash B) \ge s(A) + s(B)\) holds, we consider an optimal cover C for \(A \oslash B\). Then, we have \(s(A \oslash B) = \mathfrak {s}(C_{|A}) + \mathfrak {s}(C_{|B}) \ge s(A) + s(B)\), as \(|C_{|A}| = p(A) = p( A \oslash B)= p(B) = |C_{|B}|\), see Remark 4.

  6. 6.

    We distinguish two cases. At first, let \(s(A) > p(B) - p(A)\). To show that \(s(A \oslash B) \le s(A) + s(B) - (p(B) - p(A))\) holds, we consider optimal covers C and D for A and B. We construct a cover E for \(A \oslash B\) as follows. We first split \(p(B) - p(A)\) many paths of C at Steiner vertices as stated in Remark 3. After, we combine each of the resulting paths with a path of D. On this way, it holds that \(|E| = p(A \oslash B) = p(B)\) and therefore, \(s(A \oslash B) \le \mathfrak {s}(C) + \mathfrak {s}(D) - (p(B) - p(A)) = s(A) + s(B) - (p(B) - p(A))\). Please note, a Steiner path cover C for \(A \oslash B\) with \(\mathfrak {s}(C_{|A}) > 0\) is not optimal if \(|C_{|A}| < |C| = p(A \oslash B)\) holds. By Remark 3 a path of \(C_{|A}\) could be splitted at a Steiner vertex and the number of Steiner vertices could be reduced. To show \(s(A \oslash B) \ge s(A) + s(B) - (p(B) - p(A))\), we look at an optimal cover C for \(A \oslash B\). Thus, we have \(s(A \oslash B) = \mathfrak {s}(C) = \mathfrak {s}(C_{|A}) + \mathfrak {s}(C_{|B})\). Further, by the previous note it holds \(|C| = p(A \oslash B) = p(B) = |C_{|A}|\). By Lemma 3 we come to \(\mathfrak {s}(C_{|A}) + |C_{|A}| \ge s(A) + p(A)\). Summing up these equations, we get \(s(A \oslash B) + p(A \oslash B) = \mathfrak {s}(C_{|A}) + |C_{|A}| + \mathfrak {s}(C_{|B})\). Finally, we come to:

    $$\begin{aligned} s(A \oslash B)= & {} \mathfrak {s}(C_{|A}) + |C_{|A}| - p(A \oslash B) + \mathfrak {s}(C_{|B}) \\\ge & {} s(A) + p(A) - p(B) + \mathfrak {s}(C_{|B}) ~ \ge ~ s(A) + p(A) - p(B) + s(B) \end{aligned}$$

    The last step holds since \(p(B) = |C_{|B}|\) and by Remark 4. Now we consider the case in which \(s(A) \le p(B) - p(A)\). To show that \(s(A \oslash B) \le s(B)\) applies, we consider optimal covers C and D for A and B. We then build a cover E for \(A \oslash B\) such that we first split as many paths of C at Steiner vertices as possible in a way described in Remark 3. After this, all Steiner vertices of C have been removed and we combine each of the resulting paths with a path of D. Consequently, we get \(|E| = p(A \oslash B) = p(B)\) and thus, \(s(A \oslash B) \le \mathfrak {s}(E) = s(B)\). To show that \(s(A \oslash B) \ge s(B)\) applies, we consider an optimal cover C for \(A \oslash B\). By the above note we know that \(\mathfrak {s}(C_{|A}) = 0\), otherwise, C would not be optimal. Then, by \(|C_{|B}| = p(B)\) and by Remark 4 it follows \(s(A \oslash B) = \mathfrak {s}(C_{|B}) \ge s(B)\).

  7. 7.

    Can be shown similar to the previous item. \(\square \)

A directed co-tree can be computed in linear time from the input directed co-graph (Crespelle and Paul 2006). Combining this with Lemma 2 and 4, we come to the following result.

Theorem 2

The value of a directed Steiner path cover of minimum cost for a directed co-graph can be computed in linear time with respect to the size of the directed co-expression.

Lemma 3 allows us to minimize the following additional cost function.

Corollary 1

The value of a directed Steiner path cover C for a directed co-graph G such that \(|C| + \mathfrak {s}(C)\) is minimal can be computed in linear time with respect to the size of the directed co-expression.

3.3 Computing an optimal directed Steiner path cover

Now, we give an algorithm to compute an optimal directed Steiner path cover for a directed co-graph. The function SeriesComp, see Algorithm 1, returns an optimal Steiner path cover for a co-graph \(G = A \otimes B\), if we have some optimal covers \(C_A\) for A and \(C_B\) for B given as parameter. Let \(T_A \subseteq V(A)\) and \(T_B \subseteq V(B)\) be the sets of terminal vertices, and let further \(|T_A| \le |T_B|\). The function append(pu) used in Algorithm 1 in lines 4, 5, and 7 appends path p by vertex u. The function Combine\((p, p', u)\) used in lines 14 and 20, combines path p and \(p'\) by vertex u. The function Replace(pvu) used in line 26 removes the vertex v from path p and replaces it by vertex u. The function Insert(pvu) used in line 29 inserts vertex u between vertex v and its successor in path p.

figure l

Similar to function SeriesComp we introduce a function OrderComp, see algorithm 2, which returns an optimal directed Steiner path cover for some directed co-graph \(G = A \oslash B\), if some optimal covers for A and B are given as parameter. The function Concat\((p,p')\) used in line 16 extends path p by adding path \(p'\) at its end.

figure m

Let G be a directed co-graph represented by its binary directed co-tree \(\mathcal{T}(G)\). The function DirectedSteinerPathCover, see algorithm 3, recursively computes a directed Steiner path cover of minimum cost of the subgraph of G induced by the vertices of \(\mathcal{T}(G)\). For the series composition we assume that the left subtree \(\mathcal{T}_{x_\ell }\) of any vertex x of \(\mathcal{T}(G)\) contains no more terminal vertices than its right subtree \(\mathcal{T}_{x_r}\). Otherwise we only had to swap the children of the vertex x.

figure n

By algorithm DirecedSteinerPathCover we obtain the following result.

Theorem 3

A directed Steiner path cover of minimum cost for a directed co-graph can be computed in linear time with respect to the size of the directed co-expression.

Proof

The correctness follows by Lemma 2 and 4. The running time can be achieved by storing the paths using double-linked, linear lists, where the paths that contain Steiner vertices are stored in one set and the paths that contain no Steiner vertices are stored in another set. The lists each have a pointer to the first and last element, which are terminal vertices, and they have a pointer to the first and last Steiner vertices. Additionally, we store the number of terminal and Steiner vertices for each list. Each of the operations in Algorithm DirectedSteinerPathCover can be done in constant time.\(\square \)

4 Hamiltonian path problem

Our motivation to study the Hamiltonian Path problem comes from the problem of minimizing change-over times in pick-and-place machines in electronic industry. Before we study this problem, we recall the definition of Hamiltonian Path problem.

  • Name:   Shortest Directed Hamiltonian Path

  • Instance:   A directed graph G and edge weights \(c: E(G) \rightarrow {\mathbb R}^{\ge 0}\).

  • Task:   Find a directed Hamiltonian path P in G that minimizes \(\sum _{e\in P} c(e)\).

Pick-and-place machines have been studied for many years, see Crama et al. (2002). Often, the focus of planning problems in printed circuit board assembly is on optimizing the throughput of these pick-and-place machines to produce many printed circuit boards (short PCB) a day. We are interested in minimizing change-over times for refilling the machines.

Mid-sized companies in the electronic industry often have to produce different PCBs on one day. Each of these PCBs has to be produced in small quantities. The SMD components like capacitors, resistors, or integrated circuits are usually positioned on the boards by pick-and-place machines and are soldered in a re-flow oven. Different types of components are fed in to the machine by feeders or trays. After one type of boards has been assembled, the pick-and-place machine has to be reassembled with other types of SMD components. The time for this refilling of components is often limited by the time the last PCBs of the previous series stay in the re-flow oven. The change-over must not take too long to avoid an unnecessary downtime of the machine. Therefore, PCBs must be processed in an order that only a few component groups have to be replaced. We show how a graph model can be used to describe the problem, and we give an integer program to solve the problem.

Assume that a company produces different PCBs, and the available pick-and-place machine can be equipped with at most k different types of SMD components, since no more trays are available. At one day, n different PCBs have to be produced, that together contain m different types of SMD components \(t_1,\ldots ,t_m\). Each board \(b_i\) has to be equipped with a set \(T_i \subset \{t_1,\ldots ,t_m\}\) of component types. We represent each board by an m-tuple such that the i-th entry is one if and only if type \(t_i\) is needed to produce the board. The number of ones in such a tuple is at most k. For example, \(b_i = (0,1,0,1,0)\) means that components of type 2 and 4 must be used, types 1, 3, and 5 are not used for producing this board.

In our graph model, each board is represented by a vertex, and two vertices are connected by an edge. To change over from a board u to a board v those types have to be removed from the machine, that are used by u but not used by v. Those types, that are not used by u but are used by v have to be inserted in to the machine. Thus, the cost of change over is described by the hamming distance of u and v.

Example 1

Given \(n=4\) jobs \(b_1=(1,0,0,0)\), \(b_2=(0,1,0,1)\), \(b_3=(1,0,1,0)\), and \(b_4=(1,1,1,0)\). The resulting graph contains the artificial vertex \(b_0=(0,0,0,0)\) to represent the initial state of the machine, where all trays are empty. It is shown in Fig. 6 together with the edge costs. To minimize the overall change-over time is to produce the boards in the order \(b_1=(1,0,0,0)\), \(b_3=(1,0,1,0)\), \(b_4=(1,1,1,0)\), and \(b_2=(0,1,0,1)\) which sum up to 6.

Fig. 6
figure 6

Graph modeling 4 boards. The dashed lines show a Hamiltonian path that defines an order in which the change-over times sum up to 9

A shortest Hamiltonian path that starts at the artificial vertex representing the initial state of the machine minimizes the overall change-over time. In the next section we give a mixed integer program to solve the problem.

4.1 Integer program

In the following we assume that a directed graph \(G=(V,E,c)\) is given, where V denotes the set of vertices, E denotes the set of edges, and c denotes a function that assigns the cost c(e) to edge e. We represent an undirected graph \(G'\) by a directed graph G in such a way that each edge \(e=\{u,v\}\) of \(G'\) is represented by two anti-parallel edges \(e_1=(u,v)\), \(e_2=(v,u)\) in G, and the weight of the new edges are \(c(e_1)=c(e_2)=c(e)\).

Our integer program is based on the following idea. Choose a vertex as the first vertex on the path. This is typically an artificial vertex that represents the empty machine at the beginning of a day. The order of vertices on the path implies for two vertices u and v following immediately on the path, that the cost of edge (uv) represents the change-over time.

Our integer program uses binary variables x[ij], \(i,j \in [n]\), such that x[ij] is equal to one, if and only if edge (ij) is on the Hamiltonian path. We use integer valued variables p[i], \(i \in [n]\), such that \(p[i] = \ell \), if and only if vertex i is the \(\ell \)-th vertex on the Hamiltonian path.

The first constraints ensure that the path is simple, i.e., each vertex is contained at most once in the path. The path leaves each vertex i at most once, and the path enters each vertex j at most once, which can be stated as follows.

$$\begin{aligned} \sum _{(i,j) \in E} x[i,j] \le 1 ~~ \forall i \in V \quad \quad \text{ and } \quad \quad \sum _{(i,j) \in E} x[i,j] \le 1 ~~ \forall j \in V \end{aligned}$$

The following constraints prevent loops over anti-parallel edges. If there are anti-parallel edges then choose at most one of them. This is ensured by the constraints:

$$\begin{aligned} x[i,j] \le 1 - x[j,i] ~~ \forall (i,j) \in E: (j,i) \in E \end{aligned}$$

The length (number of edges) of each Hamiltonian path is \(n-1\):

$$\begin{aligned} \sum _{(i,j) \in E} x[i,j] = n-1 \end{aligned}$$

With just this constraint on the number of edges, we may accept solutions that are not connected. It is still possible to have cycles and a path instead of one path only. The next constraints are used to avoid cycles. If edge (ij) belongs to the path then \(p[j] - p[i] = 1\) must hold true:

$$\begin{aligned} \begin{array}{crcl} &{} -n + (n+1) \cdot x[i,j] &{} \le &{} p[j] - p[i] ~~ \forall (i,j) \in E \\ \text{ and } &{} p[j] - p[i] &{} \le &{} n - (n-1) \cdot x[i,j] ~~ \forall (i,j) \in E \end{array} \end{aligned}$$

The objective is to minimize the sum of the change-over times, i.e. to make the path length as small as possible, so we have to minimize the objective function \(\sum _{(i,j) \in E} c[i,j] \cdot x[i,j]\).

Alternatively, if we want to minimize the largest change-over time, we have to introduce another integer valued variable z and the following constraints:

$$\begin{aligned} c[i,j] \cdot x[i,j] \le z ~~ \forall (i,j) \in E \end{aligned}$$

The objective function then is to minimize the largest change-over time z.

4.2 Experimental results

We ran the mixed integer program on CPLEX 12.8.0 using default settings on some standard PC with Intel i7, 2.80 GHz CPU and 16 GB RAM running Ubuntu 18.10 as operating system.

Running times (in seconds) to solve the Hamiltonian Path problem on randomly generated tournaments with randomly generated edge weights are shown in Table 3. The running times are average values over 10 runs. Bold numbers represent times when the time limit of 1800 seconds, which is equal to 30 minutes, was reached at least once and at most twice. For the calculation of the average and standard deviation, we assumed that the run times of the aborted jobs were 1800 seconds. It is well-known that any tournament on a finite number of vertices contains a Hamiltonian path (Rédei 1934).

Table 3 Running times and standard deviation of MIP to calculate Hamiltonian Path on randomly generated tournaments

Running times (in seconds) to solve the Hamiltonian Path problem on complete bipartite graphs \(K_{n,m}\) with unit edge weights for different values of n and m are shown in Table 4. The complete bipartite graph \(K_{n,m}\) contains a Hamiltonian path if and only if \(|n-m| \le 1\). The absence of such a path can be detected very quickly by the MIP as can be seen in the columns \(m=n+2\). All complete bipartite graphs are co-graphs so these results show that our algorithm with running time linear in the input size would be very much faster than the integer program. Running times of more than 30 minutes are indicated by a bar −.

Table 4 Running times of MIP to calculate Hamiltonian Path on complete bipartite graphs \(K_{n,m}\) for different values of n and m

5 Steiner path problem

5.1 Integer program

We assume that a directed graph \(G=(V,E,c)\) is given, where V denotes the set of vertices, E denotes the set of edges, and c denotes a function that assigns the cost c(e) to edge e. Further, we have a set \(T\subseteq V\) of terminal vertices. We represent an undirected graph \(G'\) by a directed graph G in such a way that each edge \(e=\{u,v\}\) of \(G'\) is represented by two anti-parallel edges \(e_1=(u,v)\), \(e_2=(v,u)\) in G, and the weight of the new edges are \(c(e_1)=c(e_2)=c(e)\).

In addition to the variables used in the MIP of Sect. 4.1 we use additional binary variables y[i], \(i \in [n]\), such that y[i] is equal to one, if and only if vertex i is contained in the path. These variables are only used to simplify the formulation of the following constraints 1 and 2. As in the last section, our integer program uses binary variables x[ij], \(i,j \in [n]\), such that x[ij] is equal to one, if and only if edge (ij) is on the Steiner path. We use integer valued variables p[i], \(i \in [n]\), such that \(p[i] = \ell \), if and only if vertex i is the \(\ell \)-th vertex on the Steiner path. Each terminal vertex i has to be contained in the path.

$$\begin{aligned} p[i] \ge 1 ~~ \forall i \in T \end{aligned}$$

If \(x[i,j] = 1\) then i and j lie on the path, i.e. if \(x[i,j] = 1\) then \(p[i] \ne 0\) and \(p[j] \ne 0\) which can be stated by the following constraints.

$$\begin{aligned} x[i,j] \le p[i] ~~ \forall (i,j) \in E \quad \quad \text{ and } \quad \quad x[i,j] \le p[j] ~~ \forall (i,j) \in E \end{aligned}$$

If there are anti-parallel edges then choose at most one of them. This is ensured by the constraints:

$$\begin{aligned} x[i,j] \le 1 - x[j,i] ~~ \forall (i,j) \in E: (j,i) \in E \end{aligned}$$

It holds \(y[i] = 1\) if and only if \(p[i] > 0\), i.e. \(y[i] = 1\) if and only if vertex i is on the path.

$$\begin{aligned} y[i] \le p[i] ~~ \forall i \in V \quad \quad \text{ and } \quad \quad p[i] \le n \cdot y[i] ~~ \forall i \in V \end{aligned}$$

The path has to contain one edge less than vertices.

$$\begin{aligned} \sum _{(i,j) \in E} x[i,j]= & {} \sum _{i \in V} y[i] - 1 \end{aligned}$$
(1)

If \(p[i] \ne 0\) then there has to be a \(x[i,j] = 1\) or \(x[j,i] = 1\).

$$\begin{aligned} p[i] \le n \cdot \left( \sum _{(i,j) \in E} x[i,j] + \sum _{(j,i) \in E} x[j,i]\right) ~~ \forall i \in V \end{aligned}$$

For each vertex \(i \in V-T\) contained in the path we have one in-going and one out-going edge.

$$\begin{aligned} \sum _{(i,j) \in E} x[i,j] = y[i] ~~ \forall i \in V-T&\text{ and }&\sum _{(i,j) \in E} x[i,j] = y[j] ~~ \forall j \in V-T \end{aligned}$$
(2)

The path leaves each vertex \(i \in T\) at most once and it enters each vertex \(j \in T\) at most once.

$$\begin{aligned} \sum _{(i,j) \in E} x[i,j] \le 1 ~~ \forall i \in T \quad \quad \text{ and } \quad \quad \sum _{(i,j) \in E} x[i,j] \le 1 ~~ \forall j \in T \end{aligned}$$

Finally, to avoid cycles, if edge (ij) belongs to the path then \(p[j] - p[i] = 1\) must hold true:

$$\begin{aligned} \begin{array}{crcl} &{} -n + (n+1) \cdot x[i,j] &{} \le &{} p[j] - p[i] ~~ \forall (i,j) \in E \\ \text{ and } &{} p[j] - p[i] &{} \le &{} n - (n-1) \cdot x[i,j] ~~ \forall (i,j) \in E \end{array} \end{aligned}$$

The objective is to minimize the sum of edge-weights \(\sum _{(i,j) \in E} c[i,j] \cdot x[i,j]\). For graphs with unit edge weights this objective function results in a Steiner path with least number of Steiner vertices.

5.2 Experimental results

Running times (in seconds) to solve Steiner Path problems on complete bipartite graphs \(K_{n,3n}\) for different values of n and t many randomly chosen terminal vertices are shown in Table 5. As long as at most n vertices from the second set are selected as terminal vertices, a Steiner path exists. The absence of such a path can be detected very quickly by the MIP as can be seen in the columns \(t=2n\).

Table 5 Running times and standard deviation to solve Steiner path problems on complete bipartite graphs \(K_{n,3n}\) for different values of n and t

The running times (in seconds) are average values over 10 runs, bold numbers represent times when the time limit of 1800 seconds, which is equal to 30 minutes, was reached at least once and at most twice. If the time limit was exceeded in more than half of the measurements, we marked this with a bar −. When calculating the average and the standard deviation, we assumed 1800s for the measurements that were stopped due to timeout.

6 Steiner path cover problem

6.1 Integer program

To simplify the objective function and the IP we modify the given directed graph \(G=(V,E,c)\) in such a way, that a new vertex s as source and another new vertex \(t \ne s\) as sink is added. \(T\subseteq V\) is the set of terminal vertices with \(T\ne \emptyset \). Vertex s gets edges to each terminal vertex and each terminal vertex gets an edge to the sink t as shown in Fig. 7.

Fig. 7
figure 7

Graph extended by a source and a sink to compute an optimal Steiner Path Cover

The additional edges get a weight of \(M := |V| \cdot |E|\) if the graph has no edge weights, otherwise \(M := |V| \cdot C\) where \(C := \max _{e \in E}\{ c(e) \}\) denotes the maximum edge weight of the graph. More formally, we get graph \(G'=(V',E',c')\) such that \(V' := V \cup \{s,t\}\), \(E' := E \cup \{(s,v) \mid v \in T\} \cup \{(v,t) \mid v \in T\}\), and \(c'(e) = c(e)\) for each \(e \in E\) and \(c'(e) = M\) if \(e=(s,v)\) or \(e=(v,t)\) for some \(v \in T\). If graph G has no edge weights then \(c'(e) = 1\) for each \(e \in E\), if G is an undirected graph we replace each edge \(\{u,v\} \in E\) by two anti-parallel edges (uv) and (vu), the weight of them are taken from the corresponding undirected edges.

As in the last section, our integer program uses binary variables y[i], \(i \in [n]\), such that y[i] is equal to one, if and only if vertex i is contained in one of the paths. Binary variables x[ij], \(i,j \in [n]\), are used such that x[ij] is equal to one, if and only if edge (ij) is on the Steiner path cover. We use integer valued variables p[i], \(i \in [n]\), such that \(p[i] = \ell \), if and only if vertex i is the \(\ell \)-th vertex on one of the paths of the Steiner path cover.

The number of paths in the cover is equal to \(\sum _{(s,v) \in E'} x[s,v]\), and since there is at least one path in a Steiner Path Cover for G if \(T \ne \emptyset \), the following conditions must hold true:

$$\begin{aligned} \sum _{(s,v) \in E'} x[s,v]> 0 \quad \quad \text{ and } \quad \quad \sum _{(v,t) \in E'} x[v,t] > 0 \end{aligned}$$

Because we have inserted artificial vertices, the path leaves each terminal vertex i exactly once, and the path enters each terminal vertex j exactly once, which can be stated as follows.

$$\begin{aligned} \sum _{(i,j) \in E'} x[i,j] = 1 ~~ \forall i \in T \quad \quad \text{ and } \quad \quad \sum _{(i,j) \in E'} x[i,j] = 1 ~~ \forall j \in T \end{aligned}$$

Since non-terminal vertices need not be contained in the path, the path leaves each non-terminal vertex i at most once, and the path enters each non-terminal vertex j at most once, which can be stated as follows.

$$\begin{aligned} \sum _{(i,j) \in E} x[i,j] \le 1 ~~ \forall i \in V-T \quad \quad \text{ and } \quad \quad \sum _{(i,j) \in E} x[i,j] \le 1 ~~ \forall j \in V-T \end{aligned}$$

The previous two conditions also ensure that paths are vertex-disjoint. If a path contains a non-terminal vertex j then there has to be an edge that enters j and an edge that leaves j, which can be stated as:

$$\begin{aligned} \sum _{(i,j) \in E} x[i,j] = \sum _{(j,k) \in E} x[j,k] ~~ \forall j \in V-T \end{aligned}$$

If there are anti-parallel edges then choose at most one of them. This is ensured by the constraint:

$$\begin{aligned} x[i,j] \le 1 - x[j,i] ~~ \forall (i,j) \in E: (j,i) \in E \end{aligned}$$

Each terminal vertex i has to be contained in one of the paths.

$$\begin{aligned} p[i] \ge 1 ~~ \forall i \in T \end{aligned}$$
Table 6 Running times and standard deviation to solve Steiner path cover problems on complete bipartite graphs \(K_{n,3n}\) for different values of n and t

If \(x[i,j] = 1\) then i and j lie on one of the paths, i.e. if \(x[i,j] = 1\) then \(p[i] \ne 0\) and \(p[j] \ne 0\) which can be stated by the following constraints.

$$\begin{aligned} x[i,j] \le p[i] ~~ \forall (i,j) \in E \quad \quad \text{ and } \quad \quad x[i,j] \le p[j] ~~ \forall (i,j) \in E \end{aligned}$$

If \(p[i] \ne 0\) then there has to be a \(x[i,j] = 1\) or \(x[j,i] = 1\), which is stated by the following constraints.

$$\begin{aligned} p[i] \le n \cdot \left( \sum _{(i,j) \in E} x[i,j] + \sum _{(j,i) \in E} x[j,i]\right) ~~ \forall i \in V \end{aligned}$$

Finally, to avoid cycles, if edge (ij) belongs to one of the paths then \(p[j] - p[i] = 1\) must hold true:

$$\begin{aligned} \begin{array}{crcl} &{} -n + (n+1) \cdot x[i,j] &{} \le &{} p[j] - p[i] ~~ \forall (i,j) \in E \\ \text{ and } &{} p[j] - p[i] &{} \le &{} n - (n-1) \cdot x[i,j] ~~ \forall (i,j) \in E \end{array} \end{aligned}$$

Minimizing an objective function like \(\sum _{v \in T} x[s,v]\) would only minimize the number of paths. If there are two different path covers of minimum size, we have to select that one with the least number of Steiner vertices. By our definition of the costs of the additional edges from s to terminal vertices, we can choose the same objective function as above, when we also take care of the additional edges, see the right part in Fig. 7. Each path would add at least \(2 \cdot M\) to the value of the objective function. The objective is to minimize the sum of edge-weights \(\sum _{(i,j) \in E'} c[i,j] \cdot x[i,j]\). For graphs with unit edge weights this objective function results in a Steiner path cover P of minimum size, with least number of Steiner vertices \(\sum _{p \in P} \sum _{e \in p} c(e)\).

Table 7 Running times and standard deviation to solve Steiner path cover problems on randomly generated co-graphs with n vertices and t many randomly chosen terminal vertices

6.2 Experimental results

Running times (in seconds) to solve Steiner Path Cover problems on complete bipartite graphs \(K_{n,3n}\) for different values of n and t many randomly chosen terminal vertices are shown in Table 6. The running times are average values over 10 runs, bold numbers represent times when the time limit was reached at least once and at most twice. If the time limit was exceeded in more than half of the measurements, we marked this with a bar −. When calculating the average and the standard deviation, we assumed 1800s for the measurements that were stopped due to timeout.

Table 7 shows running times to solve Steiner Path Cover problems on randomly generated co-graphs with n vertices and t many randomly chosen terminal vertices.

The co-graphs will be generated recursively as shown in Algorithm 4. The probability p is set to be \(\nicefrac {1}{3}\) in our experiments.

figure o

7 Conclusion

In this paper we considered the directed Steiner path cover problem for directed co-graphs. We could show a linear time solution for computing the minimum number of paths within a directed Steiner path cover and the minimum number of Steiner vertices in such a directed Steiner path cover in directed co-graphs. The results allowed us to give an algorithm which constructs a directed Steiner path cover of minimum cost for a directed co-graph in linear time. This leads to a linear time computation of an optimal directed Steiner path, if it exists, for directed co-graphs.

Undirected co-graphs can be generated starting with the single vertex graph by disjoint union and join operations, see Corneil et al. (1981). For an undirected co-graph G, we can solve the Steiner path cover problem in linear time by the following transformation. We replace every edge \(\{u,v\}\) of G by two directed edges (uv) and (vu) and apply our solution for directed co-graphs. This reproves our result of Gurski et al. (2020).

The directed Hamiltonian path problem can be solved by an XP-algorithm w.r.t. the parameter directed clique-width (Ganian et al. 2013). Since directed co-graphs have directed clique-width at most two (Gurski et al. 2016) a polynomial time solution for the directed Hamiltonian path problem follows. Such an algorithm is also given in Gurski (2017). A directed Hamiltonian path exists if and only if we have \(T=V(G)\) and \(p(G)=1\). Thus, our results lead to the first linear time algorithm for the directed Hamiltonian path problem on directed co-graphs, which is a generalization of the known results for undirected co-graphs of Lin et al. (1995).

In our future work we want to investigate whether these results can be carried over to other graph classes such as chordal graphs, interval graphs, or distance-hereditary graphs.