Computational Geometry: Theory and Applications

Slanted and curved nonograms are a new type of picture puzzles introduced by Van de Kerkhof et al. (2019). They consist of an arrangement of lines or curves within a frame B , where some of the cells need to be colored in order to obtain the solution picture. For solving the puzzle, up to two clues need to be attached as numeric labels to each line on either side of B . In this paper we study the algorithmic problem of optimizing or deciding the existence of a placement of the given clue labels to such a nonogram. We provide polynomial-time algorithms for restricted cases and prove NP -completeness in general. © 2021 The Author(s). Published by Elsevier B.V. This is an open access article under the CC BY license (http://creativecommons.org/licenses/by/4.0/).


Introduction
Nonograms Nonograms, also known as Japanese puzzles, paint-by-numbers, or griddlers, are a popular puzzle type where one is given an empty grid and a set of clues on which grid cells need to be colored, typically resulting in a picture (see Fig. 1 (left)). There is quite some work in the literature on the difficulty of solving nonograms [1,2], and this remains an active topic of discussion [3].
Curved nonograms Van de Kerkhof et al. introduced curved nonograms, a variant in which the puzzle is no longer played on a grid but on any arrangement of lines or even curves [4]. Such nonograms have been investigated in several Master theses [5][6][7]. Of special interested are slanted nonograms [6], in which all curves are straight lines, possibly limited to a fixed number k of orientations (also known as sloped nonograms or, in the case of k = 4, tangram nonograms). Fig. 1 illustrates the different variants.
Van de Kerkhof et al. describe heuristics to generate puzzles, but they leave open the question of how to attach labels with clues. It turns out this is actually a non-trivial task, as labels could be placed in several valid locations. Each curve to be labeled enters and leaves the picture frame (bold black rectangle in the figures) once, and the information about which incident cells of the arrangement should be filled is summarized in two clues, one on each side of the curve (we refer the reader to [4] for a full description of the rules). This gives two logical potential locations for each clue. Furthermore, it may be possible to extend curves outside the frame to make more room for the clues, and not necessarily all clues need to be given for the puzzle to be solvable. On the left, we show all possible labels, creating a large amount of overlap. On the right, some possible ways to resolve overlapping labels: extend labels from the same port, extend parallel labels, draw both labels of the same line at the same side, or allow crossings outside the puzzle frame. (bottom) A curved nonogram, showing a subset of labels (some extended to avoid overlap) which still results in a unique puzzle.
Related work Nonogram labeling is closely related to the boundary labeling problem in information visualization, where a set of point features in a rectangular frame B is to be annotated with labels (names or short descriptions) that are placed outside B and connected to their features with straight or polygonal leader lines; see the recent state-of-the-art report by Bekos et al. [8]. Boundary labeling algorithms typically require that the leader lines are crossing-free and optimize a quality measure such as their length or the number of bends [9][10][11][12], but also more complex criteria and image contours have been studied [13]. Bekos et al. [14] considered labeling of collinear points (such as the intersection points of curves in a nonogram with its frame) by connecting points and labels with two-bend polyline leaders. The majority of the studied boundary labeling problems are tractable and can be solved by dynamic programming algorithms.
Despite some clear similarities, nonogram labeling is different in several respects: Since the curves/lines in curved or slanted nonograms intersect the frame B in two fixed locations, the possible positions for the clues are very restricted, while in boundary labeling the label can basically be placed anywhere along B as long as the resulting leader lines are valid. Labels and leaders in boundary labeling are typically axis-aligned, but the clues in nonograms are aligned with their respective nonogram line or curve. Finally, by extending the nonogram curves with their specific slope beyond the frame to gain extra space, we obtain a new degree of freedom that has been rarely used in boundary labeling, with some exceptions of multi-row labeling [15,16].
Our results for nonogram labeling may be interpreted more generally as boundary labeling of an arrangement. This problem has, to the best of our knowledge, not been studied before.
Contributions In Section 2, we formalize the nonogram labeling problem and identify various hard and soft constraints that a good labeling should satisfy. We define several variants of the problem, depending on which constraints should be satisfied. With these definitions in place, at the end of the section, a more precise overview of our results is given. In Section 3, we show that some highly constrained variants admit polynomial-time algorithms: when labels are not to be extended at all, or when each label is pre-assigned to some side of the puzzle. For the first result, we use a 2-SAT model, for the latter dynamic programming. In contrast, in Section 4, we show that the general problem is NP-hard. In Section 5 we work towards a more practical solution by showing that the hardness proof crucially depends on sufficiently long labels: for unit-sized labels, a natural variant of the problem, we obtain a polynomial-time algorithm, again based on a 2-SAT model.
Our results provide a first understanding of the complexity of the nonogram labeling problem, but a gap still remains between tractable and hard versions. At the same time we note that for practical purposes, on the one hand, the number of (conflicting) labels may be low enough that exact exponential-time algorithms may actually be feasible, while on the other hand, approximate solutions may be acceptable.

Model
We discuss how to formalize the problem of labeling a slanted or curved nonogram. We will assume that the interior of the nonogram is fixed, and that the (sub)set of curves which need to be labeled is predetermined.
Properties of curved and slanted nonograms In principle, the interior of the nonogram is not relevant for the labeling problem: it is fully determined by a set of ports, that is, locations on the puzzle frame where a label could be attached. However, some considerations on good curve arrangements for nonogram generation [4] do give rise to different patterns on the puzzle frame. Here we briefly review some of them.
Curved nonograms should have curves which are unambiguous to trace, and faces which are sufficiently large and fat to be easily distinguished. This places a natural bound on the density. It is also reasonable to assume that adjacent ports have a certain minimum spacing, or that the angle at which the ports exit the frame have a minimum value.
Van de Kerkhof [7] further observes that nonogram curves are easier to trace when they do not wind too much (for us: the two ports of the same curve are not on the same edge of the frame), and that the rules are easier to understand when no three curves cross in a common point (for us: all ports are at distinct points), or when curves do not have selfintersections (irrelevant to the labeling problem). He notes that it is of interest to be able to generate puzzles with or without these properties, to allow for puzzles of different difficulty.
For slanted nonograms, Parment [6] observes that avoiding points in which multiple lines intersect is difficult. Furthermore, there is some evidence that an arrangement of sufficiently many lines with the property that all faces are sufficiently large must necessarily form (an affine transformation of) some grid-like pattern. In this case, the number of different slopes of ports may be limited, and the spacing between the ports may follow a particular structure: for instance, all distances may be integer, or, as in the example in Fig. 1 (middle), of the form a + bφ where a and b are integers and φ = 1 2 + 1 2 √ 5 is the golden ratio.
Problem statement In the nonogram labeling problem we are given the following input: (i) a nonogram frame, which is a simple convex polygon B; (ii) a set L of n nonogram lines passing through B, each l ∈ L defining a pair (p l , q l ) of ports at the intersection points of l with B; and (iii) a pair of non-negative integers (a l , b l ) for each l ∈ L, where a l defines the width of the label above l and b l defines the width of the label below l, and a width of 0 indicates the label is absent. 2 As output, we ask for a labeling of L, such that for each label of nonogram line l ∈ L: (i) is assigned to one of the two ports p l or q l ; (ii) is assigned an extension length δ (which could be 0); (iii) is assigned a boolean value right or left indicating whether the label should be placed on the left or right side of the line (when exiting the frame).
Given an output specification, we draw a leader of length δ from its assigned port p l or q l aligned with the slope of l, and the label itself as an a l × 1 (or b l × 1) rectangle, also aligned with the slope of l, and anchored at the end of its leader. We say that a label is right-flipped (left-flipped) if it is attached to the right (left) side of the leader from its assigned port. A labeling is valid if no two labels overlap each other, no label overlaps a leader of another label, and no label intersects the frame. In addition to being valid, we identify three further desired properties (refer to Fig. 2).
• Crossing-free. We disallow intersections between leaders. • Balanced. We require the two labels of a line l ∈ L to be assigned to opposite ports. • Compact. We require all leaders to be of length 0, or the shortest length necessary to avoid an intersection between the label and the frame.
Ideally, we would like to find a crossing-free, compact, and balanced labeling. However, this may not exist (see, e.g., Fig. 1 (middle)). We study the computational problems of testing for the existence of a solution, and minimizing the total leader length among all possible solutions, for several combinations of these properties. Additionally, we investigate whether the maximum length c of the labels to be placed influences the problem, or, for slanted nonograms, whether the number k of distinct slopes of lines in L does.
Clearance In real nonogram puzzles, we do not want to only avoid labels from overlapping, but we would like to have a bit of space between them. We can model this by simply enlarging each label and the nonogram frame by a small amount Fig. 3. A labeled instance in which all labels and the nonogram frame observe a minimum amount of clearance δ to each other is equivalent to another instance in which the labels and the frame are dilated by an amount of δ/2.  (refer to Fig. 3). Therefore, we ignore the issue of clearance in the remainder of our discussion. Similarly, we would also like a minimum amount of clearance between leaders. We assume that this follows from the locations of the ports, which are, after all, determined by the interior of the nonogram.
Initial observations First, we observe that a balanced solution, which is not crossing-free and not compact always exists: we simply extend the leaders sufficiently far. Observation 1. Given a nonogram labeling instance, a non-compact potentially crossing labeling always exists and can be computed in linear time. This is true regardless of whether we require it to be balanced.
Since this does not give a satisfactory result in practice, we focus on more restricted variants in the remainder. For example, in the special case where the puzzle frame is a rectangle, and all ports have 45 • angles with respect to the frame, there are several ways in which two labels can be intersecting when we place them as close to the frame boundary as possible (see Fig. 4). Depending on the configuration, such a conflict can be resolved by extending one or both of the leaders.
In the setting where we require a balanced solution, it might also be possible to resolve conflicts by flipping one of the labels (see Fig. 5). Note that this only makes sense if we also flip the label on the opposite side of the line. Hence, this operations creates dependencies between different sides of the puzzle.

Results
In Section 3.1, we show that testing whether a compact solution exists for any curved or slanted nonogram is possible in polynomial time. In Section 3.2, we show that for slanted nonograms, a non-crossing balanced solution of minimal total leader length can be computed in polynomial time, if the assignment of labels to ports is given and k = 2 for slope ±1. In Section 4, we show that the problem of testing whether a crossing-free solution exists is NP-complete, even for slanted nonograms when k = 2 for slopes ±1. Interestingly, the hardness proof works even when all labels are short, that is, have sizes of at most aspect ratio 1 : 4. This motivates the study of the problem for small labels, and we show in Section 5 that for square labels on a unit grid, the problem can be solved with dynamic programming even without assigning the sides in advance. (See Table 1.)

Algorithms
We present two polynomial-time algorithms for special cases of nonogram labeling. Section 3.1 considers compact labelings and Section 3.2 assumes that each label is assigned to a fixed port in the input and the extension length of each leader must be computed.

Compact labeling
In our first result, we assume that each label must be placed as close to the frame B as possible, i.e., must touch B. This leaves only one degree of freedom for each label of a nonogram line l, namely whether it is placed at port p l or q l . Similarly to more general point labeling problems with two alternative positions per label [17], we define a simple 2-SAT model to determine a feasible nonogram labeling or report that none exists.
indicates that the label below l is assigned to p l (q l ). It is clear that a variable assignment is in bijection to a port assignment of the labels and it remains to add some clauses to ϕ to model the valid labelings. For each overlap of a label of a line l with a label of another line l (we call that a conflict), we add a clause that prevents both labels to be selected simultaneously. As an example consider the case that the label above l and the label below l intersect if both assigned to their ports p l and p l . Then we add the clause ¬x a l ∨ ¬x b l . Now a satisfying assignment for ϕ corresponds to an assignment of each label to a port of its nonogram line such that no two labels intersect each other; otherwise some clause would not be satisfied.
To ensure that the labeling is balanced, we would add the additional clauses x a l ∨ x b l and ¬x a l ∨ ¬x b l for each l ∈ L.
Solving the 2-SAT instance takes linear time [18] in the size of ϕ, where the number of clauses of ϕ is linear in the number of nonogram lines and the number of label conflicts, which is O (n 2 ).
We remark that this 2-SAT model is independent of the type and shape of the curves and the shape of B. It depends only on the set of conflicting candidate label positions.
A compact labeling does not always exist. If one does insist on a compact and crossing-free labeling, one option is to shrink the label sizes. As an easy corollary of the above result, we show that the largest scale for which a compact labeling exists can also be obtained in polynomial time.

Corollary 1. Given a nonogram labeling instance, we can compute in O (n 2 log n) time the largest label size such that a compact labeling
exists. This is true regardless of whether we require it to be balanced.

Proof.
We create a 2-SAT instance as above. We distinguish two types of clauses: hard clauses which must be obeyed regardless of the label size, and soft clauses which only need to be obeyed for certain label sizes. The hard clauses include those which describe that each clue is placed exactly once, and, in case of a balanced solution, those that enforce that the labeling is balanced. The soft clauses occur between each pair of label placements which could potentially overlap, if the labels were sufficiently large. The key observation is that, for each such pair, there is a unique scale s such that for all scales smaller than s the labels are disjoint, and for all scales larger than s the labels are intersecting. Hence, we annotate each soft clause with this value s. The task is now to find the largest value of s such that all hard clauses and all soft clauses with annotated value smaller than s together admit a solution. We perform binary search on the value of s and apply Theorem 1 for each scale; this yields the O (n 2 log n) time bound.

Fixed side assignment
Our second algorithm allows extensible leaders, but disallows leader intersections and assumes that a balanced assignment of the labels to the ports of each nonogram line is given in the input. We further assume that the nonogram lines are all of slopes ±1. Lemma 1. For a nonogram labeling instance I with n lines and a balanced fixed side assignment for each label we can discretize the relevant extension lengths of each label to O (n 2 ) values such that we can find a labeling of minimum total extension length among  Proof. The first observation is that in a minimum-length labeling, every label of a nonogram line l is shifted as close to the frame B as possible without intersecting another label as otherwise a solution with shorter total length would exist. This means that either (i) touches B and the extension length is 0 or 1 (depending on which side of l is labeled and the slope of l), or (ii) touches another label blocking from moving closer to B. Observe that these are the only two possibilities; in particular, it is not possible that is blocked by the leader of another label when there are no leader intersections. We show that the number of extension lengths of type (ii) for a single label is bounded by O (n 2 ).
We assume that l crosses the top edge of the frame B in a port p l to which label is assigned. The situation for ports on the other edges of B is symmetric. Let us first consider the case that both and its blocking label are assigned to a port on the top edge of B. Without loss of generality let the slope of l be +1 and distinguish two cases for the blocking label .
1. In the first case, belongs to a line of different slope, meaning that the extension length of is determined by the intersection point of the two nonogram lines (possibly +1 depending on the side assignment of ), see Fig. 6(a). This case can occur only if is flipped to the right of l. There are O (n) such intersection points for .
2. In the second case, the lines of and are parallel and of distance at most 2 (otherwise cannot block ). Such blocking relations can form chains, where itself is blocked again by a third label and so on.
• If the chain of blocking relations comprises only parallel lines and all of them have the labels on the same side Finally, we consider the situation of a port p l near the top-right corner of the frame B. For the top-left corner the arguments are symmetric. So let be a label assigned to a port on the top edge of B that is blocked by a label of the right edge of B as shown in Fig. 7. 1. If the slope of l is +1 and is right-flipped there are three subcases.
• We can have a chain of right-flipped labels of parallel lines that starts on the top edge and continues on the right edge as shown in Fig. 7(a). As before, there is a single feasible shortest extension length for . This case can in fact also be triggered from a label on the bottom edge that pushes the first label of the chain on the right edge into its position (combine Fig. 7(a) and Fig. 7(d), rotated clockwise by 90 • ). • If belongs to a line of slope −1 then there is a single possible extension length of as shown in Fig. 7(c). 2. If the slope of l is −1 and is right-flipped, it can be blocked by a left-flipped label of a line with slope +1 as shown in Fig. 7(d). This yields a single possible extension length.
3. Finally, if the slope of l is +1 or −1 and is left-flipped, then no label assigned to the right edge can block .
The above arguments imply that the number of relevant positions for a label to consider is O (n 2 ) in the worst case. Based on the discretization of the relevant extension lengths of Lemma 1 we can now describe a dynamic programming algorithm to solve a nonogram labeling sub-instance defined on one of the four edges of the frame B. More precisely, we also specify the extension length of the first and last label of the instance so that we can compose compatible sub-solutions for all four edges of B into a globally optimal solution. Lemma 2. Given a nonogram labeling instance I in a rectangular frame B, a specified edge e of B crossed by n lines, a fixed side assignment for each label, and a fixed extension length for the labels of the two outermost lines crossing e, we can decide in O (n 9 ) time, whether an assignment of an extension length to each label exists such that the resulting labeling is valid and crossing-free. If this is the case we can find one of minimum total extension length.
Proof. Assume without loss of generality that e is the top edge of B and that the lines are indexed l 1 , . . . , l n in the leftto-right order of their ports p 1 , . . . , p n on e. For the two outermost lines l 1 and l n fixed extension lengths δ 1 and δ n are specified in the input. These two lines play a special role and we will mark any sub-instance invalid, in which they would be assigned any other extension length. This guarantees that we consider only solutions that respect the specified extension lengths of l 1 and l n .
To initialize the instance we further introduce two dummy lines l 0 and l n+1 , where l 0 has slope −1 and has its dummy port one unit left of the top-left corner of B and l n+1 has slope +1 and its port is one unit to the right of the top-right corner of B. Therefore, l 0 and l n+1 cannot intersect any line or label of I. We assign an extension length δ max to l 0 and l n+1 that exceeds the maximum of any of the discretized extension lengths of the lines on e as defined in Lemma 1. Recall further that by Lemma 1 we know that it is sufficient to consider at most O (n 2 ) many extension lengths for each label. For a line l i let i be the set of its relevant extension lengths. Note that for the outermost lines l 1 and l n we set 1 = {δ 1 } and n = {δ n }. We recursively define a sub-instance of I by two boundary lines l i and l j (i < j) and a discretized extension length a and b for each of them. Since there are n lines and each of them has at most O (n 2 ) many relevant extension lengths, this defines O (n 6 ) different sub-instances. In such a sub-instance, any line l k with a port p k between p i and p j is restricted to stay within the region bounded by l i , l j , and a horizontal line through the topmost point of the shorter of the two lines l i , l j , see Fig. 8. This means that the extension length of l k must be such that it does not extend beyond the trapezoidal region defined by the sub-instance. Let T [i, j, a, b] be the minimum total extension length of the instance defined by l i of length a and l j of length b, if a valid and crossing-free solution exists; otherwise the instance is invalid and To solve such a sub-instance recursively, we use dynamic programming and optimize over all lines l k (i < k < j) contained in the sub-instance and all admissible and intersection-free extension lengths c for l k and compose the solution from the stored solutions of the two sub-instances defined by l i and l k as well as l k and l j . The recursion stops for i = j − 1, where the extension length T [i, j, a, b] = a + b. Accordingly, for j − i > 1, we can recursively define The optimization of one entry of table T considers O (n 3 ) combinations of a line l k and an extension length c ∈ k and must verify that l k with the assigned extension length c and its label k is valid, i.e., it does not intersect neither l i , l j nor their labels. This intersection test takes constant time.
We obtain the minimum-length solution of I from cell T [0, n + 1, δ max , δ max ], which is computed by the above dynamic programming algorithm in O (n 9 ) time. If its value is ∞, then no valid, crossing-free solution exists.
It remains to construct a valid, crossing-free, and minimum-length solution for the entire nonogram by composing it from compatible sub-solutions for the four sides of the frame B. We remark that in many real-world nonograms the number of relevant extension lengths per line is expected to be much less than O (n 2 ), e.g., since parallel lines will have a certain minimum distance. Moreover, we can have dependencies between different edges of the frame B only if the ports of their outermost lines are very close to the corners of B. Otherwise the sub-instance induced by each edge can be solved independently and be combined without further constraints. Combined with the fact that, in practice, n is not expected to be extremely large, we believe the algorithm may be of practical value despite the high degree of the polynomial.

Hardness
We will show in Section 4.1 that the problem of testing whether a valid labeling exists, in the setting where we are allowed to choose at which port each label is placed and are allowed to extend the leaders to any desired length, is NPhard. The proof uses only straight lines of slopes +1 and −1 in the interior of the puzzle, and hence applies already to slanted nonograms with two distinct slopes. It uses lines which have only a single label, but it can easily be extended to the case where we have two labels per line: we simply always place them both at the same port (some distances need to be slightly adjusted). Notice that this does not translate to the balanced setting. The proof also relies on some labels having arbitrary length; we show in Section 4.2 how to adapt the proof so that it uses only labels (and leaders) of constant length.

Unbounded label size
We will construct an instance with a rectangular frame which only has lines of slopes +1 and −1. We will reduce from 3-SAT: we are given an instance consisting of a set of n variables x 1 , . . . , x n and m clauses c 1 , . . . , c m , and we will construct a nonogram labeling instance which can be labeled without crossings if and only if the 3-SAT instance is satisfiable.
Variables The bulk of the construction consists of cross gadgets. A cross gadget consists of two short labels intersecting each other at 90 • angles. Fig. 9 shows a single cross gadget.
Note that for a cross gadget it is not relevant on which side of the lines the labels are, and that although labels can be extended, doing so does not change the combinatorial choices of which combinations of sides are possible.
A cross gadget has three possible valid states, and we wish to use them to represent variables, which have two valid states. Furthermore, we would like to enforce multiple cross gadgets to represent the same variable. We can achieve both of these properties by connecting several cross gadgets into a variable loop. Fig. 10 illustrates a variable loop. By connecting the cross gadgets into a loop, we ensure that only two valid solutions remain for each cross gadget. Note that we can increase the number of occurrences of a cross of the same variable by making the frame wider, and we can increase the distance between consecutive crosses by making the frame higher. We can embed multiple independent loops next to each other, as illustrated in Fig. 11.
In the construction, we will also need to place some labels which cannot be removed. We achieve this by creating a special variable loop, for which one of the states is disabled by creating a crossing between a positive and a negative label. We achieve this by making them longer (see Fig. 12). Note that forced (black) labels are only forced to be on a particular side of B; they can still be extended, but we will use them in a way where extending black labels is never useful.    Clauses Next, we construct clause gadget. A clause gadget essentially consists of a single clause label which is forced to be at a specific port, but we can choose how far we extend it. Depending on how far it is extended, it will intersect different variable labels. The clause label is restricted to only three essentially different positions by two fixed labels. Fig. 13 illustrates a clause gadget and its valid solutions.
Note that for the clause label, it is important on which side of the line the label is placed: is must be faced towards the variable labels. The clause label is also the only label in the construction for which we use the possibility to extend its leader. We need to connect the clause gadget to the correct literals of the three variables involved in the clause, as well as to the fixed loop on two sides. For this, we need to make some very long labels. Fig. 14 illustrates how a clause gadget is connected, showing only the relevant labels. Note that the variables need to be connected to two different groups of cross gadgets in the variable loops, depending on whether or not they occur negated in the clause.
The global picture Globally, we embed the different clauses horizontally next to each other. Each clause, including its connections, covers a horizontal distance of a constant number of variable zig-zags. These connections are placed between the Fig. 13. Clause gadget and three possible valid solutions. The black labels are forced. The blue label is the clause label, which must be placed at the top of the construction but can be extended by 0, 1, or 2 units. The orange, red, and purple labels are three different variables which will connect to the variable loops (refer to Fig. 14). (For interpretation of the colors in the figure(s), the reader is referred to the web version of this article.)

Fig. 14.
Connecting a clause gadget to the correct literals. The picture corresponds to the clause ¬x 1 ∨ x 2 ∨ ¬x 3 . In particular, note that the groups of variable gadgets on the frame are alternating in which direction corresponds to true and which to false, and note that the red label for x 2 is connected to a different group than the purple and orange labels for ¬x 1 and ¬x 3 . variable loops, so they do not interfere. Fig. 15 shows how the first clause and the beginning of the second clause could look globally, without hiding any labels. Now, if the 3-SAT formula is satisfiable, then we put the variables in the corresponding states, and then each clause admits a non-crossing labeling. If the 3-SAT formula is not satisfiable, then no matter how we arrange the variable labels, there must be at least one clause gadget where all three positions for the clause label are blocked; hence, there is no non-crossing labeling.
The construction is polynomial in n and m. The total number of nonogram lines we place is O (nm). The total horizontal distance covered will be O (nm), and the vertical distance is O (n), so all locations can be represented with polynomial coordinates.
We conclude: Theorem 3. Given a nonogram instance without side assignment and extensible leaders, it is NP-complete to decide whether a valid labeling exists.

Bounded label size
The construction above uses mostly only labels of constant size; however, we need a few long labels: they need to be of length linear in the number of variables in order to "jump over" sufficiently many variable gadgets. We next extend the proof to label sizes of 1 by c; first we treat c as an abstract constant and then we argue that the problem remains hard   even for c = 4. The main idea is to replace each pair of long labels by a sufficiently large horizontal section, in which we make several shorter jumps instead. As a result, the total width of the construction will increase by a factor of (n).
Long forced labels The first place where we need long labels is in the special variable loop which has a forced state. We need two labels of one of the potential states to intersect, in order to make it infeasible. Instead of doing this directly, we instead create a fixing gadget: a longer path between variable gadgets, jumping back and forth (n) times. Fig. 16 illustrates the idea.
We reserve a section of length (n 2 ) at the left end of the global construction to embed the fixing gadget, without interfering with the rest of the construction. Note that, depending on the exact length of the labels, we can freely choose any (constant) horizontal displacement, and it will be possible to "interweave" the fixing gadget with the other variable loops.
Long labels to connect to clauses The second place where we need long labels is in the connection of the clause gadget to the correct variable loops. Again, we replace each such connection by a longer connection gadget, which has, for each literal in the clause, a path which jumps between the top and bottom as often as necessary ( (i) times for literal x i , and (n + i) times for literal ¬x i ), and we reserve a sufficiently long horizontal distance between consecutive clause gadgets to make this possible. Since each clause consists of three literals, at the start of each connection gadget three of these paths need to be embedded simultaneously, therefore requiring slightly wider gaps between consecutive variable loops. Fig. 17 illustrates the idea.

Required label sizes
In order to make the adaptations above, we require labels to be "nested" by one level: a sequence of labels abcd such that a crosses d and b crosses c, but there are no crossings between a or d and b or c. This can be created using 1 × 1 labels for b and c, and 1 × 3 labels for a and d; refer to Fig. 18 (left). However, we need larger labels than this for the clause gadget. The clause gadget requires labels of three different sizes for the literals in the clause: we may use 1 × 1, 1 × 2 and 1 × 3. Then, the intersecting clause label with variable leader length can also be a 1 × 1 label. Finally, we need a forced label that blocks the variable leader label, which must be disjoint from the three literal labels but does not need to extend beyond them; therefore, this is possible with a 1 × 4 label. Fig. 18 (right) shows a precise construction. 3 Theorem 4. Given a nonogram instance without side assignment and extensible leaders, it is NP-complete to decide whether a valid labeling using labels of sizes up to 1 × 4 exists.

Unit-sized labels
In this section we consider labels of bounded size. By Theorem 4 we know that for labels of size at most 1 × 4 the problem is NP-complete. In the following we prove that a special case for labels of size 1 × 1 can be resolved in time O (n 3 ).
For this special case we assume that the set of lines L forms a regular grid, i.e., ports can only appear at equally spaced intervals. Note that this does not mean that the coordinates of the ports are rational, as the unit of the grid could be irrational, e.g., square-root two. As discussed in Section 2 this is not as big of a restriction as it might seem at first glance.
To then make sure that the labels do not intersect on their boundaries we shrink them by an epsilon-amount compared to the unit of the grid. Moreover, we assume that all lines in L have slope ±1, that the frame B is a rectangle, that the labeling we search is balanced, and every line l ∈ L has two labels attached to it, i.e., in a valid nonogram labeling every port has a label placed at it. Now, the only remaining degree of freedom in this setting is to choose for each label on which side of the frame it is placed. As the labels are all 1 × 1 boxes though and since every line has two labels and we require the labeling to be balanced, this can be thought of as choosing if at a port the label is right-or left-flipped. Note that, if labels of size two or three were allowed, this would not be the case and we would need to consider which of the two labels of a line is placed on which port of their line.
In the following we will call a port p full if there exist two lines l, m ∈ L with slopes +1 and −1, respectively, that intersect in p. Let p l and p m be ports on edge e of B at distance d ∈ N (this is well defined as we assume the underlying grid to be regular), then there exist at most d − 1 other ports p l+1 . . . p m−1 = p m+d−1 on e that lie in between p l and p m if d ≥ 2 and no other port if d = 1. To ease notation we sometimes also speak of the lines l and m being at distance d if the considered edge is clear from the context. For an edge e of B we call a mapping σ from the labels on ports along e to values in {left-flipped, right-flipped} a flip-assignment. Note that this is well defined since we only deal with unit-size labels and every line has exactly two labels.
The following definitions are described only for the top-edge of B; for the other edges they can be defined analogously.
Two lines l 1 , l 2 ∈ L with corresponding ports p l 1 and p l 2 are called opposing if p l 1 , and hence l 1 , is left of p l 2 and l 1 has slope +1 and l 2 has slope −1.

Definition 1.
A flip-assignment σ is called extendable, if we find that for every two labels 1 , 2 on opposing lines l 1 , l 2 ∈ L at distance d ∈ N with 1 right-flipped and 2 left-flipped it holds that d = 1 and ⎧ ⎪ ⎨ ⎪ ⎩ if d = 2 ⇒ no third label is assigned to a port between p l 1 and p l 2 , if d = 3 ⇒ there are at most two labels at the ports between p l 1 and p l 2 , if d ≥ 4 ⇒ not all ports between p l 1 and p l 2 are full.
Definition 1 provides a set of conditions that must hold for a flip-assignment to be extendable without providing a way to compute the extension lengths. The definition of extendable does this by listing all forbidden patterns for two labels at  Fig. 20. For a straight line of slope ±1 that crosses two opposing edges of B, it is possible for both labels to be flipped away from the frame, but for a line that crosses two adjacent edges, one of the labels must be flipped towards the frame. opposing lines where the one on the line with slope +1 is right-flipped and the one on the line with slope −1 is leftflipped. As we will see in the two subsequent lemmas (Lemma 3 and 4) this is sufficient, since only in the presence of one of these forbidden configurations the labeling might not be possible.
Finally, let p be a full port and 1 and 2 labels on lines l 1 and l 2 intersecting in p, such that l 1 has slope +1 and l 2 slope −1, then we say 1 and 2 form a two-block if 1 is left-flipped and 2 is right-flipped. Two such labels can then be drawn in two ways such that the sum of the lengths of the extensions of l 1 and l 2 is one; see Fig. 19. If l 1 is extended we call the two-block right-flipped and if this is true for l 2 we denote it as left-flipped.

Remark 1.
Intuitively, placing labels that are flipped to point away from a frame B are easier to place. Especially, if all labels could be flipped away from B we only need series of two-blocks to find a valid labeling. If B consisted only of two parallel lines this would be possible as all labels could be flipped such that they point away from the frame (see Fig. 20 on the left). In case B consists not just of two parallel edges, e.g., if B has edges orthogonal to each other, finding such a trivial labeling is not possible as for one line crossing two orthogonal edges and its two labels one label has to be flipped towards the frame (see Fig. 20 on the right). This makes the problem non-trivial.
We begin by considering nonogram instances with given flip-assignment per edge. For simplicity, our descriptions are written for the top-edge of B, but the statements hold analogously for every edge. Proof. Straight-forwardly, if for no line l ∈ L with slope +1 its label is right-flipped and for no line l ∈ L with slope −1 its label is left-flipped, the labels can be placed as a series of non-intersecting two-blocks. Now, let the labels 1 , . . . , k at lines l 1 , . . . , l k with slope +1 be right-flipped and assume that 1 , . . . , k are all such labels. Observe that, regardless of how the corresponding lines are extended, no two distinct labels i , j intersect. This means we only need to guarantee that none of the labels / ∈ { 1 , . . . , k } and their lines intersect any label. This can be done by simply placing all such labels either with no extension, or, if the corresponding block is full, as a right-flipped two-block. Fig. 21 illustrates this. The labels 1 , . . . , k can then always be resolved by extending their lines l 1 , . . . , l k by one if there is no port one unit to the right, by two if there is one label at the port one unit to the right, and by three if that port is full. To compute such a labeling we can simply traverse the ports from right to left in O (n) time. We can argue symmetrically for left-flipped labels at lines with slope −1 and no right-flipped labels at lines with slope +1, by exchanging right-and left-flipped as well as letting the algorithm go from left to right.
Finally, if there are labels 1 and 2 at lines l 1 , l 2 ∈ L with slopes +1 and −1 respectively, such that 1 is right-flipped and 2 is left flipped, then we know that l 1 and l 2 are not opposing by assumption. Hence, the port corresponding to l 2 is never right of l 1 's port. Consequently, we can split the instance at the rightmost such label 1 and line l 1 when traversing the ports along the boundary from left to right.  Fig. 23. The cases of two opposing lines at distance two and how they interact with a full port in-between.

Fig. 24.
The cases of two opposing lines at distance three and how they interact with four, three, and two labels in between.
Next we show that for one edge and a given flip-assignment a valid nonogram labeling with the desired constraints exists if and only if the flip-assignment is extendable. Proof. Let L denote the set of lines given with the nonogram labeling instance. We begin by assuming that there exists a valid nonogram labeling in which the labels are flipped according to σ , yet σ is not extendable. By Definition 1 not being extendable requires that there are at least two labels, say 1 and 2 , at opposing lines l 1 , l 2 ∈ L such that the corresponding labels 1 and 2 are right-flipped and left-flipped respectively. Note, that l 1 and l 2 are extended by at least one unit in any valid labeling of this instance. Let d ∈ N be the distance between l 1 and l 2 and p 1 and p 2 the two ports of l 1 and l 2 . Recall that then there are at most d − 1 other ports between p 1 and p 2 . We proceed via case analysis over d.
As noted, l 1 and l 2 are extended by at least one unit, yet there is only one unit of space between the corresponding ports and 1 and 2 are of size 1 × 1. Hence, they have to intersect, see also Fig. 22a for an illustration. d = 2 Since σ is not extendable we find that there is a non-empty port between p 1 and p 2 , say p . Let l ∈ L be the line corresponding to p and its label at p . Without loss of generality assume l has slope +1. By the previous case we know that must be left-flipped. For and 1 to not intersect we extend l 1 by another unit to two units. Note that now l 2 cannot be extended more than one unit without 1 and 2 intersecting. Though, if l 2 is extended only one unit we get that 2 is intersecting . Since we must extend l 2 this is inevitable. See Fig. 23 for an illustration.
Let p 1 and p 2 be the two ports in between p 1 and p 2 , which exist by assumption. Moreover, again since σ is not extendable we find that there are more than two labels at the ports p 1 and p 2 , consequently, at least one of them is full. Without loss of generality let p 1 be the full port. Clearly, in any valid labeling we have to place its two labels in a right-flipped two-block, see also Fig. 24a. Now we can argue as in the case of d = 2 that, w.l.o.g. l 1 has to be extended three units. As a consequence 2 intersects either the label at p 2 , a label in the two-block at p 1 , or 2 .
By Definition 1 we get that all ports between p 1 and p 2 are full. Without loss of generality we can assume that all the labels at these ports are arranged as two-blocks and that they all are right-flipped. Clearly, we can extend l 1 by three units such that 1 is above the first two-block. Though for l 2 this is not possible as the two-block directly left of it would need to be left-flipped. But this is impossible as the left-flipped two-block will intersect the previous right-flipped two-block. Fig. 25a illustrates this case.
The above case distinction leads to a contradiction. Consequently, σ has to be extendable.
For the other direction of the proof, assume that σ is extendable. We claim that then there always exists a valid nonogram labeling and that the labels are flipped as prescribed by σ . By Lemma 3 we know that the case of no two opposing lines having their labels right-and left-flipped can be dealt with. Now, consider two opposing lines l 1 , l 2 ∈ L, with labels 1 and 2 at ports p 1 and p 2 . Moreover, assume that 1 is rightflipped and 2 left-flipped. Since σ is extendable we know that l 1 and l 2 have distance d > 1. Denote with q 1 , . . . , q d−1 the at most d − 1 ports between p 1 and p 2 . If d = 2 or d = 3 we can easily find extension lengths for the leaders and additionally no label at a port left of p 1 or right of p 2 can intersect a label at a port q i , i ∈ {1, 2}. See Fig. 24b for an illustration of case d = 3.
Let us assume d ≥ 4 and that for no q i we find a line intersecting the edge of B at q i such that it is opposing to l 1 or l 2 and its label is left-flipped or right-flipped, respectively. Let q i , i ∈ {1, . . . , d − 1}, be a non-full port. Using Lemma 3 we know that all the labels at q 1 , . . . , q i−1 can be drawn as right-flipped two-blocks and all labels at ports q i+1 , . . . , q d−1 as left-flipped two-blocks. Extending l 1 and l 2 the necessary two or three units yields the labeling. Fig. 25b illustrates this.
Next, assume that there is at least one port q i with a line m i intersecting the edge in q i and such that m i has slope +1 and its label is right-flipped or it has slope −1 and its label is left-flipped. Let q i be such a port with line m i at slope +1 and its label right-flipped or −1 and its label left-flipped. Without loss of generality assume that m i has slope +1.
We assume that between p 1 and q i there are no more such lines. Using the above discussion as an induction basis, we can assume by induction that all labels attached to lines intersecting ports between q i and p 2 are already drawn. Then, by Lemma 3 we can draw all the labels attached to lines intersecting the edge in ports q 1 , . . . , q i . Moreover, none of these labels or their leaders can intersect a label at a line intersecting the edge in port q h that lies between q i and p 2 , as we never extend a leader more than three units. Hence, we can also determine the extensions of the lines l 1 and l 2 to be one, two, or three units.
It remains to prove our main theorem. On a high level we are going to construct a 2-SAT formula with one variable x l per line l ∈ L. This formula models the conditions of a flip-assignment being extendable. From a satisfying truth-assignment we can then read off how to flip the labels. Using Lemma 4 we then know a valid nonogram labeling exists and we compute it recursively by considering sections of labels between opposing lines where the label at the left line is right-flipped and the label on the right line is left-flipped. Proof. We construct a 2-SAT formula as follows. For every line l ∈ L we define one variable x l . To treat the other edges of B correctly we consider them all rotated such that they are parallel to the top edge of B and such that if we traverse the ports from left to right the nonogram interior would be on the right-hand side. Consequently, the slope of lines relative to the left and right edge flip their sign, i.e., a line with slope +1 has slope −1 when we consider the left or right edge. We describe the creation of the formula ϕ for one edge e of B. Note that in the end we take the conjunction of the formulas created for each edge and in this way guarantee that the obtained labeling is consistent around the whole frame. For every pair of opposing lines l, m ∈ L with l = m and both intersecting e, let d be the distance between the ports p l and p m of l and m along e. We introduce a clause x l ∨ x m to the 2-SAT formula whenever one of the following holds: (i) d = 1, (ii) d = 2 and there is a third port between p l and p m , (iii) d = 3 and there are two ports between p l and p m such that more than two lines pass through these, or (iv) d ≥ 4 and all ports between p l and p m are full.
We interpret the truth-values of an assignment of ϕ as follows. If for a label at a line l ∈ L with slope +1 we find x l set to true in the assignment we flip to the left and to the right otherwise. If l was at slope −1 and x l set to true we would flip to the right and to the left otherwise. In this manner, we can immediately conclude that whenever a clause evaluates to false in an assignment of the formula ϕ we violate Definition 1 as we then constructed two opposing lines with labels not fulfilling one of its four conditions. Now, if the assignment is satisfying no clause evaluates to false and for every clause evaluating to true we find that the conditions of Definition 1 are in fact met, i.e., there are no two opposing lines with labels fulfilling any of its four conditions. Consequently, we obtain the following. Let ϕ be the formula constructed in this manner from a nonogram labeling instance with the above conditions. If ϕ is satisfiable, then the induced flip-assignment is extendable for each edge and hence with Lemma 4 there exists a valid nonogram labeling with the desired conditions.
Next we argue that if there exists an extendable flip-assignment σ for each edge of B, then ϕ is satisfiable. We begin with the top edge of B. Let be a label on a line l ∈ L that intersects the top edge of B. We set the truth value of the corresponding variable x l to false if has slope +1 and its label is right-flipped or it has slope −1 and its label is left-flipped and to true otherwise. We do the same for all other edges. Note that this can never lead to a contradictory truth-assignment as the labeling has to be balanced. Now assume that this derived assignment is not satisfying ϕ. This means there exists a clause which evaluates to false. Let 1 and 2 be two labels of lines l 1 and l 2 in L such that their corresponding variables form this clause x l 1 ∨ x l 2 . By construction, this clause was only introduced if one of the conditions in Definition 1 was fulfilled. Both literals being false though implies that the labels are such that one of these conditions is violated, a contradiction with Lemma 4 as the resulting flip-assignments are not extendable. Hence, the assignment is satisfying ϕ. It remains to argue the running time. Computing the side assignment σ is done by solving the 2-SAT formula which is clearly in size O (n 2 ). Hence solving it takes also time O (n 2 ) [18]. The extension lengths can then be computed in linear time if the conditions of Lemma 3 are also fulfilled or by using one of the constant number of possibilities for ports at distance two or three. In every other case we recursively compute the solution of the labels in between pairs of opposing lines with their labels being right-flipped and left-flipped. Recall that since we do not need any extension lengths of more than three units when applying Lemma 3, the extension lengths for the two surrounding opposing lines can be easily computed and are independent from the remaining lines and labels. Doing so in the fashion of Lemma 4 yields a running time of O (n 3 ) as there are at most O (n 2 ) such pairs. Hence the whole algorithm runs in time O (n 3 ).
Remark 2. As in Section 3.2 the labels could intersect around corners. For the here considered 1 × 1 unit size this is not a problem as the spacing of the labels is of the same size. For longer label lengths a similar argumentation as in Section 3.2 would be required.

Conclusions
In this paper we initiated the study of labeling nonograms. While the arguably most natural version of the problem proved to be NP-complete already for constant sized labels, we identified several restricted cases which can be solved in polynomial time.
Even though the versions we solve apply only in restricted settings and the polynomials are of rather high degree, we do believe our algorithms may be of practical value. The reason for this is three-fold: 1. in nonogram puzzles n is typically not very large, since otherwise it would not be fun as a puzzle anyway; 2. the high dependence on n is to a large degree based on worst-case configurations which arguably are not likely to occur; 3. some of our restrictions, such as having slopes of 45 • and uniform spacing, may in fact be met by real puzzles because of aesthetical considerations.
We remark that the labeling problem is independent of the interior of a curved nonogram, and depends only on the set of ports. Hence, our results apply in principle to both curved and slanted nonograms. We do note that some restrictions such as slopes ±1 or equally spaced ports are more likely to arise for slanted nonograms.
Future work Several interesting questions in nonogram labeling remain open. When aiming for a compact labeling it is an interesting open question if the number of lines whose labels are placed at opposite ports can be maximized in an efficient manner. Further, we considered here the objective of minimizing the total leader length. For practical puzzle design, however, it may also be interesting to minimize the space on a piece of paper occupied by the labels outside the actual puzzle. This translates into minimizing the area of the bounding box, which is determined by the longest leaders on each edge of the frame. In regard to the NP-completeness result, recall that we could show in Theorem 4 that the problem remains NP-complete even if we restrict all labels to be of size 1 × 4. Naturally, one can ask if the length 4 is required or if the problem remains NP-complete for even smaller labels. Moreover, since the result in Theorem 5 requires that the labeling is balanced and each line has to have two labels attached to it, it is an interesting open question if the problem actually remains NP-complete for constant-size labels under the same restrictions.
Another important direction for future research is to investigate to what extent the polynomial-time solutions extend to nonograms with more, or different, slopes, or even to curved nonograms where each port may have a distinct slope. For curved nonograms, it would also be interesting to investigate a model where the extensions outside the frame may also be curved, giving more options to avoid crossings. Similarly, it would be interesting to see to what extent other boundary shapes can be supported.
Finally, it would be of interest to test our algorithms on real puzzles, to investigate whether the running times are acceptable and whether the resulting layouts are usable in practice.

Declaration of competing interest
The authors declare that they have no known competing financial interests or personal relationships that could have appeared to influence the work reported in this paper.