1 Introduction

Drawing planar graphs on a small integer grid is an active research area [17], which is motivated by various practical needs such as for VLSI circuit layout and small-screen visualization. Trees are one of the most studied graph classes in this context. While computing a minimum-area planar straight-line drawing of an arbitrary planar graph is known to be NP-complete [11], even for planar graphs with bounded pathwidth [11] and outerplanar graphs [4], the problem seems very intriguing for trees.

In this paper we examine rooted and unordered trees, i.e., one of the vertices is designated as the root and the left to right order of the children can be chosen arbitrarily. A natural way to display such a tree is to compute a (strictly) upward drawing, where each vertex is mapped to an integer grid point such that the parents have (strictly) larger y-coordinates than their children, each edge is drawn with y-monotone polylines with bends at integer grid points, and no two edges cross except possibly at their common endpoint. The width, height and area of the drawing are respectively the width, height, and area of the smallest axis-parallel rectangle that encloses the drawing. In a straight-line (strictly) upward drawing, the edges are restricted to be straight line segments.

We refer the reader to [17, Chapt. 5] or [8] for a survey on small-area drawings of trees. Here we review only the results that focus on exact minimization. In the fixed-embedding setting, there exist polynomial-time algorithms to compute minimum-area drawings for certain classes of planar graphs [4, 15], namely, those that simultaneously have bounded treewidth and bounded face-degrees. On the other hand, the problem becomes NP-hard as soon as one of the above constraints is dropped [4]. The intractability of minimum-area tree drawings has been well established under some edge length and slope restrictions, e.g., when edges must be drawn with unit length or the slopes of the edges in the drawing must belong to one of the (k/2) slopes determined by a k-grid. Note that an orthogonal grid is a 4-grid. Determining whether a tree can be drawn on a given k-grid, where \(k\in \{4,6,8\}\) with edges of unit length is an NP-complete problem [2, 3, 10]. Similar hardness results hold also for ordered trees under various aesthetic requirements [7].

Not much is known about minimum-area upward drawings of trees. Trevisan [18] showed that every complete tree (resp., Fibonacci tree) with n vertices admit a strictly-upward straight-line drawing in \(n+O(\log n\sqrt{n})\) (resp., \(1.17n ++O(\log n\sqrt{n})\)) area, and conjectured that exact minimization may be possible in polynomial time. Trevisan mentioned that the problem of minimum-area strictly-upward drawing of a complete tree is a ‘sparse problem’. Therefore, proving this NP-hard would imply \(P=NP\) by Mahaney’s theorem [12].

Interestingly, there exist polynomial-time algorithms for computing minimum-width upward drawings of trees, where the height is unbounded [5], and minimum-height drawings where the width is unbounded [1] (even for non-upward drawings [14]). Marriott and Stuckey [13] showed that minimizing the width of a strictly-upward straight-line drawing is NP-hard under the additional constraint that the x-coordinate of a parent is the average of the x-coordinates of its children. This additional requirement implies that a vertex with a single child must be placed directly above the child. Minimizing the width is known to be NP-hard even for ordered tree drawings under several other constraints [16].

We show that computing a strictly-upward drawing of a tree that resides in a given \(W\times H\)-grid is NP-hard. Formally, we consider the following problem:

  • Problem: Strictly-Upward Tree Drawing (SUTD)

  • Instance: An unordered rooted tree T, and two natural numbers W and H.

  • Question: Does T admit a strictly-upward (polyline or straight-line) drawing with width at most W and height at most H?

2 NP-Hardness

We prove the NP-hardness of SUTD by a reduction from the following problem:

  • Problem: Numerical 3-Dimensional Matching (N3DM)

  • Instance: Positive integers \(r_i,g_i,b_i\), where \(1\le i\le k\), and an integer B such that \(\sum _{i=1}^k (r_i+b_i+g_i) = k\cdot B\).

  • Question: Do there exist permutations \(\pi \) and \(\pi '\) of \(\{1,\dots ,k\}\) such that \(r_{\pi (i)}+b_i+g_{\pi '(i)} = B\) for all \(1\le i\le k\)?

N3DM is strongly NP-complete [9], and remains NP-complete even if we require all \(b_i\)’s to be odd, and the \(b_i\)’s are large and the \(g_i\)’s are huge relative to the \(r_i\)’s. (More precisely, \(3k^c \le r_i \le 4k^c\), \(k^{2c} \le b_i \le k^{2c}+k^c\), and \(k^{4c} \le g_i \le k^{4c}+k^c\), where \(c>1\) is a constant.) See the full version [6] for further details.

Idea of the Reduction: One crucial ingredient is to construct a tree whose height matches the height-bound H of the drawing; this determines the layer for all vertices on paths to the deepest leaves because in a strictly-upward drawing every edge must lead to a layer farther down. Another crucial ingredient is to add so many vertices to the tree that (other than on the topmost layer) all grid-points must have a vertex on them. The next ingredient is to add “walls” that force the given \(W\times H\)-grid to be divided into k regions that have B grid points available in each. (These walls are simply high-degree vertices, but since every grid-point must be used, nearly all neighbours of such a vertex must be in the layer below.) Finally we add gadgets that encode \(r_i,b_i,g_i\) in such a way that \(b_i\) vertices must be in the ith region defined by the walls, while \(r_i\) and \(g_i\) can freely choose into which of the regions they fall. Therefore, the division of them into the regions gives rise to a solution to N3DM.

Construction of T : Given an instance \(\mathcal {I}\) of N3DM, we construct a tree T with height \(2k+3\) as follows. We start with the spinal path \(v_1,\ldots ,v_{2k+3}\) and choose \(v_1\) to be the root of T. We add two supporting paths \(u_2, \ldots , u_{2k+3}\) and \(w_2, \ldots , w_{2k+3}\) where \(u_2\) and \(w_2\) are children of \(v_1\). We set \(H:=2k+3\); hence in any strictly-upward drawing, \(u_i,v_i,w_i\) must be on layer \(\ell _i\) for \(2\le i\le 2k+3\). (We count the layers from top to bottom, i.e., layer \(\ell _1\) is the layer with y-coordinate H that contains the root \(v_1\), and \(\ell _i\) is the layer whose y-coordinate is \(H{-}i+1\).)

Next we add the “walls” as shown in Fig. 1(a). Namely, add \(B+1\) leaves to T that are children of the root; the star graph induced by \(v_1\) and its children is called the wall of \(v_1\) with wall root \(v_1\). The wall children of \(v_1\) are these \(B\,+\,1\) added leaves, as well as child \(v_2\). Similarly we add a wall of \(v_{2j}\) for \(j\in \{1,\dots ,k+1\}\), by adding \(B+1\) leaves to T that are children of \(v_{2j}\) so that \(v_{2j}\) has \(B+2\) wall children (including \(v_{2j+1}\)).

Fig. 1.
figure 1

(a) Illustration for the spinal path, supporting paths and walls. (b) Construction of the blue vertices. (c) Construction of the red and green vertices. For space reasons, the numbers in this example do not satisfy the constraints that we imposed in N3DM. (Color figure online)

Finally, we encode \(r_i,g_i,b_i\) of the N3DM instance. For \(1\le i\le k\), we add \(b_i{-}1\) leaves to T and make them children of \(v_{2i+1}\), see Fig. 1(b). We will call \(v_{2i+1}\) a blue parent and its \(b_i\) children (including \(v_{2i+2}\)) the blue children of \(v_{2i+1}\). For each \(r_i\) (resp., \(g_i\)), we create a star with \(r_i\) (resp., \(g_i\)) leaves and connect the center to \(u_2\) (resp., \(w_2\)), see Fig. 1(c). We refer to the stars corresponding to \(r_i\) and \(g_i\) as the red and green stars, respectively. This finishes the construction of tree T. Set \(W:=B+4\) and observe that T has \(2kB+2B+8k+9 = (B+4)(2k+2) + 1 = W\times (H-1)+1\) vertices, which means that in any strictly-upward drawing in a \(W\times H\)-grid, all layers except the top one must be completely full because the top layer can contain only one vertex (the root \(v_1\)).

From N3DM to Tree Drawing: If \(\mathcal {I}\) is a yes-instance, then we create a straight-line strictly-upward drawing of T on a \(W\times H\) grid as illustrated in Fig. 2. The root \(v_1\) is anywhere in the top layer. We place the supporting paths in layers \(\ell _2,\dots ,\ell _{2k+3}\) in the leftmost (resp., rightmost) column, as forced by the strictly-upward constraints. Vertex \(v_1\) has \(B+4\) children (\(u_2,w_2\) and the \(B+2\) wall children); we place all these in the second layer. We do not yet pick which of these \(B+2\) wall children becomes \(v_2\); this will be determined later.

The solution to \(\mathcal {I}\) gives two permutations \(\pi ,\pi '\). Place the red, blue and green parents corresponding to \(r_{\pi (i)}\), \(b_i\) and \(g_{\pi '(i)}\) on layer \(\ell _{2i+1}\). More precisely, from left to right, we have first \(u_{2i}\), then the red parent, then B wall children of \(v_{2i}\), then the green parent, and then \(w_{2i}\). Later, we will choose one of these B children to be \(v_{2i+1}\), hence the blue parent corresponding to \(b_i\). Observe that layer \(\ell _{2i+1}\) is completely filled with vertices of T.

Place the red/blue/green children corresponding to \(r_{\pi (i)}\), \(b_i\) and \(g_{\pi '(i)}\) on layer \(\ell _{2i+2}\). More precisely, from left to right, we have first \(u_{2i}\), then \(r_{\pi (i)}\) red children, then one wall child of \(v_{2i}\), then \(b_i\) blue children of \(v_{2i+1}\), then another wall child of \(v_{2i}\), then \(g_{\pi '(i)}\) green children and finally \(w_{2i}\). Since \(r_{\pi (i)}+b_i+g_{\pi '(i)} = B\), layer \(\ell _{2i+2}\) is also completely filled.

Fig. 2.
figure 2

Construction of a drawing of T on a \(W\times H\) grid. (a) Placement of the vertices of T, drawing of the edges of the supporting paths, and the wall of \(v_1\). (b)–(c) Drawing of the blue stars and the remaining walls. (d) Drawing of the red and green stars. (Color figure online)

We must argue that all edges can be connected straight-line. This holds for the red stars since red children lie in the layer below their parent, and red parents lie in the column next to \(u_2\). See also Fig. 2(d). Similarly we can connect green stars. For the blue stars, because the \(b_i\)’s are much larger than the \(r_i\)’s, one can argue that the blue intervals of children of \(v_{2i-1}\) and \(v_{2i+1}\) overlap. We pick \(v_{2i}\) to be within this overlap in such a way that it can connect to the two wall children that are on layer \(\ell _{2i+2}\) without using up grid points. We use as \(v_{2i+1}\) the point directly below \(v_{2i}\); due to the choice of \(v_{2i}\) then \(v_{2i+1}\) can connect to the blue interval on layer \(\ell _{2i+2}\) without crossing. Details are in the full version [6].

From Tree Drawing to N3DM: We now show that any strictly-upward polyline drawing of T in a \(W\times H\)-grid gives rise to permutations \(\pi \) and \(\pi '\) such that \(r_{\pi (i)}+b_i+g_{\pi '(i)} = B\) holds for \(1\le i\le k\). We select \(r_{\pi (i)}\) and \(g_{\pi '(i)}\) in a bottom-up fashion, i.e., we first construct the triple \((r_{\pi (k)},b_k,g_{\pi '(k)})\), then the triple \((r_{\pi ({k-1})},b_{k-1},g_{\pi '({k-1})})\), and so on.

Since H equals the height of the tree, we know that \(v_i,u_i,w_i\) (for \(i>1\)) are on layer \(\ell _{i}\), and the wall children of \(v_{2k+2}\) are on layer \(\ell _{2k+3}\) (the bottommost layer). Hence layer \(\ell _{2k+3}\) contains these \(B+2\) wall children, as well as \(u_{2k+3}\) and \(w_{2k+3}\). By \(W=B+4\) this layer is full and contains no other vertices. Also \(v_{2k}\) lies is on layer 2k, and so all its wall children must be on layers \(\ell _{2k+1}\) and \(\ell _{2k+2}\). We need an observation that crucially requires that all grid points are used. Details are in the full version [6].

Lemma 1

Presume we know that all wall children of \(v_{2i}\) are on layers \(\ell _{2i+1}\) and \(\ell _{2i+2}\). Then at most two wall children of \(v_{2i}\) are on layer \(\ell _{2i+2}\), and the wall children of \(v_{2i}\) on layer \(\ell _{2i+1}\) occupy a consecutive set of points.

Thus there are at least B wall children of \(v_{2k}\) that form an interval on \(\ell _{2k+1}\). Also \(u_{2k+1}\) and \(w_{2k+1}\) are on layer \(\ell _{2k+1}\), leaving at most two points in this layer free to be used for red or green stars, or blue or wall children from higher up.

We argue that indeed these two points must be used for a red parent and a green parent. To see this, consider the interval \(\lambda _m\) that consists of the middle \(W{-}16\,{=}\,B{-}12\) points on layer \(\ell _{2k+2}\). T has \(O(k^{2c+1})\) vertices that are not in green stars while \(B>3k^c+k^{2c}+k^{4c}\) so there must exist a green vertex in \(\lambda _m\). It must be a green leaf \(l_g\) since layer \(\ell _{2k+3}\) is full.

We claim that if a vertex x uses a point in \(\lambda _m\), then its parent \(p_x\) is either \(v_{2k}\) or \(p_x\) is on layer \(\ell _{2k+1}\), i.e., one layer above x. To see this, assume otherwise and consider the place where edge \((x,p_x)\) traverses layer \(\ell _{2k+1}\). If \(p_x\ne v_{2k}\) then this point must be outside the interval of the \(B=W-4\) wall children of \(v_{2k}\) in this layer, else we would intersect an edge. So this point is within the leftmost or rightmost four units of \(\ell _{2k+1}\). Since \(\lambda _m\) covers all but the leftmost and rightmost 8 points of layer \(\ell _{2k+2}\), this forces \(p_x\) to be outside the allotted width of the drawing, a contradiction. See the details in the full version [6].

Consequently, the green parent \(p_g\) of \(l_g\) is on layer \(\ell _{2k+1}\) and its green children are all on \(\ell _{2k+2}\). Due to our assumptions on N3DM, these green children, plus the blue children of \(v_{2k+1}\), are not enough to fill \(\lambda _m\), but leave too little space to place another green star. Therefore some point in \(\lambda _m\) must be occupied by a red child \(l_r\), and its parent \(p_r\) hence is in layer \(\ell _{2k+1}\). Let \(r_{\pi (k)}\) and \(g_{\pi '(k)}\) be the numbers corresponding to the red and green stars at \(p_r\) and \(p_g\).

We had \(u_{2k+1},w_{2k+1}\)\(p_r\)\(p_g\), and at least B wall children in \(\ell _{2k+1}\), which by \(W=B+4\) means that there are exactly B wall children in \(\ell _{2k+1}\) and no other vertices. So two wall children are in layer \(\ell _{2k+2}\). These two, plus \(u_{2k+2}\) and \(w_{2k+2}\), leave B points for the red, blue and green children, so \(r_{\pi (k)}+b_k+g_{\pi '(k)}\le B\). On the other hand, \(\lambda _m\) cannot contain any vertex x other than two wall-children of \(v_{2k}\) and these red, blue and green children, because layer \(\ell _{2k+1}\) has no space left for the parent of x. So \(r_{\pi (k)}+b_k+g_{\pi '(k)}\ge B-14\). Since all input numbers are big enough, this implies \(r_{\pi (k)}+b_k+g_{\pi '(k)} = B\), as desired.

It also follows that \(\ell _{2k+1}\) is completely filled by these vertices, which means that no wall children of \(v_{2k-2}\) can be in layer \(\ell _{2k+1}\) or below. We can now apply the same argument iteratively to compute the upper level triples \((r_{\pi ({k-1})},b_{k-1},\) \(g_{\pi '({k-1})}), \ldots , (r_{\pi ({1})},b_{1},g_{\pi '({1})})\). This completes the NP-hardness reduction.

We can extract \(\pi \) and \(\pi '\) from any polyline drawing, while a solution to \(\mathcal {I}\) gives rise to a straight-line drawing. Therefore, the reduction holds both in polyline and straight-line drawing settings. Since SUTD is clearly in NP we hence have:

Theorem 1

Given a tree T, and two natural numbers W and H, it is NP-complete to decide whether T admits a strictly-upward (polyline or straight-line) drawing with width at most W and height at most H.

3 Directions for Future Research

Several interesting questions remain. In our reduction, we crucially used that the underlying tree has high degree, that the height of the drawing equals the height of the tree, that the order among children is not fixed, and that the drawing is strictly upward. Does SUTD remain NP-hard for bounded degree trees? What if the given width is optimal for the tree and the height needs to be minimized? How about order-preserving drawings and/or upward drawings?

The above questions are open also for many other popular styles for drawing trees. Specifically, is the problem of computing (not necessarily upward) drawings of trees on a given grid NP-hard? Are there polynomial-time algorithms that can approximate the area within a constant factor?