Improving the running time of embedded upward planarity testing

Improving the running time of embedded upward planarity testing

Information Processing Letters 110 (2010) 274–278 Contents lists available at ScienceDirect Information Processing Letters www.elsevier.com/locate/i...

209KB Sizes 2 Downloads 78 Views

Information Processing Letters 110 (2010) 274–278

Contents lists available at ScienceDirect

Information Processing Letters www.elsevier.com/locate/ipl

Improving the running time of embedded upward planarity testing Sarmad Abbasi a , Patrick Healy b , Aimal Rextin b,∗,1 a b

117-BB, Phase IV, DHA, Lahore, Pakistan Computer Science Department, University of Limerick, Ireland

a r t i c l e

i n f o

a b s t r a c t

Article history: Received 23 December 2008 Received in revised form 20 January 2010 Accepted 8 February 2010 Available online 10 February 2010 Communicated by F.Y.L. Chin

We consider the standard algorithm by Bertolazzi et al. to test the upward planarity of embedded digraphs. We show how to improve its running time from O (n + r 2 ) to 3

O (n + r 2 ), where r is the number of sources and sinks in the digraph. We also discuss an application of this technique: improving the running time of getting a quasi-upward planar drawing for an embedded digraph with minimum number of bends. © 2010 Elsevier B.V. All rights reserved.

Keywords: Algorithms Graph drawing Upward planarity

1. Introduction A digraph G = ( V , E ) is upward planar if it has a planar drawing with all edges monotonically pointing upward [6]. Generally, humans prefer drawings of digraphs such as PERT networks, subroutine call charts etc. to be upward planar [3]. An upward planarity testing algorithm checks if a planar digraph G has a drawing that is simultaneously upward and planar. Although an upward planar digraph must be both upward and planar, these two properties alone do not guarantee upward planarity. In fact, Garg and Tamassia [7] showed that upward planarity testing is N P -complete for general digraphs, so efficient upward planarity testing algorithms have been developed for some restricted versions of the problem. Hutton and Lubiw [9] presented an O (n2 ) upward planarity testing algorithm for single-source digraphs, this algorithm was later optimized to O (n) by Bertolazzi et al. [4]. An O (n + r 2 ) time algorithm was developed by Bertolazzi et al. to test the upward planarity of digraph with a fixed embedding [3], where r

*

Corresponding author. E-mail addresses: [email protected] (S. Abbasi), [email protected] (P. Healy), [email protected] (A. Rextin). 1 The author is thankful to the Centre for Advanced Studies in Mathematics, LUMS, Pakistan for hosting him during the initial phase of this work. 0020-0190/$ – see front matter doi:10.1016/j.ipl.2010.02.004

© 2010

Elsevier B.V. All rights reserved.

is the number of sources and sinks in the digraph and n is the number of vertices of digraph. An st-digraph G is a directed acyclic graph (DAG) with a single source s, a single sink t and an edge (s, t ). Di Battista et al. and Kelly independently showed that a digraph is upward planar if and only if it is a spanning subgraph of a planar st-digraph [1, 10]. In this paper, we present an improved algorithm for testing the upward planarity of an embedded digraph. The algorithm by Bertolazzi et al. [3] works by solving a cmatching problem. The flow network for this problem has a particular structure which allows for the maximum flow 3

to be found in O (r 2 ) time [11]. However, it takes another O (r 2 ) time because we also need to check for a feasible external face [3]. We reduce the running time of the algorithm by giving an efficient way to find a feasible external face. Since checking the upward planarity of an embedded digraph is also used as a subroutine in other algorithms, for example [8,5], improving the time taken by this algorithm may also make other algorithms efficient. In a quasi-upward drawing Γ of a digraph, a horizontal line through each vertex in Γ locally splits the incoming edges from the outgoing edges [2]. In a quasi-upward drawing, a bend is a point on an edge e where the tangent of e is horizontal. Quasi-upward planarity is a generalization of upward planarity; and all digraphs with a bimodal

S. Abbasi et al. / Information Processing Letters 110 (2010) 274–278

planar embedding have a quasi-upward planar drawing. Let G φ be an embedded digraph with a fixed external face. A quasi-upward planar drawing of G φ with minimum bends can be computed by finding the minimum cost flow in a network N [2]. We also present how the practical running time of computing a quasi-upward planar drawing with minimum number of bends can be improved. 2. Preliminaries We assume basic familiarity with graph theory. Let B = ( V , W , E ) be a bipartite graph. Let S ⊆ W , we let N ( S ) denote the neighborhood of S, i.e. N ( S ) = { v ∈ V : ( v , w ) ∈ E and w ∈ S }. Let c : W → N, where N is the set of natural numbers. For each vertex w ∈ W , we call c ( w ) the capacity of w. The capacitated matching or c-matching M of B with respect to c is a set M ⊆ E and has the following properties: (a) a vertex v ∈ V is incident to at most 1 edge in M; and (b) vertex w ∈ W is incident to at most c ( w ) edges in M. We also define a function I M : V ∪ W → N, such that for a v ∈ V ∪ W , I M ( v ) equals the number of edges incident to v in M. A vertex v ∈ V is called matched if I M ( v ) = 0, otherwise it is called free. A vertex w ∈ W is called saturated if I M ( w ) = c ( w ), and it is called unsaturated if 0 < I M ( w ) < c ( w ). A c-matching M is called a perfect capacitated matching if

|M | =



c ( w ).

w ∈W

A path P = w 0 , v 1 , w 1 , . . . , w k , v k+1 is called an augmenting path for a c-matching M if ( w i , v i +1 ) ∈ E \ M and ( v i , w i ) ∈ M, such that I M ( v k+1 ) = 0 and I M ( w 0 ) < c ( w 0 ). Given M and an augmenting path P , we can obtain a c-matching M  = M ⊕ P , such that every ( w i , v i +1 ) ∈ M  and every ( v i , w i ) ∈ E \ M  for all v i appearing on P ; and every edge e not on P is in M  if and only if e was in M. Note that | M  | = | M | + 1. A perfect c-matching (if it exists) can be computed by changing B = ( V , W , E ) to a flow network N and finding the maximum flow in it. We do this by first orienting every edge e of B from V to W such that each edge has a capacity u (e ) = 1, adding a source s and a sink t, adding an edge e  = (s, v ) between s and every vertex v ∈ V with capacity u (e  ) = 1, and finally an edge e  = ( w , t ) from every  w ∈ W to t with capacity u (e ) = c ( w ). If the maximum flow has a value equal to w ∈ W c ( w ) then B has a perfect c-matching with respect to c. We can obtain this perfect c-matching by taking all edges between V and W with non-zero flow in N. The following theorem is from [11].

275

the set of sources and sinks in a digraph G by S and T respectively, we also let K = S ∪ T . An embedding is an equivalence class of planar drawings for a graph G, such that an embedding φ has a fixed circular order of edges around each vertex of G. A graph G with a given embedding is denoted by G φ and we call it an embedded digraph. Two drawings with the same embedding have the same set of faces F . We can draw G φ in a planar fashion with any face in F as the external face. An embedded digraph G φ is bimodal if the edges incident to each vertex in G φ can be partitioned into consecutive incoming and outgoing edges. We can check if a planar digraph G φ is bimodal in O (n) time. Assume that the vertex v has the edges e 1 and e 2 incident to it in a face f of an embedded digraph G φ , such that e 1 and e 2 are consecutive edges in the facial boundary of f . The triplet e 1 , v , e 2 is a switch if both e 1 and e 2 either point toward v or away from v. The number of switches in a face f in an embedded DAG is even, and is denoted by 2n f . Lemma 1. (See Bertolazzi et al. [3].) Let F be the set of faces in a bimodal embedded digraph G φ . Then



(n f − 1) + 2 = | S | + | T |.

f ∈F

A mapping of sources and sinks to the faces of G φ is a consistent assignment if the following conditions are satisfied: – Each source or sink is mapped to exactly one face. – A face h has nh + 1 sources or sinks mapped to it. – Every other face f has n f − 1 sources or sinks mapped to it. The embedded digraph G φ cannot be upward planar if it is non-planar or non-bimodal or has directed cycles. Bimodality, planarity and acyclicity can be tested in linear time. Hence, the O (n + r 2 )-time embedded upward planarity testing algorithm by Bertolazzi et al. [3] first ensures that G φ satisfies these properties and then checks if it satisfies the following theorem: Theorem 2. (See Bertolazzi et al. [3].) Let G φ be a planar and bimodal embedded DAG. G φ is upward planar if and only if we can find a consistent assignment of the sources or sinks of G φ to its faces.

Corollary 1. In a bipartite graph B = ( V , E ), we can determine if a perfect c-matching with respect to a function √ c exists and compute such a c-matching (if it exists) in O ( | V | · | E |) time.

Bertolazzi et al. showed that there exists a consistent assignment for G φ if a bipartite graph B G φ has a perfect c-matching. The graph B G φ = ( K , F , E ) has an edge ( v , f ) if v ∈ K is incident to f ∈ F in G φ . Bertolazzi et al. show that | F | is O (r ), where r is the number of sources and sinks in G. The graph B G φ is planar and hence it has O (r ) edges. The graph B G φ can be constructed in O (n) time [6, 3]. Given B G φ , one can check if a consistent assignment exists for G φ by using the following 2 step procedure:

In a digraph, a source vertex has only outgoing edges and a sink vertex has only incoming edges. We denote

1. Define function c : F → N, such that c ( f ) = n f − 1 for every face f in G φ . Then find the c-matching in B G φ

Theorem 1. Let N = (√ V , E ) be a network. The maximum flow in N can be found in O ( | V | · | E |) time if either out-degree( v )  1 or in-degree( v )  1 for every vertex v of N.

276

S. Abbasi et al. / Information Processing Letters 110 (2010) 274–278

with respect to c. If perfect c-matching does not exist then G φ is not upward planar, otherwise each face f can act as an internal face in an upward planar drawing of G φ . 2. If the previous test is successful, then we try to find a feasible external face, if it exists. We let c (h) = nh + 1 for a face h and check if we can have a perfect cmatching. If we get a perfect c-matching then G φ is upward planar, otherwise we revert back to c (h) = nh − 1 and let c ( g ) = n g + 1 for a face g, we then find the maximum c-matching. We repeat this until we find a perfect c-matching or we run out of faces, in which case G φ is not upward planar.

3. An improved upward planarity testing algorithm Let F denote the set of faces of G φ and K = S ∪ T denote the sources and sinks in G φ . Assume we are given a bipartite graph B G φ = ( K , F , E B ) such that B G φ has an edge (s, f ) if and only if the source or sink s is incident on the face f in G φ . Furthermore, we are given a function c : F → N, such that c ( f ) = n f − 1 for each f ∈ F . We note that B G φ is planar and hence has O (n) edges [3]. If B G φ has no perfect c-matching then G φ does not have a consistent assignment. Similarly, if B G φ has a perfect c-matching, then each face f in G φ can be assigned n f − 1 sources or sinks. We can find if B G φ has a perfect c-matching with 3

Bertolazzi et al. proposed an O (r 2 ) method for step 1 [3], by converting it from a matching problem to a maximum flow problem on a network N  as discussed earlier. However, we conclude from Corollary 1 that step 1 can be 3

done in O (r 2 ) time because B G φ is planar. Bertolazzi et al. proposed an O (r 2 ) method for step 2 [3], which essentially corresponds to iteratively checking each face if it can act as the external face. We show in the next section how it can be done in linear time. A quasi-upward planar drawing with minimum bends for an embedded digraph G φ with a fixed external face can be computed by finding the minimum cost flow in a network N [2]. So we will briefly describe min cost flows. Assume we have a network N = ( V , E ), such that each edge e has an associated value c (e ) called cost and u (e ) called capacity. We also have a integer d( v ) with each vertex v called demand, if d( v ) < 0 then v is a supplynode. The minimum cost flow problem computes a function x : E → N, called flow, that minimizes



c (e )x(e ).

e∈ E

We let Val(x) represent the sum of flow “supplied” by the supply-nodes. Let R (N ,x) be the residual graph for the network N with respect to the flow x. The residual graph R (N ,x) has the same vertices as N , for each edge e = (u , v ) in N , R (N ,x) has an edge e  = (u , v ) and an edge e  = ( v , u ). The edge e  has a cost c (e  ) = c (e ) and an upper bound on flow u (e  ) = u (e ) − x(e ), while the edge e  has a cost c (e  ) = −c (e ) and an upper bound on flow u (e  ) = x(e ). The cost of a path P in R (N ,x) is defined as

cost( P ) =



c (e ),

∀e ∈ P

while the capacity of a path P is the minimum u (e ) for all edges e on P . Theorem 3. Let x be a flow of minimum cost of value Val(x) in N and let P be a the minimum cost path in R (N ,x) . Let x be a flow in R (N ,x) with non-zero flow only along P . Then the flow x is the minimum cost flow of value Val(x) + Val(x ), such that

x (e ) = x(e ) + x (e )

for all e ∈ E

Theorem 3 leads to Successive Path Augmentation algorithm to find the minimum cost flow [11].

respect to the function c in O (r 2 ) time according to Corollary 1. Assume that M is a perfect c-matching in B G φ . The embedded digraph G φ is upward planar if we can show that there exists a face h, such that h can be assigned nh + 1 sources or sinks. From Lemma 1 there will be two unmatched sources/sinks, say, s and s . Suppose a face f is reachable from s. That is, there exists an alternating path P given by

s = s0 , f 1 , . . . , sk−1 , f k = f satisfying (si −1 , f i ) ∈ E B \ M and (si +1 , f i ) ∈ M. Similarly, suppose f is also reachable from s via another alternating path P  given by

s = s0 , f 1 , . . . , sl−1 , f l = f satisfying (si −1 , f i ) ∈ E B \ M and (si +1 , f i ) ∈ M. We claim that the first common vertex between P and P  is a face. Suppose on the contrary that the first common vertex between P and P  is a source or sink sˆ . Let ˆf be the preceding vertex of sˆ in P , and ˆf  be the preceding vertex of sˆ on P  . Then ˆf = ˆf  . But this implies that both ˆf and ˆf  are matched to the same source/sink sˆ , contradicting the fact that M matches a source/sink to only one face. A simple modified version of BFS or DFS can search for all faces reachable from s and s in linear time. Let Pˆ be the alternating sub-path of P from s to ˆf and Pˆ  be the alternating path from s to ˆf . We let c ( ˆf ) = n ˆf + 1 and apply these paths to obtain a capacitated matching, such that I M ( ˆf ) = n ˆf + 1 and I M ( f ) = n f − 1 for all f = ˆf . We obtain a consistent assignment for G φ by assigning a source/sink v to a face f in G φ if the edge ( v , f ) ∈ M. It only remains to be shown that if the set of faces reachable from both s and s are disjoint then there is no consistent assignment. In this case, let R 1 and R 2 be the set of faces reachable via alternating paths from s and s respectively. Then R 1 ∩ R 2 = ∅. We show that

    N ( R 1 ) > nf −1 f ∈R1

where N ( R 1 ) is the neighborhood of R 1 in B G φ . Each face f ∈ R 1 must have been assigned to n f − 1 different  n sources/sinks. This accounts for f ∈ R 1 f − 1 sources/sinks in the neighborhood of R 1 . Since s is also  included in this neighborhood its total size is at least ( f ∈ R 1 n f − 1) + 1. This implies that in a consistent assignment the external

S. Abbasi et al. / Information Processing Letters 110 (2010) 274–278

277

Algorithm 1 Improved Upward Planarity Check-

ing 1: Input: A planar, acyclic and bimodal embedded digraph G φ 2: Output: True if G φ is upward planar; False otherwise 3: Construct the bipartite graph B G φ = ( K , F , E B ) 4: For each f ∈ F set c ( f ) = n f − 1. 5: Find the maximum c-matching M in B G φ 6: if | M | < | S | + | T | − 2 then 7: Return False 8: end if  9: u and v  are the two vertices in B G φ that are not saturated 10: Run BFS from u  coloring each face as red. 11: RUN BFS from v  12: if reached a red face vertex then 13: Return True 14: else 15: Return False 16: end if

face must belong to R 1 . A similar argument shows that in a consistent assignment

    N ( R 2 ) > nf −1 f ∈R2

and the external face must belong to R 2 . But this is impossible as R 1 ∩ R 2 = ∅. Hence we have the following theorem. Theorem 4. If B G φ has a perfect c-matching then we can check if G φ has a consistent assignment in linear time. The improved upward planarity testing algorithm is shown in Algorithm 1. We can construct B G φ in O (n) 3

time. We can check in O (r 2 ) time if B G φ has a perfect c-matching with respect to a function c, such that c ( f ) = n f − 1 for each face f ∈ G φ . Moreover, checking for a feasible external face takes linear time. Hence, the time 3 taken by our algorithm is O (n + r 2 ). 4. Application: Improving quasi-upward planarity testing We now show how to improve the practical running time of computing a quasi-upward planar drawing with minimum number of bends for an embedded digraph. Bertolazzi et al. showed that this can be done by solving a minimum cost flow network N [2]. The flow network N is constructed in the following manner. The vertices of N consists of the sources/sinks of G φ and the faces of G φ . Let f be a face in G φ then d( f ) = n f + 1 if f is the external face and d( f ) = n f − 1 otherwise. Note that if f is a directed cycle and is an internal face then d( f ) = −1, i.e. it is a supply-node. Each source or sink v has a demand of d( v ) = −1. There is an edge e = ( v , f ) in N with u (e ) = 1 and c (e ) = 0 if the source or sink v is incident to the face f . There is an edge e in N with u (e ) = ∞ and c (e ) = 2 between every pair of adjacent faces. We now use c-matching to reduce the number of iterations needed to find a minimum cost flow in N . Let B G φ = ( K , F , E ) be a bipartite graph, such that K is the set of sources and sinks in G φ while F is the set of faces in G φ . Assume that we also have a function cap : F → N such that cap( f ) = n f + 1 when f is the external face, cap( f ) = 0 when f is an internal face with 0

Fig. 1. Results.

switches, and cap( f ) = n f − 1 when f is an internal face with positive number of switches. We find the maximum c-matching M in B G φ with respect to the function cap. If | M | = | S | + | T | and none of the facial boundaries of G φ is a directed cycle, then G φ is upward planar, which means that G φ has a quasi-upward planar drawing with no bends. A quasi-upward planar drawing of G φ has positive number of bends in the following 2 cases: when none of the facial boundaries of G φ are directed cycles and | M | < | S | + | T |, or when at least one facial boundary of G φ is a directed cycle. We can map M to a flow x in N in the following manner: if for a source or sink v and a face f the edge ( v , f ) ∈ M, then we add a unit flow on the edge ( v , f ) in N . The flow x will have a value of | M |, and the total cost of x is 0 because x(e ) is positive only for edges with 0 cost. We argue that this will be the minimum cost flow of value | M | in N . Lemma 2. If x is the flow obtained from running c-matching on B G φ with respect to the function cap, then f is the minimum cost flow of value Val(x) = | M |. Proof. Assume we have a flow x in N such that Val(x ) = Val(x) and c (x) > c (x ). This means that c (x )  −1 because c (x) = 0, but there are no negative cost edges in N . Hence we have a contradiction. 2

278

S. Abbasi et al. / Information Processing Letters 110 (2010) 274–278

Let U be those supply-nodes with 0 out-flow with respect to the flow x. We experimentally check the value of |U | by finding the maximum c-matching for B G φ for 3012 randomly generated planar bimodal connected digraphs. For each digraph G in our test-suite, we first find a bimodal embedding G φ and then find the maximum cmatching in B G φ with respect to the function cap. We find that mostly |U | is below 2 and very rarely goes above 3. The detailed result of our experiment is given in Fig. 1. Hence, it is reasonable to believe that in practical situations |U | will be a small number. We then modify the network N so that it has a single supply-node sˆ and a single demand-node tˆ. The modified network remains to be O (n) in size, and the modification will take O (n) time. We can find the minimum cost flow in N by repeatedly taking the cheapest augmenting path from sˆ to tˆ until the maximum flow value is attained [11]. We usually find the cheapest augmenting path in O (n log n) time by using Dijkstra’s algorithm. However, we can do it in O (n) time by modifying N to a new network N  . N  is constructed by replacing every edge (h, g ), where h and g are faces in G φ , by a dummy vertex v and the edges e 1 = (h, v ) and e 2 = ( v , g ). Moreover, we let u (e 1 ) = u (e 2 ) = u (e ) and c (e 1 ) = c (e 2 ) = 1. We can now find the cheapest augmenting path in O (n) time by using a modified BFS algorithm. It takes O (n) time to construct the flow network N . Hence, we can find the minimum bend drawing for 3

G φ in O (n + r 2 + |U |n). We have seen experimentally that |U | ≈ O (1), so practically one can expect the run3

ning time be close to O (n + r 2 ). Recall that the algorithm described by Bertolazzi et al. has a running time of O (n2 log n) [2].

5. Conclusion We showed that testing if an embedded digraph is up3

ward planar can be done in O (n + r 2 ) time by presenting an efficient way to find a feasible external face. We also showed how to improve the practical running time of computing a quasi-upward planar drawing with minimum number of bends by using our improved upward planarity testing algorithm. References [1] Giuseppe Di Battista, Roberto Tamassia, Algorithms for plane representations of acyclic digraphs, Theor. Comput. Sci. 61 (1988) 175– 198. [2] Paola Bertolazzi, Giuseppe Di Battista, Walter Didimo, Quasi-upward planarity, Algorithmica 32 (3) (2002) 474–506. [3] Paola Bertolazzi, Giuseppe Di Battista, Giuseppe Liotta, Carlo Mannino, Upward drawings of triconnected digraphs, Algorithmica 12 (6) (1994) 476–497. [4] Paola Bertolazzi, Giuseppe Di Battista, Carlo Mannino, Roberto Tamassia, Optimal upward planarity testing of single-source digraphs, SIAM J. Comput. 27 (1) (1998) 132–169. [5] Hubert Chan, A parameterized algorithm for upward planarity testing, in: Proceedings of ESA, 2004, pp. 157–168. [6] Giuseppe Di Battista, Peter Eades, Roberto Tamassia, Ioannis G. Tollis, Graph Drawing: Algorithms for the Visualization of Graphs, Prentice Hall, 1999. [7] Ashim Garg, Roberto Tamassia, On the computational complexity of upward and rectilinear planarity testing, SIAM J. Comput. 31 (2) (2001) 601–625. [8] Patrick Healy, Karol Lynch, Fixed-parameter tractable algorithms for testing upward planarity, in: Proceedings of SOFSEM, 2005, pp. 199– 208. [9] Michael D. Hutton, Anna Lubiw, Upward planar drawing of singlesource acyclic digraphs, SIAM J. Comput. 25 (2) (1996) 291–311. [10] D. Kelly, Fundamentals of planar ordered sets, Discrete Math. 63 (2– 3) (1987) 197–216. [11] Kurt Mehlhorn, Graph Algorithms and NP-Completeness, SpringerVerlag New York, Inc., New York, NY, USA, 1984.