On Recognizing Staircase Compatibility

For the problem to find an m-clique in an m-partite graph, staircase compatibility has recently been introduced as a polynomial-time solvable special case. It is a property of a graph together with an m-partition of the vertex set and total orders on each subset of the partition. In optimization problems involving m-cliques in m-partite graphs as a subproblem, it allows for totally unimodular linear programming formulations, which have shown to efficiently solve problems from different applications. In this work, we address questions concerning the recognizability of this property in the case where the m-partition of the graph is given, but suitable total orders are to be determined. While finding these total orders is NP-hard in general, we give several conditions under which it can be done in polynomial time. For bipartite graphs, we present a polynomial-time algorithm to recognize staircase compatibility and show that staircase total orders are unique up to a small set of reordering operations. On m-partite graphs, where the recognition problem is NP-complete in the general case, we identify a polynomially solvable subcase and also provide a corresponding algorithm to compute the total orders. Finally, we evaluate the performance of our ordering algorithm for m-partite graphs on a set of artificial instances as well as real-world instances from a railway timetabling application. It turns out that applying the ordering algorithm to the real-world instances and subsequently solving the problem via the aforementioned totally unimodular reformulations indeed outperforms a generic formulation which does not exploit staircase compatibility.


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 resorted 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 polyno-mial 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 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 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 i j 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 i j 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.

Notation
The following notation is used throughout the article. We denote the neighbourhood of a vertex u ∈ V in an undirected graph G = (V , E) by N G (u):={v ∈ V | {u, v} ∈ E}, and for subsets U ⊆ V we analogously write N G (U ):= u∈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 mpartite graphs.
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 i j and can be used to efficiently solve certain clique problems on the graph. It is defined as follows.
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 i j . The two conditions are illustrated in Fig. 1. Note that both (SC1) and (SC2) are trivially satisfied for complete bipartite subgraphs G i j . Therefore, it is sufficient for the ordering on G to be staircase if they hold Fig. 1 Illustration of the two staircase conditions. If the solid edges are contained in the graph, the dashed ones must be contained as well 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 i j does not contain vertices with degree zero. In the next definition, we canonically extend the notion of a staircase ordering onto a partition V on V .
We close this section with some notation used to more concisely describe our algorithms.

Definition 2.4 (Minimal and Maximal Neighbour Index) Let
be a bipartite graph with total orders > 1 , > 2 on V 1 and V 2 , respectively, and u ∈ V 1 with deg G (u) ≥ 1. We write for the minimal and maximal index of any neighbour of u in V 2 in the total order > 2 .

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.

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.
. . , v |V 2 | } be a bipartite graph and consider the canonical ordering on G, which fulfils the following two conditions: We are able to show that it can be checked in polynomial time whether the canonical ordering on G is staircase.
Proof Note that for any triplet u i , u j , u k ∈ 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.

Algorithm 1 Recognizing a staircase ordering on a bipartite graph
. . , v |V 2 | } Output: Returns whether the canonical ordering on G is staircase return "The canonical ordering violates (SC1)" 3: end if 4: for i ∈ {2, . . . , return "The canonical ordering violates (SC2)" 7: return "The canonical ordering violates (SC1)" 10: end if 11: end for 12: return "The canonical ordering on G is staircase" In Line 1, we directly check (SC1) for the neighbourhood of u 1 . Afterwards, we check (SC1) for all remaining u i ∈ 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 ∈ V 1 . Suppose there exists some i ∈ {2, . . . , |V 1 |} such that k:=λ V 2 (u i ) < j:=λ V 2 (u i−1 ), i.e. we fulfil the first condition of the check However, this is a contradiction to (SC2). Therefore, In this case, we can find 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.
Hence, for some i ∈ {i 1 + 1, . . . , i 2 } the check in Line 5 fails. Analogously, either of the checks in Line 5 or 8 must fail if is violated, the check in Line 1 or the one in Line 8 fails, depending on whether u = u 1 or u ∈ {u 2 , . . . , u |V 1 | }. Otherwise, we can find u 1 , u 2 , u 3 ∈ V 1 , v ∈ V 2 such that (SC1) is violated, i.e. we have {u 1 , v}, {u 3 , v} ∈ E, while {u 2 , v} / ∈ E. However, the vertex u 2 is by assumption not isolated and we can therefore find some vertex v ∈ V 2 with {u 2 , v } ∈ E. As Fig. 2 illustrates, the quadruple u 1 , 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 λ V 2 (u) and λ V 2 (u) as well as the preprocessing step to identify the connected components of G can be done within this amount of time.

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 ∪ V 2 , E), the following two conditions w.l.o.g.: 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
Further, let u, v, w ∈ 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 | ≤ |P uw |.
Proof We prove the claim by contradiction. Suppose there exist vertices u, v, w ∈ V 1 with u > 1 v > 1 w and shortest paths P uv = (u, . . . , v), P uw = (u, . . . , 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: , 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 / ∈ N (x) must hold. However, Fig. 3 illustrates that this already violates (SC1).
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.
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 Although the paths P uv and P uw are of equal length, the ordering shown is not staircase since the quadruple u, v, x, y violates (SC2) 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 ∈ V 1 with u > 1 v > 1 w, and analogously for vertices in V 2 , we have P uv ≤ 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. (1) and (2). Then, a staircase ordering {> 1 , > 2 } on G is unique up to reversal.

be a bipartite graph which satisfies Conditions
Proof Suppose there are two different staircase orderings {> 1 , > 2 } and { 1 , 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 ∈ V 1 and x, y ∈ V 2 that induce the subgraph shown in Fig. 5.
In this subgraph, we have 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 ∪ V 2 | ≥ 5. Moreover, neither > 1 and 1 nor > 2 and 2 coincide and we can assume w.l.o.g. that there exist vertices u, v ∈ V 1 such that 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 ∈ V 1 with u > 1 v > 1 w and u 1 w 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, . . . , 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} ∈ 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) ∩ N (v) and N (u) ∩ N (w) are both non-empty. From (SC1) and u This theorem fully characterizes the operations which can be applied to a staircase ordering to retain the staircase property.

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.
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 ∈ 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 ∈ 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.

Algorithm 2 Finding a staircase ordering on a bipartite graph
Lists L 1 , L 2 of the vertices in V 1 , V 2 representing a staircase ordering on G or the result that no staircase ordering exists 1: for r ∈ V 1 do 2: Apply breadth-first search to G starting in r to sort vertices in shortest path from r to v is of length i}, and let i max be the length of the longest shortest path in G starting in r . 3: Sort vertices u ∈ N i in descending order by their degree in H . 7: if i < i max then 8: Let H the subgraph of G induced by N i ∪ N i+1 9: Sort vertices u ∈ N i with equal degree in H in ascending order by their degree in H . 10: end if 11: if i even then 12: Append the (now ordered) elements in N i to the list L 2 13: else 14: Append the (now ordered) elements in N i to the list L 1 15: end if 20: end for 21: return "A staircase ordering on G does not exist" 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 ∈ 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 .
This case can only occur if |N i | > 1 as well as The vertex x, in turn, must be connected to some vertex w ∈ N i−2 . As illustrated in Fig. 7a, v > * u must hold as otherwise (SC1) is violated.
Here, |N i | > 1 must hold and we therefore have i ≥ 1. Analogously to the previous case, we can find a vertex x ∈ N i+1 with {u, x} ∈ E and {v, x} / ∈ E. Figure 7b illustrates that, in order to not violate (SC2), v > * u must hold.
Suppose there are vertices x, y ∈ N i−1 with {u, x}, {v, y} ∈ E, {v, x}, {u, y} / ∈ E, which can only occur if i ≥ 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 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.
) via counting sort. The time complexity of the combined time required for sorting can be bounded by the estimate 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.
In Sects. 3.1-3.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.
given and it is to be determined whether V is a staircase partition, i.e. if there exist corresponding staircase orderings {> 1 , . . . , > m } on its subsets.
It can be shown that recognizing staircase partitions in the general case is NPcomplete. 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 i j 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.

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].
The problem to decide whether a staircase ordering on G exists is NP-complete, even if the dependency graph 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 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) ∈ 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 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 u, v, w 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 G is a tree of depth 2 with root vertex V 0 and leaves V t , t ∈ 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 ∈ 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 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 i j 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.

A Polynomial Special Case
In this subsection, we will show that the problem becomes polynomially solvable if all G i j are connected. Before proving this statement, we introduce preorders as a way to represent all staircase orderings on a given connected subgraph G i j 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 ≥ i and i be preorders on some vertex set V i . The composed preorder of ≥ i and i is defined as A preorder ≥ i represents a total order > i if for all u, v ∈ V i we have As mentioned in Sect. 3.2, there are two ways to modify an ordering on a connected bipartite graph G i j while maintaining the staircase property of the ordering. First, we can simultaneously reverse the ordering and second, we can switch places of vertices 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 i j and for k ∈ {i, j} consider the total preorders given by As a consequence of Theorem 3.1, the preorders ≥ i , ≥ j then represent, up to reversal, exactly the staircase orderings {> i , > j } on G i j . Lemma 4.1 formalizes the merging step of two preorders ≥ i and i on some V i , and Example 4.1 illustrates the procedure.
Lemma 4.1 Let G i j and G jk be bipartite connected subgraphs of some graph G with total preorders ≥ i , ≥ j and j , k representing a set of staircase orderings on G i j and G jk , respectively. There exist total orders > i , > j , > k fulfilling the conditions if and only if the composed preorder j of ≥ j and j is total. Moreover, the respective total preorders ≥ i , j , 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 ∈ V j . If u > j v, then u ≥ j v ∧ u j v must hold, which implies u j v. Analogously, we obtain v j u if v > j u. Since u and v were chosen arbitrarily, the preorder j is total. Moreover, since the total orders > i , > j , > k were chosen arbitrarily and j indeed represents > j , the respective preorders ≥ i , j , k represent all total orders > i , > j , > k fulfilling Conditions (6a)-(6e). Conversely, let the composed preorder j be total and > j a total order represented by j . Further, let > i and > k be two total orders that are represented by ≥ i and k , respectively. The total order > j is represented by both ≥ j and j , since j is the composition of these two preorders. Consequently, the total orders > i , > j , and Fig. 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 > k fulfil Conditions (6a)-(6e). Moreover, since the three total orders were chosen arbitrarily, any three total orders represented by ≥ i , j , and k , respectively, fulfil Conditions (6a)-(6e). 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 ≈ 2 v 1 , respectively. As a result, we have v 3 = 2 v 2 ≥ 2 v 1 and v 3 2 v 2 ≈ 2 v 1 . The composed preorder, which is indeed total, is then given by v 3 2 v 2 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 ≈ 2 v 2 2 v 3 on V 2 . In this case, the composed preorder is no longer total due to v 3 ≥ 2 v 1 and v 1 2 v 3 . Indeed, there is no staircase ordering on G represented by ≥ i , j , and k .

Example 4.1 Consider the graph G shown in
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.
Composing the preorders ≥ j and j in Fig. 9a leads to a total preorder, whereas composing ≥ j and the reverse of 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.
If i and i are both total, then at least one of the preorders ≥ i and i is trivial.
Proof Suppose ≥ i is not trivial, i.e. there exist verticesũ,ṽ ∈ V i with ¬(ũ ≥ iṽ ), and let u, v ∈ V i . We distinguish three cases and show that u i v always holds.
Analogously to the previous case, we obtain u i v via the preorder i .
In this case, we find a vertex w ∈ V i with either Similarly to the first two cases we obtain u i w and w i v using i and i . Transitivity of i then implies u i v.
This shows that i is trivial.
In other words, if we have two nontrivial preorders ≥ i and i , then composing ≥ i and i or composing ≥ i and the reverse of i yields a nontotal composed preorder. We proceed to prove the main theorem of this section, which states that, if all subgraphs G i j 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 V = {V 1 , . . . , V m } of the vertex set such that all G i j 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 i j 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 i j . If the dependency graph 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 G is connected as well.
It is clear that a staircase ordering on G cannot exist if Algorithm 2 determines that for some G i j 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 i j , which yields new total preorders i , j on V i and V j , respectively. Suppose there already exists some total preorder ≥ i on V i . Then it remains to check the composed preorder If the composed preorder is not total, then by Lemma 4.1 there exists no staircase ordering on G represented by ≥ i and i and we attempt the composition with the reverse of i . We want to emphasize that applying Algorithm 2 to some G i j yields two total preorders ≥ i , ≥ 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 i j . This includes the case where, for example, ≥ 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 ≥ i and i is valid. If the breadth-first search finishes and the last merging step succeeds, we have total preorders on all V i ∈ 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.

Algorithm 3
Finding a staircase ordering on m-partite graphs with connected subgraphs G i j Input: A graph G = (V , E) together with a partition V of its vertex set V and the property that all subgraphs G i j are connected Output: Returns a staircase ordering or the message that no staircase ordering exists 1: Initialize total preorders on all V i ∈ V as u ≥ v ∀u, v ∈ V i . Select root vertex R ∈ V 2: for edge {i, j} being traversed by the breadth-first search starting in R do 3: Sort G i j via Algorithm 2 4: if No staircase ordering on G i j exists then 5: return "A staircase ordering on G does not exist" 6: end if 7: Compute corresponding preorders on V i and V j 8: Unify the new preorders with existing preorders on V i and V j 9: if Unification failed then 10: return "A staircase ordering on G does not exist" 11: end if 12: end for 13: return The computed staircase ordering on G 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 G at most once, Algorithm 2 runs in polynomial time, and finally, each merging step can be done in polynomial time as well.
It is worth mentioning that, since the instances in the proof of Theorem 4.1 may feature subgraphs G i j with isolated vertices, it does not state that deciding whether staircase sorting is hard under the assumption of non-empty neighbourhoods in each G i j , 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 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 i j are connected and each V i ∈ V contains at most two elements. V = {V 1 , . . . , V m } of the vertex set such that all G i j 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.

Theorem 4.3 Let G be an m-partite graph with a 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 Hence, all resulting bipartite subgraphs G i j are connected. Therefore, sorting u < u and v < 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 S, T corresponds to a choice for total orderings on the with v > v ∀v ∈ T , and vice versa.

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 Net-workX 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.

Computational Experiments on Random Instances
The artificial instances in our first benchmark set were generated as follows. First, we created a dependency graph G = (V, E) with m vertices and E = ∅. For each pair of vertices V i , V j ∈ V, i = j in G, we then added the edge {V i , V j } with probability p ∈ [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 G and iteratively added the corresponding edge {V i , V j } until the entire dependency graph was connected. If the so-obtained 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 i j (see below). From this dependency graph G, we then generated G as follows. For each V i ∈ V, we randomly drew a subset size n i ∈ [n, n], n, n ∈ N + and added the vertices v i,1 , . . . , v i,n i to subset V i . For any two subsets V i , V j ∈ V with {V i , V j } / ∈ E, the corresponding subgraph G i j is, in accordance with the definition of the dependency graph G, complete bipartite. Finally, for each edge {V i , V j } ∈ E, we use Algorithm 4 as a generator for the (connected) subgraph G i j .
After initializing the vertex sets V i and V k , the edge set E as well as the auxiliary variables minIdx, 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 i j to be connected by connecting the remaining vertices in V j to v i,n i . If the algorithm chooses minIdx = 1 and maxIdx = n j in all iterations, then the resulting G i j 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 i j .
To test the performance of Algorithm 3 on non-staircase instances, we added a nonstaircase duplicate of each instance to our test set. Since Algorithm 2 already detects Algorithm 4 Generator for bipartite graphs with staircase canonical ordering Input: Subset sizes n i , n j ∈ N + Output: A bipartite graph G i j such that the canonical ordering is staircase 1: V i = {v i,1 , . . . , v i,n i } 2: V j = {v j,1 , . . . , v j,n j } 3: E i j = ∅ 4: minIdx = 1 5: maxIdx = 1 6: for k = 1, 2, . . . , n i do 7: foundNewMin =False 8: while not foundNewMin do 9: Choose x ≥ 0 with probability density function f (x) = λe −λx where λ = n i n j 10: if newMinIdx ≤ n j then 12: minIdx = min{newMaxIdx, maxIdx} 13: foundNewMin =True 14: end if 15: end while 16: foundNewMax =False 17: while not foundNewMax do 18: Choose x ≥ 0 with probability density function f (x) = λe −λx where λ = n i n j 19: {round to nearest integer} 20: if minIdx ≤ newMaxIdx ≤ n j then 21: maxIdx = newMaxIdx 22: foundNewMax =True 23: end if 24: end while 25: . . , maxIdx}} 26: end for 27: E i j = E i j ∪ {{v i,k , v j,l } | l ∈ {minIdx, minIdx + 1, . . . , n j }} 28: if |E(G i j )| = n i · n j then 29: Restart algorithm 30: end if 31: if no staircase ordering exists on a given G i j , 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 i j into account. In particular, we created a set of all edges in E that are contained in at least one cycle in G and chose one of these edges uniformly at random. In the corresponding subgraph G i j = (V i ∪ V j , E i j ), we then flipped V j , i.e. we changed the edge set tõ To further improve the accuracy of our computations, we created five random instances for each set of parameters m, p, n, n used. Our result tables use the parameter naming scheme m-p-n-n, with the postfix "-nonSC" to indicate the non-staircase duplicates.
Finally, we prepend each vertex in V and V with four random lowercase letters and rebuild the graphs 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: Variables z uv ∈ F stand for the decision to order vertex u ∈ V before vertex v ∈ V for each u and v in the same subset of 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 i j 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 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 Pythonbased 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 |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.
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 i j , 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.
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 i j . The results can be seen in Table 3. Here we see that the computation time of Algorithm 3 scales linearly with |E|. Again, computation times for non-staircase instances are significantly lower, but show higher variance.
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.
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 i j 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.

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 Table 1 Performance of sorting algorithm and linear-ordering MIP on small random instances  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 i j were connected in these instances, some additional presolve was required. In particular, a vertex with degree zero in any G i j 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 i j 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 Δ(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.
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 i j 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.

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 i j 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 i j 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 Table 5 Comparison of optimization performance on railway instances