Information Processing North-Holland
EMBEDDING Frank
30 September
Letters 29 (1988) 75-79
RECTILINEAR
HOFFMANN
GRAPHS
and Klaus
Karl- Weterstrafl-Institut ftir Mathematik, German Democratic Republic
IN LINEAR
1988
TIME
KRIEGEL Academic
der Wissenschaften
der DDR, Mohrenstrasse
39, DDR-I086
Berhn,
Communicated by T. Lengauer Received 21 September 1987 Revised 13 May 1988
A linear-time algorithm is presented to recognize and embed rectilinear graphs which are embeddable on the plane. Previously, only the O(n’) time algorithm of Viyajan and Wigderson (1985) was known. The algorithm described here uses a different and surprisingly simple strategy.
Keywords:
Rectilinear
graph,
graph
embedding,
linear-time
1. Preliminaries A rectilinear-graph is a finite, connected digraph G = (V, E, A), where A is an edge labelling, h : E + D, with D being the set of the four main compass directions (n = north, e = east, s = south, w = west). If there is an edge (u, u) labelled X(U, U) = r E D, Jhen there should be an edge from u to u in E labelled by the opposite (with usual meaning) direction ? E D. No two edges starting in a node can have the same label. A rectilinear graph G is embeddable (on the plane) iff there is a mapping cp : G + Z2 to the integer grid such that: (i) cp maps graph nodes to grid nodes, (ii) QI maps the edges of G to straight line segments (parallel to the axis and of arbitrary length) in Z2 which connect images of the corresponding nodes in such a way that cp is consistent with h with respect to the canonical compass in Z2, and (iii) no two images of edges cross each other except when they have a common node. The class of rectilinear graphs, respectively embeddable rectilinear graphs was introduced by Budach [l] and studied in the context of maze 0020-0190/88/$3.50
6 1988, Elsevier Science Publishers
algorithm
solving problems (see also [2,3]). He called them labyrinths, respectively quasi-planar labyrinths. These classes of graphs were independently redefined by Vijayan and Wigderson in [7], who were motivated by VLSI-layout design questions. They proved a linear-time algorithm to check the embeddability of rectilinear graphs and an 0(n2) time algorithm to produce an embedding of any given embeddable rectilinear graph on n nodes. Our aim is to present a linear-time algorithm using a different layout strategy.
2. Embeddable rectilinear graphs The cyclic permutation [ of D given by n -+ w H s r--)e canonically defines a rotation system for any given rectilinear graph (see [4] for details on rotation systems). Hence, we have a set { 7r(.}[,t I’, where TV, is a cyclic permutation of the edges leaving U; let F(G) be the set of faces determined by this canonical rotation system which corresponds to the clockwise orientation of the plane. A face is represented by a closed path f : u0 +“I u, +rl . . . +/J+ ‘I u0 such that r(,.,+,(?,) = r,+i for all 0 < i < I, with I + 1 := 0. We shall identify f
B.V. (North-Holland)
75
Volume
29, Number
2
INFORMATION
PROCESSING
with all cyclic shiftings of f. Observe that a face is not necessarily a simple path and every directed edge belongs to exactly one face. Then, the Euler-PoincarC formula gives the following result.
LETTERS
2.1. Algorithmic
If a rectilinear
This means
genus(G)
A[i, := #V-
$#,!?+
#F(G)
aspects
that nodes are numbered
graph is embedda-
ble, then
1988
We introduce some data structures. An n-node rectilinear graph G is given by an n x 4 matrix A.
0 2.1. Proposition.
30 September
j I
- 2 = 0.
if there is no edge labelled starting
r] =
1 by n and
r
from i,
if j is a neighbor in direction
of i
r.
Now it is trivial to define a procedure
For
any pair of compass
directions
r, r’ E D
we define 0 -2
x( rr’) :=
+1 i -1
to
if r = r’,
tions, i.e.,
if r= -r’, if r’={(r), if r={(r’),
be the angle enclosed
FR[i]
by r and r’. Let f : u. -+rcl a closed path in a recti-
0, + rl . . + c’l-+ rlu. be linear
graph.
The rotation
GENF(j, r) that generates the face starting in direction r from node i. Such a face can be stored by an array F[l..n] of all nodes on the closed path and an array FR[l..n] of the corresponding local rota-
index
x(f)
is
defined
:=x(F[i-
11 -“F[i]
+“‘F[i+
11).
Using these structures one can easily convert our Theorem 2.3 into a linear-time algorithm which checks
the embeddability
of any given rectilinear
graph.
by 3. An embedding where I + 1 := 0. x(f):= f: x(r,r,+,), r=O 2.2. Proposition. IfG is an embeddable rectilinear graph, then there is exact4 one face f,, (called periphery) with x( f,) = 4 and for all other faces f ( called inner faces) we have x ( f ) = + 4.
2.3. Theorem
(see [3]). Let G be a rectilinear
G is embeddable
graph.
iff
= 0, and there is exactly one face fP with x( f,) = - 4 and, for all other faces f, x( f ) = + 4 holds. (i)
genus(G)
(ii)
The characterization of embeddable graphs given in [7] is similar; however, be mentioned that Theorem 5.1 of [7] mistake. A condition for a periphery added because otherwise the theorem for example, a complete grid on a torus ble on the plane. 76
rectilinear it should contains a has to be classifies, embedda-
strategy
using convex
faces
In the following we shall restrict ourselves embeddable rectilinear graphs without nodes
to of
outdegree 1. These are graphs which are equal to their topological root. The reader will easily extend all considerations below without the need of additional techniques. Let us describe the main idea for the layout algorithm. It can happen that for some pair u, u of nodes from G the placement of these nodes with respect to the east-west relation is fixed. For example, any embedding places u left of u. In other cases this is up to the designer. We shall add edges to G in a canonical way such that each node will have canonical neighbours with respect to the east-west and north-south relation and its overall position in the layout can be computed using these local neighbourhoods only. A face f is called convex iff for any embedding off any cut parallel to the axis of the finite region enclosed by f is connected, see Fig. 1. We shall use the following lemma.
PROCESSING
INFORMATION
Volume 29, Number 2
30 September 1988
LETTERS
and let k” be the maximal number smaller than k’ with FR[k”] # 0. The second cut node Cut2 is now defined by
0
‘F[k”]
a
F[k”+
l]
Fig. 1. (a) A convex face. (b) A spiral-shaped nonconvex face.
3.1. Lemma. An inner face f is convex iff its sequence of local rotations does not contain a subsequence consisting of two times - 1 separated by a (possibly empty) sequence of 0’s.
c%(r)) - 1 C FR[i]=O C(r)+1 and for all j with C(r) < C({(r)), h FR[i]>,O r=C(r)+l holds. Clearly, corners are not uniquely determined for nonconvex faces. We divide a face into convex faces by dividing its four sides. The DIVIDE Procedure. Let F[l],. . ., F[I] be a side of an inner face. If Cj=, FR[ i] < 1 for all j < I we are done. Otherwise, we traverse from F[l] to F[/] and find the first k with k-l
FR[k]
= -1
and
c
FR[i]
+ +l,
if FR[k”]
= +l
and
FR[ k” + l] = 0, cut2 :=
Now, let us define a procedure which subdivides any given face into convex faces. First, we find four extremal nodes (called corners) of a face. The indices C(n), C(e), C(s), C(w) of these nodes of an inner face have the following property: The segment (called side) F[C(r), F[C(r) + 11, . . . . F[C(~(r))] starts and ends with label r. Furthermore,
if FR[k”]
otherwise, where nl + 1 is a new node to be added in between F[k”] and F[k’].
nl + 1
i
Remark: nl denotes the current size of the node set. At the beginning, nl is equal to n. Finally, we add a pair of edges, opposite to each other, connecting Cut1 and Cut2 by updating A accordingly. That means, we cut off a ‘rectangle’ from the face and obtain a shorter side of a new inner face having the same corner set. Moreover, the partial sum of local rotations traversing the resulting shorter side from F[2] to F [ k] is the same as in the original side. Hence, we can look for the next Cut1 node, and so on. We stop at F[I] eventually. In the worst case, we have to add a linear number of new nodes and edges. We divide an embeddable rectilinear graph by applying the DIVIDE Procedure to all sides of all inner faces. To divide the periphery f,, one first inverts fp and finds four corners and sides. Then, each side is inverted again and subdivided as before. 3.2. Proposition. (a) Applying the DIVIDE Procedure to an embeddable rectilinear graph G yields an embeddable rectilinear graph. (b) After applying the DIVIDE Procedure to all sides of all faces, the graph G’ obtained from G consists of convex faces only.
> 1.
i=2
This is the first cut node Cut1 (compare
with Fig.
2). Then, traversing f backward find the maximal k’ with
from
F[k]
we
‘a*
--L__
r----l
Cut l= F[kl
-_
--
k-l c
i-k’
FR[i]
F[k’l -I
-_
=2 Fig. 2. 11
Volume
29, Number
INFORMATION
2
PROCESSING
Proof. (a) Straightforward from Theorem 2.3. (b) Assume there is an inner face f such that on one side, say F[l], . . . , F[I], there are two indices 1 < i -c j -c I with FR[i] = FR[j] = -1 and FR[k] = 0 for all i < k cj. But, then,
C(s)
?Jl
p
;;-
Y
m.2wml 2 2 for, otherwise,
F[i] is a Cut1 node.
An embeddable rectilinear graph that consists of convex faces only is called convex. We are going to describe now a layout algorithm for such graphs. The algorithm determines the x- respectively y-coordinate for each node. We explain how to find the x-coordinate. Basically, this will be a topological sorting.
+L”,
C(e)
Fig. 3 Adding
0
‘diagonals’
to inner convex faces.
First of all, nodes which are on a north-south These path have to get the same x-coordinate. nodes are represented by the southernmost node of the path. If u is such a southernmost node with respect to node u, we write Repr( u) = u and [u]_:= REPR
{u’]Repr(u)=Repr(u’)}. denotes
a list of all southernmost
nodes.
begin for all j E REPR do Counter(j) := 0; for i := 1 to nl do for all j E Succ(i) do Counter(Repr(j)) := Counter(Repr( j)) + 1; for all j E REPR do if Counter(j) = 0 then insert j in List(O); k := 0; x := 0; while k < nl do begin for all j E List(x) do for all i E [j] _ do begin for all i ’ E Succ( i ) do begin Counter(Repr(i’)) := Counter(Repr(i’)) - 1; if Counter(Repr(i’)) = 0 then insert Repr( i’) in List(x + 1); end; Coordinate(i) := x; k := k + 1; end, x:=x+1; end; end. Fig. 4. Algorithm
78
1988
C(w)
r-1
Hence,
30 September
LETTERS
x-sorting
Volume 29, Number 2
INFORMATION
PROCESSING LETTERS
A direct successor of a node u is either (i) a node u such that (u, u) is an edge in G and X(u, u)=e, or (ii) the node u’ if u is a C(e)-corner and u’ is a C(w)-corner of the same inner face. We remark that the second kind of successor is to avoid an overlapping of opposite face sides in the embedding (see Fig. 3). More precisely, the fact that the x-sorting will place the C(e)-corner left of the C(w)-corner ensures by definition of convexity that the segments F[ C(n)], . . . , F[ C(w)] and F[ C(s)], . . . , F[C(e)] cannot intersect in the layout. Determining y-coordinates one has to add a relation between C(s)- and C(n)-corners. Let Succ(u) denote the set of successors of node u and, in the following, List(i) will be a list of southernmost nodes with x-coordinate i. The main point of our layout strategy is that for all nodes u (but the westernmost nodes on the periphery) there is at least one immediate neighbour of u on the left side. That is why we need a convex rectilinear graph. Recall that nodes are numbered 1 by nl, where nl is the size of the node set after dividing the graph into convex faces. It can easily be seen that the algorithm in Fig. 4 is a linear-time algorithm.
4. Conclusions and remarks (1) The algorithm described above is well suited for implementation because of the reasonable constant behind its O(n) time behaviour. On the other hand, the algorithm always computes an embedding of the whole graph. If one has already given an embedded graph and wants to have local changes only, one would probably prefer the Vijayan and Wigderson strategy. (2) Rectilinear graphs can be laid out in quadratic area. The spiral (see Fig. l(b)) is a worst-case example. The designer has possibilities to influence the final layout shape in the way he subdivides faces into convex faces. It can hardly be expected to obtain area-optimal layouts because this problem is similar to an (but not proven) NP-hard problem (see [7]).
30 September 1988
(3) Obviously, the first part of the algorithm can be parallelized with respect to the number of faces, but this does not yield a faster algorithm in the worst case. (Consider, for example, graphs consisting of two faces only.) Nevertheless, one can parallelize in a fast and effective way the embeddability check and the DIVIDE Procedure of the embedding algorithm using O(log n) parallel time and O(n) processors in the CREW PRAM model. As to the x-sorting part of the algorithm we only know a parallelization using O(log n)’ time and O(n’Og ‘) processors based on an idea similar to computing the transitive closure of a relation.
Note added in proof Recently, we have learnt that a linear-time algorithm for embedding rectilinear graphs was found independently by Tamassia [S]. The algorithm which is based on similar ideas is used as a subprocedure for grid embeddings of general planar graphs (see also [6]).
Acknowledgment We would like to thank the referee for valuable comments.
References 111L. Budach, Automata and labyrinths, Math. Nuchrichten 86 (1978) 195-282.
121F. Hoffmann, One pebble does not suffice to search planar labyrinths, Proc. FCT’81, Lecture Notes in Computer Setewe, Vol117 (Springer, Berlin, 1981) 433-444. [31 F. Hoffmann and K. Kriegel, Quasrplane Labyrinths. Preprint P-Math-20/83, Inst. f. Mathematik, AdW der DDR, 1983. survey, J. Graph [41 S. Stahl, The embeddings of a graph-A Theory 2 (1978) 275-298.
[51 R. Tamassia, On embedding a graph in the grid with the minimum number of bends, SZAMJ. Comput. 16 (3) (1987) 421-444. [6] R. Tamassia and I.G. Tollis, Efficient embedding of planar graphs in linear time, in: Proc. IEEE Internat. Symp. on Circuits and Systems, Vol. 2 (1987) 495-498. [7] G. Vijayan and A. Wigderson, Rectilinear graphs and their embeddings, SIAM J. Comput. 14 (2) (1985) 355-372.
79