1 Introduction

The clique problem is among the most prominent combinatorial optimization problems studied in the literature. The interest in this problem is closely related to its importance for the modelling of applied optimization tasks, such as scheduling problems. In order to solve practical problems involving cliques efficiently, it is usually important to exploit the structure of the underlying graph as far as possible. Being able to recognize special cases of the problem from the incidence structure of the graph can beneficially inform both the choice of the optimization model used and the derivation of fast solution algorithms.

In this work, we focus on recognizing a polynomially solvable subcase of the problem to find an m-clique in an m-partite graph. This special case is called staircase compatibility and allows for totally unimodular linear programming formulations of polynomial size, see [5]. In particular, the formulation based on a dual network flow problem introduced there has shown to be very efficient for solving problems arising in different applications, such as railway timetabling [9] and flow problems with piecewise-linear arc costs [18]. In [16], the authors present a new formulation for the vertex colouring problem based on the same dual-flow formulation and show that it is computationally superior on many instances. Although their instances do not have the staircase property, a reformulation trick allows them to use this formulation anyway. From these successes, there arises a natural interest to analyse the problem of recognizing the staircase property on a given graph \( G = (V, E) \) and to generalize the applicability of the aforementioned dual-flow formulation for the clique problem.

At its core, staircase recognition reduces to finding suitable total orders on the subsets of a partition of the vertex set of the given graph. By way of incidence matrices, staircase compatibility gives an alternate approach to so-called staircase matrices. Namely, a bipartite graph has the staircase property if its adjacency matrix can be re-sorted into completely dense staircase form, i.e. the nonzeros in each row form a single block which is not interrupted by zero elements. This way, staircase compatibility is closely linked to staircase matrices, which are an important structure in applied mathematics, cf. [12]. These matrices are the natural target form to be achieved when performing Gauss–Jordan elimination and allow for efficient algorithms to solve linear programs (see [10, 11]), to name only two examples.

Constructing conflict graphs from constraint matrices is one possible way to find clique structures in mixed-integer programming problems, see e.g. [1, 4]. Apart from staircase compatibility, m-cliques in an m-partite graph can also be found in polynomial time if the so-called dependency graph is a forest; see [7], where the problem is studied under the name clique problem with multiple-choice constraints. For polyhedral properties of the general clique problem, we refer to [2, 3].

Contribution We study the problem to recognize the staircase property in a graph with a given m-partition of the vertex set. For bipartite graphs, we can check the staircase property in time \( {\mathcal {O}}(|V | + |E |) \), and the algorithm can easily be extended to the case of m-partite graphs. Furthermore, we can give a full characterization of staircase orderings on bipartite graphs by showing that they are unique up to a few trivial operations on the two total orders they consist of. We will see that on bipartite graphs, the computation of staircase orderings is possible in polynomial time as well. For this task, we derive an algorithm which runs in \( {\mathcal {O}}(|V_1 |(|V | + |E |)) \), where \( V_1 \) can be chosen to be the smaller of the two bipartite subsets of vertices. This scheme can be extended to a polynomial-time algorithm for computing staircase orderings on m-partite graphs where all bipartite subgraphs \( G_{ij}\) induced by two subsets \( V_i \) and \( V_j \) of the partition are connected. For general m-partite graphs, however, we show that deciding existence of a staircase ordering is NP-complete. Finally, if a given ordering is not staircase on an m-partite graph, it is possible to establish the staircase property by adding new edges to the graph. The problem of finding an ordering with the minimal number of edges to be added to the graph such that the staircase property is fulfilled is NP-complete even in the case that all bipartite subgraphs \( G_{ij} \) are connected.

These results are particularly interesting because in many cases they allow for efficiently checking practical instances of m-clique problems on m-partite graphs for the staircase property of their underlying graph. Such staircase-sortable instances are then solvable in polynomial time as a linear program (LP), as shown in [5]. In a computational study, we will demonstrate that our algorithms can efficiently check for the staircase property. In the first part, we compare our algorithms to an MIP-based approach. Although it outperforms our Python-based algorithms in pure model solving times, it cannot compete if model building times are included and with increasing instance size the model building times become prohibitively long for practical use. The second part evaluates our algorithms’ runtime behaviour on larger instances with different characteristics. Finally, for instances from a real-world timetabling problem we will see that first sorting them to be staircase and then solving them via the formulation from [5] are significantly faster than using a generic model for the timetabling problem.

Structure The remainder of the article is organized as follows. Section 2 introduces the required notation and definitions to describe the problem. We then focus on bipartite graphs in Sect. 3, where we consider the recognition and uniqueness of staircase orderings and present a polynomial-time algorithm to compute staircase orderings. In Sect. 4, we extend our results to m-partite graphs. We first show that recognizing staircase partitions is NP-complete in general. Then, we present an algorithm to solve the problem in a polynomially solvable special case. Finally, we present the results of our computational experiments on artificial instances as well as real-world instances from a railway timetabling application in Sect. 5 and end with a short outlook in Sect. 6.

2 Notation

The following notation is used throughout the article. We denote the neighbourhood of a vertex \( u \in V \) in an undirected graph \( G = (V,E) \) by \( N_G(u) {:}{=}\{ v \in V \mid \{u, v\} \in E\} \), and for subsets \( U \subseteq V \) we analogously write \( N_G(U) {:}{=}\bigcup _{u \in U} N_G(u)\). If it is clear which graph is referred to, we may simply write N(u) . The following definition introduces some fundamental graph structures related to the clique problem on m-partite graphs.

Definition 2.1

(Subgraphs \( G_{ij}\) and Dependency Graph \( {\mathcal {G}}\)) Let \( G = (V, E) \) be an m-partite graph together with a partition \( {\mathcal {V}}= \{V_1, \ldots , V_m\} \) of V. For two subsets \( V_i, V_j \in {\mathcal {V}}\) with \( i \ne j \), we write

$$\begin{aligned} G_{ij}{:}{=}(V_i \cup V_j, E_{ij}) \end{aligned}$$

for the subgraph of G induced by \( V_i \cup V_j \), where \( E_{ij}\) is the corresponding edge set. Note that all subgraphs \( G_{ij}\) are bipartite.

Further, we call \( {\mathcal {G}}= ({\mathcal {V}}, {\mathcal {E}}) \) with

$$\begin{aligned} {\mathcal {E}}{:}{=}\left\{ \{V_i, V_j\} \subseteq {\mathcal {V}}\mid i \ne j \wedge \exists u \in V_i :N_{G_{ij}}(u) \ne V_j\right\} \end{aligned}$$

the dependency graph of G. Note that \( \{V_i, V_j\} \in {\mathcal {E}}\) is equivalent to \( G_{ij}\) not being a complete bipartite graph.

The main focus of our work is the so-called staircase property of a graph together with an m-partition of its vertex set and total orders on each subset. This property establishes local structures on all bipartite subgraphs \( G_{ij}\) and can be used to efficiently solve certain clique problems on the graph. It is defined as follows.

Definition 2.2

(Ordering, Staircase Ordering) Let \( G = (V, E) \) be an m-partite graph, together with a partition \( {\mathcal {V}}= \{V_1, \ldots , V_m\} \) of V and total orders \( >^i \) on the subsets \( V_i \in {\mathcal {V}}\). For simplicity, we refer to the set \( \{>^1, \ldots , >^m\} \) of total orders as an ordering on G if \({\mathcal {V}}\) is clear from the context. An ordering on G is staircase if for all subgraphs \( G_{ij}\) of G the following two conditions hold:

$$\begin{aligned} u \in V_i \wedge v_1>^j v_2 >^j v_3 \in V_j \wedge \{u, v_1\}, \{u, v_3\} \in E_{ij}&\Rightarrow \{u, v_2\} \in E_{ij}\end{aligned}$$
(SC1)
$$\begin{aligned} u_1>^i u_2 \in V_i \wedge v_1 >^j v_2 \in V_j \wedge \{u_1, v_2\}, \{u_2, v_1\} \in E_{ij}&\Rightarrow \{u_1, v_1\}, \{u_2, v_2\} \in E_{ij}. \end{aligned}$$
(SC2)

Condition (SC1) ensures that the neighbourhoods of all vertices are continuous with respect to the total order, whereas (SC2) yields a kind of monotonicity on the edge set \( E_{ij} \). The two conditions are illustrated in Fig. 1.

Fig. 1
figure 1

Illustration of the two staircase conditions. If the solid edges are contained in the graph, the dashed ones must be contained as well

Note that both (SC1) and (SC2) are trivially satisfied for complete bipartite subgraphs \( G_{ij}\). Therefore, it is sufficient for the ordering on G to be staircase if they hold for those \( G_{ij} \) with \( \{V_i, V_j\} \in {\mathcal {E}}\). From the definition it also immediately follows that removing vertices from the graph does not destroy the staircase property. Moreover, it is easy to show that (SC1) is implied by (SC2) if \( G_{ij}\) does not contain vertices with degree zero. In the next definition, we canonically extend the notion of a staircase ordering onto a partition \( {\mathcal {V}}\) on V.

Definition 2.3

(Staircase Partition) Let \( G = (V, E) \) be an m-partite graph with vertex set partition \( {\mathcal {V}}= \{V_1, \ldots , V_m\} \). We call \( {\mathcal {V}}\) staircase if there exists a staircase ordering on \( {\mathcal {V}}\).

We close this section with some notation used to more concisely describe our algorithms.

Definition 2.4

(Minimal and Maximal Neighbour Index) Let \( G = (V_1 \cup V_2, E) \) be a bipartite graph with total orders \(>^1, >^2 \) on \( V_1 \) and \( V_2 \), respectively, and \( u \in V_1 \) with \( \deg _G(u) \ge 1 \). We write

$$\begin{aligned} \underline{\lambda }_{V_2}(u)&{:}{=}|\{v \in V_2 \mid w>^2 v\, \forall w \in N_G(u)\} | + 1\\ \overline{\lambda }_{V_2}(u)&{:}{=}|V_2 | - |\{v \in V_2 \mid v >^2 w\, \forall w \in N_G(u)\} | \end{aligned}$$

for the minimal and maximal index of any neighbour of u in \( V_2 \) in the total order \( >^2 \).

3 Staircase Orderings on Bipartite Graphs

We start by presenting our results for staircase orderings on bipartite graphs. First, we state an algorithm to determine within polynomial time whether a given ordering on a bipartite graph is staircase. In Sect. 3.2, we then show that a staircase ordering on a bipartite graph is unique up to some easily recognizable situations where the order of vertices may be changed. Finally, an algorithm to compute a staircase ordering on a given bipartite graph within polynomial time is given in Sect. 3.3.

3.1 Recognizing Staircase Orderings

In this section, we will give a polynomial-time algorithm to verify whether a given ordering on a bipartite graph is staircase. Let \( G = (V_1 \cup V_2, E) \) with \( V_1 = \{u_1, \ldots , u_{|V_1 |}\} \) and \( V_2 = \{v_1, \ldots , v_{|V_2 |}\} \) be a bipartite graph and consider the canonical ordering on G, which fulfils the following two conditions:

$$\begin{aligned} u_i, u_j \in V_1 \wedge i> j \Rightarrow u_i>^1 u_j \qquad \text {and} \qquad v_i, v_j \in V_2 \wedge i> j \Rightarrow v_i >^2 v_j. \end{aligned}$$

We are able to show that it can be checked in polynomial time whether the canonical ordering on G is staircase.

Lemma 3.1

It can be checked in time \( O(|V | + |E |) \) whether the canonical ordering \( \{>^1, >^2\} \) on a bipartite graph \(G = (V_1 \cup V_2, E)\) with \( V_1 = \{u_1, \ldots , u_{|V_1 |}\} \) and \( V_2 = \{v_1, \ldots , v_{|V_2 |}\} \) is staircase.

Proof

Note that for any triplet \( u_i, u_j, u_k \in V_1 \), \( i> j > k \) where \( u_i \) and \( u_k \) are in the same connected component C of G, Condition (SC1) ensures that \( u_j \) is in C as well. In other words, all vertices belonging to C appear consecutively in their respective total orders \( >^1 \) and \( >^2 \), which can be verified in time \( O(|V | + |E |) \) using breadth-first search. Apart from this requirement, the staircase conditions can be checked on each connected component of G separately. For the remainder of the proof, we therefore assume w.l.o.g. that G is connected. The following algorithm then solves the problem.

figure a

In Line 1, we directly check (SC1) for the neighbourhood of \(u_1\). Afterwards, we check (SC1) for all remaining \( u_i \in V_1 \) and (SC2) for certain key vertices of G. The following case distinction shows that these checks indeed suffice to decide whether the canonical ordering on G is staircase:

  • Case 1—The canonical ordering on G fulfils (SC1) and (SC2): It is clear that the checks in Lines 1 and 8 do not fail as they are equivalent to (SC1) with the vertex \(u \in V_1\). Suppose there exists some \(i \in \{2,\ldots ,|V_1 |\}\) such that \(k {:}{=}\underline{\lambda }_{V_2}(u_i) < j {:}{=}\underline{\lambda }_{V_2}(u_{i-1})\), i.e. we fulfil the first condition of the check in Line 5. Then we have \(u_i >^1 u_{i-1}\) as well as \(v_j >^2 v_k\). Moreover, we have \(\{u_i, v_k\}, \{u_{i-1}, v_j\} \in E\) and \(\{u_{i-1}, v_k\} \notin E\) by definition of \(\underline{\lambda }_{V_2}(u_{i-1})\). However, this is a contradiction to (SC2). Therefore, \(\underline{\lambda }_{V_2}(u_i) \ge \underline{\lambda }_{V_2}(u_{i-1})\) must hold for all \(i \in \{2,\ldots ,|V_1 |\}\). Analogously, we get that \(\overline{\lambda }_{V_2}(u_i) \ge \overline{\lambda }_{V_2}(u_{i-1})\) holds for all \(i \in \{2,\ldots ,|V_1 |\}\). Hence, Algorithm 1 works correctly if the canonical ordering is staircase.

  • Case 2a—The canonical ordering violates (SC2): In this case, we can find \(u_{i_1}, u_{i_2} \in V_1, v_{j_1}, v_{j_2} \in V_2\) with \(i_1< i_2, j_1 < j_2\) and edges \( \{u_{i_1}, v_{j_2}\}, \{u_{i_2}, v_{j_1}\} \in E\) such that \(\{u_{i_1}, v_{j_1}\} \notin E\) or \(\{u_{i_2}, v_{j_2}\} \notin E\) holds. Assume we have \(\{u_{i_2}, v_{j_2}\} \notin E\). If \(N_G(u_{i_2})\) contains a vertex \(v_{j_3}\) with \(j_2 < j_3\), the check in Line 8 will fail for \(i = i_2\). Therefore, we can assume w.l.o.g. \(j_1 \le \overline{\lambda }_{V_2}(u_{i_2}) < j_2\). However, this immediately yields \(j_1 \le \overline{\lambda }_{V_2}(u_{i_2}) < j_2 \le \overline{\lambda }_{V_2}(u_{i_1})\). Hence, for some \(i \in \{i_1 + 1,\ldots ,i_2\}\) the check in Line 5 fails. Analogously, either of the checks in Line 5 or 8 must fail if \( \{u_{i_1}, v_{j_1}\} \notin E \).

  • Case 2b—The canonical ordering violates (SC1): If we find \(u \in V_1, v_1, v_2, v_3 \in V_2\) such that (SC1) is violated, the check in Line 1 or the one in Line 8 fails, depending on whether \(u = u_1\) or \(u \in \{u_2,\ldots ,u_{|V_1 |}\}\). Otherwise, we can find \(u_1, u_2, u_3 \in V_1, v \in V_2\) such that (SC1) is violated, i.e. we have \(\{u_1, v\}, \{u_3, v\} \in E\), while \(\{u_2, v\} \notin E\). However, the vertex \(u_2\) is by assumption not isolated and we can therefore find some vertex \(v' \in V_2\) with \(\{u_2, v'\} \in E\). As Fig. 2 illustrates, the quadruple \(u_1, u_2, v', v\) violates (SC2) if \(v >^2 v'\) and the quadruple \(u_2, u_3, v, v'\) violates (SC2) if \(v' >^2 v\). In both cases, we can apply the arguments used in Case 2a.

To summarize, Algorithm 1 correctly recognizes a canonical ordering which is staircase. For non-staircase canonical orderings, at least one of the checks performed in Lines 1, 5 and 8 fails. The running time of Algorithm 1 is \( O(|V | + |E |) \), as the computation of all \( \underline{\lambda }_{V_2}(u) \) and \( \overline{\lambda }_{V_2}(u) \) as well as the preprocessing step to identify the connected components of G can be done within this amount of time. \(\square \)

Fig. 2
figure 2

Illustration of the reduction of Case 2b to Case 2a

3.2 Uniqueness of Staircase Orderings

We have just established that recognizing whether an ordering on a bipartite graph is staircase can be done in polynomial time. Now we will further analyse the structure of staircase orderings on bipartite graphs. More precisely, we will show that it is possible to explicitly state all operations under which a given ordering remains staircase. This gives a full characterization of staircase orderings on bipartite graphs as they are indeed unique up to the following three operations.

First, it is easy to see that w.r.t. an ordering being staircase we can consider each connected component separately. Second, if we have \(N_G(u) = N_G(v)\) for a pair of vertices in a given subset \( V_i \), we can remove one of the two, consider the remaining graph and reinsert the removed vertex at the end. We will therefore assume, for a given bipartite graph \(G = (V_1 \cup V_2, E)\), the following two conditions w.l.o.g.:

$$\begin{aligned}&G \text { is connected} \end{aligned}$$
(1)
$$\begin{aligned}&(\forall u, v \in V, u \ne v)\, N_G(u) \ne N_G(v). \end{aligned}$$
(2)

Third, any given staircase ordering remains staircase if all total orders are reversed at once, to which we refer as reversing the ordering. Note that this also holds for staircase orderings on m-partite graphs with \( m > 2 \). Theorem 3.1 states that a given staircase ordering on a bipartite graph is in fact unique up to the operations described above. Before proving Theorem 3.1, which is the main result of this section, the following auxiliary lemma gives additional insight on the lengths of shortest paths in bipartite graphs with a given staircase ordering.

Lemma 3.2

Let G be a connected bipartite graph together with a staircase ordering \( \{>^1, >^2\} \). Further, let \( u, v, w \in V_1 \) such that \(u>^1 v >^1 w\), and let \( P_{uv} \) and \( P_{uw} \) be shortest paths between u and v or w, respectively. Then \( |P_{uv} | \le |P_{uw} | \).

Proof

We prove the claim by contradiction. Suppose there exist vertices \(u, v, w \in V_1\) with \(u>^1 v >^1 w\) and shortest paths \( P_{uv} = (u, \ldots , v), P_{uw} = (u, \ldots , w', x, w) \) such that \( |P_{uv} | > |P_{uw} | \). Note that \( P_{uw} \) is of even length greater than or equal to two, as G is bipartite. We distinguish two cases:

  • Case 1—\(w' >^1 v\): If we have \(v \in N(x)\), then \(P_{uv}\) is no longer a shortest path since we can construct a shorter one by simply choosing v instead of w as the last vertex in \(P_{uw}\). Hence, \(v \notin N(x)\) must hold. However, Fig. 3 illustrates that this already violates (SC1).

  • Case 2—\(v >^1 w'\): In this case, we have \(u>^1 v >^1 w'\) and \(|P_{uv} | > |P_{uw'} | = |P_{uw} | - 2\). Therefore, we can start from the beginning with \(w'\) assuming the role of w. This must eventually yield a contradiction because the path \(P_{uw}\) is shortened by two, and once its length is equal to two this second case can no longer occur due to \(u = w'\) and \(u>^1 v >^1 w\). \(\square \)

Fig. 3
figure 3

Contradiction for \(w' >^1 v\)

The condition that G be connected in Lemma 3.2 is not strictly necessary and could be replaced by assuming the paths \( P_{uv} \) and \( P_{uw} \) to actually exist. Then it would suffice to consider only the induced (connected) subgraph. With this observation in mind, it may seem that Lemma 3.2 yields an alternate characterization of the staircase property in the sense that an ordering \(\{>^1, >^2\}\) on a bipartite graph G could be proved to be staircase if for all vertices \(u,v,w \in V_1\) with \(u>^1 v >^1 w\), and analogously for vertices in \(V_2\), we have \(P_{uv} \le P_{uw}\). However, as the example in Fig. 4 shows, this reverse implication of Lemma 3.2 does not hold, even if G is connected. Nonetheless, Lemma 3.2 allows us to prove the main result of this section.

Fig. 4
figure 4

Although the paths \(P_{uv}\) and \(P_{uw}\) are of equal length, the ordering shown is not staircase since the quadruple uvxy violates (SC2)

Theorem 3.1

Let \(G = (V_1 \cup V_2, E)\) be a bipartite graph which satisfies Conditions (1) and (2). Then, a staircase ordering \( \{>^1, >^2\} \) on G is unique up to reversal.

Proof

Suppose there are two different staircase orderings \( \{>^1, >^2\} \) and \( \{\succ ^1, \succ ^2\} \) that cannot be transformed into each other by reversing either ordering.

Since the graph G is connected but not complete bipartite, we can find vertices \( u, v \in V_1 \) and \( x, y \in V_2 \) that induce the subgraph shown in Fig. 5.

In this subgraph, we have

$$\begin{aligned} u>^1 v&\Leftrightarrow x >^2 y \text { and}\\ u \succ ^1 v&\Leftrightarrow x \succ ^2 y \end{aligned}$$

due to Condition (SC2). Consequently, it is not possible to retain the staircase conditions by reversing only one total order of a given ordering. This directly implies that there is, up to reversal, only one staircase ordering on the graph shown in Fig. 5, and therefore we have \(|V_1 \cup V_2 | \ge 5\). Moreover, neither \(>^1\) and \(\succ ^1\) nor \(>^2\) and \(\succ ^2\) coincide and we can assume w.l.o.g. that there exist vertices \(u,v \in V_1\) such that \(u >^1 v \wedge v \succ ^1 u\) holds.

After possibly reversing all four total orders at once as well as possibly switching \(V_1\) and \(V_2\), we can, by the arguments above, find vertices \(u,v,w \in V_1\) with \(u>^1 v >^1 w\) and \(u \succ ^1 w \succ ^1 v\). Note that in this case, Lemma 3.2 directly implies \(|P_{uv} | = |P_{uw} |\).

We now show that we can additionally assume the paths \(P_{uv}\) and \(P_{uw}\) to be of length two. If \(P_{uw}\) is of length greater than or equal to four, let \( P_{uw} = (u, \ldots , w', z, w) \). Then the path \(P_{uw'}\) is shorter than \(P_{uw}\), and with the contraposition of Lemma 3.2 we have \(u>^1 w' >^1 w\). By the same argument applied to \(P_{uw'}\) and \(P_{uv}\) we obtain \(u>^1 w' >^1 v\) and, hence, we have \(u>^1 w'>^1 v >^1 w\). This, together with \(\{w', z\}, \{w, z\} \in E\), implies that the edge \(\{v, z\}\) must be contained in E to not violate (SC1). Renaming \(w'\) to u now yields the desired assumption.

As the shortest paths \(P_{uv}\) and \(P_{uw}\) are of length two, we have that \(N(u) \cap N(v)\) and \(N(u) \cap N(w)\) are both non-empty. From (SC1) and \(u>^1 v >^1 w\) it follows that \(N(u) \cap N(w) \subseteq N(u) \cap N(v)\). Analogously, from (SC1) and \(u \succ ^1 w \succ ^1 v\) we can also conclude \(N(u) \cap N(v) \subseteq N(u) \cap N(w)\) and hence, \(N(u) \cap N(v) = N(u) \cap N(w) \ne \emptyset \).

Finally, Fig. 6a and b illustrates that \(N(v) \setminus N(w)\) and \(N(w) \setminus N(v)\) must both be empty, as otherwise (SC2) is violated. However, we then have \(N(v) = N(w)\), which is a contradiction to Condition 2. This completes the proof. \(\square \)

Fig. 5
figure 5

Induced subgraph in G that implies the same order between uv as between xy

Fig. 6
figure 6

Violations of (SC2)

This theorem fully characterizes the operations which can be applied to a staircase ordering to retain the staircase property.

3.3 Computing Staircase Orderings

We next introduce a polynomial-time algorithm that computes a staircase ordering on a given bipartite graph if possible, and otherwise states that no such ordering exists. As in the previous section, we may consider each connected component of a bipartite graph separately. Algorithm 2 then solves the problem.

figure b

There are no immediately obvious candidates for the uppermost vertex in a staircase ordering. Hence, the outer for-loop iterates over all vertices of \(V_1\). For each choice \(r \in V_1\), the inner for-loop constructs an ordering with r as the uppermost vertex. The constructed ordering is then tested, e.g. by Algorithm 1, whether it is staircase. We prove the correctness of Algorithm 2 in Lemma 3.3 and show in Lemma 3.4 that its runtime is \(O(|V_1 |(|V | + |E |))\).

Lemma 3.3

Algorithm 2 is correct.

Proof

It is clear that the algorithm terminates in all cases.

If no staircase ordering exists on G, the staircase test in Line 16 will always fail and the algorithm correctly returns that no staircase ordering exists on G.

Otherwise, the outer for-loop will eventually choose a root \(r \in V_1\) as the uppermost vertex of the total order \(>^1\) such that a staircase ordering can be constructed. It remains to show that the operations within the inner for-loop are either strictly necessary to create a staircase ordering or simply choose one of multiple valid options.

Due to the contraposition of Lemma 3.2, vertices must be ordered by the length of the shortest path to the root vertex r. This is done by the combination of sorting the vertices into subsets \(N_i\) during the breadth-first search and then appending them to the lists \(L_1\) and \(L_2\) in the correct order. Second, the exact order of vertices within each \(N_i\) is determined by the operations in Lines 6 and 9. Depending on which operation determines the order between two vertices \(u, v \in N_i\) we distinguish three cases. For ease of notation, we write \(>^*\) to refer to either \(>^1\) or \(>^2\) depending on whether the vertices are contained in \(V_1\) or \(V_2\).

  • Case 1—\(\deg _H(u) < \deg _H(v)\): This case can only occur if \(|N_i | > 1\) as well as \(|N_{i-1} | > 1\) hold. Due to \(|N_0 | = 1\), we have \(i \ge 2\). Further, we can find a vertex \(x \in N_{i-1}\) with \(\{u,x\} \notin E, \{v,x\} \in E\). The vertex x, in turn, must be connected to some vertex \(w \in N_{i-2}\). As illustrated in Fig. 7a, \(v >^* u\) must hold as otherwise (SC1) is violated.

  • Case 2—\(\deg _H(u) = \deg _H(v) \wedge \deg _{H'}(u) > \deg _{H'}(v)\): Here, \(|N_i | > 1\) must hold and we therefore have \(i \ge 1\). Analogously to the previous case, we can find a vertex \(x \in N_{i+1}\) with \(\{u,x\} \in E\) and \(\{v,x\} \notin E\). Figure 7b illustrates that, in order to not violate (SC2), \(v >^* u\) must hold.

  • Case 3—\(\deg _H(u) = \deg _H(v) \wedge \deg _{H'}(u) = \deg _{H'}(v)\): Suppose there are vertices \(x, y \in N_{i-1}\) with \(\{u,x\}, \{v,y\} \in E, \{v, x\}, \{u, y\} \notin E\), which can only occur if \(i \ge 2\). Then, by the same arguments as in the first case, we get \(u >^* v\) as well as \(v >^* u\), which is a contradiction. Therefore, we can conclude \(N_H(u) = N_H(v)\). Analogously, we can apply the arguments from the second case to conclude \(N_{H'}(u) = N_{H'}(v)\). In total, we have \(N(u) = N(v)\), and hence, u and v can be ordered arbitrarily.

Altogether, all sorting operations are either strictly necessary for an ordering to be staircase, or simply represent a choice between multiple valid orders. Therefore, the algorithm does find a staircase ordering if and only if one exists. \(\square \)

Fig. 7
figure 7

Subcases for proof of Lemma 3.3

Lemma 3.4

Algorithm 2 runs in time \(O(|V_1 |(|V | + |E |))\).

Proof

Breadth-first search has time-complexity \(O(|V | + |E |)\). Each set \(N_i\) can be sorted in time \(O(|N_i | + \max _{v \in N_i}{deg_G(v)})\) via counting sort. The time complexity of the combined time required for sorting can be bounded by the estimate

$$\begin{aligned} \sum _{i=1}^{i_{\max }}(|N_i | + \max _{v \in N_i}{\deg _G(v)}) \le \sum _{i=1}^{i_{\max }} |N_i | + \sum _{i=1}^{i_{\max }} \max _{v \in N_i}{\deg _G(v)} \le |V | + |E |. \end{aligned}$$

As stated by Lemma 3.1, each computed ordering can be checked in time \(O(|V |+|E |)\) by Algorithm 1.

In total, an iteration of the outer for-loop takes \(O(|V | + |E |\)) time. Since there are at most \(|V_1 |\) iterations, this completes the proof. \(\square \)

In Sects. 3.13.3, we have studied staircase orderings on bipartite graphs and concluded that we can determine an ordering on a bipartite graph to be staircase within polynomial time and also compute one in polynomial time. Further, a staircase ordering on a bipartite graph is unique up to changing the order of connected components, changing the order of vertices with equal neighbourhood and reversing the entire ordering. We will see in the next section that the recognition problem on m-partite graphs with given partition is NP-complete in the general case. However, we will identify a special case which is solvable in polynomial time.

4 Staircase Orderings on m-Partite Graphs

We now consider the staircase recognition problem on m-partite graphs. More precisely, the partition \( {\mathcal {V}}= \{V_1, \ldots , V_m\} \) on the vertex set of the graph is assumed to be given and it is to be determined whether \( {\mathcal {V}}\) is a staircase partition, i.e. if there exist corresponding staircase orderings \( \{>^1, \ldots , >^m\} \) on its subsets.

It can be shown that recognizing staircase partitions in the general case is NP-complete. In Sect. 4.1, we will give a proof that uses a reduction from the NP-complete Betweenness Problem.

In the special case where all bipartite subgraphs \( G_{ij}\) of an m-partite graph are connected, we will see in Sect. 4.2 that Algorithm 2 can be extended to compute staircase orderings within polynomial time.

4.1 Complexity of Recognizing Staircase Partitions

We start by showing that recognizing a staircase partition is NP-complete on general m-partite graphs. This result has been presented in the dissertation [19].

Theorem 4.1

Let \( G = (V, E) \) be an m-partite graph with vertex set partition \( {\mathcal {V}}= \{V_1, \ldots , V_m\} \). The problem to decide whether a staircase ordering on G exists is NP-complete, even if the dependency graph \( {\mathcal {G}}\) is cycle-free.

Proof

We prove the theorem by giving a polynomial-time reduction from the Betweenness Problem. In an instance of Betweenness, a ground set M is given together with a set \({\mathcal {M}}\) of ordered triples of elements from M. One has to determine whether there exists a total order on M such that the middle item of each given triple is placed somewhere between the other two items. This problem is known to be NP-complete [20].

From an instance of Betweenness, we construct an equivalent instance of the recognition problem for staircase partitions as follows: We have a special subset \( V_0 = M \), where each vertex is identified with an item from Betweenness. For the triples \(t=(u,v,w) \in {\mathcal {M}}\), we construct subsets consisting of 3 vertices each that are identified with the items from the respective triple (but of course different from the vertices in \(V_0\)). Each vertex in such a subset only has a single neighbour in \( V_0 \), which is the vertex corresponding to the same item. Moreover, each subset \( V_t \) representing a triple t from \({\mathcal {M}}\) comes together with an auxiliary subset \(V_t'\) consisting of two vertices. This subset \(V_t'\) induces a complete bipartite graph with every other subset but \( V_t \), and the edges between \(V_t'\) and \( V_t \) are designed to force \( v_t \), the designated middle element of the triple, to be sorted in between the other two elements in \( V_t \) (see Fig. 8). Note that there are exactly two possibilities for choosing orderings on \( V_t \) and \( V_t' \), which correspond to the two possible orderings of the elements uvw from t that put v in the middle. The bipartite subgraphs between subsets or auxiliary subsets corresponding to different triples are complete bipartite graphs such that the dependency graph \( {\mathcal {G}}\) is a tree of depth 2 with root vertex \( V_0 \) and leaves \(V_t'\), \(t \in {\mathcal {M}}\).

On the one hand, any solution of Betweenness gives us a total order on M that we can use for \( V_0 \). This ordering will ensure that the middle element of each \( V_t \) for \( t \in {\mathcal {S}} \) is between the other elements of t. Therefore, there will be no crossing of edges between \( V_t \) and \( V_0 \), and hence no violation of (SC2) if the ordering on \(V_t \) and \( V_t' \) is oriented correctly. On the other hand, every choice of total orders on the subsets that yield a staircase ordering on G immediately gives us an ordering of \(V_0\) that is a solution to Betweenness. Finally, the transformation of instances is clearly polynomial in the encoding size of the input. \(\square \)

Fig. 8
figure 8

Illustration of the construction from the proof of Theorem 4.1, showing subsets \( V_0 \) as well as \(V_t\) and \(V_t'\) for a triple \(t=(u,v,w)\) from the Betweenness problem. The edges of the complete bipartite subgraph induced by \(V'_t \cup V_0\) have been left out for visual clarity

We would like to remark that for an instance of the recognition problem constructed as in the above proof, no ordering can possibly violate (SC1) without also violating (SC2), which means that finding orderings that just satisfy (SC2) is also NP-complete.

Generally, instances in the proof of Theorem 4.1 feature subgraphs \( G_{ij}\) with isolated vertices. Although the general staircase ordering problem is NP-complete, we can show that the problem becomes solvable in polynomial time under an additional connectedness assumption.

4.2 A Polynomial Special Case

In this subsection, we will show that the problem becomes polynomially solvable if all \(G_{ij}\) are connected. Before proving this statement, we introduce preorders as a way to represent all staircase orderings on a given connected subgraph \(G_{ij}\) as well as composed preorders as a way to intersect these representations. Definition 4.1 lays out the necessary notions.

Definition 4.1

(Preorder, Composed Preorder, Representation of Total Orders) A preorder is a reflexive, transitive binary relation.

Let \(\ge ^i\) and \(\succeq ^i\) be preorders on some vertex set \(V_i\).

The composed preorder of \(\ge ^i\) and \(\succeq ^i\) is defined as

$$\begin{aligned} u \mathbin {\underline{\triangleright }}^i v :\Leftrightarrow u \ge ^i v \wedge u \succeq ^i v. \end{aligned}$$
(3)

A preorder \(\ge ^i\) represents a total order \(>^i\) if for all \(u,v \in V_i\) we have

$$\begin{aligned} u >^i v \Rightarrow u \ge ^i v. \end{aligned}$$
(4)

As mentioned in Sect. 3.2, there are two ways to modify an ordering on a connected bipartite graph \(G_{ij}\) while maintaining the staircase property of the ordering. First, we can simultaneously reverse the ordering and second, we can switch places of vertices uv with \(N_{G_{ij}}(u) = N_{G_{ij}}(v)\).

The first operation can only yield a staircase ordering on G if the total orders of all \(V_i\) are reversed at once. Therefore, we can “ground” the ordering by fixing the total order on a certain \(V_i\) and aligning all other total orders with the fixed one.

In contrast, the second operation can change the total order on a given \(V_i\) in a way that cannot be uniformly extended to all other subsets \(V_j\). Let \(\{>^i,>^j\}\) be a staircase ordering on some bipartite connected subgraph \(G_{ij}\) and for \(k \in \{i,j\}\) consider the total preorders given by

$$\begin{aligned} u \ge ^k v :\Leftrightarrow u >^k v \vee N_{G_{ij}}(u) = N_{G_{ij}}(v), \quad u,v \in V_k. \end{aligned}$$
(5)

As a consequence of Theorem 3.1, the preorders \(\ge ^i\), \(\ge ^j\) then represent, up to reversal, exactly the staircase orderings \(\{>^i, >^j\}\) on \(G_{ij}\). Lemma 4.1 formalizes the merging step of two preorders \(\ge ^i\) and \(\succeq ^i\) on some \(V_i\), and Example 4.1 illustrates the procedure.

Lemma 4.1

Let \(G_{ij}\) and \(G_{jk}\) be bipartite connected subgraphs of some graph G with total preorders \(\ge ^i\), \(\ge ^j\) and \(\succeq ^j\), \(\succeq ^k\) representing a set of staircase orderings on \(G_{ij}\) and \(G_{jk}\), respectively.

There exist total orders \(>^i\), \(>^j\), \(>^k\) fulfilling the conditions

$$\begin{aligned}&\ge ^i \text { represents } >^i, \end{aligned}$$
(6a)
$$\begin{aligned}&\ge ^j, \succeq ^j \text { represent } >^j, \end{aligned}$$
(6b)
$$\begin{aligned}&\succeq ^k \text { represents } >^k, \end{aligned}$$
(6c)
$$\begin{aligned}&\{>^i, >^j\} \text { is a staircase ordering on } G_{ij}, \end{aligned}$$
(6d)
$$\begin{aligned}&\{>^j, >^k\} \text { is a staircase ordering on } G_{jk}, \end{aligned}$$
(6e)

if and only if the composed preorder \(\mathbin {\underline{\triangleright }}^j\) of \(\ge ^j\) and \(\succeq ^j\) is total. Moreover, the respective total preorders \(\ge ^i\), \(\mathbin {\underline{\triangleright }}^j\), \(\succeq ^k\) represent exactly the total orders \(>^i\), \(>^j\), \(>^k\) fulfilling the conditions above.

Proof

Let \(>^i\), \(>^j\), \(>^k\) be total orders fulfilling Conditions (6a)–(6e). Clearly, we have either \(u >^j v\) or \(v >^j u\) for any two distinct vertices \(u,v \in V_j\). If \(u >^j v\), then \(u \ge ^j v \wedge u \succeq ^j v\) must hold, which implies \(u \mathbin {\underline{\triangleright }}^j v\). Analogously, we obtain \(v \mathbin {\underline{\triangleright }}^j u\) if \(v >^j u\). Since u and v were chosen arbitrarily, the preorder \(\mathbin {\underline{\triangleright }}^j\) is total. Moreover, since the total orders \(>^i\), \(>^j\), \(>^k\) were chosen arbitrarily and \(\mathbin {\underline{\triangleright }}^j\) indeed represents \(>^j\), the respective preorders \(\ge ^i\), \(\mathbin {\underline{\triangleright }}^j\), \(\succeq ^k\) represent all total orders \(>^i\), \(>^j\), \(>^k\) fulfilling Conditions (6a)–(6e).

Conversely, let the composed preorder \(\mathbin {\underline{\triangleright }}^j\) be total and \(>^j\) a total order represented by \(\mathbin {\underline{\triangleright }}^j\). Further, let \(>^i\) and \(>^k\) be two total orders that are represented by \(\ge ^i\) and \(\succeq ^k\), respectively. The total order \(>^j\) is represented by both \(\ge ^j\) and \(\succeq ^j\), since \(\mathbin {\underline{\triangleright }}^j\) is the composition of these two preorders. Consequently, the total orders \(>^i\), \(>^j\), and \(>^k\) fulfil Conditions (6a)–(6e). Moreover, since the three total orders were chosen arbitrarily, any three total orders represented by \(\ge ^i\), \(\mathbin {\underline{\triangleright }}^j\), and \(\succeq ^k\), respectively, fulfil Conditions (6a)–(6e). \(\square \)

Example 4.1

Consider the graph G shown in Fig. 9a, which can be ordered to be staircase. In the subgraph \(G_{12}\), we can arbitrarily arrange \(v_2\) and \(v_3\), and in the subgraph \(G_{23}\) we can arbitrarily arrange \(v_1\) and \(v_2\). We write this as \(v_3 =^2 v_2\) and \(v_2 \approx ^2 v_1\), respectively. As a result, we have \(v_3 =^2 v_2 \ge ^2 v_1\) and \(v_3 \succeq ^2 v_2 \approx ^2 v_1\). The composed preorder, which is indeed total, is then given by \(v_3 \mathbin {\underline{\triangleright }}^2 v_2 \mathbin {\underline{\triangleright }}^2 v_1\).

In contrast, consider Fig. 9b showing the same graph with reversed staircase ordering on \(G_{23}\), i.e. we assume the preorder \(v_1 \approx ^2 v_2 \succeq ^2 v_3\) on \(V_2\). In this case, the composed preorder is no longer total due to \(v_3 \ge ^2 v_1\) and \(v_1 \succeq ^2 v_3\). Indeed, there is no staircase ordering on G represented by \(\ge ^i\), \(\succeq ^j\), and \(\mathbin {\underline{\triangleright }}^k\).

To summarize, although a staircase ordering on the graph G exists, this would not necessarily be the case if it were a subgraph of a larger graph \(G'\) where the preorder on \(V_3\) is “grounded” as shown in Fig. 9b.

Fig. 9
figure 9

Example preorder compositions on a graph G. Depending on the assumed “grounding” of the preorders on \(V_2\), the composed preorder on \(V_2\) is total (left) or not (right). If the composed preorder is not total, then there is no staircase ordering on G that aligns with the assumed preorders. The edges of the complete bipartite subgraph \(G_{13}\) have been left out for visual clarity

Composing the preorders \(\ge ^j\) and \(\succeq ^j\) in Fig. 9a leads to a total preorder, whereas composing \(\ge ^j\) and the reverse of \(\succeq ^j\) does not. In fact, Lemma 4.2 shows that this is the case for merging any two nontrivial preorders, i.e. preorders where at least one pair of elements is not related.

Lemma 4.2

Let \(\ge ^i\) and \(\succeq ^i\) be two total preorders on some vertex set \(V_i\). Consider the composed preorder \(\mathbin {\underline{\triangleright }}^i\) of \(\ge ^i\) and \(\succeq ^i\) as well as the composed preorder \(\sqsubseteq ^i\) of \(\ge ^i\) and the reverse of \(\succeq ^i\), i.e.

$$\begin{aligned} u \sqsubseteq ^i v :\Leftrightarrow u \ge ^i v \wedge v \succeq ^i u, \quad u,v \in V_i. \end{aligned}$$
(7)

If \(\mathbin {\underline{\triangleright }}^i\) and \(\sqsubseteq ^i\) are both total, then at least one of the preorders \(\ge ^i\) and \(\succeq ^i\) is trivial.

Proof

Suppose \(\ge ^i\) is not trivial, i.e. there exist vertices \({\tilde{u}},{\tilde{v}} \in V_i\) with \(\lnot ({\tilde{u}} \ge ^i {\tilde{v}})\), and let \(u,v \in V_i\).

We distinguish three cases and show that \(u \succeq ^i v\) always holds.

  • Case 1a—\(u \ge ^i v \wedge \lnot (v \ge ^i u)\): Due to \(\lnot (v \ge ^i u)\) we have \(\lnot (v \mathbin {\underline{\triangleright }}^i u)\). Since \(\mathbin {\underline{\triangleright }}^i\) is total, we get \(u \mathbin {\underline{\triangleright }}^i v\), which in turn implies \(u \succeq ^i v\).

  • Case 1b—\(\lnot (u \ge ^i v) \wedge v \ge ^i u\): Analogously to the previous case, we obtain \(u \succeq ^i v\) via the preorder \(\sqsubseteq ^i\).

  • Case 2—\(u \ge ^i v \wedge v \ge ^i u\): In this case, we find a vertex \(w \in V_i\) with either \(\lnot (u \ge ^i w) \wedge \lnot (v \ge ^i w)\) or \(\lnot (w \ge ^i u) \wedge \lnot (w \ge ^i v)\). Similarly to the first two cases we obtain \(u \succeq ^i w\) and \(w \succeq ^i v\) using \(\mathbin {\underline{\triangleright }}^i\) and \(\sqsubseteq ^i\). Transitivity of \(\succeq ^i\) then implies \(u \succeq ^i v\).

This shows that \(\succeq ^i\) is trivial. \(\square \)

In other words, if we have two nontrivial preorders \(\ge ^i\) and \(\succeq ^i\), then composing \(\ge ^i\) and \(\succeq ^i\) or composing \(\ge ^i\) and the reverse of \(\succeq ^i\) yields a nontotal composed preorder.

We proceed to prove the main theorem of this section, which states that, if all subgraphs \(G_{ij}\) are connected, the problem to decide whether a staircase ordering exists on a given graph with given partition is solvable in polynomial time. Our proof will be constructive, yielding an algorithm to compute a staircase ordering if it exists, and we will investigate its performance in Sect. 5.

Theorem 4.2

Let G be an m-partite graph with a partition \( {\mathcal {V}}= \{V_1, \ldots , V_m\} \) of the vertex set such that all \(G_{ij}\) are connected. Then it can be decided within polynomial time whether a staircase ordering on G exists.

Proof

We prove this theorem by describing an algorithm that solves the problem in polynomial time. The idea is to perform a breadth-first search on the dependency graph and to use Algorithm 2 on \(G_{ij}\) whenever an edge \(\{V_i, V_j\}\) is traversed. If necessary, the computed total orders on \(V_i\) and \(V_j\) will be merged with already existing orders on \(V_i\) and \(V_j\). In the case where a staircase ordering exists, the breadth-first search traverses all edges and the last merging step yields a staircase ordering on G. Otherwise, the process stops as one of the merging steps fails or Algorithm 2 already recognizes that no staircase ordering exists for some subgraph \(G_{ij}\). If the dependency graph \({\mathcal {G}}\) consists of more than one connected component, we can simply apply the above procedure to each connected component. Therefore, we can assume w.l.o.g. that \({\mathcal {G}}\) is connected as well.

It is clear that a staircase ordering on G cannot exist if Algorithm 2 determines that for some \( G_{ij}\) no staircase ordering can be found. On the other hand, if a staircase ordering on G does exist, Algorithm 2 does find a staircase ordering on \(G_{ij}\), which yields new total preorders \(\succeq ^i, \succeq ^j\) on \(V_i\) and \(V_j\), respectively. Suppose there already exists some total preorder \(\ge ^i\) on \(V_i\). Then it remains to check the composed preorder

$$\begin{aligned} u \mathbin {\underline{\triangleright }}^i v \Leftrightarrow u \ge ^i v \wedge u \succeq ^i v. \end{aligned}$$

If the composed preorder is not total, then by Lemma 4.1 there exists no staircase ordering on G represented by \(\ge ^i\) and \(\succeq ^i\) and we attempt the composition with the reverse of \(\succeq ^i\). We want to emphasize that applying Algorithm 2 to some \( G_{ij}\) yields two total preorders \(\ge ^i, \ge ^j\) that need to be merged and we must either reverse both new total preorders or none, as reversing only one of them would yield an ordering that is not staircase on \(G_{ij}\). This includes the case where, for example, \(\ge ^j\) is the first total preorder on \(V_j\). Moreover, Lemma 4.2 ensures that the merging step either fails, in which case no staircase ordering exists, or exactly one “grounding” of \(\ge ^i\) and \(\succeq ^i\) is valid. If the breadth-first search finishes and the last merging step succeeds, we have total preorders on all \(V_i \in {\mathcal {V}}\). Deriving any combination of total orders then yields a staircase ordering on G. The resulting algorithm can then be summarized as shown in Algorithm 3.

figure c

In short, the breadth-first-search approach can indeed decide whether a staircase ordering exists on the graph G. Further, its running time is polynomial in the input size, as we traverse each edge in the dependency graph \({\mathcal {G}}\) at most once, Algorithm 2 runs in polynomial time, and finally, each merging step can be done in polynomial time as well. \(\square \)

It is worth mentioning that, since the instances in the proof of Theorem 4.1 may feature subgraphs \(G_{ij}\) with isolated vertices, it does not state that deciding whether staircase sorting is hard under the assumption of non-empty neighbourhoods in each \(G_{ij}\), which is a slightly weaker assumption than the one made in Theorem 4.2.

Before presenting the results of our computational experiments, we want to state an interesting observation. Any given ordering on any given graph can be made staircase by adding additional edges to the graph until the staircase conditions are fulfilled. In fact, only adding edges of violated staircase conditions until the ordering becomes staircase, yields, in terms of the number of additional edges, the optimal staircase relaxation. If the partition \({\mathcal {V}}\) is not staircase, it may then be relevant to find an ordering with minimal staircase relaxation. The following theorem, which is a slight modification of [19, Theorem 5.20], extending it to graphs satisfying our connectedness assumption from Theorem 4.2, yields a surprising result. Namely, it shows that finding such an ordering remains NP-complete, even if all bipartite subgraphs \(G_{ij}\) are connected and each \(V_i \in {\mathcal {V}}\) contains at most two elements.

Theorem 4.3

Let G be an m-partite graph with a partition \( {\mathcal {V}}= \{V_1, \ldots , V_m\} \) of the vertex set such that all \(G_{ij}\) are connected. Then, the optimization problem to find an ordering on G such that its staircase relaxation is minimal is NP-complete, even when the problem is restricted to at most two elements per partition.

Proof

We show the theorem by a polynomial-time reduction from the famous MaxCut problem. For a given graph H, it asks for partitioning the vertices into two sets such that the number of arcs between both partitions is maximized. The weighted version of the corresponding decision problem was one of Karp’s 21 NP-complete problems [17], and also the unweighted case is well known to be NP-hard [13].

From an instance of MaxCut on a graph H, we construct an equivalent instance of the optimization problem from Theorem 4.3 with only two elements per partition: For each \(v \in V(H)\), construct a partition \(V_v = \{\underline{v}, {\overline{v}}\}\) consisting of the two elements \(\underline{v}\), \({\overline{v}}\). For \(\{u, v\} \in E(H)\) we then add the edges \(\{\underline{u}, {\overline{v}}\}, \{{\overline{u}}, {\overline{v}}\}, \{{\overline{u}}, \underline{v}\}\) (see Fig. 10). For \(u, v\ \in V(H), \{u, v\} \notin E(H)\) we add edges such that \( G_{uv} \) is complete bipartite, i.e. \(\{V_u, V_v\} \notin {\mathcal {G}}\). Hence, all resulting bipartite subgraphs \( G_{ij} \) are connected.

Therefore, sorting \(\underline{u} < {\overline{u}}\) and \(\underline{v} < {\overline{v}}\) would contribute exactly one missing edge to the objective, while flipping one of the orderings (but not both) satisfies (SC1) and (SC2) for the two partitions \(V_u\) and \(V_v\). As a consequence, any cut in H containing l edges with vertex subsets ST corresponds to a choice for total orderings on the subsets \(V_v,~v \in V(H)\) with \(|E(H) | - l\) violations, namely \(\underline{v} < {\overline{v}}~\forall v \in S\) together with \(\underline{v} > {\overline{v}}~\forall v \in T\), and vice versa. \(\square \)

Fig. 10
figure 10

Illustration of the construction from the proof of Theorem 4.3, showing subsets \(V_u\) and \(V_v\) for \(u,v \in E(H)\)

5 Computational Experiments

In the following, we will empirically evaluate Algorithm 3 to decide whether a staircase ordering on a given m-partite graph exists, and to compute such an ordering if it does. First, we consider two sets of randomly generated instances. On a set of small instances, we benchmark the performance of Algorithm 3 against the mixed-integer program (MIP) for finding staircase orderings suggested in [19]. Several sets of larger instances are then used to test the limits of Algorithm 3. Second, we demonstrate that our algorithm can staircase-sort a set of real-world railway timetabling instances. It will turn out that using the sorted version, which allows for using the totally unimodular dual-flow formulation from [5], is much more efficient than using a generic model for the problem.

We have implemented Algorithm 3 in Python 3.7.9, using the Python library NetworkX 2.5 [15] for our graph operations. For the MIP approach, we used the Python API of Gurobi 9.1.0 [14]. All experiments have been run on machines with Xeon E3-1240 v6 CPUs (“Kaby Lake”, 4 cores, HT disabled, 3.7 GHz base frequency) and 32 GB RAM.

5.1 Computational Experiments on Random Instances

The artificial instances in our first benchmark set were generated as follows. First, we created a dependency graph \( {\mathcal {G}}= ({\mathcal {V}}, {\mathcal {E}}) \) with m vertices and \( {\mathcal {E}}= \emptyset \). For each pair of vertices \( V_i, V_j \in {\mathcal {V}}\), \( i \ne j \) in \( {\mathcal {G}}\), we then added the edge \( \{V_i, V_j\} \) with probability \( p \in [0, 1] \). To ensure that the generated instances do not decompose into multiple smaller instances, we randomly chose two vertices \( V_i \) and \( V_j \) from two different randomly chosen connected components of \( {\mathcal {G}}\) and iteratively added the corresponding edge \( \{V_i, V_j\} \) until the entire dependency graph was connected. If the so-obtained \( {\mathcal {G}}\) was cycle-free, we randomly added one more edge such that it contained at least one cycle. This later allowed us to create a non-staircase version of the instance without simply adding a non-staircase subgraph \( G_{ij}\) (see below). From this dependency graph \( {\mathcal {G}}\), we then generated G as follows. For each \( V_i \in {\mathcal {V}}\), we randomly drew a subset size \( n_i \in [\underline{n}, {\overline{n}}], \underline{n}, {\overline{n}} \in N^+ \) and added the vertices \( v_{i, 1}, \ldots , v_{i, n_i} \) to subset \( V_i \). For any two subsets \(V_i,V_j \in {\mathcal {V}}\) with \(\{V_i,V_j\} \notin {\mathcal {E}}\), the corresponding subgraph \(G_{ij}\) is, in accordance with the definition of the dependency graph \({\mathcal {G}}\), complete bipartite. Finally, for each edge \( \{V_i, V_j\} \in {\mathcal {E}}\), we use Algorithm 4 as a generator for the (connected) subgraph \( G_{ij}\).

figure d

After initializing the vertex sets \( V_i \) and \( V_k \), the edge set E as well as the auxiliary variables \(\textit{minIdx}, \textit{maxIdx}\) in Lines 1–5, Algorithm 4 iterates over the vertex indices in \(V_i\) in Line 6. For each \(v_{i,k}\), its neighbourhood is generated as follows. First, we draw an integer which will represent the smallest index of any neighbour of \(v_{i,k}\) in Lines 9 and 10. The exponential function is chosen in order to prevent the generated graphs from being sparse at one end of the ordering while being very dense at the other end of the ordering. We then check if the drawn value indeed represents the index of a neighbour of \( v_{i, k} \) (Line 11), and, if necessary, adjust it in Line 12 such that the generated graph remains connected. A similar procedure follows for finding a new maximal neighbour index. When valid indices are found, the new edges are added in Line 25. In Line 27, we ensure \(G_{ij}\) to be connected by connecting the remaining vertices in \(V_j\) to \(v_{i,n_i}\). If the algorithm chooses \(\textit{minIdx} = 1\) and \(\textit{maxIdx} = n_j\) in all iterations, then the resulting \(G_{ij}\) is complete bipartite. This rare edge case is caught in Line 28, and we simply restart the algorithm. Note, that the minimal and maximal neighbourhood indices increase monotonously in each iteration, and therefore the canonical ordering is indeed staircase for \( G_{ij}\).

To test the performance of Algorithm 3 on non-staircase instances, we added a non-staircase duplicate of each instance to our test set. Since Algorithm 2 already detects if no staircase ordering exists on a given \(G_{ij}\), which therewith holds for the entire graph G, we broke the staircase property in a way that forces Algorithm 3 to also take interdependencies between subgraphs \(G_{ij}\) into account. In particular, we created a set of all edges in \({\mathcal {E}}\) that are contained in at least one cycle in \({\mathcal {G}}\) and chose one of these edges uniformly at random. In the corresponding subgraph \(G_{ij}= (V_i \cup V_j, E_{ij})\), we then flipped \(V_j\), i.e. we changed the edge set to

$$\begin{aligned} {\tilde{E}}_{ij} = \{\{v_{i,n},v_{j,m'}\} \mid \{v_{i,n}, v_{j,m}\} \in E_{ij}, m' = |V_j | - m + 1\}. \end{aligned}$$

To further improve the accuracy of our computations, we created five random instances for each set of parameters \( m, p, \underline{n}, {\overline{n}} \) used. Our result tables use the parameter naming scheme

$$\begin{aligned} m\text {-}p\text {-}\underline{n}\text {-}{\overline{n}}, \end{aligned}$$
(8)

with the postfix “\(\text {-}nonSC\)” to indicate the non-staircase duplicates.

Finally, we prepend each vertex in \({\mathcal {V}}\) and V with four random lowercase letters and rebuild the graphs \({\mathcal {G}}\) and G by adding vertices and edges in the resulting random lexicographic order to destroy any orders that NetworkX might derive from the vertex names or the order in which they have originally been added to the graphs.

An MIP formulation based on the linear ordering problem, which is able to decide whether a staircase ordering exists [19], can be stated as follows:

$$\begin{aligned} {\textrm{find}}\;\;\, {z} \end{aligned}$$
(9a)
$$\begin{aligned} \mathrm{s.t.}\quad \quad \quad {z_{uv} + z_{vu}}\;\;\;\,&=\;\;\;\,{1}\quad \;\;\;\;\, {\forall V_i \in {\mathcal {V}}\ \forall ~u \ne v \in V_i}{} & {} \end{aligned}$$
(9b)
$$\begin{aligned} \quad \quad \quad {z_{uv} + z_{vw} + z_{wu}}\;\;\;\,&\le \;\;\;\;{2}\;\;\,{\forall V_i \in {\mathcal {V}}\ \forall ~u \ne v \ne w}{} & {} \end{aligned}$$
(9c)
$$\begin{aligned} \quad \quad \quad {z_{uv} - z_{v'u'}}\;\;\;\,&=\;\;\;\,{0}\;\;\;\;\;\;\;\,{\forall u>^i v, u' >^j v': \text { {(SC2)} is violated}}{} & {} \end{aligned}$$
(9d)
$$\begin{aligned} \quad \quad \quad {z_{uv}}\;\;\;\,&\in \;\;\;\,{\{0, 1\}}\;\;\;\,{\forall (u, v) \in \mathop {\bigcup }\nolimits _{i = 1}^m (V_i \times V_i).}{} & {} \end{aligned}$$
(9e)

Variables \( z_{uv} \in F \) stand for the decision to order vertex \( u \in V \) before vertex \( v \in V \) for each u and v in the same subset of \( {\mathcal {V}}\). Constraint (9a) ensures that exactly one of two possible orders between two vertices is chosen, while Constraint (9b) requires the order within a subset to be acyclic. Finally, Constraint (9d) demands that the orders fulfil Condition (SC2). Note that we do not need to explicitly model (SC1), as it is implied by (SC2) if \(G_{ij}\) is connected.

Table 1 shows our results for the comparison between building and solving the MIP and using Algorithm 3. In the columns, we state the parameter set [see (8)], the dimensions of the graphs \({\mathcal {G}}\) and G as well as the time required to build the MIP model, solve the MIP model, or solve the problem with Algorithm 3. For increased readability, each row represents the averages of the five instances with the given parameter set, where the resulting graph dimensions are rounded to integer values.

In terms of performance, we make two important observations. On the one hand, when comparing the pure solution times, the MIP model outperforms our Python-based implementation of Algorithm 3 on the given benchmark set. Second, however, the MIP model takes a very long time to build, as the number of constraints (9d) is doubly quadratic in the number of vertices per subset of the partition. For example, the MIP model for the first instance with parameters 10-0.5-10-10 consists of only 9182 rows, 900 columns and 20,764 nonzeros, whereas the MIP model of the first instance with parameters 10-0.5-50-50 already consists of 1,391,617 rows, 24,500 columns and 3,175,234 nonzeros. As a result, increasing the subset size from 10 to 50 already led to a 1000-fold increase in MIP build time in our test. We point out that it is possible to decrease the model size substantially by manually handling constraints (9d) in Python and identifying the corresponding variables before actually creating them in the Gurobi model. However, this has not been implemented as it does not affect the time complexity of model building.

Although the picture remains largely the same between staircase and non-staircase instances, we can observe that solution times tend to be lower for the non-staircase instances, as either method can stop once it has found the first conflict. The largest variance has been found for the five instances of 150-0.5-10-10, ranging from 57.8 to 65.0 s, whereas solution times for the five non-staircase instances show much higher variance and range from 6.6 to 56.6 s.

Finally, we conclude that although the model construction time of the MIP scales roughly linearly with \( |{\mathcal {E}} | \), it quickly becomes prohibitively long for practical application as instance size increases. We therefore neglect this approach for the larger instances we present in the following.

Table 1 Performance of sorting algorithm and linear-ordering MIP on small random instances

The results on the impact of varying the subset size are given in Table 2. The experiments with the first five parameter sets show that computation time quickly increases with subset size, as Algorithm 2, which is used to sort the bipartite subgraphs \(G_{ij}\), is of quadratic complexity in the number of vertices per subset. The experiments with the next five parameter sets show that increasing the variance in subset size while leaving the expected number of vertices per subset at 250 vertices also slightly increases computation time. As before, we find that even with just a single conflict prohibiting a staircase ordering from existing, computation times are significantly lower for non-staircase instances.

Table 2 Performance of sorting algorithm on random instances with varying subset sizes

The next set of random instances features a gradually increasing number of vertices in the dependency graph. We consider it to study the impact of an increasing number of bipartite subgraphs \( G_{ij}\). The results can be seen in Table 3. Here we see that the computation time of Algorithm 3 scales linearly with \( |{\mathcal {E}} | \). Again, computation times for non-staircase instances are significantly lower, but show higher variance.

Table 3 Performance of sorting algorithm on random instances with varying dependency graph sizes

Finally, we created a testset that, in terms of dependency graph size and density as well as subset size, approximates the structure of the real-world railway timetabling application studied in the next section. Table 4 states the results. First, we want to emphasize that the smaller subset size allows us to solve the problem for much larger graphs. Further, the results confirm that the computation time grows linearly with the number of bipartite subgraphs and is generally lower for non-staircase instances. On the non-staircase instances of this testset, we could once again observe significant variance of solution times. The greatest outliers arose for 250000-0.00002-10-10-nonSC, where solution time ranged from 463.5 to 1573.7 s, an almost fourfold increase between the fastest and slowest recognitions.

Table 4 Performance of sorting algorithm on random instances with large sparse dependency graph

To summarize, the results show that Algorithm 3 clearly outperforms the MIP if model build time is considered. If model build time is disregarded then, at least on our testset consisting of very small instances, the MIP outperforms our Python-based implementation of Algorithm 3. Further, computation times scale, in accordance with our theoretical results, linearly with number of bipartite subgraphs \(G_{ij}\) and quadratically with subset size. Finally, computation times tend to be shorter on non-staircase instances as the algorithm can terminate on finding the first conflict.

5.2 Computational Experiments on Timetabling Instances

Now, we evaluate the performance of Algorithm 3 on a set of real-world railway timetabling instances. In the application, we are given a timetable draft, and the task is to slightly adjust the departure times to reduce peak power consumption, which typically accounts for 20–25% of the train operator company’s electricity bill. For full details of the application and different solution approaches, we refer to [5, 8, 9], as well as [6, 7] for an extension of the approach onto underground timetabling. In [5], it has been shown that explicit knowledge of a staircase ordering allows the usage of a compact formulation to efficiently solve the timetabling problem and drastically reduce solution times. In the case of the largest instance, containing all nationwide passenger traffic of Germany’s largest railway company over a whole day, the computation time has been reduced from about three hours to about three minutes.

In our test, we simulated the lack of explicit knowledge of the staircase ordering by randomizing the vertex names as described for the random instances in the previous section and also tested five randomizations for each instance. As not all bipartite subgraphs \(G_{ij}\) were connected in these instances, some additional presolve was required. In particular, a vertex with degree zero in any \(G_{ij}\) can be removed entirely as it cannot be part of a feasible solution, i.e. a valid timetable. After removing all isolated vertices, the condition that all \(G_{ij}\) are connected is fulfilled and we can directly apply Algorithm 3.

We have repeated the computations in [5, Table 1] with our up-to-date version of the instances and the soft- and hardware specified at the beginning of this section. The column \(\varDelta (s)\) in Table 5 states the time saved if the problem is solved by first applying the aforementioned presolve method (Presol.), then sorting the graphs using Algorithm 3 (Alg.) and finally using the dual-flow formulation (DF) to optimize the schedule, instead of directly solving the problem via the naive formulation (NA). In this test, we even used an improved version of the naive formulation, see [7, Formulation (1)], which performs slightly better than the version used in [5]. On most instances in the test set, our three-step approach clearly outperforms solving the problem via the (improved) naive formulation. Most notably, we are able to solve the instance Würzburg Hbf in less than 30 minutes, whereas the naive formulation runs into the time limit of 10 hours.

Table 5 Comparison of optimization performance on railway instances

We would like to note that in [7] each scheduled departure could also choose from a discrete set of travel times, which changes the structure of the problem such that a large portion of the subgraphs \(G_{ij}\) no longer fulfil the staircase property. We ran preliminary tests on these instances and our algorithm determined all instances to be non-staircase within less than 1 second.

6 Outlook

In this work, we have discussed theoretical and practical aspects of the recognizability of staircase compatibility as a whole and have dealt with the subcase where all subgraphs \(G_{ij}\) are connected. In this subcase, the merging step constitutes a small amount of the total sorting time. However, in the general case one could consider hybrid techniques that use Algorithm 2 to sort the connected components of the subgraphs \(G_{ij}\) followed by a heuristic or an MIP-based approach to merge the computed preorders. Another avenue for further research is to study whether the dual-flow formulations perform well on instances where no staircase ordering exists, but where adding a low number of edges leads to a fulfilment of the staircase property. Since Theorem 4.3 shows that finding an ordering with minimal staircase relaxation remains NP-complete even if all subgraphs \(G_{ij}\) are connected, it is especially interesting to study whether there exist interesting polynomially solvable subcases of this problem.