TikZSyntax and Options for Creating Layered Drawings 73
For crossing minimization and node positioning, only the methods presented by Gansner et al.
in [27] were implemented. Their crossing minimization algorithm incorporates several of the other ideas, such as the layer-by-layer sweep, the median heuristic, and greedy switching. As a consequence, implementing these individual methods separately is not expected to provide better results. TheGansnerKNV1993algorithm for node positioning reuses the network simplex code already implemented for the layer assignment algorithm with the same name. This made it simple to implement. All that had to be done was to construct the auxiliary graph described earlier. Since this algorithm yields optimal results fast, no other technique was investigated in the course of the thesis.
Edge routing was implemented in the most trivial way possible. In thesimplealgorithm shipped with TikZ, all dummy nodes along an edge are replaced by bend points, resulting in regular poly-line drawings. Using the/tikz/rounded cornersoption for edges, such drawings can easily be transformed into polyline drawings with rounded corners.
74 Algorithms for Layered Drawings of Directed Graphs
4.3.2 Separation of Layers and Nodes
Nodes and layers need to be separated by a certain distance along thex- andy-axis. By default, the horizontal and vertical distance is set to 1cm, which is a sane choice for graphs with rela-tively small nodes rendered on A4 or A5 paper. This covers many graphs found in literature and lecture notes about combinatorial mathematics and computer science.
The vertical separation of the different layers of a drawing can be changed by setting the/graph drawing/level distanceoption to a mathematical expression that may include units.
\tikz \graph [layered drawing] { a -> b };
\tikz \graph [layered drawing,level distance=1cm] { a -> b };
\tikz \graph [layered drawing,level distance=0.5cm*3] { a -> b };
a b
a b
a
b
The horizontal separation between consecutive nodes on the same layer can be adjusted in a similar way by using the/graph drawing/sibling distanceoption.
\tikz \graph [layered drawing]
{ a -> { b, c } };
\tikz \graph [layered drawing,sibling distance=2*1cm]
{ a -> { b, c } };
a
b c
a
b c
4.3.3 Support for Node Clusters
The modular layered drawing algorithm supports horizontal clusters of nodes that are posi-tioned on the same layer. Clusters are defined by passing the/tikz/graphs/new cluster op-tion to the\graphmacro. This option takes a cluster name as its only argument and may be used multiple times to create more than one cluster. Nodes are added to a cluster by passing the cluster name to them as an option. In the following example, three clustersfirst,secondand thirdare defined, forcing all nodes in each cluster to be placed on the same layer.
TikZSyntax and Options for Creating Layered Drawings 75
\tikz \graph [ layered drawing, new cluster=first, new cluster=second, new cluster=third, ] {a [first] -> {
b [second], c [first],
d [third] -> { e [third], f [second] }, }
};
a b
d c f
e
Without node clusters the same graph is drawn as a tree with all edges pointing downwards.
\tikz \graph [layered drawing] { a -> {
b, c,
d -> { e, f }, };}
a
b c d
e f
4.3.4 Edge Parameters
In adherence to the layer concept, edges are drawn pointing downwards with a minimum span of at least one layer. However, there are many scenarios where support for horizontal edges is important, e.g. when working with graphs of finite state machines. Also, forcing certain edges to have an increased minimum span can sometimes unravel drawings with undesired edge crossings. For this purpose, the modular layered drawing algorithm provides the/graph drawing/layered drawing/minimum layersedge option. This option takes an integer greater or equal to zero and can be used to adjust the minimum layer span of individual edges, as is demonstrated in the following examples. First, let us take a look at a very simple graph drawn with default parameters.
\tikz \graph [layered drawing]
{ a -> { b, c, d } };
a
b c d
76 Algorithms for Layered Drawings of Directed Graphs
In the next example, the same graph is used, only this time two of its edges have zero minimum layer span. The algorithm is not capable of detecting overlapping edges between nodes in the same layer and thus, the ordering is not optimal. This requires one of the edges to be bent to the left. In the example this is done using the/tikz/bend leftoption that generates a simple Bézier curve.
\tikz \graph [layered drawing] {
a ->[layered drawing={minimum levels=0}] b,
a ->[layered drawing={minimum levels=0},bend left] c, a -> d,
};
a b c
d
In the final example, the same graph is drawn with its three edges having a minimum layer span of 1, 2, and 3, respectively.
\tikz \graph [layered drawing,level distance=0.5cm] { a -> b,
a ->[layered drawing={minimum levels=2}] c, a ->[layered drawing={minimum levels=3}] d, };
a b
c d
Several of the main algorithm steps, that is, layer assignment, crossing minimization, and node positioning support weights or priorities to be attached to individual edges. The modular lay-ered drawing algorithm has an edge option called/graph drawing/layered drawing/weight for this. It takes any integer or floating point number greater or equal to zero and is set to 1 by default. Edges with a higher weight are prioritized when it comes to making edges short and to straightening them. The following example demonstrates this effect by showing the drawing of a graph with and without prioritizing its longest edge(a,d).
\tikz \graph [layered drawing] { a -> b -> c -> d,
a -> d, b -> d,
};\tikz \graph [layered drawing] { a -> b -> c -> d,
a ->[layered drawing={weight=10}] d, b -> d,
};
a b c
d
a b c
d
Extension Interface of the Algorithm Framework 77
4.3.5 Options for Changing the Individual Steps
As is explained in section 4.2.2, TikZ ships a number of algorithms for the different steps of the modular layered drawing algorithm By default, theGansnerKNV1993methods are used at every step, except for edge routing, which defaults to thesimplealgorithm. This behavior can be changed easily with the help of dedicated options that TikZ provides as part of the library graphdrawing.layered. To remove cycles with theBergerS1990balgorithm, for example, the /graph drawing/layered drawing/cycle removaloption needs to be changed.
\tikz \graph [layered drawing] { a -> b -> c -> d,
a -> d };
\tikz \graph [layered drawing={cycle removal=BergerS1990b}]
{
a -> b -> c -> d, a -> d
};
a b c
d a b
c d
In the same way, alternatives for all the other steps may be selected using the optionslayer assignment,crossing minimization,node positioningandedge routing.