• Nenhum resultado encontrado

3.3 Spring and Spring-Electrical Algorithms Implemented for TikZ

3.3.1 Hu's Spring-Electrical Algorithm

36 Force-Based Graph Drawing Algorithms for Arbitrary Graphs

Due to its approximative nature the Barnes–Hut algorithm may compute forces inaccurately and therefore result in drawings of lower quality. The effects are critical with small graphs in particular where deviations from perfect layouts may easily disturb authors and viewers alike.

However, this optimization can lead to significant speedups and thus becomes valuable when laying out large graphs.

Spring and Spring-Electrical Algorithms Implemented forTikZ 37

The largerp, the weaker the long range electric force and therefore the peripheral distortion.

This model is an generalization of the previous definition of fe, which is obtained by setting p= 1.

The consideration of node weights and Barnes–Hut cell masses when computing the force be-tween either two nodes or a node and a cell is important for the system energy optimization to work properly. Throughout [31], node weights are assumed to be 1. This implies that the mass of a Barnes–Hut cell is the number of nodes it contains. Inspired by Walshaw, I decided to support arbitrary node weights by including the node weight and cell mass infeas a factor:

fe(u,v) =C·weight(v)·k1+p

||xvxu||p , p>0. (3.10) When using the Barnes–Hut method,weight(v)is replaced with the mass of individual cells used to approximate the electric forces onu. In this case, the mass of a cell is defined as the sum of weights of the nodes contained in the cell and all its subcells. Instead ofxv, the center-of-mass of the cell is used. This modification also allows users to specify weights for individual nodes, which is useful in situations where nodes need to be isolated visually. Note that, unlike in formula 3.4, only the weight ofvneeds to be included infe(u,v)since the electric force betweenuandvis computed twice—the second time asfe(v,u), which includes the weight ofu.

Node Movement

Once the total force f on a nodeu has been computed (line 8–12 in listing 3.1), the node is moved along this force. The force itself is a two-dimensional vector with a direction and a length, the latter of which represents the strength of the force. One would expect that this strength is reflected in the node's movement. Surprisingly, Hu chose to move all nodes by the same amount—step size—and interpret the force only as a direction vector [31]. Thus, inHu2006 spring electrical, the node movement is implemented as follows.

MOVE-NODE-ALONG-FORCE(u,f,step size) 1 returnxu+step size·f/||f||

Again, this implies that despite forces of varying strength, all nodes are moved by the same amount. In [31], Hu provides no reasons or evidence for this decision.

Step Size Control and Convergence

Nodes are moved by the amount specified using the step sizestep size. In order to reducing the movements over time and converge towards a minimum energy layout, this step size is updated

38 Force-Based Graph Drawing Algorithms for Arbitrary Graphs

after every iteration (line 16 in listing 3.1). This is calledcooling. The cooling function by which step sizeis updated needs to support escaping local minima as well as a quick convergence of the algorithm. Many techniques are discussed in [21] including simulated annealing, sintering, simmering, and quenching.

Hu uses two different cooling functions [31]. In the refinement phase of the multilevel algorithm a simple scheme is employed wherestep sizeis gradually reduced by acooling f actor <1until it is zero:

UPDATE-STEP-SIZE(step size,cooling f actor) 1 returnstep size·cooling f actor

Hu proposes to setcooling f actor = 0.9, which is stated to be adequate enough if the layout to be refined is not entirely random.

When applying the regular spring-electrical algorithm to a random initial layout, however, a more advancedadaptive coolingmethod is employed. Its central idea is to keep the step size un-changed if the total system energy is being reduced once but to increase the step size after five consecutive improvements. The step size is reduced only if the energy increases. This adap-tive method requires a modification to the energy optimization loop (lines 4–16 in listing 3.1).

Three variables are introduced, calledprogress,energy prev, andenergythat hold the number of consecutive improvements and the computed system energy after the previous and current iteration, respectively. These variables are passed to UPDATE-STEP-SIZE, which looks as follows.

UPDATE-STEP-SIZE(step size,progress,cooling f actor,energy prev,energy) 1 ifenergy<energy prevthen

2 progressprogress+ 1 3 ifprogress≥5then

4 progress←0

5 step sizet/cooling f actor 6 else

7 progress←0

8 step sizestep size·cooling f actor 9 returnstep size,progress

The total system energy is defined as the squared sum of the combined forces (3.6) on all nodes energy= X

uV(G)

f2(u). (3.11)

and is computed along with the forces and movements.

Spring and Spring-Electrical Algorithms Implemented forTikZ 39

Hu further states that a suitable choice of the natural spring dimensionkand the constantCis helpful in converging towards a minimum energy layout. Ideally, these parameters reflect the scaling and node distances in the initial layout. Throughout [31],Cis fixed atC= 0.2whilekis held variable.

Initial Layout and Scaling

In Hu's spring-electrical algorithm, the initial layout is generated randomly. No details are given about the scaling of this initial layout. A common technique used in [21 and 51] is to generate a random initial layout and then set the natural spring dimensionkto the average length of all edges. This, however, implies that users only have indirect control over the scaling and natural spring dimension by changing the range of the random values.

Clearly, a more intuitive and predictable approach is to choose a random distribution that re-sults in an average node separation of k in the final layout and let the user choose k. This is roughly what theHu2006 spring electricalimplementation in TikZ does. While exper-imenting with a variety of initial placements I noticed that, for dense graphs likeKn, the algo-rithm tends to generate compressed-looking drawings if the initial layout is scaled poorly. To overcome this problem, my implementation ofHu2006 spring electricaldistributes nodes in a range that is proportional to the desired natural spring dimensionkand the density of the graph, denoted bydensity(G). More precisely, all random initial positions lie in an area defined by

pos(v) = (random(r,+r),random(r,+r)), vV(G) (3.12) whereris a scaling factor proportional to the graph density:

r= 3k·density(G)·p

|V(G)|/ 2. (3.13)

After the initial layout has been generated, the natural spring dimension specified by the user and used for the random distribution is replaced with the average edge length in the initial layout. Even though this choice still does not guarantee an average edge length ofkin the final drawing, it works reasonably well with dense and sparse graphs alike. If the input graph or the coarsest graph contains only two nodes, one is placed at(0,0)while the other is placed r apart in a random direction. This initial placement may appear somewhat artificial and other techniques may yield even better results. However, taking the density of the input graph into account seems to make a notable difference and is this worth to investigated further in future research.

40 Force-Based Graph Drawing Algorithms for Arbitrary Graphs

When reverting the graph coarsening from a coarse graphGito a less coarse graphGi1, a naive interpolation of node coordinates and application of the regular spring-electrical algorithm to Gi1may destroy useful properties inherited fromGi. In order to avoid this, Hu proposes to scale the coordinates ofGi1after interpolation fromGiand before applying the regular spring-elec-trical algorithm [31]. The factorsi1that he uses to scale the coordinates ofGi1is defined as

si1 =diam(Gi1) /diam(Gi), (3.14) where diam(G) denotes the pseudo-diameter of a graph G, that is, an approximation of the longest shortest path from any node to another. There are many ways to compute the pseudo-di-ameter, including such versatile methods as the one proposed by Paulino et al. [45]. My imple-mentation of Hu's spring-electrical algorithm finds the pseudo-diameter by starting at a nodeu with minimum degree. It computes the graph distances to all other nodes and from the nodes that are farthest away it picks the nodevwith the smallest degree. The graph distance between uandvis stored as the temporary pseudo-diameter. This process is repeated withvas the new starting node until the pseudo-diameter cannot be increased further. While this algorithm fails to always find an exact solution, it is expected to converge quickly.