16
Acta Informatica 18, 377-392 (1983) Springer-Verlag 1983 The Complexity of Drawing Trees Nicely* Kenneth J. Supowit i and Edward M. Reingold Department of Computer ScienceUniversityof Illinois at Urbana-Champaign Urbana, Illinois 61801, USA Summary. We investigate the complexity of producing aesthetically pleasing drawings of binary trees, drawings that are as narrow as possible. The notion of what is aesthetically pleasing is embodied in several constraints on the placement of nodes, relative to other nodes. Among the results we give are: (1) There is no obvious "principle of optimality" that can be applied, since globally narrow, aesthetic placements of trees may require wider than necessary subtrees. (2) A previously suggested heuristic can produce drawings on n-node trees that are O(n) times as wide as necessary. (3) The problem can be reduced in polynomial time to linear programming; hence, if the coordinates assigned to the nodes are continuous variables, then the problem can be solved in polynomial time. (4) If the placement is restricted to the integral lattice then the problem is NP-hard, as is its approximation to within a factor of about 4 per cent. From a tree no proof can be brought. Talmud: Baba Metziah 59b I. Introduction Recent papers [-7, 10, 9], have studied the problem of producing narrow, well- shaped drawings of tress. The notion of "well-shaped" is incorporated by several aesthetics [-7, 10] designed to capture various aspects of shapeliness. The basic task is to assign a pair of coordinates (x, y) to each node of the tree; after such an assignment of coordinates, the tree is easily printed or drawn on some output device. In this paper, we will examine the complexity of determining the assign- ment of coordinates that gives the narrowest possible drawing while satisfying the aesthetics. Let T be a (rooted, binary) tree. A placement of T is a function ~: {nodes of T}~R 2. * This research was supported in part by the National Science Foundation, grant numbers NSF MCS 77-22830, NSF MCS 79-04897, and NSF MCS 81-17364 1 Present address: Hewlen-Packard,Computer Research Lab, Palo Alto, CA94304, USA 0001-5903/83/0018/0377/$03.20

The complexity of drawing trees nicely

Embed Size (px)

Citation preview

Page 1: The complexity of drawing trees nicely

Acta Informatica 18, 377-392 (1983)

�9 Springer-Verlag 1983

The Complexity of Drawing Trees Nicely*

Kenneth J. Supowit i and Edward M. Reingold

Department of Computer Science University of Illinois at Urbana-Champaign Urbana, Illinois 61801, USA

Summary. We investigate the complexity of producing aesthetically pleasing drawings of binary trees, drawings that are as narrow as possible. The notion of what is aesthetically pleasing is embodied in several constraints on the placement of nodes, relative to other nodes. Among the results we give are: (1) There is no obvious "principle of optimality" that can be applied, since globally narrow, aesthetic placements of trees may require wider than necessary subtrees. (2) A previously suggested heuristic can produce drawings on n-node trees that are O(n) times as wide as necessary. (3) The problem can be reduced in polynomial time to linear programming; hence, if the coordinates assigned to the nodes are continuous variables, then the problem can be solved in polynomial time. (4) If the placement is restricted to the integral lattice then the problem is NP-hard, as is its approximation to within a factor of about 4 per cent.

From a tree no proof can be brought. Talmud: Baba Metziah 59b

I. Introduction

Recent papers [-7, 10, 9], have studied the problem of producing narrow, well- shaped drawings of tress. The notion of "well-shaped" is incorporated by several aesthetics [-7, 10] designed to capture various aspects of shapeliness. The basic task is to assign a pair of coordinates (x, y) to each node of the tree; after such an assignment of coordinates, the tree is easily printed or drawn on some output device. In this paper, we will examine the complexity of determining the assign- ment of coordinates that gives the narrowest possible drawing while satisfying the aesthetics.

Let T be a (rooted, binary) tree. A placement of T is a function

~: {nodes of T}~R 2.

* This research was supported in part by the National Science Foundation, grant numbers NSF MCS 77-22830, NSF MCS 79-04897, and NSF MCS 81-17364 1 Present address: Hewlen-Packard, Computer Research Lab, Palo Alto, CA94304, USA

0001-5903/83/0018/0377/$03.20

Page 2: The complexity of drawing trees nicely

378 K.J. Supowit and E.M. Reingold

The mapping n specifies, for each node p in T, the coordinates n(p)= (x, y) of the point in the Euclidean plane where p is to be placed when the tree is drawn. Drawing a tree T means drawing, for each edge (p, q) in T, a straight line segment joining point n(p) to point n(q). We will use nx(p) and nr(p) to denote the x and y coordinates of n(p), respectively. A placement n of T is eumorphous (from the Greek eylaopc~og meaning "well-shaped") if it satisfies the following six aesthetic constraints:

Aesthetic 1. For each i>0, there is a straight line fl such that for each node p which has level i, n(p) lies on fi. (The level of a node is the number of branches between it and the root; that is, the root has level 0, and each other node has level one greater than that of its father). Furthermore, the lines fi are all mutually parallel and evenly spaced. Without loss of generality, we require that the lines fi are all parallel to the x-axis.

Aesthetic 2. Each right son is placed strictly to the right of its father, and each left son strictly to the left of its father. In particular, for each node p that has a right son,

nx(rightson(p))-nx(p) > 1,

and for each node p that has a left son,

n~(p) - n~(leftson(p)) > 1.

Aesthetic 3. For each i >0, for each two nodes p, q having level i, p and q must be placed at least 2 units apart, that is,

Inx(p)- nx(q) l > 2.

Note that Aesthetic 3 is not implied by Aesthetic 2, since p and q need not have the same father.

Aesthetic 4. Fathers must be centered over their sons. That is, for each node p that has a left son and a right son,

n~(rightson(p)) - n~(p)= nx(p) - n~(leftson(p)).

Aesthetic 5. No two tree edges cross each other when the tree edges are drawn as straight line segments. That is, if two tree edges are drawn so as to intersect, then they share a common endpoint.

Aesthetic 6. If T 1 and T2 are isomorphic subtrees of T, then n must place T 1 and T z identically, up to a translation. We use the term isomorphic to denote what Knuth calls similar ([-5], p. 325): Two binary trees 7"1, T 2 are isomorphic if either (1) they are both empty, or (2) they are both non-empty and their left and right subtrees are respectively isomorphic. By a translation, we mean that there are two real numbers A~ and Ay such that for each node p~ of 7"1,

rt~(p~) = n~(p2) + Ax,

and nr(p~) = roy(p2) + d r,

where P2 is the node of T2 which corresponds to Pl under the isomorphism.

Page 3: The complexity of drawing trees nicely

The Complexity of Drawing Trees Nicely 379

Some justification for the first of these aesthetics is given in [10], but they are rather self-evident. We note, however, that in drawing parse trees, one might want all the leaves to lie on one horizontal line; for that application Aesthetic 1 is not desirable. In this case, though, the width (as defined below) of the placement is fixed and so the minimum width placement problem for such parse trees is not interesting. We therefore restrict our attention to the wide class of applica- tions for which Aesthetic 1 is desirable.

Unlike the first five aesthetics, Aesthetic 6 perhaps does not immediately come to mind as a desirable property of tree drawings. However, we have included it for two reasons:

(1) In I-7] an example is given of an undesirable placement of a tree by the algorithm of [-10]. That placement satisfies Aesthetics 1 through 5, but not Aesthetic 6. The undesirable features of this placement are precluded by Aes- thetic 6. (In fact, this is why the authors of [7] proposed Aesthetic 6.)

(2) In some applications, one wishes to examine large trees to find repeated patterns; the search for patterns is facilitated by having isomorphic subtrees drawn isomorphically. For example, Bitner (see [6]) was looking for a way to prune a certain class of backtrack search trees. He discovered a recurrent pattern by visually examining a tree with 348 nodes. Such examinations are facilitated by Aesthetic 6 since it can aid in human pattern recognition.

Our problem is: Given a tree T, find a eumorphous placement ~ of T of minimum width, where the width of n is defined as

width(re(T)) = max {rex(p)- rex(q): p, q are nodes of T}.

Because of the Aesthetic 1, the assignment of y-values to the nodes does not affect the width of the placement. Therefore we need consider only those eu- morphous placements rc such that for each node p other than the root, ~y(p) = roy(father(p))+ 1; in other words, the Euclidean distance from {i to fi+l is 1, for each i >0.

The Wetherell-Shannon algorithm, given in [10], produces placements satisfying the first five Aesthetics. In [7], Reingold and Tilford give examples for which the Wheterell-Shannon algorithm produces placements wider than necessary. They also present a heuristic algorithm that produces placements of narrow width satisfying all six aesthetics (i.e., eumorphous placements). Both the Wetherall-Shannon and the Reingold-Tilford algorithms can be implemented in time O(n), where n is the number of nodes in the tree being drawn.

There are four main results of the present paper. In Sect. II, we show that there is no obvious "principle of optimality" that could lead to a dynamic programming solution, since there exist trees whose minimum width placement cannot be achieved without making some subtrees wider than necessary. Also, we show that for an infinite class of integers n, there are n-node trees T such that

width(re(T)) _ n + 2 width(~'(T)) 6 '

where ~' is a minimum-width eumorphous placement of T and rc is the placement of T produced by the Reingold-Tilford algorithm. In Sect. III, we show that

Page 4: The complexity of drawing trees nicely

380 K.J. Supowit and E.M. Reingold

the problem of finding a minimum width placement can be reduced in polynomial time to linear programming, and hence can be solved in polynomial time. However, as shown in Sect. IV, if we restrict placements to map nodes onto points in the integral lattice, then the problem is NP-hard; in fact, we show that if P +NP, then there does not exist a polynomial time algorithm A which produces eumorphous placements such that for each tree T,

width (Tz(T)) 25

width (rc'(T)) < ~ '

where re' is the minimum width eumorphous placement and rc is the placement produced by algorithm A.

II. Suboptimality of the Reingold-Tilford algorithm

The Reingold-Tilford (RT) algorithm for producing eumorphous placements works on a tree T essentially as follows:

1. If T has a nonempty left subtree, then apply the algorithm recursively to that subtree.

2. If T has a nonempty right subtree, then apply the algorithm recursively to that subtree.

3. (a) If root (T) has only one nonempty subtree, then place that subtree, as positioned by (1) or (2), so that the root of the subtree is horizontal distance 1 from its father, on the appropriate side.

(b) If T has two nonempty subtrees then place them, as positioned by (1) or (2), as close together as possible without violating the aesthetics, and then place the root of T midway between the sons in accordance with Aesthetic 4.

It is simple to show by induction on the height of T that the algorithm gives a eumorphous placement of T.

A plausible principle of optimality would be: if a eumorphous placement rt of a tree T has minimum width then for each subtree T' of T, the placement n(T')

Fig. 1. Schematic view of a tree that violates the principle of optimality

Page 5: The complexity of drawing trees nicely

The Complexity of Drawing Trees Nicely 381

Fig. 2. A tree that violates the principle of optimality

has minimum width. It turns out, however that this is not always true, as we now show. We first informally describe the tree T pictured schematically in Figs. l(a) and l(b). If the subtree rooted at node p is placed with a minimum width eumor- phous placement (as in Fig. l(b)), then Aesthetic 3 forces T 3 to be placed rather far to the right, in order that the leftmost node of T 3 does not collide with the right- most node of T1. On the other hand, if the root of T t is placed farther from the root of T2, then T 3 can be moved to the left (see Fig. l(a)); thus making the subtree rooted at p wider than necessary, but giving the entire tree T minimum width.

To be more precise, an example of such a tree T is given in Fig. 2(a) and 2(b). Because of Aesthetics 1 through 3, for each eumorphous placement ~ of T such that width (~(subtree rooted at p))) has minimum width (namely, 2), we have width (~(T)) > 16; such a placement is shown in Fig. 2 (a). However, the eumorphous placement 7z of T shown in Fig. 2(b) satisfies

width (lz(subtree rooted at p)) = 5,

and width (~z(T))= 14.5.

Thus, T is a counterexample to the principle of optimality stated above. Because Algorithm RT works recursively on each nonempty subtree of its input tree, the falseness of the principle of optimality proves that Algorithm RT does not always produce minimum width eumorphous placements. In fact, we now show that algorithm RT does not always even approximate the minimum width eumorphous placement.

Page 6: The complexity of drawing trees nicely

382 K.J. Supowit and E.M. Reingold

Fig. 3(a). Algorithm RT's placement of T 1 (b). The minimum width eumorphous placement of T 1

Fig. 4(a). Algorithms RT's placement of T k (h). The minimum width eumorphous placement of Tk

Figure 3 (a) shows the placement rt produced by Algorithm RT for a tree 7"1 ; Figure 3(b) shows a minimum width eumorphous placement of T1. Note that

width(re(T0)=3 and width(rc'(T1))=2.

The tree Tk in Figs. 4(a) and 4(b) consists of k copies of the tree T1 of Fig. 3, linked together vertically. Figure 4 (a) shows the placement zt of T k produced by Algorithm RT; Figure 4 (b) shows the minimum width eumorphous placement r(. The number of nodes in Tk is

Also,

k((the number of nodes in T1)- 1)+ 1 = 6 k + 1.

n + 2 width (n(Tk)) = k (width (re(T1)) - 1) + 1 = 2 k + 1 --

3 '

where n = 6 k + 1 is the number of nodes of Tk. Thus, for an infinite class of n,

Page 7: The complexity of drawing trees nicely

The Complexity of Drawing Trees Nicely

there exist trees T such that

width ( r e (T) ) . ( n ~ 2 ) +2 width(n '(r)) / 2 = n 6 '

where n' is the minimum width placement of T.

383

III. A Polynomial-Time Algorithm for the Continuous Case

In this section, we prove that the problem of computing a minimum width eumor- phous placement of a tree T can be done in deterministic polynomial time. The proof is a polynomial time reduction of the problem to a linear program, which can be solved in deterministic polynomial time by Khachian's algorithm [3]. We stress that this result is not meant to be of practical importance; it serves only to contrast with the NP-hardness results of the next section. For that reason, we have not concentrated much on the efficiency of the resulting polynomial time algorithm, but have been satisfied with somewhat crude bounds on performance.

The linear program that we will construct has variables nx(p) for each node p in the tree, and two auxiliary variables X and x. In addition to the inequalities that we describe below, we have, for all nodes p,

x > ~x(p),

and x < rex(p).

The objective function to be minimized in the linear program is X - x , the width of the mapping rc. Thus we will have a minimum width placement, subject to the constraints of the inequalities, as constructed from the tree and the aesthetics.

As we have stated above, the first aesthetic does not concern the nx(p). For the second aesthetic, we introduce the inequalities

n~(rightson(p))-n~(p) > 1, (1)

for all nodes p for which rightson(p) exists, and

nx(p) - n~(leftson (p)) > 1, (2)

for all nodes p for which leftson(p) exists. These constraints can be computed in a preorder traversal of the tree [5]. For the third aesthetic, we have the inequalities

~x(P2) - n~,(P a) > 2, (3)

for all nodes Pl, P2 such that pl and P2 are on the same level of the tree and P2 is the level order successor of p~. These constraints can be computed on a level order traversal of the tree [6]. Constraints (1), (2) and (3) also force the placement rc to satisfy the fifth aesthetic, as a simple inductive argument shows. To force compliance with the fourth aesthetic, we have the equalities

~ (rightson (p)) - ~(p) = rc~(p) - z~x(leftson (p)), (4)

for all nodes for which both leftson(p) and rightson(p) exist.

Page 8: The complexity of drawing trees nicely

384 K.J. Supowit and E.M. Reingold

Aesthetic 6 is the most troublesome, since it requires the determination of all pairs of isomorphic subtrees in the tree. We can do this in polynomial time by labelling each node in the tree with a label consisting of

(1) the number of nodes in the subtree rooted at that node, and (2) the rank of that subtree, where the rank is as defined by Knott in [4]. The computation of the rank at the root requires (recursively) the computation

of the rank for each of the subtrees, and so the labelling requires a single application of an appropriate form of the rank procedure given in [4]. In addition, we may encode the number of nodes in the subtree with the rank, as Knott suggests. The rank of a subtree, together with its size, uniquely determines the subtree, up to isomorphism. Thus by sorting the nodes in increasing order by label, we can parti- tion them into equivalence classds based on isomorphism. For each non-singleton equivalence class of subtrees other than the leaves, we add constraints as follows. Let {Pl, P2 .... , Pk} be a set of roots of subtrees in some equivalence class. We include inequalities

n ~ ( r i g h t s o n (Pi)) - 7Zx(Pi) = n~(rightson (Pi + 1)) - 7~x(Pi + 1) (4 a)

for all i, l __< i < k such that right subtrees exist for trees in the equivalence class, and

rex (pl) - rc~(leftson (Pi)) = ~(Pi+ 1) - rc~(leftson (pi+ 1)) (4 b)

for all i, l < i < k, such that right subtrees do not exist for trees in the equivalence class.

Because corresponding subtrees of isomorphic trees are isomorphic, constraints (4 a) and (4b) are sufficient to force compliance with Aesthetic 6. The time required to compute (4a) or (4b) depends on the time to compute the rank function but that is clearly polynomial, as follows from a straightforward analysis of Knott's formulae.

The total number of constraints (1), (2), (3) and (4) is O(n) for a tree with n nodes.

Thus the reduction to a linear program can be done in polynomial time and by [3] can be solved in polynomial time.

IV. The NP-Hardness of the Discrete Case

In this section, we restrict placements to the integral lattice; that is, we allow only placements r~ such that for each node p, ~x(P) and ~y(p) are integers. This case is of far more practical significance than the continuous case discussed in Sect. III, because in actually drawing trees the resolution is finite and very crude on line printers, and better but still finite on plotting devices.

We will show the following discrete optimization problem to be NP-hard: Given a tree T, find a minimum width eumorphous placement of the nodes of T on the integral lattice. We prove this by showing that the corresponding decision problem is NP-complete: Given a tree T and an integer W > 1, is there a eumor- phous placement ~z of T on the integral lattice so that width(~(T))<= W? Because integer programming is in NP [1], the results of Sect. III imply that this decision problem is in NP.

Page 9: The complexity of drawing trees nicely

The Complexity of Drawing Trees Nicely 385

Fig. 5. Schematic view of T(E), where E = ( F 1 A F 2 A ... A Fr)

To show NP-hardness of the decision problem, we will give a reduction from 3-SAT (see [2]) to the specific decision problem in which W=24. Let

E=F, AG~...AF~

be a Boolean expression over the variables x l , x2 . . . . . x, , with clauses

F i = ( Y i , 1 + Yi, 2 + Yi, 3),

for each i, l < i < r, where the Yij are literals. We will construct a tree T(E) for which there is a eumorphous placement of width <24 if and only if E is satisfiable. Conceptually, T is of the form shown in Fig. 5. That is, T(E) can be thought of as consisting of r clause trees, each corresponding to a clause F i. Denote the clause tree for F i by CT(Fi). If E is satisfiable then there is a eumorphous placement g such that width(n(CT(F~)))<24, for each i, 1 <i<_r. On the other hand, i fE is not satisfiable, then for each eumorphous placement g, at least one clause tree Fj satisfies width (n(CT(Fj))) >_ 25; hence width (n(T(E))) > 25.

Page 10: The complexity of drawing trees nicely

386 K.J. Supowit and E.M. Reingold

Fig. 6. Schematic view of a clause tree CT(F), where F=(y I +yz+y3)

Fig. 7. The variable tree VT(x~)

Fig. 8. The literal tree LT(y), where y-~x k

Construction o f a Clause Tree

We n o w descr ibe CT(F) , for a c lause F = ( y 1 +Y2+Y3) . C T ( F ) c o n t a i n s as sub- trees three literal trees (deno ted LT(yl) , LT(y2), a n d LT(y3)), one c o r r e s p o n d i n g to each l i teral of F, as s h o w n in Fig. 6: v~ is the roo t of CT(F) . I t has a left son v 2 and a r ight son v 3. v z has a left son v 4 a n d a r ight son v 5. v3 has a r ight son v6. v~ has as a left son the roo t of LT(yi,1). v 5 has as a r ight son the roo t of LT(yi,2).

Page 11: The complexity of drawing trees nicely

The Complexity of Drawing Trees Nicely 387

Fig. 9. The literal tree LT(y), where Y='X'k

Fig. 10. The clause tree CT(F), where F=(xI+x2+xr The diamond is the root of CT(F); the squares are the roots of the three literal trees; the circles are the roots of the three variable trees

V 6 has as a r ight son the roo t of LT(yi. 3)" Not ice tha t the roo ts of the three l i teral trees all have the same level (namely, 3) in CT(F).

We now descr ibe LT(y), for a l i teral y in F. The var iable in the l i teral y is x k for some k, 1 < k < n; that is, y is x k or Xk. LT(y) contains , as a subtree, the variable tree (denoted VT(Xk)) for x k. VT(xk) is shown in Fig. 7(a) and 7(b). p is the roo t of VT(Xk). There is a zigzagging tai l of k + 1 nodes qo, ql, ..., qk. That is, for all m, 1 < r e < k - 1 , q,, has exact ly one son q , ,+l . I f m is even then q,,+t is a left son, o therwise it is a r ight son.

If y is uncomplemen ted , i.e., y = x k, then LT(y) is as shown in Fig. 8(a) and 8(b). Otherwise, when y=Xk, LT(y) is as shown in Fig. 9(a) and 9(b). In ei ther case, not ice that VT(Xk) is a subt ree of LT(y).

This comple tes the descr ip t ion of CT(F) (later, we will say why we dis t inguish the node w in Figs. 8 and 9). The example CT(F), where F = (x 1 + x2 + x3), is shown in Fig. 10. In Fig. 10, the roo t of CT(F) is shown as a d i amond , the three roots of l i teral trees are shown as squares, and the three roo t s of var iable trees are shown as circles.

Page 12: The complexity of drawing trees nicely

388 K.J. Supowit and E.M. Reingold

Linking the Clause Trees Together

The root of CT(F1) is also the root of T(E). The only remaining detail of T(E) to be specified is how the clause trees are connected to each other. Fix some i, 1 < i < r - 1. We will describe how CT(Fz) is connected to CT(Fi+ 1). Consider the node of LT(y~, 2) (i.e., the middle literal of Fi) labelled w in Figs. 8 and 9. There is a zigzagging tail of n + 6 nodes coming down from w. The (n + 6) th node in the tail is the root of CT(F~+ O.

As an example, the complete tree T(E) produced by this reduction for the expression

E = (x i + X2 -~- X3) A (.~ 1 + X2 "-[- X4) A (X2 "-~ X3 + "~4)

is shown in Fig. 11. The diamonds, squares, and circles are used to distinguish types of nodes exactly as in Fig. 10.

Proof that E is Satisfiable iff T(E) can be Placed in Width <= 24

Note that each occurrence of each variable tree in T(E) is a subtree of T(E). Therefore, for all k, 1 < k < n, Aesthetic 6 causes each occurrence of VT(Xk) to be placed identically, up to translations. This is a key point in the proof.

Assume that E is satisfiable with the truth assignment

~: {xl, x2, ..., x,}--, {true, false}.

We will construct a placement rc of T(E) such that width(7~(T(E)))< 24. For all k, 1 < k < n, if ~(Xk)= true then let n place V T(xk) as shown in Fig. 7 (a);

that is, each son has the minimum (1 unit ) allowed horizontal distance from its father. If Z(Xk)=false, then rc places VT(Xk) as shown in Fig. 7(b); that is, each son has the minimum horizontal distance from its father, except for the son of p, which has horizontal distance 2 from p.

For all i and j such that 1 < i < r and 1 =<j__< 3, if Yij=Xk is uncomplemented, then let n place it as in Fig. 8 (a) if z (Xk)= true, and as in Fig. 8 (b) if z (xk)= false. If yij='Yk is complemented, then place it as in Fig. 9(b) if Z(Xk)=true, and as in Fig. 9 (a) if z (Xk)= false. Note that if Yij is true under z, then width(r~(LT(yij)))= 6 (Figs. 8(a) and 9(a)). If Yij is false under z, then width(n(LT(yij)))=7 (Figs. 8(b) and 9 (b)).

We now consider the placement of the clause trees. Let 1 < i_< r. Note that for all j, 1 _<j __< r, the root of LT(y~j) is the only son of its father (see Fig. 6). Therefore we have the freedom to place those three roots close together without worrying about Aesthetic 4. More specifically, place the root of LT(y~. 1) one unit left of its father in CT(F~). Then place the root of LT(y~, 2) as far left as possible while having each node of LT(yi, 2) placed at least 2 units to the right of each node of LT(yi, 1) (so that Aesthetic 3 is not violated). Similarly, place the root of LT(y~, a) as far left as possible. Thus,

width (r~(CT(F~))) < width (rc(LT(y~, 1))) + 2

+ width (~(LT(y~, 2))) + 2

+ width (~(LY(yi, 3))).

Page 13: The complexity of drawing trees nicely

The Complexity of Drawing Trees Nicely 389

Fig. I1. A placement zc of T(E), where

E=(X1 ~-X2 -[- X3) A (X1 -}-X2 ~-X4) A (X2 -~X3 q- X4)"

The diamonds are the roots of the clause trees; the squares are the roots of the three literal trees; the circles are the roots of the three variable trees, r~ corresponds to the truth assignment z which makes xl, xt, and x 4 true, and x 3 false

N o w s ince x sa t i s f ies E, a t l eas t o n e of t he t h r e e l i t e ra l s in Fi is t r u e u n d e r z. T h e r e -

fore a t leas t o n e of LT(y~, 1), LT(yc2), o r LT(y~, 3) is p l a c e d in w i d t h 6 b y re, a n d

t he o t h e r t w o a re p l a c e d in w i d t h 6 o r 7. T h e r e f o r e

w i d t h (rc(CT(Fi))) < 7 + 7 + 6 + 2 + 2 = 24 .

Page 14: The complexity of drawing trees nicely

390 K.J. Supowit and E.M. Reingold

We want to align the clause trees ofz (E) so that the width of T(E) is equal to the width of its widest clause tree. We are free to do this without violating the Aesthetics because of the length and flexibility of the zigzagging chains that connect the clause trees: their length (i.e., n + 6 nodes) guarantees that there will be no interference between clauses, while their flexibility allows for proper alignment. Thus, for all i, 1 <__i<=r, let 7z place the root of CT(Fi) so that the leftmost node in CT(F~) has x-value 0. Thus

width(rc(T(E)))= max {widthOz(CT(Fi))) } =<24. l~i<-_r

as claimed. Figure 11 illustrates r~ for a satisfiable E. We now show the converse, namely that if T(E) can be placed in width __< 24

then E is satisfiable. Assume that there exists a eumorphous placement re' of T(E) such that width(r((T(E)))<24. We will construct a truth assignment z' satisfying E. Define z' as follows: for all k, 1 _<__ k _<_ n, let

true, if there is an occurrence of VT(Xk) in T(E) such that r( places the root of VT(Xk) exactly one unit to the right of its son (for example, 7z' might place VT(Xk) as shown

72'(Xk)'~- in Fig. 7(a)).

false, otherwise.

Recall that since each occurrence of VT(Xk) is a subtree of T, Aesthetic 6 tells us that each occurrence of the root of VT(xk) will have the same distance from its son.

For convenience, if T is a tree and rc a placement of T, then we define widthe(rc(T)) ) to be the distance between the rightmost and leftmost nodes on level ( in T, as placed by ~. Now consider some LT(y). It is straightforward to show if y is false under the truth assignment z', then width 3(r((LT(y))) _-> 7. We now conclude that z' satisfies E, since otherwise there would exist an i, 1 _<i_ r such that F i is false under z'. But then, by the above remarks,

width(~'(T(E)))__>width(~'(CT(Fi)))

__> width 6 (7~'( C T (F~)))

> 7 + 2 + 7 + 2 + 7

=25, contradicting the choice of ~'.

T(E) has O (nk) levels, each containing O(1) nodes. Therefore the construction of T(E) can be performed in time polynomial in the size of E.

We summarize this result as: Theorem. Given a rooted binary tree T and a positive integer W, the problem of determining the existence of a eumorphous placement on the integral lattice of T of width at most W is NP-complete. In fact, the specific sub-problem in which W = 24 is NP-complete.

Immediate from the theorem is the following result on the complexity of near- minimum width tree-drawings:

Page 15: The complexity of drawing trees nicely

The Complexity of Drawing Trees Nicely 391

Corollary. I f P 4= NP, then there is no polynomial time algorithm A that produces eumorphous placements on the integral lattice such that for all trees T,

the width of A' s placement of T 25

< ~-~ (width of the narrowest eumorphous placement of T).

Informally, the corollary says that it is NP-hard to approximate minimum width eumorphous placements of trees to within a factor of less than about 4 per cent.

V. Conclusions

We can generalize Aesthetic 2 to state that for each node p that has a right son,

7rx(rightson (p)) - rex(p) > 6,

and for each node p that has a leftson

rex(p) - gx(leftson (p)) >6 ;

and Aesthetic 3 to state that for each two nodes p, q having the same level,

]rL,(p ) - rc~(q)] >2 6 ,

where 6 >0 is a real number giving the minimum horizontal separation. Thus, as we have phrased Aesthetics 2 and 3 in Sect. I, 6 = 1. The results of Sect. II and III are not changed by this generalization. The NP-hardness reduction in Sect. IV easily generalizes to the case in which [6] is odd. The Corollary generalizes to: For all 6 > 0 such that 1-6] is odd, P + N P implies that there is no polynomial time algorithm A for the discrete optimization problem with minimum horizontal separation 6 such that for all trees T,

width of A's placement of T < 24 [6] + 1

23 [6] + 1

We believe that a similar result also holds when [6] is even. The open problems concerning the complexity of tree drawing include: 1. If we drop the Aesthetic 6, is the discrete optimization problem still NP-

hard? Note that our reduction depends heavily on Aesthetic 6. 2. By the corollary, it is NP-hard to approximate the discrete optimization

problem to within a factor of less than z~.2 5 Is there some constant c > 0 and some polynomial time heuristic algorithm that always approximates the optimal width to within a factor of c? For the general integer programming problem, it is known that for all constants c> 0, P + NP implies that no polynomial time algorithm approximates the optimal to within a factor ofc [8]. Is this also true for the special case of integer programming that corresponds to our discrete tree-drawing optimization problem?

3. Is there an algorithm for the continuous optimization problem that is faster or easier to implement than Khachian's linear programming algorithm [3]?

4. Is there a simple set of aesthetics that more accurately captures our intuitive notion of nice tree drawings?

Page 16: The complexity of drawing trees nicely

392 K.J. Supowit and E.M. Reingold

References

1. Borosh, I., Treybig, L.B.: Bounds on Positive Integral Solutions of Linear Diophantine Equations. Proc. Amer. Math. Soc. 55, 299-304 (1976)

2. Garey, M.R., Johnson, D.S.: Computers and Intractability: A Guide to the Theory of NP- Completeness. W.H. Freeman and Co., San Francisco, 1979

3. Khachian, L.G.: A Polynomial Algorithm for Linear Programming. Doklady Akad. Nauk SSSR 244, 191-194 (1979)

4. Knott, G.D.: A Numbering System for Binary Trees. Comm. ACM 20, 113-115 (1977) 5. Knuth, D.E.: The Art of Computer Programming, Vol.l: Fundamental Algorithms. Addison-

Wesley Publishing Co., Reading, MA, 1968 6. Reingold, E.M., Nievergelt, J., Deo, N.: Combinatorial Algorithms: Theory and Practice.

Prentice-Hall: Englewood Cliffs, New Jersey, 1977 7. Reingold, E.M., Tilford, J.S.: Tidier Drawings of Trees. IEEE Trans. Software Engineerg. 7

223-228 (1981) 8. Sahni, S., Gonzalez, T.: P-complete Approximation Problems. J. ACM 23, 555-565 (1976) 9. Vaucher, J.G.: Pretty-Printing of Trees. Software-Practice and Experience 10, 553-561 (1980)

10. Wetherell, C., Shannon, A.: Tidy Drawings of Trees. IEEE Trans. Software Engineerg. 5, 514-520 (1979)

Received February 25, 1981/September 20, 1982