{'fraction_non_alphanumeric': 0.04507216860001771, 'fraction_numerical': 0.015274949083503055, 'mean_word_length': 3.7075864943726553, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 34, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
Angle Optimization of Graphs Embedded in the Plane

In this paper we study problems of drawing graphs in the plane using edge length constraints and angle optimization. Specifically we consider the problem of maximizing the minimum angle, the MMA problem. We solve the MMA problem using a spring-embedding approach where two forces are applied to the vertices of the graph: a force optimizing edge lengths and a force optimizing angles. We solve analytically the problem of computing an optimal displacement of a graph vertex optimizing the angles between edges incident to it if the degree of the vertex is at most three. We also apply a numerical approach for computing the forces applied to vertices of higher degree. We implemented our algorithm in Java and present drawings of some graphs.


Introduction
Angular resolution is one of the aesthetic criteria measuring the quality of graph drawings in terms of human comprehension. The angular resolution of a straight-line drawing in the plane is the minimum angle between any two incident edges. The study of graph drawing with angular resolution started by Formann et al. [12] in 1990. They introduced the angular resolution of a graph as the supremum angular resolution over all straight-line drawings of the graph. The problem of computing the angular resolution of a graph is NP-hard (even the problem of deciding if R = π/2 for graphs with vertex degrees at most four is NP-hard).
The main focus in the early investigation [12,18] was on bounding the angular resolution of a graph in terms of the maximum vertex degree d. The obvious upper bound is R(d) ≤ 2π/d. A lower bound R(d) ≥ Ω(1/d) has been proved [12] for many graphs including planar graphs and complete graphs. A lower bound R(d) ≥ Ω(1/d 2 ) holds for all graphs [12]. If we insist on planar straight-line drawings, then R(d) ≥ α d for some constant 0 < α < 1 [18].
There was also a study of the optimization problem where the angular resolution of a given graph is maximized. Matousek et al. [19] considered an angle-optimal placement of point in polygon. In this problem, the task is to find a point p in the kernel of a star-shaped polygon P such that after connecting p to all the vertices of P by straight edges, the minimum angle between two adjacent edges is maximized. The kernel of P is defined as the locus of all points inside P that see all the edges and vertices of P (it is not empty since P is star-shaped). They showed that it is an LP-type problem of combinatorial dimension 3. Amenta et al. [2] studied various problems of optimal point placement for mesh smoothing (using different mesh quality measures). A related result is the polynomial-time algorithm for computing a Steiner point in a star-shaped polygon, minimizing the maximum angle. A parallel algorithm for mesh smoothing is presented in [13].
Carlson and Eppstein [6] considered tree drawings such that all faces form convex polygons (the infinite faces are created by extending the edges incident on leaves to the infinity). They showed that the optimal angular resolution can be computed in linear time and even the lengths of the tree edges may be chosen arbitrarily. In the recent paper, Eppstein and Wortman [11] considered graph drawing in the plane with faces drawn as centrally symmetric convex polygons. They found a polynomial time algorithm for computing a drawing maximizing the angular resolution.
Another direction in graph drawing with the angle resolution is Lombardi drawing where the graph edges are represented as circular arcs instead of straight-line segments [7,8,9]. Relaxing the condition of straight-line segments allows to achieve perfect angular resolution where the edges are equiangularly spaced around each vertex. The classes of graphs admitting Lombardi drawings are presented in [9] (and the algorithms for finding these drawings). Duncan et al. [8] found that unrooted trees can drawn with perfect angular resolution and polynomial area.
In this paper we study the problem of maximizing the angular resolution using the force-directed approach. This idea is not new and some algorithms for optimizing angular resolution using the force-directed or spring-embedding approach [3,7]. The common feature of these approaches is that the forces are directed from one vertex to another vertex. Our approach is different in the sense that we want to optimize the dislocation of a vertex. It can be viewed as a restricted version of the angle-optimal placement of point in polygon where a graph is embedded in the plane with straight-line edges and we want to find a new position of a given vertex by moving it at distance at most r > 0 (for a given r) and optimizing the incident angles. We call it Max-Min Angle Problem (MMA). This perturbation problem is interesting in its own right. From combinatorial point of view, it is an LP-type problem with the same combinatorial dimension as the angle-optimal placement of point in polygon [19] since only one new constraint is added. However, the situation is quite different from the algebraic point of view.
The angle-optimal placement of point in polygon is related to the famous Fermat problem (which appears as a special case when the vertex degree is three). In general it is known as the Fermat-Weber problem: given n points in a Euclidean space R d , find a point minimizing the sum of the distances to n given points. This point is called the geometric median. We assume d = 2. If n ≤ 4 then the geometric median can be computed exactly. However, it cannot be computed exactly if n ≥ 5, in general (i.e. for some instances) [4].
The MMA problem is harder algebraically than the angle-optimal placement of point in polygon since the optimal point can be at distance r from the initial point. In fact, we show that even for a vertex of degree three, the solution involves polynomials of degree 6 in some (difficult) cases. The polynomials of degree at least five cannot be solved exactly in general. The main result of this paper is that the solution of the MMA problem for a vertex of degree three can be computed exactly (we show that it can be expressed using a polynomial of degree four only).
Another reason why we introduce and study the MMA problem is that the parameter r allows to control the strength of the angular resolution force applied to vertices. This works well if we use more than one force. For example, we applied it to the spring embedding with length constraints.
The paper is organized as follows. In Section 2 we briefly describe force-directed graph drawing and introduce Max-Min-Angle problem. In Section 3 we recall the classical Fermat problem that appears as the special case of the MAA problem. In Section 4 we provide a solution of the MMA problem for vertex degree two. Optimal solution for degree three vertices is provided in Section 5. The algorithm and its performance is discussed in Section 6. Finally we conclude in Section 7.

Spring Embedder and Problem Statement
Force-directed graph drawing is a popular technique and there is a growing literature on force-directed drawing algorithms, see the recent survey by Kobourov [17]. Eades [10] introduced a mechanical model for graph drawing. To achieve aesthetically pleasing layouts and capture the edge length constraints he applied attracting/repelling force between two vertices if the distance between them is less/greater than the desired length. He found that the Hookes Law (linear) springs are too strong when the vertices are far apart but the logarithmic force solves this problem. As initial embedding of the graph the algorithm places its vertices at random locations. The algorithm stops after a sufficient number of iterations. If a state of equilibrium is reached i.e. all forces are zero, then the graph embedding reaches the desired positioning in the plane and remains static. An example of such drawing is shown in Figure 1.
Fruchterman and Reingold [14] added a condition of "even vertex distribution" which is modeled by attractive forces between adjacent vertices and repulsive forces between all pairs of vertices. This increases the number of forces (|V | 2 repulsive forces for a graph G = (V, E)) and slows down the algorithm.
The algorithms by Eades [10] and Fruchterman and Reingold [14] are just two examples of force-directed graph drawing. There are many spring embedders nowadays [17]. We consider a new force-directed approach for angular resolution where the vertex displacement is optimized locally.

Problem Statement
We consider one step of the spring embedder where the vertices of a graph G are embedded in the plane and they are allowed to move slightly. The spring embedder may take into account several forces, for example the forces aiming to achieve the desired lengths of the edges of G. We introduce a force that aims to optimize, for all vertices v, the angles between edges incident to v and embedded in the plane. Ideally, we want the edges to spread evenly around v. This may not be possible due to other constraints of the drawing (edge lengths, for example). Our goal is to maximize, for every v, the smallest angle between two edges incident to v. For this, we formulate our problem. Max-Min-Angle (MMA) Problem. Let G be a graph embedded in the plane. Let P be the position of a vertex v of G and let A 1 , A 2 , . . . , A k be the positions of the adjacent vertices of P in G. Let r > 0 be a radius. Find a point P * (the best position to move P within distance r) such that |P P * | ≤ r and the smallest angle ∠A i P * A j is maximized.
Let Z be the circle centered at P and radius r. We will solve the MMA problem in Sections 4 and 5 by considering degree k of vertex v.

Fermat problem
When the degree of v is three, the MMA problem is related to the classical Fermat problem: Given triangle ABC in the plane, find a point F such that the total distance from the three vertices of the triangle to F is the minimum possible. The solution of the Fermat problem (called Fermat point or Torricelli point) depends on triangle ABC. Case 1: All angles of triangle ABC are smaller than 120 • . Construct three new regular triangles ABC , AB C, and A BC out of the three sides of triangle ABC. Then the point F is the intersection of three lines AA , BB , and CC , see Figure 2(a). In this case the Fermat point F is coincident with the first isogonic center of the triangle [22], where the angles subtended by the sides of the triangle are all equal, i.e. ∠AF B, ∠BF C, ∠CF A = 120 • .

Case 2:
There exists an angle in triangle ABC greater than or equal to 120 • . Only one angle of the triangle is greater than or equal to 120 • , say ∠ACB ≥ 120 • , see Figure 2(b) for example. Then the Fermat point is coincident with C.

The MMA problem
We solve the MMA problem depending on the degree of vertex v.

Vertex of Degree 2
Suppose that the degree of v is two. Let A and B be the positions of its adjacent vertices. The task is to find a point P * with |P P * | ≤ r maximizing angle θ = min(∠AP B, ∠BP A) (the angles are in the counterclockwise order). Suppose that segment AB intersects circle Z. Obviously, θ = 180 • is achieved by any point P * in the intersection of AB and Z. The solution is unique if AB ∩ Z is a single point (for example, AB may be tangent to Z).
Suppose that AB ∩Z is a segment of positive length, see Figure 3(a). There are infinitely many positions of P * (all in AB ∩Z achieving θ = π). If P is not on segment AB then any location of P * will change distances to A and B. We would like to preserve the ratio |AP |/|BP | and compute point Q in segment AB such that |AQ|/|BQ| = |AP |/|BP |. This implies Then the coordinates of point Q can be computed using If point Q lies in circle Z then P * = Q as shown in Figure 3(a). Otherwise we select the endpoint of segment AB ∩ Z that is closer to Q, see Figure 3(b) for an example.
We assume now that AB and Z do not intersect.
Proposition 1 If segment AB and circle Z do not intersect then P * is on the boundary of Z. Furthermore the circle passing through A, B, and P * is tangent to Z.
Proof: Suppose P * = (x, y) where x and y are unknown. Consider circular arc AP * B as shown in Figure 4(a). The angles ∠AP B are equal for all points P from arc AP * B (since the inscribed angle of a chord equals half of the central angle ,see Figure 8(b)). Therefore we can assume that P * is on the boundary of Z. Furthermore arc AP * B cannot intersect the boundary of Z twice since any point P from the arc of Z cut off by arc AP * B makes an angle larger than P * makes, i.e. ∠AP B > ∠AP * B as shown in Figure 4. Thus, arc AP * B must be tangent to Z as shown in Figure 5(a).  (4) and (5).
Let O be the center of the circle passing through A, B, and P * . It suffices to find point O since point P * is the intersection point of the circle Z and the line passing through points O and P . The first equation is (P * x − P x ) 2 + (P * y − P y ) 2 = r 2 and the second equation is linear where the subscripts denote the coordinates.
Using coordinate transformations we can assume without loss of generality that A(0, −1) and B(0, 1), see Figure  5(b). Then |OA| = |OB| implies that O(x, 0) where x is unknown. Let r = |OA| be the radius of the circle centered at O. Then |OP | = r + r and |OA| = r . They can be written as Subtracting Equation (4) from Equation (5) , we obtain By plugging x from this equation into Equation of (5) we obtain a quadratic equation in the variable r . There are two roots of the quadratic equation and they correspond to two circles shown in Figure 6. Therefore we take the smallest root of the quadratic equation.

Proposition 2
The maximum angle problem can be solved using a quadratic equation.

Vertex of degree 3
In this Section we consider the case where the degree of v is three. Let A, B and C be the positions of its adjacent vertices in G. We consider the first case of the Fermat point of triangle ABC where all angles of ABC are less than 120 • .
Proposition 3 If all angles of ABC are less than 120 • and the Fermat point F of ABC lies within circle Z, then P * = F .
Proof: Without loss of generality we assume that A, B, C are in counterclockwise order around P * , see Figure 7 (a). Suppose to the contrary that P * is a point inside Z different from F as shown in Figure 7 (b). The Fermat point lies in one of triangles AP * B, BP * C, or AP * C. Suppose that it belongs to triangle AP * C. Then ∠AP * C < ∠AF C = 120 • and P * is not the optimal point. Therefore P * = F , see Figure 7 (c). Note that in the case of Proposition 3 all angles around P * are equal 120 • . In the rest of this section we consider cases where not all angles ∠AP * B, ∠BP * C, and ∠AP * C are equal. If the smallest angle of ∠AP * B, ∠BP * C, and ∠AP * C is unique, say it is ∠AP * B, then it can be computed by solving the maximum angle problem for A, B using the method from the previous section. It remains to consider the case of two smallest angles.

Two smallest angles
In this section we assume that there are exactly two smallest angles from {∠AP * B, ∠AP * C, ∠BP * C}. First, we show that P * must be on the boundary of circle Z.

Lemma 4
If the degree of v is 3 and there are two smallest angles around P * then P * lies on the boundary of Z.
Proof: We assume that (i) A, B, C are in counterclockwise order around P * , and (ii) ∠AP * B = ∠BP * C < ∠CP * A. Suppose to the contrary that P * lies inside Z as shown in Figure 8. Draw two circles, one passing through points A, B, P * and the second passing through points B, C, P * . Since these circles intersect by two points B and P * , then their interiors intersect by a lune. Since P * is inside circle Z, then the intersection of the lune and the interior of Z is a non-empty set I, see the shaded area in Figure 8(a). For any point P in I, ∠AP B > ∠AP * B and ∠BP C > ∠BP * C (this can be seen, for example, by the fact that the inscribed angle of a chord equals half of the central angle, see Figure 8(b)). We choose P from I close enough to P * such that ∠AP B, ∠BP C < ∠CP A. Then the smallest angle around P is greater than ∠AP * B. Therefore P * is not the solution of the MMA problem. Contradiction. The main result in this section is the following Theorem 5 If the degree of vertex v is three and there are two smallest angles around P * then P * can be computed by solving a polynomial equations of degree at most four.
Proof: First we show that it is possible that there are two smallest angles. An example where two smallest angles around P * are equal is shown in Figure 9 where points P, P * and B are collinear and points A and C are symmetric about the line P B. Therefore angles ∠AP 2 B < ∠AP * B < ∠AP 1 B and P * is the solution of the MMA problem. We now prove the theorem. Suppose that ∠AP * B = ∠BP * C < ∠CP * A. We need to find coordinates of P * (x, y). By the law of cosines we write the equation cos(∠AP * B) = cos(∠BP * C) as Without loss of generality we can assume that P = (0, 0) and A y = C y . By Lemma 4 we assume |P P * | = r. Then x 2 + y 2 = r 2 and x − (A y + B y )y = a 4 x + a 5 y + a 6 (10) A B C P P * P 1 P 2 r Figure 9: Points P 1 and P 2 are the points on circle Z maximizing angles ∠AP 1 B and ∠BP 2 C, respectively. Point P * is different from P 1 and P 2 and angles ∠AP * B = ∠BP * C. where Take the square of both sides of Equation (8) (a 1 x + a 2 y + a 3 )(a 4 x + a 5 y + a 6 ) It can be written as c 1 x 3 + c 2 y 3 + c 3 x 2 + c 4 y 2 + c 5 x 2 y + c 6 xy 2 + c 7 x + c 8 y + c 9 xy + c 10 = 0 where c 1 = a 1 a 2 ), c 7 = (2a 3 a 4 a 6 + a 1 a 2 6 − (2b 3 b 4 b 6 + b 1 b 2 6 )), c 8 = (2a 3 a 5 a 6 + a 2 a 2 6 − (2b 3 b 5 b 6 + b 2 b 2 6 )), c 9 = 2(a 1 a 5 a 6 + a 2 a 4 a 6 + a 3 a 4 a 5 − (b 1 b 5 b 6 + b 2 b 4 b 6 + b 3 b 4 b 5 )), c 10 = a 3 a 2 6 . Using y 2 = r 2 − x 2 we reduce powers of y in Equation (14) c where Simplifying Equation (17)  ), e 6 = 2d 3 d 4 − (2d 6 d 7 r 2 ), e 7 = d 2 4 − d 2 7 r 2 . By Lemma 6 the sextic equation has a quadratic factor. Dividing by it, we obtain a polynomial of degree four (Equation (19), (20), or (21)) and the theorem follows.
Proof: We consider 3 cases. Case I: A y = r. Then e 5 = e 6 = e 7 = 0 and the polynomial (18) has factor x 2 . The polynomial is reduced to the polynomial of degree 4 e 1 x 4 + e 2 x 3 + e 3 x 2 + e 4 x = 0.
The lemma follows.

Algorithm
In this Section we discuss how to modify the spring embedder in order to take into account the angles between embedded edges. Let G = (V, E) be an input graph. At the initial step the spring embedder randomly places the vertices of G in the plane. Then, it iterates a simultaneous motion of the vertex positions based on one or several forces (springs). We describe a new force using angle optimization. For every vertex v with a position P in the plane, the algorithm computes a new position P * and uses vector P P * as a force applied to vertex v. Angle Optimization Algorithm Input: Graph G = (V, E) embedded in the plane and radius r. Output: New embedding of graph G where each vertex is translated within distance r.
For each vertex v ∈ V do the following steps.
1. Let P be the position of v in the plane. We compute P * as follows. Every time P * is assigned, we proceed to the next vertex v.
2. If the degree of v is at most one then set P * = P .
3. Suppose that the degree of v is equal to two. Let A and B be the positions of vertices adjacent to v.
(a) Suppose AB ∩Z = ∅. Compute point Q using Equations (2) and (3). If |P Q| ≤ r then P * = Z; otherwise assign P * to the endpoint of segment AB ∩ Q that is closer to Q.
4. Suppose that the degree of v is equal to three. Let A, B, and C be the positions of vertices adjacent to v. Compute Fermat point F of triangle ABC. If |P F | ≤ r then set P * = F . Otherwise, for each segment ab ∈ {AB, AC, BC}, compute point P ab maximizing angle ∠aP ab b (Section 4.1). If angle ∠aP ab b is the smallest angle from {∠AP AB B, ∠AP AC C, ∠BP BC C} then set P * = P ab . Otherwise compute P * as the best solution using two smallest angles for every two segments from {AB, AC, BC} (Section 5.1).

5.
For the remaining vertices of degree at least four, apply the following grid approach. Pick a grid stepsize δ, for example δ = r/3. Consider a grid with the origin at P . For every grid point Q with |P Q| ≤ r, compute the smallest angle α Q if P is moved to Q. Find Q maximizing α Q . Assign P * = Q.
We implemented this algorithm 1 and run it on several graphs. First, we tested the algorithm on graph T 10 from [21] since it contains vertices of degree two. The program draws T 10 with angles optimized, see Figure 10   We also tested the algorithm on the phylogenetic networks for Algae example [20]. The drawing of the Algae network by the spring embedder [1] is shown in Figure 1. It can be compared with our drawings in Figure 11. In all drawings (in Figures 1 and Figure 11) the edge length constraints are satisfied but the angle resolution in drawings in Figure 11 is significantly larger. The drawing shown in Figure 11 (a) uses the weighted version of the graph and the drawing in Figure 11  Finally, we run our program on the well-known graphs: the Petersen graph [16], the Heawood graph [15], and the Herschel graph [5], see Figure 12. The Petersen graph is drawn with two crossings (the Petersen graph is in fact the smallest 2-crossing cubic graph), see Figure 12(a). The Heawood graph has crossing number three (it is actually the smallest 3-crossing cubic graph) and our program found a drawing with exactly three crossings, see Figure 12(b).

Conclusion
We proposed a novel approach to the problem of optimizing the angular resolution of a drawing. It has been applied to the spring embedder and the results with good angular resolution were obtained. It is known that the running time of the spring embedder can increase with the size of the graph. Therefore it is important to perform better the intermediate steps. The optimal solution of the MMA problem provides an opportunity to decrease the number of iterations of the spring embedder.
The main result of this paper states that a vertex of degree at most three can be displaced optimally by solving a polynomial equation of degree at most four (which is interesting since the straightforward approach leads to a polynomial of degree six). A special case of MMA problem for degree three verices is the classical Fermat problem and the Fermat point is the solution for the special case. An interesting question for future research is to find the algebraic complexity of MMA problem for higher vertex degrees.