1 Introduction

Directed graphs explaining hierarchy naturally appear in multiple industrial and academic applications. Some examples include PERT diagrams, UML component diagrams, text edition networks [1], text variant graphs [19], philogenetic and neural networks. In these, and many other applications, it is essential to visualize the implied directed graph so that the viewer can perceive the hierarchical structure it contains. By far the most popular way to achieve this is to apply the Sugyiama framework – a generic network visualization algorithm that results in a drawing where each vertex lies on a horizontal line, called layer, and each edge is directed from a lower layer to a higher layer [14].

The Sugyiama framework consists of several steps: elimination of directed cycles in the initial graph, assignement of vertices to layers, vertex ordering and coordinate assignement. During each of these steps several criteria are optimized, by leading to more readable visualizations, see e.g. [14]. In this paper we concentrate on the last step of the framework, namely coordinate assignment. Thus, the subject of our study are level graphs defined as follows. Let \(G = (V, E)\) be a directed graph. A k-level assignment of G is a function \(\ell : V \rightarrow \{1, 2, \dots , k\}\) that assigns each vertex of G to one of k levels. We refer to G together with \(\ell \) as to a (k-)level graph. The length of an edge (uv) is defined as \(\ell (v) - \ell (u)\). We say that G is proper if all edges have length one. The level graph shown in Fig. 1(a) is proper, whereas the one shown in (b) is not. For a non-proper level graph G there exists a proper subdivision obtained by subdividing all edges with length greater than one which result in a proper graph.

A level drawing \(\varGamma \) of a level graph G maps each vertex \(v \in V\) to a point on the horizontal line with y-coordinate \(\ell (v)\) and a real x-coordinate \(\varGamma (v)\), and each edge to a y-monotone curve between its endpoints. A level drawing is called level-planar if no two edges intersect except in common endpoints. It is straight-line if the edges are straight lines. A level drawing of a proper (subdivision of a) level graph G induces a total left-to-right order on the vertices of a level. We say that two drawings are equivalent if they induce the same order on every level. An equivalence class of this equivalence relation is an embedding of G. We refer to G together with an embedding to as embedded level graph \(\mathcal G\). The third step of Sugyiama framework, vertex ordering, results in an embedded level graph.

The general goal of the coordinate assignment step is to produce a final visualization while further improving its readability. The criteria of readability that have been considered in the literature for this step include straightness and verticality of the edges [14]. Here we study the problem of coordinate assignment step with bounded number of slopes. The slope of an edge (uv) in \(\varGamma \) is defined as \((\varGamma (v) - \varGamma (u)) / (\ell (v) - \ell (u))\). For proper level graphs this simplifies to \(\varGamma (v) - \varGamma (u)\). We restrict our study to drawings in which all slopes are non-negative; such drawings can be transformed into drawings with negative slopes by shearing; see Fig. 1. A level drawing \(\varGamma \) is a \(\lambda \)-slope drawing if all slopes in \(\varGamma \) appear in the set \(\{0, 1, \dots , \lambda - 1\}\). We study embedding-preserving straight-line level-planar \(\lambda \)-slope drawings, or \(\lambda \)-drawings for short and refer to the problem of finding these drawings as \(\lambda \)-Drawability. Since the possible edge slopes in a \(\lambda \)-drawing are integers all vertices lie on the integer grid.

Fig. 1.
figure 1

Shearing drawings to change the slopes. In (a), the left drawing with slopes 0 and 1 is transformed into the right orthogonal drawing, i.e., one with slopes \(-1\) and 1. In (b), the left drawing with slopes 0, 1 and 2 is transformed into a drawing with slopes \(-1, 0\) and 1.

Related Work. The number of slopes used for the edges in a graph drawing can be seen as an indication of the simplicity of the drawing. For instance, the measure edge orthogonality, which specifies how close a drawing is to an orthogonal drawing, where edges are polylines consisting of horizontal and vertical segments only, has been proposed as a measure of aesthetic quality of a graph drawing [31]. In a similar spirit, Kindermann et al. studied the effect reducing the segment complexity on the aesthetics preference of graph drawings and observed that in some cases people prefer drawings using lower segment complexity [22]. More generally, the use of few slopes for a graph drawing may contribute to the formation of “Prägnanz” (“good figure” in German) of the visualization, that accordingly to the Gestalt law of Prägnanz, or law of simplicity, contributes to the perceived quality of the visualizations. This is design principle often guides the visualization of metro maps. See [28] for a survey of the existing approaches, most of which generate octilinear layouts of metro maps, and [27] for a recent model for drawing more general k-linear metro maps.

Level-planar drawing with few slopes have not been considered in the literature but drawings of undirected graphs with few slopes have been extensively studied. The (planar) slope number of a (planar) graph G is the smallest number s so that G has a (planar) straight-line drawing with edges of at most s distinct slopes. Special attention has been given to proving bounds on the (planar) slope number of undirected graph classes [4, 9,10,11,12, 20, 21, 23, 24, 29]. Determining the planar slope number is hard in the existential theory of reals [16].

Several graph visualization problems have been considered in the partial and simultaneous settings. In the partial drawing extension problem, one is presented with a graph and an immutable drawing of some subgraph thereof. The task is to determine whether the given drawing of the subgraph can be completed to a drawing of the entire graph. The problem has been studied for the planar setting [25, 30] and also the level-planar setting [8]. In the simultaneous drawing problem, one is presented with two graphs that may share some subgraph. The task is to draw both graphs so that the restrictions of both drawings to the shared subgraph are identical. We refer the reader to [5] for an older literature overview. The problem has been considered for orthogonal drawings [2] and level-planar drawings [3]. Up to our knowledge, neither partial nor simultaneous drawings have been considered in the restricted slope setting.

Contribution. We introduce and study the \(\lambda \)-Drawability problem. To solve this problem for proper level graphs, we introduce two models. In Sect. 3 we describe the first model, which uses a classic integer-circulation-based approach. This model allows us to solve the \(\lambda \)-Drawability in \(O(n \log ^3 n)\) time and obtain a \(\lambda \)-drawing within the same running time if one exists. In Sect. 4, we describe the second distance-based model. It uses the duality between flows in the primal graph and distances in the dual graph and allows us to solve the \(\lambda \)-Drawability in \(O(n \log ^2 n / \log \log n)\) time. We also address the partial and simultaneous settings. The classic integer-circulation-based approach can be used to extend connected partial \(\lambda \)-drawings in \(O(n \log ^3 n)\) time. In Sect. 5, we build on the distance-based model to extend not-necessarily-connected partial \(\lambda \)-drawings in \(O(n^{4/3} \log n)\) time, and to obtain simultaneous \(\lambda \)-drawings in \(O(\lambda n^{10/3} \log n)\) time if they exist. We finish with some concluding remarks in Sect. 6 and refer to the full version [7] for a proof that 2-Drawability is NP-hard even for biconnected graphs where all edges have length one or two.

2 Preliminaries

Let \(\varGamma \) be a level-planar drawing of an embedded level-planar graph \(\mathcal G\). The width of \(\varGamma \) is defined as \(\max _{v \in V} \varGamma (v) - \min _{v \in V} \varGamma (v)\). An integer \(\bar{x}\) is a gap in \(\varGamma \) if it is \(\varGamma (v) \ne \bar{x}\) for all \(v \in V\)\(\varGamma (v_1) < \bar{x}\) and \(\varGamma (v_2) > \bar{x}\) for some \(v_1, v_2 \in V\), and \(\varGamma (u)< \bar{x} < \varGamma (v)\) for no edge \((u, v) \in E\). A drawing \(\varGamma \) is compact if it has no gap. Note that a \(\lambda \)-drawing of a connected level-planar graph is inherently compact. While in a \(\lambda \)-drawing of a non-connected level-planar graph every gap can be eliminated by a shift. The fact that we only need to consider compact \(\lambda \)-drawings helps us to limit their width. Thus, any compact \(\lambda \)-drawing has a width of at most \((\lambda - 1)(n - 1)\).

Let u and v be two vertices on the same level i. With \([u, v]_{\mathcal G}\) (or [uv] when \(\mathcal G\) is clear from the context) we denote the set of vertices that contains uv and all vertices in between u and v on level i in \(\mathcal G\). We say that two vertices u and v are consecutive in \(\mathcal G\) when \([u, v]=\{u,v\}\). Two edges \(e = (u, w), e' = (v, x)\) are consecutive in \(\mathcal G\) when the only edges with one endpoint in \([u, v]_{\mathcal G}\) and the other endpoint in \([w, x]_{\mathcal G}\) are e and \(e'\).

A flow network \(F = (N,A)\) consists of a set of nodes N connected by a set of directed arcs A. Each arc has a demand specified by a function \(d : A \rightarrow \mathbb N_0\) and a capacity specified by a function \(c: A \rightarrow \mathbb N \cup \{\infty \}\) where \(\infty \) encodes unlimited capacity. A circulation in F is a function \(\varphi : A \rightarrow \mathbb N_0\) that assigns an integral flow to each arc of F and satisifies the two following conditions. First, the circulation has to respect the demands and capacities of the arcs, i.e., for each arc \(a \in A\) it is \(d(a) \le \varphi (a) \le c(a)\). Second, the circulation has to respect flow conservation, i.e., for each node \(v \in N\) it is \(\sum _{(u, v) \in A} \varphi (u, v) = \sum _{(v, u) \in A} \varphi (v, u)\). Depending on the flow network no circulation may exist.

3 Flow Model

In this section, we model the \(\lambda \)-Drawability as a problem of finding a circulation in a flow network. Let \(\mathcal G\) be an embedded proper k-level graph together with a level-planar drawing \(\varGamma \). As a first step, we add two directed paths \(p_{{{\,\mathrm{left}\,}}}\) and \(p_{{{\,\mathrm{right}\,}}}\) that consist of one vertex on each level from 1 to k to \(\mathcal G\). Insert \(p_{{{\,\mathrm{left}\,}}}\) and \(p_{{{\,\mathrm{right}\,}}}\) into \(\varGamma \) to the left and right of all other vertices as the left and right boundary, respectively. See Fig. 2(a) and (c). From now on, we assume that \(\mathcal G\) and \(\varGamma \) contain the left and right boundary.

Fig. 2.
figure 2

(a) An embedded level graph \(\mathcal G\). (b) The definition of the arcs of the flow network. (c) The graph \(\mathcal G\) together with the paths \(p_{{{\,\mathrm{left}\,}}}\) and \(p_{{{\,\mathrm{right}\,}}}\) in black. The resulting flow network \(F_{\mathcal G}^\lambda \) (c) consists of the blue slope arcs and the red space arcs, its nodes are formed by merging the nodes in the gray areas. The red space arcs have a demand of 1 and a capacity of \((\lambda - 1)(n - 1)\) and the blue slope arcs have a demand of zero and a capacity of \(\lambda - 1\).

The flow network \(F_{\mathcal G}^\lambda \) consists of nodes and arcs and is similar to a directed dual of \(\mathcal G\) with the difference that it takes the levels of \(\mathcal G\) into account. In particular, for every edge e of \(\mathcal G\)\(F_{\mathcal G}^\lambda \) contains two nodes \(e_{{{\,\mathrm{left}\,}}}\) and \(e_{{{\,\mathrm{right}\,}}}\), in the left and the right faces incident to e, and a dual slope arc \(e^\star = (e_{{{\,\mathrm{right}\,}}}, e_{{{\,\mathrm{left}\,}}})\) with demand 0 and capacity \(\lambda - 1\); see the blue arcs in Fig. 2(b) and (c). The flow across \(e^\star \) determines the slope of e. Additionally, for every pair of consecutive vertices uv we add two nodes \([u, v]_{{{\,\mathrm{low}\,}}}\) and \([u, v]_{{{\,\mathrm{high}\,}}}\) to \(F_{\mathcal G}^\lambda \) and connect them by a space arc \([u, v]^\star \); see the red arcs in Fig. 2(b) and (c). The flow across \([u, v]^\star \) determines the space between u and v. The space between u and v needs to be at least one to prevent u and v from colliding and can be at most \((\lambda - 1)(n - 1)\) due to the restriction to compact drawings. So, assign to \([u, v]^\star \) a demand of one and a capacity of \((\lambda - 1)(n - 1)\). To obtain the final flow network we merge certain nodes. Let \(e = (u, w)\) and \(e' = (v, x)\) be consecutive edges. Merge the nodes \(e_{{{\,\mathrm{right}\,}}}, e'_{{{\,\mathrm{left}\,}}}\), the nodes \(\{ \{u',v'\}_{{{\,\mathrm{high}\,}}} : \forall u', v' \text { consecutive in }[u, v]\}\) and the nodes \(\{ \{w', x'\}_{{{\,\mathrm{low}\,}}} : \forall w', x' \text { consecutive in }[w, x]\}\) into a single node. Next, merge all remaining source and sink nodes into one source node s and one sink node t, respectively. See Fig. 2(c), where the gray areas touch nodes that are merged into a single node. Finally, insert an arc from t to s with unlimited capacity.

The network \(F_{\mathcal G}^\lambda \) is designed in such a way that the circulations in \(F_{\mathcal G}^\lambda \) correspond bijectively to the \(\lambda \)-drawings of \(\mathcal G\). Let \(\varGamma \) be a drawing of \(\mathcal G\) and let x be the function that assigns to each vertex of \(\mathcal G\) its x-coordinate in \(\varGamma \). We define a dual circulation \(x^\star \) as follows. Recall that every arc a of \(F_{\mathcal G}^\lambda \) is either dual to an edge of \(\mathcal G\) or to two consecutive vertices in \(\mathcal G\). Hence, the left and right incident faces \(f_{{{\,\mathrm{left}\,}}}\) and \(f_{{{\,\mathrm{right}\,}}}\) of a in \(F_{\mathcal G}^\lambda \) contain vertices of \(\mathcal G\). Define the circulation \(x^\star \) by setting \(x^\star (a) := x(f_{{{\,\mathrm{right}\,}}}) - x(f_{{{\,\mathrm{left}\,}}})\). We remark the following, although we defer the proof to the next section.

Lemma 1

Let \(\mathcal G\) be an embedded proper level-planar graph together with a \(\lambda \)-drawing \(\varGamma \). The dual \(x^\star \) of the function x that assigns to each vertex of \(\mathcal G\) its x-coordinate in \(\varGamma \) is a circulation in \(F_{\mathcal G}^\lambda \).

In the reverse direction, given a circulation \(\varphi \) in \(F_{\mathcal G}^\lambda \) we define a dual function \(\varphi ^\star \) that, when interpeted as assigning an x-coordinate to the vertices of \(\mathcal G\), defines a \(\lambda \)-drawing of G. Refer to the level-1-vertex \(p_{{{\,\mathrm{right}\,}}}\) as \(v_{{{\,\mathrm{right}\,}}}\). Start by setting \(\varphi ^\star (v_{{{\,\mathrm{right}\,}}}) = 0\), i.e., the x-coordinate of \(v_{{{\,\mathrm{right}\,}}}\) is 0. Process the remaining vertices of the right boundary in ascending order with respect to their levels. Let (uv) be an edge of the right boundary so that u has already been processed and v has not been processed yet. Then set \(\varphi ^\star (v) = \varphi ^\star (u) + \varphi ((u, v)^\star )\), where \((u, v)^\star \) is the slope arc dual to (uv). Let wx be a pair of consecutive vertices so that x has already been processed and w has not yet been processed yet. Then set \(\varphi ^\star (w) = \varphi ^\star (x) + \varphi ([w, x]^\star )\), where \([w, x]^\star \) is a space arc. It turns out that \(\varphi ^\star \) defines a \(\lambda \)-drawing of \(\mathcal G\).

Lemma 2

Let \(\mathcal G\) be an embedded proper level-planar graph, let \(\lambda \in {\mathbb N}\) and let \(\varphi \) be a circulation in \(F_{\mathcal G}^\lambda \). Then the dual \(\varphi ^\star \), when interpeted as assigning an x-coordinate to the vertices of \(\mathcal G\), defines a \(\lambda \)-drawing of G.

While both Lemmas 1 and 2 can be proven directly, we defer their proofs to Sect. 4 where we introduce the distance model and prove Lemmas 3 and 4, the stronger versions of Lemmas 1 and 2, respectively. Combining Lemmas 1 and 2 we obtain the following.

Theorem 1

Let \(\mathcal G\) be an embedded proper level-planar graph and let \(\lambda \in {\mathbb N}\). The circulations in \(F_{\mathcal G}^\lambda \) correspond bijectively to the \(\lambda \)-drawings of \(\mathcal G\).

Theorem 1 implies that a \(\lambda \)-drawing can be found by applying existing flow algorithms to \(F_{\mathcal G}^\lambda \). For that, we transform our flow network with arc demands to the standard maximum flow setting without demands by introducing new sources and sinks. We can then use the \(O(n \log ^3 n)\)-time multiple-source multiple-sink maximum flow algorithm due to Borradaile et al. [6] to find a circulation in \(F_{\mathcal G}^\lambda \) or to determine that no circulation exists.

Corollary 1

Let \(\mathcal G\) be an embedded proper level-planar graph and let \(\lambda \in {\mathbb N}\). It can be tested in \(O(n \log ^3 n)\) time whether a \(\lambda \)-drawing of \(\mathcal G\) exists, and if so, such a drawing can be found within the same running time.

3.1 Connected Partial Drawings

Recall that a partial \(\lambda \)-drawing is a tuple \((\mathcal G, \mathcal H, \varPi )\), where \(\mathcal G\) is an embedded level-planar graph, \(\mathcal H\) is an embedded subgraph of \(\mathcal G\) and \(\varPi \) is a \(\lambda \)-drawing of \(\mathcal H\). We say that \((\mathcal G, \mathcal H, \varPi )\) is \(\lambda \)-extendable if \(\mathcal G\) admits a \(\lambda \)-drawing \(\varGamma \) whose restriction to \(\mathcal H\) is \(\varPi \). Here \(\varGamma \) is referred to as a \(\lambda \)-extension of \((\mathcal G, \mathcal H, \varPi )\).

In this section we show that in case \(\mathcal H\) is connected, we can use the flow model to decide whether \((\mathcal G, \mathcal H, \varPi )\) is \(\lambda \)-extendable. Observe that when \(\mathcal H\) is connected \(\varPi \) is completely defined by the slopes of the edges in \(\mathcal H\) up to horizontal translation. Let \(F_{\mathcal G}^\lambda \) be the flow network corresponding to \(\mathcal G\). In order to fix the slopes of an edge e of \(\mathcal H\) to a value \(\ell \), we fix the flow across the dual slope arc \(e^\star \) in \(\mathcal H\) to \(\ell \). Checking whether a circulation in the resulting flow network exists can be reduced to a multiple-source multiple-sink maximum flow problem, which once again can be solved by the algorithm due to Borradaile et al. [6].

Corollary 2

Let \((\mathcal G, \mathcal H, \varPi )\) be a partial \(\lambda \)-drawing where \(\mathcal H\) is connected. It can be tested in \(O(n \log ^3 n)\) time whether \((\mathcal G, \mathcal H, \varPi )\) is \(\lambda \)-extendable, and if so, a corresponding \(\lambda \)-extension can be constructed within the same running time.

4 Dual Distance Model

A minimum cut (and, equivalently, the value of the maximum flow) of an st-planar graph G can be determined by computing a shortest \((s^\star , t^\star )\)-path in a dual of G [17, 18]. Hassin showed that to construct a flow, it is sufficient to compute the distances from \(s^\star \) to all other vertices in the dual graph [13]. To the best of our knowledge, this duality has been exploited only for flow networks with arc capacities, but not with arc demands. In this section, we extend this duality to arcs with demands. The resulting dual distance model improves the running time for the \(\lambda \)-Drawability, lets us test the existence of \(\lambda \)-extensions of partial \(\lambda \)-drawings for non-connected subgraphs, and allows us to develop an efficient algorithm for testing the existence of simultaneous \(\lambda \)-drawings.

We define \(D_{\mathcal G}^\lambda \) to be the directed dual of \(F_{\mathcal G}^\lambda \) as follows. Let \(a = (u, v)\) be an arc of \(F_{\mathcal G}^\lambda \) with demand d(a) and capacity c(a). Further, let \(f_{{{\,\mathrm{left}\,}}}\) and \(f_{{{\,\mathrm{right}\,}}}\) denote the left and the right faces of a in \(F_{\mathcal G}^\lambda \), respectively. The dual \(D_{\mathcal G}^\lambda \) contains \(f_{{{\,\mathrm{left}\,}}}\) and \(f_{{{\,\mathrm{right}\,}}}\) as vertices connected by one edge \((f_{{{\,\mathrm{left}\,}}}, f_{{{\,\mathrm{right}\,}}})\) with length c(a) and another edge \((f_{{{\,\mathrm{right}\,}}}, f_{{{\,\mathrm{left}\,}}})\) with length \(-d(a)\); see Fig. 3.

Observe that to obtain \(F_{\mathcal G}^\lambda \) from \(\mathcal G\) (with left and right paths \(p_{{{\,\mathrm{left}\,}}}\) and \(p_{{{\,\mathrm{right}\,}}}\)) we added dual arcs to edges of \(\mathcal G\) and dual arcs to the space between two consecutive vertices on one level. Consider for a moment the graph \(\mathcal G'\) obtained from \(\mathcal G\) by adding edges (uv) for all consecutive vertices u v, where u is to the right of v. Graph \(G'\) and  \(D_{\mathcal G}^\lambda \) are identical and therefore  \(D_{\mathcal G}^\lambda \) has the vertex set V of \(\mathcal G\) and contains a subset of its edges. Recall that the dual slope arcs in \(F_{\mathcal G}^\lambda \) have demand 0 and capacity \(\lambda -1\), therefore the edges of  \(D_{\mathcal G}^\lambda \) that connect vertices on different layers have non-negative length. While the edges of \(D_{\mathcal G}^\lambda \) between consecutive vertices on the same level have length \(-1\).

Fig. 3.
figure 3

Definition of the dual edges for a flow network arc \(a = (u, v)\) with demand d(a) and capacity c(a). Let \(f_{{{\,\mathrm{left}\,}}}\) and \(f_{{{\,\mathrm{right}\,}}}\) denote the vertices corresponding to the faces to the left and right of a in \(F_{\mathcal G}^\lambda \). Then add the edge \((f_{{{\,\mathrm{left}\,}}}, f_{{{\,\mathrm{right}\,}}})\) with length c(a) and the reverse edge \((f_{{{\,\mathrm{right}\,}}}, f_{{{\,\mathrm{left}\,}}})\) with length \(-d(a)\). Edges with infinite length are not created because they do not add constraints.

Fig. 4.
figure 4

The distance network \(D_{\mathcal G}^2\) obtained from the flow network \(F_{\mathcal G}^2\) shown in Fig. 2(c). The x-coordinate of every vertex is its distance from \(v_{{{\,\mathrm{right}\,}}}\) in \(D_{\mathcal G}^2\). All red arcs have length \(-1\), all blue arcs pointing up have length 1 and all blue arcs pointing down have length 0. For every red arc there exists an arc in the reverse direction with length \(\infty \). We omit these arcs because they do not impose any constraints on the shortest distance labeling.

A distance labeling is a function \(x: V \rightarrow \mathbb Z\) that for every edge (uv) of \(D_{\mathcal G}^\lambda \) with length l satisfies \(x(v) \le x(u) + l\). We also say that (uv) imposes the distance constraint \(x(v) \le x(u) + l\). A distance labeling for \(D_{\mathcal G}^\lambda \) is the x-coordinate assignment for a \(\lambda \)-drawing: For an edge (uv) of \(D_{\mathcal G}^\lambda \) where uv are consecutive vertices in \(\mathcal G\), the distance labeling guarantees \(x(v) \le x(u)-1\), i.e., the consecutive vertices are in the correct order and do not overlap. If an edge (uv) between layers has length \(\lambda - 1\), then the distance labeling ensures \(x(v) \le x(u) + \lambda - 1\), i.e., (uv) has a slope in \(\{0,\dots , \lambda -1\}\). Computing the shortest distances from \(v_{{{\,\mathrm{right}\,}}}\) in \(D_{\mathcal G}^\lambda \) to every vertex (if they are well-defined) gives a distance labeling that we refer to as the shortest distance labeling. A distance labeling of \(D_{\mathcal G}^\lambda \) does not necessarily exist. This is the case when \(D_{\mathcal G}^\lambda \) contains a negative cycle, e.g., when the in- or out-degree of a vertex in \(\mathcal G\) is strictly larger than \(\lambda \). For a distance labeling x of \(D_{\mathcal G}^\lambda \) we define a dual circulation \(x^\star \) by setting \(x^\star (a) := x(f_{{{\,\mathrm{right}\,}}}) - x(f_{{{\,\mathrm{left}\,}}})\) for each arc a of \(F_{\mathcal G}^\lambda \) with left and right incident faces \(f_{{{\,\mathrm{left}\,}}}\) and \(f_{{{\,\mathrm{right}\,}}}\) (Fig. 4).

Lemma 3

Let \(\mathcal G\) be an embedded level-planar graph and \(\varGamma \) be a \(\lambda \)-drawing of \(\mathcal G\). The function x that assigns to each vertex of \(\mathcal G\) its x-coordinate in \(\varGamma \) is a distance labeling of \(D_{\mathcal G}^\lambda \) and its dual \(x^\star \) is a circulation in \(F_{\mathcal G}^\lambda \).

Proof

Since \(\varGamma \) preserves the embedding of \(\mathcal G\), for each consecutive vertices v,  u, with v preceeding u in  \(\mathcal G\) it holds that \(\varGamma (v)<\varGamma (u)\). Since \(\varGamma \) is a grid drawing  \(\varGamma (v) \le \varGamma (u) - 1\), which implies \(x(v)\le x(u) + \ell \), where \(\ell =-1\) is the length of (uv). Since  \(\varGamma \) is a \(\lambda \)-drawing, i.e. every edge (uv) between the two levels has a slope in \(\{0,\dots \lambda -1\}\), it holds that \(\varGamma (u) \le \varGamma (v) \le \varGamma (u) + \lambda -1\), which implies \(x(u) \le x(v)+0\), for the edge (vu) of \(D_{\mathcal G}^\lambda \) with length zero and \(x(v)<x(u)+\lambda -1\) for the edge (uv) of \(D_{\mathcal G}^\lambda \) with length \(\lambda -1\). Hence, x is a distance labeling of \(D_{\mathcal G}^\lambda \).

We now show that \(x^\star \) is a circulation in \(F_{\mathcal G}^\lambda \). Let \(f_1, f_2, \dots , f_t, f_{t + 1} = f_1\) be the faces incident to some node v of \(F_{\mathcal G}^\lambda \) in counter-clockwise order. Let a be the arc incident to v and dual to the edge between \(f_i\) and \( f_{i + 1}\) with \(1 \le i \le t\). If a is an incoming arc, it adds a flow of \(x(f_{i + 1}) - x(f_{i})\) to v. If a is an outgoing arc, it removes a flow of \(x(f_i) - x(f_{i + 1})\) from v, or, equivalently, it adds a flow of \(x(f_{i + 1}) - x(f_{i})\) to v. Therefore, the flow through v is \(\sum _i \left( x(f_{i + 1}) - x(f_{i})\right) \). This sum cancels to zero, i.e., the flow is preserved at v. Recall that the edge \((f_{{{\,\mathrm{left}\,}}}, f_{{{\,\mathrm{right}\,}}})\) with length c(a) in \(D_{\mathcal G}^\lambda \) ensures \(x(f_{{{\,\mathrm{right}\,}}}) \le x(f_{{{\,\mathrm{left}\,}}}) + c(a)\), which gives \(x^\star (a) \le c(a)\). So, no capacities are exceeded. Analogously, the edge \((f_{{{\,\mathrm{right}\,}}}, f_{{{\,\mathrm{left}\,}}})\) with length \(-d(a)\) in \(D_{\mathcal G}^\lambda \) ensures \(x(f_{{{\,\mathrm{left}\,}}}) \le x(f_{{{\,\mathrm{right}\,}}}) - d(a)\), which gives \(x^\star (a) \ge d(a)\). Hence, all demands are fulfilled and \(x^\star \) is indeed a circulation in \(F_{\mathcal G}^\lambda \).    \(\square \)

Recall from Sect. 3 that for a circulation \(\varphi \) in \(F_{\mathcal G}^\lambda \) we define a dual drawing \(\varphi ^\star \) by setting the x-coordinates of the vertices of \(\mathcal G\) as follows. For the lowest vertex of the right boundary set \(\varphi ^\star (v_{{{\,\mathrm{right}\,}}}) = 0\). Process the remaining vertices of the right boundary in ascending order with respect to their levels. Let (uv) be an edge of the right boundary so that u has already been processed and v has not been processed yet. Then set \(\varphi ^\star (v) = \varphi ^\star (u) + \varphi ((u, v)^\star )\), where \((u, v)^\star \) is the slope arc dual to (uv). Let wx be a pair of consecutive vertices so that x has already been processed and w has not yet been processed yet. Then set \(\varphi ^\star (w) = \varphi ^\star (x) + \varphi ([w, x]^\star )\), where \([w, x]^\star \) is a space arc. It turns out that \(\varphi ^\star \) is a distance labeling of \(D_{\mathcal G}^\lambda \) and a \(\lambda \)-drawing of \(\mathcal G\).

Lemma 4

Let \(\mathcal G\) be an embedded level-planar graph, let \(\lambda \in \mathbb N\), and let \(\varphi \) be a circulation in \(F_{\mathcal G}^\lambda \). The dual \(\varphi ^\star \) is a distance labeling of \(D_{\mathcal G}^\lambda \) and the drawing induced by interpreting the distance label of a vertex as its x-coordinate is a \(\lambda \)-drawing of \(\mathcal G\).

Proof

We show that \(\varphi ^\star \) is a distance labeling in \(D_{\mathcal G}^\lambda \). The algorithm described above assings a value to every vertex of \(D_{\mathcal G}^\lambda \). We now show that \(\varphi ^\star \) is indeed a distance labeling by showing that every edge satisfies a distance constraint.

Observe that the distance constraints imposed by edges dual to the space arcs are satisfied by construction. To show that the distance constraints imposed by edges dual to the slope arcs are also satisfied, we prove that for every edge (uv), it holds that \(\varphi ^\star (v) = \varphi ^\star (u) + \varphi ((u, v)^\star )\). We refer to this as condition \(\mathcal C\) for short. Since \(\varphi ((u, v)^\star ) \le \lambda -1\) and the length \(\ell \) of (uv) is \(\lambda -1\) we obtain \(\varphi ^\star (v) = \varphi ^\star (u) + \ell \), which implied that \(\phi ^\star \) is a distance labeling of \(D_{\mathcal G}^\lambda \).

The proof is by induction based on the bottom to top and right to left order among the edges of \(D_{\mathcal G}^\lambda \). We say that (ab) precedes (cd) if either \(\ell (a)<\ell (c)\), or \(\ell (a)=\ell (c)\) and a is to the right of c, or \(\ell (a)=\ell (c)\) and b is to the right of d (in case \(a=c\)). For the base case observe that the edges with both end-vertices on the first level and the edges of \(p_{{{\,\mathrm{right}\,}}}\) satisfy condition \(\mathcal C\) by the definition of \(\varphi ^\star \). Now let (uv) be an edge not addressed in the base case and assume that for every edge \((u',v')\) preceding edge (uv) condition \(\mathcal C\) holds. For the inductive step we show that condition \(\mathcal C\) also holds for (uv). Let \((u', v')\) denote the edge to the right of (uv) so that (uv) and \((u', v')\) are consecutive; see Fig. 5. Because v is not the rightmost vertex on its level this edge exists. Let A denote the set of space arcs \(v_1v_2^\star \) in \(F_{\mathcal G}^\lambda \) with \(v_1, v_2 \in [v', v]\). Analogously, let B denote the set of space arcs \(u_1u_2^\star \) in \(F_{\mathcal G}^\lambda \) with \(u_1, u_2 \in [u', u]\). It is \(\varphi ^\star (v) = \varphi ^\star (v') + \sum _{a \in A} \varphi (a)\) by definition of \(\varphi ^\star \). Further, by induction hypothesis and since \((u',v')\) precedes (uv) it holds that \(\varphi ^\star (v') = \varphi ^\star (u') + \varphi ((u', v')^\star )\). Inserting the latter into the former equation, we obtain \(\varphi ^\star (v) = \varphi ^\star (u') + \varphi ((u', v')^\star ) + \sum _{a \in A} \varphi (a)\). Again, by definition of \(\varphi ^\star \), it is \(\varphi ^\star (u) = \varphi ^\star (u') + \sum _{b \in B} \varphi (b)\). By subtracting \(\varphi ^\star (u)\) from \(\varphi ^\star (v)\) we obtain

$$\begin{aligned} \varphi ^\star (v) = \varphi ^\star (u) - \sum _{b \in B} \varphi (b) + \varphi ((u', v')^\star ) + \sum _{a \in A} \varphi (a) \end{aligned}$$
(1)

Flow conservation on the vertex of \(F_{\mathcal G}^\lambda \) to which edges of A and B are incident gives \(\varphi ((u, v)^\star ) - \sum _{a \in A} \varphi (a) - \varphi ((u', v')^\star ) + \sum _{b \in B} \varphi (b) = 0\). Solving this equation for \(\varphi (u, v)\) and inserting it into (1) yields \(\varphi ^\star (v) = \varphi ^\star (u) + \varphi ((u, v)^\star )\), i.e. the condition \(\mathcal C\) holds for (uv). Therefore \(\varphi ^\star \) is a distance labeling, which we have shown to define a \(\lambda \)-drawing of \(\mathcal G\).    \(\square \)

Fig. 5.
figure 5

Proof of Lemma 4. Sets A and B contain the outgoing and incoming red flow network arcs incident to the gray oval, respectively.

Because \(D_{\mathcal G}^\lambda \) is planar we can use the \(O(n \log ^2 n / \log \log n)\)-time shortest path algorithm due to Mozes and Wulff-Nilsen [26] to compute the shortest distance labeling. This improves our \(O(n \log ^3 n)\)-time algorithm from Sect. 3.

Theorem 2

Let \(\mathcal G\) be an embedded proper level-planar graph. The distance labelings of \(D_{\mathcal G}^k\) correspond bijectively to the \(\lambda \)-drawings of \(\mathcal G\). If such a drawing exists, it can be found in \(O(n \log ^2 n / \log \log n)\) time.

5 Partial and Simultaneous Drawings

In this section we use the distance model from Sect. 4 to construct partial and simultaneous \(\lambda \)-drawings. We start with introducing a useful kind of drawing. Let \(\varGamma \) be a \(\lambda \)-drawing of \(\mathcal G\). We call \(\varGamma \) a \(\lambda \)-rightmost drawing when there exists no \(\lambda \)-drawing \(\varGamma '\) with \(\varGamma (v) < \varGamma '(v)\) for some \(v \in V\). In this definition, we assume \(x(\varGamma (v_{{{\,\mathrm{right}\,}}})) = x(\varGamma '(v_{{{\,\mathrm{right}\,}}})) = 0\) to exclude trivial horizontal translations. Hence, a drawing is rightmost when every vertex is at its rightmost position across all level-planar \(\lambda \)-slope grid drawings of \(\mathcal G\). It is not trivial that a \(\lambda \)-rightmost drawing exists, but it follows directly from the definition that if such a drawing exists, it is unique. The following lemma establishes the relationship between \(\lambda \)-rightmost drawings and shortest distance labelings of \(D_{\mathcal G}^\lambda \).

Lemma 5

Let \(\mathcal G\) be an embedded proper level-planar graph. If \(D_{\mathcal G}^\lambda \) has a shortest distance labeling it describes the \(\lambda \)-rightmost drawing of \(\mathcal G\).

Proof

The shortest distance labeling of \(D_{\mathcal G}^\lambda \) is maximal in the sense that for any vertex v there exists a vertex u and an edge (uv) with length l so that it is \(x(v) = x(u) + l\). Recall that the definition of distance labelings only requires \(x(v) \le x(u) + l\). The claim then follows by induction over V in ascending order with respect to the shortest distance labeling.    \(\square \)

5.1 Partial Drawings

Let \((\mathcal G, \mathcal H, \varPi )\) be a partial \(\lambda \)-drawing. In Sect. 3.1 we have shown that the flow model can be adapted to check whether \((\mathcal G, \mathcal H, \varPi )\) has a \(\lambda \)-extension, in case \(\mathcal H\) is connected. In this section, we show how to adapt the distance model to extend partial \(\lambda \)-drawings, including the case \(\mathcal H\) is disconnected. Recall that the distance label of a vertex v is its x-coordinate. A partial \(\lambda \)-drawing fixes the x-coordinates of the vertices of \(\mathcal H\). The idea is to express this with additional constraints in \(D_{\mathcal G}^\lambda \). Let \(v_{{\text {ref}}}\) be a vertex of \(\mathcal H\). In a \(\lambda \)-extension of \((\mathcal G, \mathcal H, \varPi )\), the relative distance along the x-axis between a vertex v of \(\mathcal H\) and vertex \(v_{{\text {ref}}}\) should be \(d_v=\varPi (v_{{\text {ref}}})-\varPi (v)\). This can be achieved by adding an edge \((v, v_{{\text {ref}}})\) with length \(d_v\) and an edge \((v_{{\text {ref}}}, v)\) with length \(-d_v\). The first edge ensures that it is \(x(v_{{\text {ref}}}) \le x(v) + d_v\), i.e., \(x(v) \ge x(v_{{\text {ref}}}) - d_v\) and the second edge ensures \(x(v) \le x(v_{{\text {ref}}}) - d\). Together, this gives \(x(v) = x(v_{{\text {ref}}}) - d_v\). Let \(D_{{\mathcal G},\varPi }^\lambda \) be \(D_{\mathcal G}^\lambda \) augmented by the edges \(\{(v, v_{{\text {ref}}}), (v_{{\text {ref}},v}): \forall v \in {\mathcal H}\}\) with lengths as described above.

To decide existence of \(\lambda \)-extension and in affirmative construct the corresponding drawing we compute the shortest distance labeling in \(D_{{\mathcal G},\varPi }^\lambda \). Observe that this network can contain negative cycles and therefore no shortest distance labeling. Unfortunately, \(D_{{\mathcal G},\varPi }^\lambda \) is not planar, and thus we cannot use the embedding-based algorithm of Mozes and Wulff-Nilsen. However, since all newly introduced edges have \(v_{{\text {ref}}}\) as one endpoint, \(v_{{\text {ref}}}\) is an apex of \(D_{\mathcal G}^\lambda \), i.e., removing \(v_{{\text {ref}}}\) from \(D_{{\mathcal G},\varPi }^\lambda \) makes it planar. Therefore \(D_{{\mathcal G},\varPi }^\lambda \) can be recursively separated by separators of size \(O(\sqrt{n})\). We can therefore use the shortest-path algorithm due to Henzinger et al. to compute the shortest distance labeling of \(D_{{\mathcal G},\varPi }^\lambda \) in \(O(n^{4/3} \log n)\) time [15].

Theorem 3

Let \((\mathcal G, \mathcal H, \varPi )\) be a partial \(\lambda \)-drawing. In \(O(n^{4/3} \log n)\) time it can be determined whether \((\mathcal G, \mathcal H, \varPi )\) has a \(\lambda \)-extension and in the affirmative the corresponding drawing can be computed within the same running time.

5.2 Simultaneous Drawings

In the simultaneous \(\lambda \)-drawing problem, we are given a tuple \((\mathcal G_1, \mathcal G_2)\) of two embedded level-planar graphs that share a common subgraph \(\mathcal G_{1 \cap 2} = \mathcal G_1 \cap \mathcal G_2\). We assume w.l.o.g. that \(G_1\) and \(G_2\) share the same right boundary and that the embeddings of \(\mathcal G_1\) and \(\mathcal G_2\) coincide on \(\mathcal G_{1 \cap 2}\). The task is to determine whether there exist \(\lambda \)-drawings \(\varGamma _1, \varGamma _2\) of \(\mathcal G_1, \mathcal G_2\), respectively, so that \(\varGamma _1\) and \(\varGamma _2\) coincide on the shared graph \(\mathcal G_{1 \cap 2}\). The approach is the following. Start by computing the rightmost drawings of \(\mathcal G_1\) and \(\mathcal G_2\). Then, as long as these drawings do not coincide on \(\mathcal G_{1 \cap 2}\) add necessary constraints to \(D_{\mathcal G_1}^\lambda \) and \(D_{\mathcal G_2}^\lambda \). This process terminates after a polynomial number of iterations, either by finding a simultaneous \(\lambda \)-drawing, or by determining that no such drawing exist.

Finding the necessary constraints works as follows. Suppose that \(\varGamma _1, \varGamma _2\) are the rightmost drawings of \(\mathcal G_1, \mathcal G_2\), respectively. Because both \(\mathcal G_1\) and \(\mathcal G_2\) have the same right boundary they both contain vertex \(v_{{{\,\mathrm{right}\,}}}\). We define the coordinates in the distance labelings of \(D_{\mathcal G_1}^\lambda \) and \(D_{\mathcal G_2}^\lambda \) in terms of this reference vertex.

Now suppose that for some vertex v of \(\mathcal G_{1 \cap 2}\) the x-coordinates in \(\varGamma _1\) and \(\varGamma _2\) differ, i.e., it is \(\varGamma _1(v) \ne \varGamma _2(v)\). Assume \(\varGamma _1(v) < \varGamma _2(v)\) without loss of generality. Because \(\varGamma _1\) is a rightmost drawing, there exists no drawing of \(\mathcal G_1\) where v has an x-coordinate greater than \(\varGamma _1(v)\). In particular, there exist no simultaneous drawings where v has an x-coordinate greater than \(\varGamma _1(v)\). Therefore, we must search for a simultaneous drawing where \(\varGamma _2(v) \le \varGamma _1(v)\). We can enforce this constraint by adding an edge \((v_{{{\,\mathrm{right}\,}}}, v)\) with length \(\varGamma _1(v)\) into \(D_{\mathcal G_2}^\lambda \). We then attempt to compute the drawing \(\varGamma _2\) of \(\mathcal G_2\) defined by the shortest distance labeling in \(D_{\mathcal G_2}^\lambda \). This attempt produces one of two possible outcomes. The first possibility is that there now exists a negative cycle in \(D_{\mathcal G_2}^\lambda \). This means that there exists no drawing \(\varGamma _2\) of \(G_2\) with \(\varGamma _2(v) \le \varGamma (v)\). Because \(\varGamma _1\) is a rightmost drawing, this means that no simultaneous drawings of \(\mathcal G_1\) and \(\mathcal G_2\) exist. The algorithm then terminates and rejects this instance. The second possiblity is that we obtain a new drawing \(\varGamma _2\). This drawing is rightmost among all drawings that satisfy the added constraint \(\varGamma _2(v) \le \varGamma _1(v)\). In this case there are again two possibilities. Either we have \(\varGamma _1(v) = \varGamma _2(v)\) for each vertex v in \(\mathcal G_{1 \cap 2}\). In this case \(\varGamma _1\) and \(\varGamma _2\) are simultaneous drawings and the algorithm terminates. Otherwise there exists at least one vertex w in \(\mathcal G_{1 \cap 2}\) with \(\varGamma _1(w) \ne \varGamma _2(w)\). We then repeat the procedure just described for adding a new constraint.

We repeat this procedure of adding other constraints. To bound the number of iterations, recall that we only consider compact drawings, i.e., drawings whose width is at most \((\lambda - 1)(n - 1)\). In each iteration the x-coordinate of at least one vertex is decreased by at least one. Therefore, each vertex is responsible for at most \((\lambda - 1)(n - 1)\) iterations. The total number of iterations is therefore bounded by \(n (\lambda - 1)(n - 1) \in O(\lambda n^2)\).

Note that due to the added constraints \(D_{\mathcal G_1}^\lambda \) and \(D_{\mathcal G_2}^\lambda \) are generally not planar. We therefore apply the \(O(n^{4/3} \log n)\)-time shortest-path algorithm due to Henzinger et al. that relies not on planarity but on \(O(\sqrt{n})\)-sized separators to compute the shortest distance labellings. This gives the following.

Theorem 4

Let \(\mathcal G_1, \mathcal G_2\) be embedded level-planar graphs that share a common subgraph \(\mathcal G_{1 \cap 2}\). In \(O(\lambda n^{10/3} \log n)\) time it can be determined whether \(\mathcal G_1, \mathcal G_2\) admit simultaneous \(\lambda \)-drawings and if so, such drawings can be computed within the same running time.

6 Conclusion

In this paper we studied \(\lambda \)-drawings, i.e., level-planar drawings with \(\lambda \) slopes. We model \(\lambda \)-drawings of proper level-planar graphs as integer flow networks. This lets us find \(\lambda \)-drawings and extend connected partial  \(\lambda \)-drawings in \(O(n \log ^3 n)\) time. We extend the duality between integer flows in a primal graph and shortest distances in its dual to obtain a more powerful distance model. This distance model allows us to find \(\lambda \)-drawings in \(O(n \log ^2 n / \log \log n)\) time, extend not-necessarily-connected partial \(\lambda \)-drawings in \(O(n^{4/3} \log n)\) time and find simultaneous \(\lambda \)-drawings in \(O(\lambda n^{10/3} \log n)\) time.

In the non proper case, testing the existence of a 2-drawing becomes NP-hard, even for biconnected graphs with maximum edge length two [7].