• Nenhum resultado encontrado

4.1 An Algorithm Framework Based on the Sugiyama Method

4.1.3 Algorithms for Crossing Minimization

An Algorithm Framework Based on the Sugiyama Method 63

it attempts to distribute nodes evenly on these layers while of course preserving the minimum length constraint of the edges.

Listing 4.5 The network simplex layer assignment algorithm, as proposed in [27].

NETWORK-SIMPLEX-LAYER-ASSIGNMENT(G) 1 T←INITIAL-FEASIBLE-TREE(G) 2

3 whileTcontains an edge with negative cut valuedo 4 e←GET-EDGE-WITH-NEGATIVE-CUT-VALUE(G,T) 5 f←FIND-TIGHT-NON-TREE-EDGE(G,T,e) 6 REPLACE-TREE-EDGE(G,T,e,f)

7

8 NORMALIZE-LAYERS(G) 9 BALANCE-LAYERS(G)

The Graphviz implementation presented in [27] includes a number of optimizations that make updating the cut values more efficient. Gansner et al. also propose a more efficient technique for checking whether a node lies in the head or tail component of a tree edge. These optimiza-tions are crucial as the same algorithm is later applied to the node positioning problem that is explained in section 4.1.4.

64 Algorithms for Layered Drawings of Directed Graphs

of crossings betweenL1 andL2. It then goes on to reorder the third layer while holding the second layer fixed. This is continued until all layers except for the first one have been examined.

Upward sweeping and sweeping from the middle work analogous.

Obviously, the central aspect of the layer-by-layer sweep is how the nodes of a specific layer are reordered using a neighbored layer as a fixed reference. This problem is known asone-sided crossing minimization, which unfortunately is NP-hard [19, 20 and 26]. In the following various heuristics to solve this problem are presented. A formalization of the crossing minimization problem and an extensive survey of the heuristics available are once more given in [3 and 18].

The Barycenter and Median Heuristics

Thebarycenterandmedianheuristics are based on the assumption that, in order to reduce edge crossings, nodes should be positioned close to their neighbors. Both methods are simple, rela-tively fast and produce pleasing results.

LetLibe the layer that is reordered andLi1the layer held fixed. The barycenter method first assigns to each nodevLi the barycenter (average) of the positions of its neighbors inLi1, defined asNi1(v) ={u|(u,v) ∈E(V)}:

barycenter(v) = 1

|Ni1(v)| X

uNi1(v)

posi1(u), (4.3)

whereposi1(u)denotes the position ofuin the layerLi1. Nodesv,wLi that are assigned the same valuebarycenter(v) = barycenter(w)need to be moved apart by a small amount. In a second step, the nodes ofLiare sorted according to their barycenter values.

An interesting property of the barycenter heuristic is that it generates an ordering with no crossings if one is possible at all [9]. Computing the barycenter values requires linear time. If the barycenter values and layer positions are restricted to be integers, sorting can be implemented inO(n)as well using integer sorting algorithms such as bucket sort or radix sort. This results in a linear runtime for the overall layer-by-layer sweep.

The median method assigns to each nodevLithe median of thex-coordinates of its neighbors Ni1(v). Letu1,u2,. . .,ujbe the neighbors ofvwithposi1(u1) < posi1(u2) < . . .< posi1(uj). In this case the median is defined as

median(v) =













posi1(udj

2e) ifjis odd andj>1, posi1(uj

21) ifjis even, or

1 ifj= 0.

(4.4)

An Algorithm Framework Based on the Sugiyama Method 65

Like with the barycenter method, the nodes ofLiare sorted by their median values afterwards. It can be shown that, for the one-sided crossing minimization problem, the median method always generates orderings with at most three times as many edge crossings than in the minimum solution. An interesting statement in this context comes from [18], where it is claimed that there is no fast method known to produce drawings with an overall number of crossings within a constant factor of the minimum. Like the barycenter heuristic, the median approach can be implemented to run in linear time.

The Greedy Switch Heuristic

Unlike the barycenter and the median heuristics the greedy switch method explicitly counts the number of crossings between two layers. This makes it more expensive as all pairs of edges between the two layers need to be checked with regard to whether they cross or not.

A pair(l,u)and(r,v)of edges withu,vLiandl,rLi1crosses if the tail and head nodes are not in the same order, that is if

sign(posi(v)−posi(u)) 6= sign(posi1(r)−posi1(l)). (4.5) Thecrossing numbercuvof two consecutive nodesu,vLiwithposi(u) < posi(v)is defined as the number of crossings between edges incident touand edges incident tov. Switching the positions ofuandvchanges the total number of crossings in the drawing bycvucuv. This observation is the basis for the main idea behind greedy switching.

At each layer to be reordered to minimize crossings, the greedy switch algorithm iterates over all consecutive pairsu,vof nodes. Whenever it comes across a pair for whichcvu<cuv, it switches the positions of the two nodes. This is repeated until no further improvement is possible.

The crossing numbers can be precomputed in linear time according to [49]. Thus, the overall complexity of this method is O(|Li|2) for each layerLi[3]. As others have noted [27], greedy switching is particularly useful as a post-processing step in combination with other heuristics due to its nature of making changes only when improvements are possible.

A Combination and Refinement of the Individual Strategies

Gansner et al. combine an initial ordering based on a depth-first search with the median and greedy switch heuristics applied in the form of an alternating layer-by-layer sweep based on a weighted median [27]. The basic structure of their algorithm is shown in listing 4.6.

66 Algorithms for Layered Drawings of Directed Graphs

Listing 4.6 Crossing minimization using a combination of the median and greedy switch layer-by-layer sweeping heuristics.

COMBINED-CROSSING-MINIMIZATION(G)

1 ordering←COMPUTE-INITIAL-DFS-ORDERING(G) 2 bestordering

3

4 fori←0,. . .,iterationsdo 5 ifiis eventhen

6 ordering←WEIGHTED-MEDIAN-SWEEP(G,ordering, 'up')

7 else

8 ordering←WEIGHTED-MEDIAN-SWEEP(G,ordering, 'down') 9

10 ordering←GREEDY-SWITCH-SWEEP(G,ordering, 'down') 11

12 ifCOUNT-CROSSINGS(G,ordering)<COUNT-CROSSINGS(G,best)then 13 bestordering

14

15 returnbest

The initial ordering is constructed while traversing the directed acyclic graph using a depth-first search, starting at its source nodes. Nodes are placed from left to right on layers that correspond to levels in the depth-first search tree. This means that the sources are moved to the first layer, their immediate children in the search tree are moved to the second layer and so forth.

The weighted median sweep is a refined version of the regular median heuristic. Compared to the original method it only behaves differently in situations where the number of neighbors on the fixed layer is even. If there are only two neighbors on the fixed layer, the arithmetic mean of their position is used as the median value. If the number of neighbors is even and there are more than two neighbors, a weighted median is applied that moves nodes to the side where their neighbors are more closely packed [27].