Snapping Graph Drawings to the Grid Optimally
aa r X i v : . [ c s . C G ] A ug Snapping Graph Drawings to the Grid Optimally
Andre Löffler, Thomas C. van Dijk, and Alexander Wolff
Lehrstuhl für Informatik I, Universität Würzburg, Germany.
Abstract.
In geographic information systems and in the production ofdigital maps for small devices with restricted computational resourcesone often wants to round coordinates to a rougher grid. This removesunnecessary detail and reduces space consumption as well as computationtime. This process is called snapping to the grid and has been investigatedthoroughly from a computational-geometry perspective. In this paperwe investigate the same problem for given drawings of planar graphsunder the restriction that their combinatorial embedding must be keptand edges are drawn straight-line. We show that the problem is NP-hard for several objectives and provide an integer linear programmingformulation. Given a plane graph G and a positive integer w , our ILP canalso be used to draw G straight-line on a grid of width w and minimumheight (if possible). When compressing geographic data, for example in order to ship it to deviceswith small memory, small screens and slow CPUs, the main objective is to reduceunnecessary detail. One way to do this is to round data points to a grid.In the computational geometry community, a process called snap rounding has been proposed and has since become well-established: given an arrangementof line segments, each grid cell that contains vertices or intersections is “hot”.Then every segment becomes a polygonal chain whose edges ( fragments ) connectcenter points of hot cells, namely those that the original segment ( ursegment )intersects. Guibas and Marimont [7] showed that during snap rounding, verticesof the arrangement never cross a polygonal chain, so after snapping no two frag-ments cross. Moreover, the circular order of the fragments around an outputvertex is the same as the order in which the corresponding ursegments inter-sect the boundary of its grid cell. The resulting arrangement approximates theoriginal one in the sense that any fragment lies within the Minkowski sum ofthe corresponding ursegments and a unit square centered at the origin. However,the structure of the graph can be affected (vertices merge, faces disappear, edgesbend). Further work in this direction includes that of De Berg et al. [3].Motivated by the above GIS application, we investigate the problem of mov-ing the drawing of a graph to a given grid. Since we still want to be able torecognize the original graph, we do not tolerate new incidences. Then we mustaccept the possibility that a vertex does not go to the nearest grid point, but we
A. Löffler et al. still want to minimize change. This can by measured, for example, by the sumof the distances or the maximum distance in the Euclidean ( L -) or Manhattan( L -) metric. Apparently, this problem, which we call Topologically-SafeSnapping , has not been studied yet. (Note that we carry over the term “snap-ping,” although we don’t necessarily snap to the nearest grid point.)From a graph-drawing perspective, restricting to the grid has a (relatively)long history. Motivated by the fact that Tutte’s barycenter method [14] for draw-ing planar graphs yields drawings that need precision linear in the size of thegraph, Schnyder [13] and, independently, de Fraysseix et al. [5] have shown thatany planar graph with n vertices admits a straight-line drawing on a grid of size O ( n ) × O ( n ) . This is asymptotically optimal in the worst case [5]. Chrobak andNakano [2] have investigated drawing planar graphs on grids of smaller width,at the expense of a larger height. Grid-snapping techniques can be found in anydiagram creation tool. Aesthetic properties of force-directed drawing algorithmsare widely researched, see e.g. Kieffer et al. [8] for grid layouts of diagrams.Although minimizing the area of straight-line grid drawings has been thetopic of several graph drawing contests, there has been rather little previouswork. It is known that the problem is NP -hard [9], but not even for special casesexact or approximation algorithms have been proposed. Our contribution.
We show that optimal snapping is NP -hard, with a reductionthat asks for compressing each coordinate by just a single bit (Sect. 2). The proofis somewhat similar in concept to the proof of the NP -hardness of Metro-MapLayout [11,15], but new constructions are required since the snapping problemdoes not easily allow the construction of “rigid” gadgets. Second, we give aninteger linear program (ILP) for optimal snapping (Sect. 3). This ILP generalizesthe one for Metro-Map Layout [12]. Where that ILP assumes a constant numberof possible edge directions (namely 8), we have to cope with a number that isquadratic in the size of the grid. The numbers of variables and constraints ofour ILP are polynomial in grid and graph size, but are quite large in practice. Infact, on a grid of size k × k , there are Θ ( k ) edge directions. Thus, for an n -vertexplanar graph, we must generate O ( k n ) constraints, among others, to preserveplanarity and the cyclic order of edges around the vertices. To ameliorate this, weapply delayed constraint generation, a technique that adds certain constraintsonly when needed. Still, runtime is prohibitive for graphs with more than about15 vertices. Our techniques can be adapted to draw (small) graphs with minimalarea. This is interesting even for small graphs since minimum-area drawings canbe useful for validating (counter)examples in graph drawing theory. We start with a formal definition of
TopologicallySafeSnapping – or
TSS for short. To measure the cost of rounding a graph, we utilize Manhattan distanceand the total cost of rounding a graph is the sum over the individual costs ofthe vertices. As input we take a plane graph G = ( V, E ) with vertex positions napping Graph Drawings to the Grid Optimally 3 X Y Z (a) (b) (c) (d) (e)
Fig. 1. (a) Graph H ( F ) for formula F = ( X ∨ Y ∨ Z ) ∧ ( X ∨ Y ) ∧ ( X ∨ Z ) , (b) horizontalline gadget, (c) bottom-to-right bend gadget, both with possible roundings, (d) gadgetfor variable with two negated and one unnegated occurrences, (e) all-negated clausegadget with three negated variables. Inner area of each gadget highlighted gray. and a bounding box [0 , X max ] × [0 , Y max ] . The TSS problem is then to minimizethe cost of rounding the vertices of G to the integer grid within the box withoutaltering the topology with respect to the given plane straight-line drawing of G .We prove NP -hardness of TSS by considering the decision variant: is therea rounding that does not exceed a given cost bound c ? We reduce from Planarmonotone 3-SAT (which is NP -hard [4]): given a formula F in 3-CNF that ismonotone and whose graph H ( F ) is planar, is F satisfiable? The graph H ( F ) hasa vertex for each variable and each clause of F and an edge between a variablevertex v X and a clause vertex v C if X is part of C . We will only considerformulae whose graphs are planar and that are monotone in the usual sense:for any clause C , variables in C either are all negated or all unnegated. We canassume that the graph H ( F ) can be laid out as in Fig. 1 (a): all variable verticeslie on the x-axis, the vertices of all-negated clauses lie above the x-axis, and thevertices of all-unnegated clauses lie below the x-axis [4]. Theorem 1.
TopologicallySafeSnapping is NP -hard.Proof. For a given monotone, planar 3-CNF formula F , we construct a costbound c min and a plane graph G with vertices at half-integer coordinates. Thesum of all vertex movements induced by rounding G to integer coordinates isexactly c min if and only if F is satisfiable. To achieve this, we introduce gadgetsfor the elements of H ( F ) – variables, clauses, edges and bends – and construct G and c min in polynomial time.For exposition, we consider two types of vertices. Black vertices start oninteger grid points and do not need to be rounded. Moving a black vertex toanother integer grid point is allowed, but we will show that this is not optimalif F is satisfiable. White vertices start at grid cell centers and thus will alwaysmove at least one unit by rounding. Let W ⊆ V ( G ) be the set of white vertices.Now we give the construction of the various gadgets.First, we introduce the line and bend gadgets. These ensure consistency be-tween variable and clause gadgets. Every segment of the line gadget consists of A. Löffler et al. four black vertices and two edges forming a tunnel , and a single white vertexinside; see Fig. 1 (b). The white vertex can be rounded most cheaply to exactlytwo possible integer grid points, depicted by the red and blue arrows. By round-ing a white vertex in one direction, we prohibit the neighbor in that directionto go the opposite way – as both vertices would end up on the same integer gridpoint (which violates topological safety). So, if the white vertex at one end ofthe line is rounded inward (blue arrow) the white vertex at the other end of thatline must be rounded outward – we say it is pushed . The same holds for the bendgadgets, as can be seen in Fig. 1 (c).Next, consider the variable gadget depicted in Fig. 1 (d). It has tunnels forvertical line gadgets for every negated and unnegated occurrence at the top andbottom respectively. At the center of this gadget, there is a white vertex thatis connected to the gadget’s walls by two triangles. Call this the assignment vertex and note that it can be rounded up or down, which makes the edges ofthe triangles block grid points on the top or bottom tunnels, respectively. Thetunnels of that direction are then all forced to push into the connected clausegadgets. This represents the truth assignment of the corresponding variable.Finally, the clause gadget is shown in Fig. 1 (e). We describe the all-negateddegree-3 version; the degree-2 version can be constructed similarly. There is awhite satisfaction vertex that can go to any of three possible integer grid pointsat equal cost. These grid points belong to line gadgets and are only available ifthe line does not “push.” Then the satisfaction vertex can be rounded at cost if and only if the clause is satisfied. Gadgets for all-unnegated clauses can beobtained by mirroring the construction of Fig. 1 (e) at a horizontal line.The rounding cost of G is bounded from below by c min = | W | since everywhite vertex must be rounded at cost at least . If F is satisfiable, there is arounding that achieves this because then we can round the assignment verticessuch that the satisfaction vertices can be rounded at cost . In the other direction,a satisfying assignment can be read off from the assignment vertices if roundingoccurred at cost c min .If none of the three candidate grid points for the satisfaction vertex areavailable, a topologically correct rounding must move a black vertex associatedwith that clause (of either the clause itself, the connected variables or the edgesand bends connecting them). This adds at least to the rounding cost withoutreducing the movement of any white vertex and thus such solutions cost strictlymore than c min . That is, if c min is exceeded, then F is unsatisfiable: any roundingcorresponding to a satisfying truth assignment is cheaper. This concludes ourKarp reduction and the claim follows. ⊓⊔ Corollary 1.
TopologicallySafeSnapping is also NP -hard when using Eu-clidean distance. In this case it is also NP -hard to minimize the maximum move-ment instead of the sum.Proof (sketch). The above proof goes through with Euclidean distance and c min = √ . + 0 . ·| W | . For minimizing the maximum movement, observe that round-ing white vertices now costs less, but moving a black vertex still has cost at napping Graph Drawings to the Grid Optimally 5 least : if F is satisfiable, the maximum movement is √ . + 0 . , otherwise itis at least . ⊓⊔ This distinction of maximum movement ( √ . + 0 . ≈ . versus ) basedon the satisfiability of F also gives the following. Corollary 2.
Euclidean
TopologicallySafeSnapping with the objective tominimize maximum movement is
APX -hard.
In this section we provide an ILP-based exact algorithm for
TSS . Recall thatan instance is a graph G = ( V, E ) with vertex coordinates. For all v ∈ V , callthese ( X v , Y v ) and introduce integer decision variables ≤ x v ≤ X max and ≤ y v ≤ Y max to represent the “rounded” output position. This leads to thefollowing objective function. Minimize P v ∈ V | x v − X v | + | y v − Y v | (1)This formula is itself not linear, but can be made so with standard transforma-tions [10]. Note that without any further constraints, this would just move everyvertex to the nearest integer grid point. We will now introduce constraints toensure topological safety, that is, in the output no two points are on same gridpoint, no two edges intersect, and the edges at every node have the same cyclicorder as in the input. Vertices do not coincide.
This can be ensured by adding the following con-straints. They too are not linear as stated, but can be readily linearized. ( x v = x w ) ∨ ( y v = y w ) ∀ v, w ∈ V, v = w (2) Possible directions.
The most important departure from the metro-map drawingILP is that, clearly, more than eight different directions are allowed. A priori wehave no further constraints than that every rounded vertex lies somewhere withinthe given bounding box. Let D be the set of unique directions D = ( D X , D Y ) in [ − X max , X max ] × [ − Y max , Y max ] . Considering the Farey sequence [6], we knowthat |D| is Θ ( X max · Y max ) . In the following, we let the set D be ordered coun-terclockwise, starting at the positive x-axis, allowing comparison of directions. No two edges cross.
The following constraints ensure that nonincident edges donot cross. (Incident edges are allowed to touch in the shared vertex.) We willfollow the idea of Nöllenburg and Wolff [12]. While producing octilinear drawingsof metro maps, they ensured planarity by forcing every pair of nonincident edgesto be separated by at least some distance D min in at least one of the eightoctilinear directions. This minimum distance was partly an aesthetic guideline,but also guarantees planarity. We are only interested in the latter and thereforepick D min such that all planar realizations on the grid are allowed. A. Löffler et al.
The separation distance D min has to be small enough to separate any non-intersecting pair of edges in the output. Here the bounding box leads to abound since it bounds the slope of the edges; it suffices to choose D min =1 / (max { X max , Y max } + 1) .For every pair of nonincident edges e , e ∈ E and every direction D ∈ D , weintroduce a binary decision variable γ D ( e , e ) ∈ { , } indicating that e and e are apart by D min in direction D . Every such pair must be separated in somedirection (following the idea of [11]). P D ∈D γ D ( e , e ) = 1 ∀ e , e ∈ E, e , e nonincident (3)Let L γ = 2 · max { X max , Y max } + 1 . Then, for any direction D ∈ D , any pair ofnonincident edges e , e and any v ∈ e , w ∈ e , we require the following. D X · ( x v − x w ) + D Y · ( y v − y w ) + (1 − γ D ( e , e )) L γ ≥ D min (4)Constraint (3) yields a unique direction D with γ D = 1 . By choice of L γ , anyconstraint (4) that involves a direction D with γ D = 0 is trivially fulfilled. Determine direction of incident edges.
For incident edges e , e ∈ E , we have toensure that the directions of e and e differ. Again, we generalize the metro-mapdrawing ILP – dropping the “relative position rule” – allowing edges to have anydirection D ∈ D .To keep track of this, we introduce a binary decision variable α D ( v, w ) ∈{ , } for every vertex v ∈ V , every neighbor w ∈ N ( v ) and every direction D ∈ D . The meaning of α D ( v, w ) = 1 is that the direction of edge ( v, w ) is D . P D ∈D α D ( v, w ) = 1 ∀ v ∈ V ∀ w ∈ N ( v ) (5)For any vertex v ∈ V , any neighbor w ∈ N ( v ) , and any direction D ∈ D ,the following ensures that edge ( v, w ) indeed has direction D . Let L α = 2 · max { X max , Y max } + 1 . x w · D Y + y v · D X − x v · D Y ± (1 − α D ( v, w )) L α R y w · D X (1 − α D ( v, w )) L α + ( x w − x v ) · D X + ( y w − y v ) · D Y ≥ (6)From constraint (5) we get that for every vertex-neighbor pair one α has to beset to . This α again enables one subset—as L α dominates all other terms—ofconstraints from (6), forcing comparison between edge slope and direction. Thisgives us the direction of edge ( v, w ) with the correct sign. Preserve cyclic order of outgoing edges.
We use a binary decision variable β ( v, w ) ∈{ , } for every vertex-neighbor pair, indicating if w is the “last” neighbor of v according to the order of D . The following preserves cyclic order. P w ∈ N ( v ) β ( v, w ) = 1 ∀ v ∈ V with deg( v ) > (7) napping Graph Drawings to the Grid Optimally 7 α D ( v, w i ) ≤ β ( v, w i ) + P D w ∈D : D w >D α D w ( v, w i +1 ) ∀ D ∈ D ∀ v ∈ V, N ( v ) = { w , w , . . . , w k } ( k = deg v > (8)For notational convenience, we let w k +1 = w , as N ( v ) is conceptually circular.For any α set to , the inequalities of (8) are trivially satisfied. Otherwise, therehas to be a neighbor whose connecting edge has a later direction (and thus thecorresponding α set to ), unless it is the last neighbor in the embedding of v .To ensure that there is only one “last neighbor”-violation of the constraints from(8), we introduce the constraints of (7). Adding β to every constraint of (8) alsoallows for the whole neighborhood of v to be rotated around it. This describesthe full ILP and gives to the following. Theorem 2.
The above ILP solves
TopologicallySafeSnapping .Graph drawing.
Replacing the objective function with
Minimize max v ∈ V y v , theILP computes a straight-line grid drawing with the given embedding, width atmost X max , and minimum height. This allows us to find minimum-area drawingsof small graphs. Delayed constraint generation.
We can apply a delayed constraint generationapproach (see for example Cinneck [1]) to the above ILP as follows. First we runthe ILP without any constraints, which snaps each vertex to the nearest gridpoint. (This takes practically no time.) We then test the result for topologicalvalidity, adding constraints corresponding to any violations. Then we repeatuntil no violations occur. This improves the runtime when few iterations sufficefor a particular instance, but the approach should still be considered practicallyinfeasible, especially for large bounding boxes: the set of possible directions D still results in a large program. Future work could focus on reducing the brute-force inclusion of all possible directions. Experimental results are found in theappendix. Acknowledgments.
We thank Gergely Mincsovics for suggesting this problemto us.
References
1. J. W. Chinneck.
Feasibility and Infeasibility in Optimization: Algorithms and Com-putational Methods . Springer, 2008.2. M. Chrobak and S.-I. Nakano. Minimum-width grid drawings of plane graphs.
Comput. Geom. , 11(1):29–54, 1998.3. M. de Berg, D. Halperin, and M. Overmars. An intersection-sensitive algorithmfor snap rounding.
Comput. Geom. , 36(3):159–165, 2007.4. M. de Berg and A. Khosravi. Optimal binary space partitions for segments in theplane.
Int. J. Comput. Geom. Appl. , 22(03):187–205, 2012.5. H. De Fraysseix, J. Pach, and R. Pollack. How to draw a planar graph on a grid.
Combinatorica , 10(1):41–51, 1990. A. Löffler et al.6. R. L. Graham, D. E. Knuth, and O. Patashnik.
Concrete Mathematics—A Foun-dation for Computer Science . Addison-Wesley, Reading, MA, U.S.A., 1994.7. L. J. Guibas and D. H. Marimont. Rounding arrangements dynamically.
Int. J.Comput. Geom. Appl. , 8(2):157–178, 1998.8. S. Kieffer, T. Dwyer, K. Marriott, and M. Wybrow. Incremental grid-like layoutusing soft and hard constraints. In S. K. Wismath and A. Wolff, editors,
Proc.21st Int. Symp. Graph Drawing (GD’13) , volume 8242 of
LNCS , pages 448–459.Springer, 2013.9. M. Krug and D. Wagner. Minimizing the area for planar straight-line grid drawings.In S.-H. Hong, T. Nishizeki, and W. Quan, editors,
Proc. 15th Int. Symp. GraphDrawing (GD’07) , volume 4875 of
LNCS , pages 207–212. Springer, 2008.10. B. A. McCarl and T. H. Spreen.
Applied mathematical programming using algebraicsystems . Texas A&M University, 1997.11. M. Nöllenburg. Automated drawing of metro maps. Master’s the-sis, Fakultät für Informatik, Universität Karlsruhe, 2005. Available at .12. M. Nöllenburg and A. Wolff. Drawing and labeling high-quality metro maps bymixed-integer programming.
IEEE Trans. Visual. Comput. Graphics , 17(5):626–641, 2011.13. W. Schnyder. Embedding planar graphs on the grid. In
Proc. 1st ACM-SIAMSymp. Discrete Algorithms (SODA’90) , pages 138–148, 1990.14. W. T. Tutte. How to draw a graph.
Proc. London Math. Soc. , 13(52):743–768,1963.15. A. Wolff. Drawing subway maps: A survey.
Informatik – Forschung & Entwicklung ,22(1):23–44, 2007.napping Graph Drawings to the Grid Optimally 9
Appendix: Experimental evaluation
In the following section, we will discuss performance of an IBM CPLEX imple-mentation of the above ILP on graphs different in vertex count, size of boundingrectangle and number of “difficult” parts. We ran experiments on a Linux ma-chine with 16 cores (2666 MHz and 4 MB cache each), 16 GB memory and 20GB swap space and using the Java bindings for CPlex. Model size is measured byconsidering the number of rows and columns before and after CPLEX completesany preprocessing step, “*” means that no model could be created within giventime and memory. Any times given are in wall clock time, and “ † ” means thatthere was no result within 10 minutes of computation.In the following, full model is used for executions of the above ILP withoutrow generation. The column first gives the time until any feasible integer solution(not necessarily optimal) is reported by the integer solver. In both cases, optimal gives the time until the solver reports an optimal solution. For the output figures,white vertices represent the initial positions with the red arrows indicating actualvertex movement.We start with a rather small graph (Fig. 2). Because of its size, building themodel and finding the solution is quick. However, its vertices are positioned sothat many constraints are not trivially satisfied and significant effort is requiredeven by the row generation approach. Input OutputFull model Row generationModel size rows columns rows columnsWithout presolve 12809 3631 3795 1133With presolve 8046 2239 3791 1053first solution optimal optimalComputation time 3.2 s 90.6 s 29.2 s
Fig. 2.
Graph 1: | V | = 9 , | E | = 10 In Fig. 3, the by far most expensive constraint is for checking the embeddingof the central node. Any other constraint is easily satisfiable. In terms of com-putation time, there is not a big difference between finding the first solution and
Fig. 3.
Graph 2: | V | = 11 , | E | = 15 closing the integrality gap. Notice, that every vertex has one preferred integergrid point that is not preferred by any other vertex, so just rounding to thenearest grid point already gives a optimal solution. This solution is found by thefirst run of the row generation approach almost immediately. (Note that the . second runtime includes setting up the Java environment, calling the CPLEXsolver and checking topology.) The difference between the full model and therow generation approach becomes even more important when the size of thebounding box increases. Consider Fig. 4. While still easy to round in the samesense as above, as size of the bounding rectangle increases, so does the time forbuilding and solving the full model. However, this has no impact on solving timefor the row generation approach for “easy” graphs.Consider the graph in Fig. 5. While too large to round with the full modelapproach in the allotted time of minutes, the row generation only has toadd one constraint from Equation 2 for two vertices of the upper-right corner.Rebuilding the model and solving with this constraint runs in reasonable time(compared to the full model). Notice that this constraint does not involve thedirection set D .When rounding graphs with vertices starting in close proximity (like in Fig. 6)several things can be noticed. First of all, small bounding box result in smalland easy-to-solve models. The size of the bounding box has extreme effect onthe runtime (compare Fig. 2, which has only two more vertices but a muchlarger bounding box). Second, when many constraints are violated during the napping Graph Drawings to the Grid Optimally 11 row generation processes, iteratively adding the constraints results in runtimeexceeding the time for solving the full model in the first place.We end this section with two rather small examples (Figs. 6 and 7). Bothhave a rather large number of vertices compared to the size of the boundingrectangle and thus include many “difficult” parts. The row generation approachclearly outperforms the full model (while still being infeasible in practice). Input OutputFull model Row generationModel size rows columns rows columnsWithout presolve * * 104 130With presolve * * 4 5first solution optimal optimalComputation time † †
Fig. 4.
Graph 3: | V | = 13 , | E | = 25 † † Fig. 5.
Graph 4: | V | = 26 , | E | = 34 Input OutputFull model Row generationModel size rows columns rows columnsWithout presolve 2603 916 2271 816With presolve 2583 896 2245 682first solution optimal optimalComputation time 0.5 s 4.8 s 20.2 s
Fig. 6.
Graph 5: | V | = 7 , | E | = 7 napping Graph Drawings to the Grid Optimally 13Input OutputFull model Row generationModel size rows columns rows columnsWithout presolve 135386 35649 15741 4174With presolve 74957 19591 9200 2402first solution optimal optimalComputation time 42.6 s 1105.6 s 21.2 s Fig. 7.
Graph 6: | V | = 19 , | E | = 18 Input OutputFull model Row generationModel size rows columns rows columnsWithout presolve 323441 82816 15161 4044With presolve 323441 82816 15127 3894first solution optimal optimalComputation time 182.1 s † Fig. 8.
Graph 7: | V | = 20 , ||
Graph 7: | V | = 20 , || E ||