pq-trees the set of all distinct of a graph ma tthias...
TRANSCRIPT
PQ-trees and the Set of All Distinct Planar Embeddings of a Graph
Ma tthias Stallmann
Department of Computer Science North Carolina State University
Raleigh, Sor t h Carolina 27695-8206
March, 1986
Abstract
A forest of PQ-trees can be used to efficiently represent the set of all distinct planar embeddings of a biconnected graph. A linear time algorithm is given for computing this representation. The representation can then be used t o compute the number of distinct embeddings, to generate all possible embeddings, or to extend a partially specified embed- ding into a complete embedding- (all of these can be done in linear time). The latter prob- lem, for which no solution was known previously, also has an on-line variant, which can be solved in almost linear time using the data structure.
1. I n t r o d u c t i o n
Planar embeddings of graphs are used in a variety of applications. They range from
faster algorithms for the planar special case of general graph problems (see e.g,
[B,F,LT,Su]) to checking isomorphism of chemical structures (an application of planar
graph isomorphism, see [HTl,HW]) to VLSI design (see, e.g. [A,D,H,Sto,V]). Th ' is paper
presents a da ta structure based on PQ-trees (see [BL]), which represents all possible
embeddings of a planar graph in linear space. The data structure can be constructed in
linear time using an algorithm similar to the planarity testing algorithm of [BL]. Efficient
solutions to the following problems are given: (a) producing one embedding of the graph,
(b) producing all embeddings of the graph, (c) verifying that a given embedding is in fact
an embedding, (d) producing any (all) embedding(s) of a graph that is (are) consistent
with some partially specified embedding. The time required for all of these problems is
linear in the size of the input (or output in the case of producing all embeddings). The
solution to an online version of problem (d) is also discussed. The time bound for the on-
line algorithm is almost linear.
PQ-tree based solutions to problems (a) and (b) have already been reported in [C].
Our approach to these problems, unlike that of [Cj, requires only simple modifications of
existing planarity testing algorithms, and does not require "direction indicators"t0 keep
track of the left to right orientation of Q-nodes. Furthermore, our embedding algorithm
outputs a succinct representation for the set of all possible embeddings. The representa-
tion may either be used directly to produce solutions for (a) or (b) or updated further to
incorporate additional constraints, solving problems (c) and (d) as well.
A graph G is a set of edges, each edge connecting two (not necessarily distinct) ver-
tices in V ( G ) . Multiple edges and loops are permitted (in VLSI applications, it is natural
to consider embeddings of graphs that have multiple edges and/or loops). Each edge in G
is said to be incident to the two vertices of V { G ) that it connects.
Definition 1.1. A graph G is planar if its edges can be drawn on the plane without
crossings. In reference to such a drawing, a face is a connected region of what remains of
the plane when all the edges of G are removed. A face boundary is a subset of G that
forms the boundary of a face. An embedding of a biconnected graph G is a set of cycles in
G that are face boundaries.
Another way to define an embedding is by giving a clockwise ordering of the edges
incident to each vertex in a drawing. This latter definition will be used in the algorithms
presented here. However, Definition 1.1 is needed to clarify when two embeddings are dis-
tinct. In particular, we deduce from Definition 1.1 that two clockwise ordering~ represent
the same embedding if (a) any list of edges is rotated (the list begins a t a different edge) or
(b) all lists of edges are reversed (if the drawing is reflected, the same embedding results).
Definition 1.2. The embedding completion problem is the following. As input, we are
given a graph and a set of specifications of the form v : (el, - - ,er), where v is a vertex
and { e l , - . l k e } is a subset of the edges incident to v . Output is an embedding which
satisfies every specification, in the sense that e precedes e à etc. in a rotational ordering
(either clockwise or counterclockwise) about v . If no such embedding exists, the output is t
I empty. Two or more specifications may be l inked, that is, they adopt the same rotational
orientation (all are satisfied with the sequence e l . - - ,e, in clockwise order or all are
satisfied with the sequence in counterclockwise order).
We consider several variations of the embedding completion problem. Output can
be all embeddings that satisfy the specifications instead of just one, or it can be the
number of distinct embeddings that satisfy the specifications. Input can be a list of par-
tial face boundaries (each partial face boundary is a list of edges that must appear in that
clockwise order around some face of the embedding). Note that problems (a) and (b)
represent the special case when the list of specifications is empty and problem (c) is the
special case when the list of specifications includes every edge twice (once per vertex or
once per face).
A particularly interestins var iant is t h e online embedding completion problem. Here,
the original graph G is processed to produce n (G) , a representation of the set of all f
distinct embeddings of G . Then specifications are given one a t a time. After each
specification q G ) is updated to be consistent with all specifications so far (and the
number of embeddings still in n (G) is reported). A new specification may be linked t o an
earlier specification or not.
The online embedding completion problem has possible application to hierarchical
VLSI layout, where a circuit is divided into components, each of which is laid out
separately. A layout of the whole circuit is achieved by laying out interconnections
among components. The layout chosen for a particular component may impose con-
straints on some of the interconnections with other components. Such constraints can be
viewed as specifications for the layout (embedding) of the whole circuit.
The main results are as follows. Let m = I G 1, i.e. the number of edges in the graph,
and p be the total number of edges in all specifications. Then all off-line variations of the
embedding completion problem, except for producing all embeddings, can be solved in
time O(m + p ) . T o generate all distinct embeddings requires time 0(md + p) , where d is
the number of embeddings generated; note that the size of the output is O ( m d ) in this
case. The online embedding completion problem requires total time
O ( m + p a ( p , c +log</)), where c is the number of independent groups of specifications
and d is the number of embeddings of the original graph. This time bound is amortized
over f l ( c + Iogd) updates.
Most of these results apply only to biconnected graphs. It is well known that any
solution to the planarity testing problem or to problem (a) can be extended to arbitrary
graphs by simply using the algorithm within each biconnected component. Our algo-
rithm for problem (c) can be extended to general graphs using the following Lemma.
Lemma 1.1. [S] A set of clockwise orderings around each vertex represents an embed-
ding iff ( i ) the subsequences corresponding; to each biconnected component specify an
embedding for that component. and ( i i ) no clockwise edge list has a subsequence of the
form e f e ' f ', where e and e ' are in one biconnected component and f and f ' are in a
different component. Q
Solutions to problems (b) and (d) appear to be difficult to extend to arbitrary graphs.
The results are presented in the following order. Section 2 discusses the da ta struc-
ture. Section 3 gives the necessary modifications to the PQ-tree planarity testing algo-
rithms for generating the data structure. Section 4 discusses how the da ta structure may
be updated to incorporate specifications, either off-line or on-line.
2. The PQ-tree Data Structure ^
In this section we show how a forest of PQ-trees can be used to represht all possible
embeddings of a biconnected planar graph. First, we observe that G can be converted
into a directed graph in such a way tha t the incoming edges to any vertex can be
separated from the outgoing edges in any clockwise ordering of incident edges.
Lemma 2.1. [L] In a biconnected graph G with n vertices, the vertices can be numbered
in such a way that vertex 1 is adjacent to vertex n and every other vertex has a t least one
neighbor with a lower number and one neighbor with a higher number. 0
Defini t ion 2.1. The numbering described in Lemma 2.1 is called an st-numbering.
An st-numbering for a biconnected graph can be found in linear time [El. Hen-
ceforth, we assume that the vertices of any biconnected graph G are st-numbered and use
x (: V ( G ) and its st-number interchangeably. There may be several st-numberings of
any graph, but the essential property required for the da ta structure holds for any st-
numbering.
Lemma 2.2. Let G be a biconnected planar graph with vertices numbered using an st-
numbering, and let a ( G ) be any embedding of G . Then a representation of a ( G ) as a set
of clockwise lists of edges about each vertex always has edges to higher (from lower) num-
bered vertices appearing consecutively in every list.
Proof. See fL ' o r '^'. 9
Let I n ( i ) be the left to right ordering of edges from vertices whose number is < i to
vertex number i , and Out(i) be the left to right ordering of edges from vertex number i
to vertices whose number is > i. A clockwise ordering around vertex i is given as In ( i )
followed by the reverse of Out[i). Two sets of left to right lists specify the same embed-
ding if all the lists in one set are the reverse of all the lists in the other.
The data structure representing all possible embeddings is a forest of PQ-trees, hen-
ceforth denoted by Il(G). For each v (: V ( G ) , there is a tree representing the possible
permutations of edges for In(v) and another tree representing the permutations for
Out ( v ) .
Definition 2.2. A PQ-tree (see [BL]) specifies a set of admissible permutations on a
ground set S, in this case the set of edges incident to a particular vertex. The leaves of a
PQ-tree represent elements of 5. In addition, there are two types of interior nodes. A P-
node specifies that its children may appear in any order, but all leaf descendants of the
P-node must appear consecutively. A Q-node specifies that its children must appear in
the order they are listed or the reverse of that order.
In our notation for PQ-trees, angle brackets <> enclose the children of a P-node,
while square brackets are used to enclose the children of a Q-node. For example,
< a 6 c > d e l denotes a PQ-tree whose root is a Q-node with three children: a P-node
and the leaves d and e . The P-node has three children: the leaves a , 6 , and c . Among
the admissible permutations of the five leaves are abcde, edabc (reverse children of Q-
node), and bacde (permute children of the P-node). Not admissible are the permutations
dabce (violates constraint imposed by Q-node) and abdce (violates P-node constraint).
Nodes of one tree in n ( G ) may be related to nodes of another tree. In particular,
every child of a P-node in the forest has a unique mate. If the children of a P-node are
permuted in any way, their mates must be permuted in the same way. Furthermore all
interior nodes are grouped into equivalence classes w r t an equivalence relation = . t f
X =: Y , where X and Y are both P-nodes, then the children of X are mates of the
children of Y. X and Y always have the same number of children in this case. If X == Y
and X and Y are Q-nodes, then a reversal of the order of X's children implies tha t the
order of the children of Y must be reversed as well. An equivalence class wrt = either
contains exactly two P-nodes with the same number of children or an arbitrary number of
Q-nodes. A P-node and a Q-node never appear in the same class.
Our notation for PQ-trees uses subscripts to denote equivalence classes wrt = . The
closing angle or square bracket for each interior node has a subscript identifying its
equivalence class.
In order to distinguish the left and right sides of a list of edges, the root of each PQ-
tree is a special Q-node that specifies left/right orientation for the tree. In general, the
root of each tree has the form [ * l e f t * tree for edges ""right*], where *lef t* and ""right*
are new leaves that mark the left and right boundaries of the list of edges. All roots
belong to the same equivalence class wrt = .
For example, if G is the graph depicted in Figure l(a), then n ( G ) is given by the
PQ-trees in Figure l fb ) . Note that the permutation of a , b , and c around vertex 1
influences the permutation of { j .q . A . J } , 6 , and c around vertex 5. The equivalence class
denoted by the subscript A keeps track of this relationship. It contains two P-nodes, each
of which has 3 children and the corresponding children are mates of each other. Note also
tha t {d, e ,h , i , j } is a triply connected component of the graph, and that the reversal of the
order o f d and e around vertex 2 forces all other parts of the component t o be reversed.
The Q-nodes with subscript B represent this triply connected component.
In the example just described, there are several Q-nodes which have only two chil-
dren. The formulation of [BL] disallows such Q-nodes, replacing them by equivalent P-
nodes. In this instance it is important to allow Q-nodes with two children, and to distin-
guish between these and P-nodes with two children. A Q-node with two children
represents a tr ipiy connected component, whereas a P-node w ith two children represents
two disjoint paths in the graph.
Motivation for the representation of all planar embeddings may be found in the work
of MacLane [MI. What follows is a brief description of the relevant results.
Definition 2.3. An isolated path of a graph G is a simple path whose interior vertices
are all degree 2 vertices of G.
Definition 2.4. A branch graph of degree k is a graph consisting of k distinct isolated
paths joining the same two vertices. The isolated paths are also called the branches of the
branch graph.
Definition 2.5. [MI A semi-split of a biconnected graph G is a decomposition of G into
G ' and G" so that G = G ' I) G , G' n G' =0 , and I!/((;') ("1 v(G")\ = 2. The
two vertices of V(G ') (^} V ( G ' ) are called a separation pair. A semi-split is called a split
if neither G' nor G' is an isolated path of G. If G is biconnected, has no split, and is not
a branch graph of degree 2 or 3, then G is triply connected.
Lemma 2.3. A biconnected planar graph has a unique embedding iff it is triply con-
nected, a simple cycle (branch graph of degree 2), or a branch graph of degree 3.
Proof. A trivial corollary to Theorem 10 in [MI.
Definition 2.6. [\I] A least split in a biconnected graph G is a split with separation pair
: r , y } such that a t least one of G ' or C has no semi-split with separation pair {x, y }.
A biconnected graph that has no least split is either triply connected or is a branch
graph.
Lemma 2.4. [M] A branch graph of degree k has min(l,(k - 1)!/2) distinct embeddings.
Lemma 2.5. [MI If G = G' (J G" is a least split with separation pair { x , y } then every
pair of embeddings a' and a" of G ' 1) {{x,y)} and G IJ {(x,y)}, respectively,
corresponds to two distinct embeddings of G . 0
MacLane uses Lemmas 2.4 and 2.5 in a decomposition that yields a count of the
number of distinct embeddings of a biconnected planar graph. The decomposition can be
viewed as a series of the following transformations on G .
( i ) Collapse Branch Graph (CBG): If G = C ' U G" is a least split with separation
pair {x,y } and C is a branch graph, replace G" by a new edge (x , y).
(ii) Collapse Component (CC): If G = G ' \J G" is a least split with separation pair
{x,y} and G" \\ {(x,y)} is triply connected, replace G" by a new edge (z,~).
CBG and CC transformations are applied until the resulting graph is either triply
connected or a branch graph (and hence all distinct embeddings may be computed
k directly). The following lemmas give a method for computing all distinct embeddings of
G given a decomposition of G by CBG and CC transformations.
Lemma 2.6. Consider a CBG operation as described above in which GI is a branch
graph of degree k. The set of embeddings of G is the set of embeddings of G' \\ { ( z , ~ ) }
with the edge (qy) replaced by each of the k! permutations of the branches of (7'.
Proof. Follows directly from Lemmas 2.4 and 2.5 and the proof of Theorem 10 in [MI. 0
Lemma 2.7. Consider a CC operation as described above. The set of embeddings of G
is the set of embeddings of G' U {(x,y)} with the edge (z ,y) replaced by each of two
reflections of the embedding of G".
Proof. Follows directly from Lemmas 2.3 and 2.5 and the proof of Theorem 10 in [MI. 0
The decomposition of G is represented in n ( G ) by equivalence classes wrt == . A
CBG transformation removing a branch graph of degree k is represented by an
equivalence class containing two P-nodes with k children. A CC transformation is 1
represented by an equivalence class of Q-nodes. The final graph (the graph remaining
after all transformations have been performed) is represented by a class of P-nodes if it is
a branch graph or a class of Q-nodes if it is triply connected. The class representing the
final graph is called the fixed class.
The class giving left/right orientation to the edge lists is called the LR class. It is
used only when processing specifications of an embedding completion problem.
Let L ( G ) be the list of all equivalence classes appearing in n ( G ) except for the fixed
class and the LR class. Suppose L ( C ) contains p classes of P-nodes having k.,...,k chil-
dren, respectively, and q classes of Q-nodes. Then the number of distinct embeddings is
k,!- - - - -kp!-2q-1-(h - I)! if the fixed class contains P-nodes with h children and
k , ! . - - - .k,,!-2q if the fixed class contains Q-nodes.
For example, Figure 2(a) shows the graph of Figure l(a) after a CC transformation
has replaced the triply connected component { d , e ,h , a , j} with the edge k . This transfor-
mation is represented by the class B in Figure l(b). Figure 2(b) shows the result of a
CBG transformation replacing the branch graph { f ,g ,k} by the edge 1 (class C in Figure
l(b)). Since the graph in Figure 2(b) is a branch graph (of degree 3) no further transfor-
mations are done (class A is the fixed class representing this final graph). There are
3!-2 = 12 distinct embeddings of the original graph.
The process of generating all distinct embeddings of G given II(G), L ( G ) , and the
identity of the fixed class is straightforward. Details are given in IS].
3. Creating the Data Structure
This section describes the modifications required in the planarity testing algorithm of
[BL] to generate 11(G), the data structure representing all embeddings of G. The funda-
mental PQ-tree operation used by this algorithm is Reduce(T ,S'), where T is a PQ-tree
defined on S and S ' C S. Reduce( T ,S ') transforms T in such a way that the elements of
S' are forced to be consecutive, or indicates that such a transformation is impossible. We
refer to [BL] or [Y] for the details of Reduce and its linear time implementation (linear in
IS'^).
In the planarity testing algorithm a single PQ-tree keeps track of constraints on the
dangling edges of a partial embedding of the graph in the following way. At the i th stage,
vertices 1 through i have been embedded. Edges from a vertex numbered ¥ i to a vertex
numbered > i are called dangling edges because only one of their endpoints has been
added to the embedding. Vertex i + 1 may be added to the embedding if it is clear that
the dangling edges can be rearranged so that all the edges into vertex i+ 1 appear con-
secutively in left to right order. The operation Reduce( T,S1 +^), where S +, is the set of
dangling edges ending a t vertex i + 1, modifies the current tree so that the edges in Si+
are forced to be consecutive (or reports that this is impossible, in which case the graph is
not planar).
After vertex i + 1 is added, the edges into it are no longer dangling edges. Another
PQ-tree operation, Replace-Su broot, allows the replacement of these edges by a new set
of dangling edges, those from vertex i + 1 to higher numbered vertices.
In our algorithm, the main PQ-tree (the one also used by the planarity testing algo-
rithm of [BL]) is referred to as the active tree. This distinguishes it from the trees that are
stored as the In and Out lists. The latter trees are constructed in the following way. The
tree for I n ( ; ) is simply the part of the active tree that constrains the dangling edges lead-
ing into vertex i . Since the Replace-Subroot removes this portion of the tree after the i th
vertex has been embedded, we can simply save it as the tree for In(i).
At this point, ReplaceSubroot adds the dangling edges from vertex i to the active
tree. The only constraint on them is that they be consecutive. Hence they are all children
of the same P-node. Our algorithm actually creates two P-nodes, one for the active tree
and one for the tree representing O u t ( i ) . The two nodes are are put into a 1-1 correspon-
dence by placing them in the same equivalence class and letting leaves that represent the
same edges be mates. As the active tree is modified in later steps, modifications also take
place in the tree for Out{ t ) to ensure that the correspondence is maintained.
A detailed description of the algorithm for creating f l {G) is given in Appendix A.
Appendix B shows the intermediate steps for the graph of Figure l(a).
Some details of the algorithm require further explanation. Consider the case when
the root of the final active tree is a P-node with 2 children. If the class represented by the
root is declared to be the fixed class, this implies that the final graph after all CBG and
CC transformations is a simple cycle. However, unless G was a simple cycle to begin
with, one transformation too many has been done: undoing the CC or CBG transforma-
tion that led to the simple cycle will result in a graph that is already triply connected or a
branch graph. The rotation done by the algorithm yields a correct representation of the
final graph (the one just before the transformation that leads to a simple cycle).
A related issue is the requirement that vertex n have degree > 3. If vertex n has
degree 2, the final PQ-tree may have a root with two children, both leaves, a condition
suggesting that G is a simple cycle. If G is not a simple cycle it is too late to unravel the
transformations at that point. Fortunately it is always possible to guarantee that vertex
n has degree 2 3 (unless G is a simple cycle).
We turn now to the implementation of the equivalence relation = and the mate
relationship. These details also apply to the algorithms for processing specifications,
described in the next section. During Reduce the active tree is modified according to a set
of local transformations called templates (see [BL],[S], or [Y] for a more detailed descrip-
tion). Some of these templates may further constrain some of the children of a P-node,
making them children of another P-node or of a Q-node. Suppose X is a P-node whose
children are transformed by such a template and X' = X. Then the same template is
applied to X'. The mate relationship is used to determine which children of X' are
affected by the template and in what way. Further details are given in [S],
Other templates insert the children of a Q-node X among the children of its parent
Y (X disappears entirely in this situation). In terms of equivalence classes. -4, the class
represented by X, must be merged into B , the class represented by Y. The difficulty is
that the children of X may have to be put in reverse order when inserted among the chil-
dren of Y. This would mean that the order of children for any Q-node in class A would
have to be reversed.
Instead of performing the reversals of children for a whole class of Q-nodes immedi-
ately, we defer them until they are actually needed to generate an embedding. An auxil-
lary data structure G ( = ) is used to keep track of relative orientation among all Q-nodes.
We can think of G(=) as an undirected labelled graph whose vertices are Q-nodes (some
of which may no longer be present in any tree). When the children of X are inserted
among the children of Y, the operation Merge(X, Y,reverse) adds an edge between X and
Y to G(=) . If no reversal was required, the edge is labelled with a 0, otherwise with a 1
(the boolean parameter reverse is true iff reversal was required).
The connected components of G(^} are the equivalence classes of Q-nodes wrt = .
A path of odd length (using the label of an edge as its length), between two Q-nodes X
and Y implies that the order of the children of X is reversed relative to the order of the
children of Y. If any component contains a cycle of odd length there is an inconsistency
in the orientation of the Q-nodes. This is not possible in the algorithm of Appendix A.
but may occur when specifications are added to the embedding (as described in the next
section).
It is not hard to show that during the algorithm of Appendix A, G ( = ) is always a
forest, and that each class has at most one representative in the active tree. Thus G ( = )
can be represented as a collection of rooted trees (see [S] for further details).
Unfortunately the linear time implementations of Reduce described in [BL] and [Y]
make it difficult to detect the relative orientation of two Q-nodes. Because of efficiency
considerations, correct parent pointers are maintained only for the end children of Q-
nodes. Furthermore. t h e list of children for each Q-node is doubly-linked. bu t the
orientation of the links is not necessarily consistent. The authors of [C] get around this
problem by augmenting the PQ-tree structure with "direction indicators."
What we do instead is to make use of old parent pointers (even if they point to nodes
that are no longer part of the tree) and ensure that the left/right sibling pointers of a
doubly linked list of children are correct relative to the parent pointers of those children.
Once any node becomes the child of a Q-node, its parent pointer never changes unless it
becomes the end child of a different Q-node. Sibling pointers are adjusted only if the
parent pointer changes, i.e. only if a node becomes an end child of a Q-node. It is easy to
detect whether an end child of a Q-node is the left or right end child (the algorithms of
[BL] and [Y] maintain pointers to the two end children for each Q-node); therefore, it is
also easy to determine whether its only sibling is a right or left sibling.
Figure 3 illustrates what happens during two applications of a template that inserts
children of a Q-node among those of its parent. Figure 3(a) shows the initial
configuration [X,Y, and Z are Q-nodes). In Figure 3(b) the children of Y are inserted
among those of X. Note that only c is made aware of the new parent. Accordingly, only
c 's left/right sibling pointers are revised to be consistent with the orientation of the new
parent. The discontinuity in sibling pointers between b and e or between Z and c indi-
cates that Y was reversed in relation to X . If, at some later time, the children of Z are
inserted among those of X as illustrated in Figure 3(c), then the orientation of Z relative
to X or Y is determined. The discontinuity between d and f indicates that Z is reversed
relative to Y. However, since h and c's sibling pointers are consistent, we know that Z
has the same orientation as X.
The reader is referred to [S] for a detailed sketch of the proof that fl(G) created by
the algorithm in Appendix A correctly represents the set of all possible embeddings of G .
The following Lemma, instrumental to the correctness of the algorithm, can be proved by
inductively relating the current forest of PQ-tree5 (in the algorithm of Appendix A) to a
transformed version of the input graph.
Lemma 3.1. [S] If G is a biconnected planar graph used as input to the algorithm in
Appendix A, the following statements are true about equivalence classes wrt == in n ( G )
in relation to a series of CBG and CC transformations on G .
( i ) Every equivalence class containing two P-nodes with k children, except the fixed
class, corresponds to a CBG transformation that removes a branch graph of degree k .
Moreover, the k children of these P-nodes correspond to the k branches of the branch
graph so that a permutation of the children is a permutation of the branches.
(ii) Every equivalence class of Q-nodes, except for the fixed class, corresponds to a
CC transformation.
(iii) The fixed class corresponds to the graph that results after all of the CBG and CC
transformations described above have been performed. If the fixed class contains Q-
nodes, the remaining graph is triply connected. If the fixed class contains two P-nodes
with k children, then the remaining graph is a branch graph of degree k ( k > 2 unless G
is a simple cycle). 0
T h e o r e m 3.1. [S] If G is a biconnected planar graph with m edges, the algorithm in
Appendix A correctly computes n ( G ) in 0 ( m ) time. 0
4. Algor i thms for Embedd ing Complet ion P rob l ems
This section discusses how n ( G ) is used to solve various kinds of offline and online
embedding completion problems. It is useful to describe each specification v: ( e l , - - - ,e,\
as a series of constraints on just two edges a t a time.
Definition 4.1. A p r i m i t i v e constraint on the embedding of a graph is a triple ( e , f ,v),
where e and f are edges incident to the vertex v . It means that only those embeddings in
which e and f appear consecutively around v are to be allowed. Several primitive con-
straints may be linked to form a compound constraint, a set of primitive constraints
{ ( e l , / , r 1 ) , . . - . ( e k . h , v k ) } such that for each i, el precedes / in a rotational ordering
of edges about u,. In case of a compound constraint, either all e , f sequences are clock-
wise about v or all are counterclockwise.
A specification is equivalent to the special case of a compound constraint in which
- - . . . - u - U 2 - - u k . If a set of specifications are linked, i.e. they all have the same
rotational orientation, they may be combined into one compound constraint. All
specifications must be linked, for example, in the problem of verifying an embedding.
It is a simple matter to translate the set of edges forming a face boundary (given in
arbitrary order) to a list of the form eo, - where e and e W q a are incident to
the same vertex v . For each such list, the compound constraint
{(eo,el?o)~ ,(ek-.,eO,vk- correctly represents the constraints imposed by having
09 . . .
l e k - 1 on the same face. A similar observation applies to partial face boundaries.
Thus the input (aside from the graph G) for the embedding completion problems may
include a set of (partial) face boundaries along with or in place of the usual specifications.
If input to the problem of verifying an embedding is given in the form of face boundaries,
the corresponding constraints do not need to be linked. (Note that computing the dual of
G will not solve the problem of handling face boundaries in the input: the dual is not
unique unless G has a unique embedding).
The remainder of this section describes how primitive and compound constraints
may be incorporated into H(G). An algorithm to solve any of the embedding completion
problems consists of three phases.
(1) Construct II(G) using the algorithm of Appendix A.
(2) Process the compound constraints that correspond to specifications (either on-line or
off-line), modifying H(G) accordingly.
(3) Use the final n ( G ) to output either a single embedding, the number of distinct
embeddings. or all distinct embeddings.
First we discuss how primitive constraints are incorporated into n(G). Suppose
(e l f ,v) is an arbitrary primitive constraint. If e and f both lead from lower numbered
vertices to v , or both lead from v to higher numbered vertices, then the operation
Reduce(In(v),{e ,/}) or Reduce(0ut ( v ) , { f , e }) transforms n(G) so that all embeddings are
subject to the primitive constraint. (Although the order of e and j in the second argu-
ment of Reduce is irrelevant in the case of a lone primitive constraint, it is used to check
orientation for a compound constraint. For example, {f ,e} implies that f should appear
to the left of e in the PQ-tree.) As in the algorithm for constructing Il(G), the update
algorithm must maintain the equivalence relation = and the mate relationship.
If e and f appear in different trees, we make use of the Q-nodes of the LR class,
described earlier. Recall that the special leaves *left * and *right* mark the left and right
boundaries of each list of edges. If e is directed into v and f is directed out of v, then the
update algorithm does Reduce( T(In(v)),{e , *right *}) and Reduce(T(0ut (v)),{ f , *right *}).
If e is directed out of v and f is directed into v, the algorithm does
Reduce(T(0ut (v)),{e , *left *}) and Reduce(T(In(v)),{j , *left *}).
Special treatment must be given to vertices v that only have one tree, either I n (v ) or
O u t ( u ) (this is always the case for vertices 1 and n , and may be the case for others as well
if a rotation is done -- see Appendix A). The list In(v) or Out(v) is circular in this case.
Reduce(ln(u),{e, f 1) or Reduce(Out(u),{ f , e } ) may fail even though e and f can be made
consecutive in the circular list. If Reduce(In(v),{e , f }) or Reduce(0ut (v),{ f , e }) fails and
v has only one tree, then the update algorithm attempts Reduce( T ,{e , *right *}) and
Reduce(T,{*left*, / }), where T is In(v) o r Out(v).
Abandoning an unsuccessful Reduce in order to try a different one presents some
implementation problems for the algorithm described in [BL]. Some templates that were
applied successfully might have to be "undone."However, the implementation in [Y] is
able to test for an irreducible configuration without altering the PQ-tree in any way.
For a compound constraint of the form {(e ,, f ,,v.), - . - ,(ek, f k , v k ) } , where k > 1,
we need to ensure that the orientation of all the primitive constraints is consistent (either
all clockwise or all counter-clockwise). This is done as follows. The Reduce operation is
modified slightly so that the least common ancestor of the two leaves involved in the
Reduce is always a Q-node after the Reduce (there is only one template that is affected
and the modification is trivial -- see [S] for details). Hence the processing of the primitive
constraints gives Q-nodes Q., - , Q,,, where k 5 h 2k (each primitive constraint
results in 1 or 2 Reduce operations). A dummy Q-node QÃ is introduced and G(=} is
modified with a Merge(Q,Q,.,reverse) for 1 5 i 5 h (reverse is true iff the order of the
two leaves specified in the second argument to Reduce is the opposite of their order of
appearance as children of Q ) .
For the off-line algorithm G ( = ) is stored directly as a graph. An edge is added
whenever a Merge is done. When all constraints have been processed, the off-line algo-
rithm checks to make sure that no cycles of odd length exist (reporting an inconsistency
in orientation if one does exist) and turns each connected component into an equivalence
class of Q-nodes. Both of these operations can be performed in time linear in the size of
G ( = ) using depth first search. It is easy to see that l ~ ( = ) l is O(m) , where m = G I . The Reduce transformations used in processing the constraints take time O ( m + p ), where
p is the number of primitive constraints.
Special care must be taken to ensure that the fixed class is still well-defined and
correct after each update. If the fixed class is a class of Q-nodes that is merged with
another class, the combined class is the new fixed class. Thus a fixed class of Q-nodes is
always maintained (and it is not hard to see that this is the correct interpretation). The
two P-nodes in a fixed class of P-nodes must obey two invariants in order for n ( G ) to give
the correct set of distinct embeddings: ( i ) they must have more than 2 children (except
when G is a simple cycle), and f i i ) they are always roots of whatever tree they appear in.
The algorithm in Appendix A ensures that both invariants are true initially for n(G). In
subsequent updates, when a template reduces the number of children for a P-node in a
fixed class to 2 or deletes the P-node completely, a class of Q-nodes is chosen as the new
fixed class (for details, see [S]). This is always possible to do correctly when the P-node in
question is the root of its tree.
T h e o r e m 4.1. The data structure l l (G) can be updated to be consistent with a set of
compound constraints in time O ( m + p ) , where m = \ G \ and p is the total number of
primitive constraints. 0
Corol lary 4.1. The off-line embedding completion problem, where output is a single
embedding, can be solved in time O(m + p ) , where m is the size of the input graph and p
is the total size of all specifications. 0
Corol lary 4.2. An embedding of a planar graph can be verified in linear time. 0
Section 2 showed how the total number of distinct embeddings could be computed in
time 0 ( r ) , where r is the number of equivalence classes in n(G) (assuming constant time
per arithmetic operation). It is easy to show that r is O ( m ) . If the sizes of the numbers
must be taken into account, this computation can be done in time O ( m logm ), which is
also the size of the output.
Corol lary 4.3. The off-line embedding completion problem, where output is the number
distinct embeddings, can be solved in time 0 ( m ) (uniform cost model) or O(m logm) (log-
arithmic cost model).
Corol lary 4.4. The off-line embedding completion problem, where output is all distinct
embeddings, can be solved in time 0 ( m d + p ) , where d is the number of embeddings gen-
erated. 0
The problems of finding a single embedding or finding all embeddings of a graph are
simple special cases of Corollary 4.4. It should be pointed out that for these problems, as
well as the embedding verification problem (Corollary 4.2), the extra Q-nodes giving
leftiright orientation for each tree are not needed. The algorithms given here, when spe-
cialized to these simple problems, have several advantages over those of [C]: the
intermediate step of generating an "upward embeddingu is eliminated -- the embedding
or embeddings can be generated directly from the PQ-tree representation; the leaves of
each tree represent edges -- thus graphs with multiple edges can be considered (this is
probably a simple modification to the algorithms of [C]); there is no need for direction
indicators -- the same information is encoded directly using existing parent and sibling
pointers (see Figure 3 and the accompanying discussion).
For on-line updates we use the set merging techniques of [TI. Every Q-node appears
in a forest of trees, each tree representing one equivalence class. The pointer PAR (X)
points to X's parent in the forest, or to X itself if X is a root. R E V ( X ) tells whether X's
orientation must be reversed relative to PAR (X). The algorithm for Merge is a combina-
tion of f ind (to find the roots of the two trees) and link (to join the roots together if
necessary) in [TI, with the additional complication that a consistency check wrt orienta-
tion must be done if the two Q-nodes occur in the same tree (using REV). Details are
given in [S].
The time bound, based on Tarjan's analysis [TI, is O(m + pa(p , r ) ) , where r is the
total number of Q-nodes. From the algorithm described in Appendix A it is apparent
that the initial n ( G ) can be set up in such a way that each equivalence class of Q-nodes
has a unique representative known to all nodes of the class. Furthermore, the new Q-
nodes generated by primitive constraints each belong to a particular compound constraint
(and end up in the same equivalence class). We can easily designate a unique representa-
tive for the set of new Q-nodes generated by each compound constraint. If a compound
constraint is linked to a previous compound constraint, the same unique representative is
used for both constraints. Thus r can be replaced by the number of equivalence classes in
the initial n((7) plus the number of compound constraints. The former is O(logd), where
d is the number of embeddings of the original graph (by the analysis of Section 2 every
distinct equivalence class of Q-nodes except for the fixed class doubles the number of
embeddings).
Updates on the total number of embeddings can be done within the above time
bound if all arithmetic operations take constant time. If the number of embeddings is too
large for this assumption to be reasonable, an alternate strategy is to estimate the loga-
rithm of the number of embeddings until this number is reduced to a reasonable size (by
the constraints).
Theorem 4.2. The on-tine embedding completion problem can be solved in time
0 ( m + p a ( p l c +logd)), where c is the number of disjoint groups of specifications and d
is the number of embeddings of the original graph. 0
It is important to note that the time bound from [TI is amortized, and assumes in
this case a sequence of f l ( c + logd) Merge operations, ,or, putting it another way, it
assumes the number of equivalence classes of Q-nodes is reduced to a constant. This is a
realistic assumption in most settings (particularly the application described in Section I),
since it is certainly satisfied when the additional constraints reduce the number of embed-
dings to a constant (the number of Q-node equivalence classes is then reduced to a con-
stant as well).
Appendix C shows the processing of a compound constraint for the graph of Figure
1. Merge operations are shown wrt equivalence classes in the original II(G) and those
I introduced by later transformations (in actual fact, these operations are done on represen-
tatives of the classes instead of the classes themselves).
References
Alok Aggarwal, Maria Klawe, David Lichtenstein, Nathan Lineal, and Avi Widg- erson, "Multi-layer grid embeddings," Proc. 26th Ann. IEEE Symp. on Founda- tions of Computer Science (Portland, 1985), 186-196.
B.S. Baker, "Approximation algorithms for NP-complete problems on planar g r a p h s , V r o c . 24th Ann. IEEE Symp. on Foundations of Computer Science (IEEE, 1983), 265-273.
K.S. Booth and G.S. Lueker, "Testing for the consecutive ones property, interval graphs, and graph planarity using PQ-tree algorithms," JCSS 13 (1976), 335-379.
N. Chiba, T. Nishizeki, S. Abe, and T. Ozawa, "A linear algorithm for embedding planar graphs using PQ-trees,"JCSS 30 (1985), 54-76.
Danny Dolev, Tom Leighton, and Howard Trickey, "Planar embedding of planar graphs," Advances in Computing Research, vol. 2 (JAI Press, 1984), 147-161.
S. Even and R.E. Tarjan, "Computing an st-numbering," Theoretical Computer Science 2 (1976), 339-344.
G.N. Frederickson, "Shortest path problems in planar graphs," Proc. %th Ann. IEEE Symp. on Foundations of Computer Science (IEEE, 1983), 242-247.
L. Heath, "Embedding planar graphs in seven pages," Proc. 25th Ann. IEEE Symp. on Foundations of Computer Science (IEEE, 1984), 74-83.
J.E. Hopcroft and R.E. Tarjan, "Isomorphism of planar graphs (Working paper)," in Complexity of Computer Computations, Miller and Thatcher, eds. (Plenum, 1972), 131-152.
J.E. Hopcroft and R.E. Tarjan, "Efficient planarity testing,"JACM 21 (1974), 549-568.
J.E. Hopcroft and J.K. Wong, "Linear time algorithm for isomorphism of planar graphs (Preliminary report),"Proc. 6th Ann. ACi^f Symp. on Theory of Comput- ing, (ACM, New York, 1974), 172-184.
A. Lempel, S. Even, and I. Cederbaum, "An algorithm for planarity testing of graphs," in Theory of Graphs: International Symposium: Rome, July, 1966 P . Rosenstiehl, ed. (Gordon and Breach, New York, 1967), 215-232.
R.J. Lipton and R.E. Tarjan, "Applications of a planar separator theorem," SIAM J. Computing 9 (1980), 615-627.
S. MacLane, "A structural characterization of planar combinatorial graphs," Duke Math. J. 3 (1937), 460-472.
M. Stallmann, "Using PQ-trees for planar embedding problems,"XCSU-CSE Technical Report TR-85-24.
James A. Storer, "The node cost measure for embedding graphs on the planar grid."Proc. 12th Ann. ACM Symp. o n Theory of Comput ing (Los Angeles, 1980). 201-210.
H. Suzuki, T. Nishizeki, and N. Saito, nMulticommodity flows in planar undirected graphs and shortest pathsln Proc. 17th Ann. ACM Symp. on Theory of Computing (ACM, New York, 1985), 195-204.
R.E. Tarjan, Data Structures and Network Algorithms ( S L A M , 1983).
Leslie G. Valiant, "Universality condiderations in VLSI circuits,VEEE Trans. on Computers, C-30, 135- 140 (February, 1981).
S.M. Young, "Implementation of PQ-tree algorithmsln Master's thesis, Dept. of Computer Science, University of Washington, 1977.
Appendix A. Algorithm for generating n(G).
procedure Embed((7) is
/* Produces a set of In and Out trees for II(G') ( G has n vertices) and an equivalence relation = among interior nodes of the forest (including the list L ( G ) of all equivalence classes and the fixed class). The vertices of G are assumed to be num- bered according to an st-numbering (hence G is biconnected). Vertex n is required to have degree 2 3 unless G is a simple cycle. */
I* Initialize the active tree T. */ T : = Create-Universal-Tree(1) for i = 2 to n - 1 do
Si = leaves of T representing edges to vertex i T := Reduce (T ,S , ) if T = 0 then
halt (the graph is not planar) end if Replace-Subroot ( T , i
end do
L ( G ) : = all equivalence classes wrt =s except the one containing the root of T if root of T is a P-node with 2 children, not both leaves then
Rotate(T) end if I n ( n ) := T f ixed-class := equivalence class of root of T add new Q-nodes for left and right orientation to each In and Out tree
end Embed
function Create_Cniversal_Tree(i) returns PQKODE is
/* Returns a tree representing the initial constraint on edges from vertex i, namely that they all appear consecutively. The initial tree for O u t ( i ) is a clone of this new tree, linked to it by the mate relationship. *I
E = edges from vertex i if \ E 1 = 1 then
e : = the single edge in El Out ( i ) := e return a leaf repesenting e
else create a new P-node p create a new P-node p ' make p = p'
for e ? El do create a new leaf z representing e create a new leaf z ' representing e mate(x) := 2'; mate(xf) := z parent(x) := p ; parent (2') := p '
end do Out(i) := p ' return p
end if end Create Universal Tree
procedure Replace_Subroot( T, i ) is
/* Replaces the full descendants of R O O T ( T , S ) with a tree representing the initial constraint on the edges from vertex i . Note: the preceding Reduce(T,S) opera- tion marks nodes of T nfulln if they are leaves in S or if all their descendants are full; a node is "emptyn if it is a leaf not in S, or if all its descendants are empty (see e.g. [BL]); ROOT(T ,S) is the lowest common ancestor of all full nodes in T. The full subtree deleted from T is stored as the tree for In(i). */
p : = Create_Universal_Tree(i) r := ROOT(T,S,) if r is a Q-node with empty children then
/* the identity of r is not necessarily known in this case */
let f , - . f, be the full children of r let e o be the empty sibling of f or 0 if it doesn't exist
/* the full subtree needs a new root because R O O T ( T , S ) has empty children */ create a new Q-node q children(q) := [f - . . f J Merge(q,parent(f l)l/ef t ( f ) = e ) In t i ) := q replace f - - f, by p in children(r)
else /* r is a P-node, a leaf, or Q-node with no empty child */ /* p replaces r and r is the root for In( i ) . */ parent (p) := parent ( r ); mate ( p ) : = mate ( r ) if r is the child of a Q-node q then
replace r by p in children(q) end if In ( 1 ) := r parent{r) := 0; mate(r) := 0
end if end Replace Subroot
procedure Rotate( T ) is
/* Gets rid of the root of T if the root is a P-node with only two children. Assumes that the root has a t least one non-leaf child. */
z := a non-leaf child of root of T y : = the other child of root of T if x is a P-node then
let z ' be the unique node = z /* x' is the root of the tree representing Out ( i ) for some vertex i # 1 */ make T ( I n ( i ) ) a child of x' T ( I n ( i ) ) := 0
end if make y a child of x make x the root of T remove the class containing z from L (G)
end Rotate
Appendix B. Example of the algorithm in Appendix A (graph from Figure 1).
Vertex 1 Active tree: <a b c > A
Out(1): <a b c > A
Vertex 2 Reduce(T,{a}) is a no-op (the subroot is a leaf) Replace_Subroot( T ,2)
In(2): a Active tree: < < d e f g >g b c > A
Out(2): <d e f g > B
Vertex 3 Reduce( T , { d } ) is a no-op (the subroot is a leaf) Rep!ace_Subroot( T ,3)
In(3): d Active tree: <<<h i>^ e f g > B b c > A
Out(3): < A i>c
Vertex 4 Reduce( T ,{e ,A})
Active tree: <<[i h e l D f g > B b c > ^
out(2): <Id 1 f g > Out(3): [i A I D (note that Class C goes away)
Replace_Subroot( T , 4 ) I n ( 4 ) : [h elD Active tree: < < [ a jID f g > Ã b Out(4): J
<- > A
Vertex 5
<<[' jID f ! ; > B b c > , Fixed = A
(the operation which adds left/right orientation to each tree is not shown)
Appendix C. Processing of a Compound Constraint.
Initial forest.
Out (1): [*left* <a b c > *right *\y
In(3): [*left* d * r i g h t * ] ; Out (3) : [ *left * [i h ] 'right*]
In(5): [*lef t* <<f g [i j ] B > c b c > A 'right*}.,;
Initially there are 12 distinct embeddings ( A is the fixed class)
Processing the compound constraint.
The compound constraint is { ( a ,/ ,2),(b, j , 5 ) } .
Changes during the processing of ( a , f ,2) -- Reduce( T (In (2)),{a , *right * }) Choose a node from Class D to represent the constraint. Reduce( T (Out (2)),{/ , *right*})
Out(2): [*left* <g [ d e ]B>c f *right*}^ Merge(F , D ,false )
In(5): [*left* <[<g [ a f \ p b c > A *right*}^
There are now only 8 distinct embeddings (Class C has only 2 children).
Changes during the processing of (6 , I ,5) -- Reduce( T ( h (5)),{b, 31)
In (5 ) : [ *left* <[b j i g f I F c >, *right *ID Merge(5 ,F ,true ) (note that i and J had to reverse order) Merge(F ,D ,false ) F is the new fixed class (in place of A )
Out(2): [*left* l q [ d f *right*ID Out(1): [*lef t* <[b a ] c > A *right *ID
There is now only one distinct embedding. (note: the children of nodes in class B appear in reverse order)
Figure 1. A planar graph G and its representation fl(G).
a ) fe
In(1): 0; Out (1): [ *lef t * <a b c > A *right *ID
In (3): [ *lef t * d *right *] ; Out (3): [ *lef t * [ a hIB *right * ]
In(4): [ * l e f t * [ h e l B *right*\..; Out ( 4 ) : [ *left * j *right * I D
In(5): [* le f t* <<f g [ I J I B > , 6 c>. *ra'ght*in; Out ( 5 ) : 0
(b)
Figure 2. CC and CBG transformations on the graph of Figure 1 .
Figure 3. Implementation of pointers for children of Q-nodes.