Debian Codegraph @

doc/libguide/layouts.tex

raw
1 \section{Inside the layouts}
2 \label{sec:layouts}
3
4 Each \gviz\ layout algorithm consists of multiple steps, some of
5 which are optional.
6 As the only entry point in the \gviz\ library for laying out
7 a graph is the function {\tt gvLayout}, the control of which
8 steps are used is determined by graph attributes, in the same
9 way this is controlled when passing a graph to one of the layout
10 programs. In this section, we provide a high-level description
11 of the layout steps, and note the relevant attributes.
12
13 Here, we will assume that the graph is connected.
14 All of the layouts handle unconnected graphs. Sometimes, though,
15 an application may not want to use the built-in
16 technique. For these cases, \gviz\ provides tools for
17 decomposing a graph, and then
18 combining multiple layouts. This is described in Section~\ref{sec:unconnect}.
19
20 In all of the algorithms, the first step is to call a layout-specific
21 initialization function. These functions
22 initialize the graph for the particular algorithm.
23 This will first call common routines to set up basic data structures,
24 especially those related to the final layout results and
25 code generation. In particular, the size and shape of nodes will
26 have been analyzed and set at this point, which the application
27 can access via the {\tt ND\_width}, {\tt ND\_height},
28 {\tt ND\_ht}, {\tt ND\_lw},
29 {\tt ND\_rw}, {\tt ND\_shape}, {\tt ND\_shape\_info}
30 and {\tt ND\_label} attributes.
31 Initialization will then establish the data
32 structures specific to the given algorithm. Both the generic
33 and specific layout resources are released when the corresponding
34 cleanup function is called in {\tt gvFreeLayout} (cf. Section~\ref{sec:clean}).
35
36 By default, the layout algorithms position the edges as well as the
37 nodes of the graph. As this may be expensive to compute and irrelevant
38 to an application, an application may decide to avoid this. This can
39 be achieved by setting the graph's {\tt splines} attribute to the
40 empty string {\tt ""}.
41
42 The algorithms all end with a postprocessing step.
43 The role of this is to do some final tinkering with the
44 layout, still in layout coordinates. Specifically, the function
45 rotates the layout for \dot\ (if {\tt rankdir} is set),
46 attaches the root graph's label, if any, and normalizes the drawing
47 so that the lower left corner of its bounding box is at the origin.
48
49 Except for dot, the algorithms also provide a node's position,
50 in inches, in the array give by {\tt ND\_pos}.
51
52 \subsection{\dot}
53
54 The \dot\ algorithm produces a ranked layout of a graph respecting
55 edge directions if possible. It is particularly appropriate for displaying
56 hierarchies or directed acyclic graphs. The basic layout scheme
57 is attributed to Sugiyama et al.\cite{stt} The specific algorithm
58 used by \dot\ follows the steps described by Gansner et al.\cite{gknv:methods}
59
60 The steps in the \dot\ layout are:
61 \begin{verbatim}
62 initialize
63 rank
64 mincross
65 position
66 sameports
67 splines
68 compoundEdges
69 \end{verbatim}
70
71 After initialization, the algorithm
72 assigns each node to a discrete rank ({\tt rank})
73 using an integer program to minimize the sum of the (discrete) edge lengths.
74 The next step ({\tt mincross}) rearranges nodes within ranks to
75 reduce edge crossings. This is followed by the assignment ({\tt position})
76 of actual coordinates to the nodes, using another integer program to
77 compact the graph and straighten edges. At this point, all nodes will
78 have a position set in the {\tt coord} attribute. In addition, the
79 bounding box {\tt bb} attribute of all clusters are set.
80
81 The {\tt sameports} step
82 is an addition to the basic layout. It
83 implements the feature, based on the edge attributes {\tt "samehead"}
84 and {\tt "sametail"}, by which certain edges sharing a node all connect
85 to the node at the same point.
86
87 Edge representations are generated in the {\tt splines} step.
88 At present, \dot\ draws all edges as B-splines, though some edges will
89 actually be the degenerate case of a line segment.
90
91 Although \dot\ supports the notion of cluster subgraphs, its model does
92 not correspond to general compound graphs. In particular, a graph cannot
93 have edges connecting two clusters, or a cluster and a node. The
94 layout can emulate this feature. Basically, if the head and tail nodes
95 of an edge lie in different, non-nested clusters, the edge can specify
96 these clusters as a logical head or logical tail using the {\tt lhead} or
97 {\tt ltail} attribute. The
98 spline generated in {\tt splines} for the edge can then be clipped
99 to the bounding box of the specified clusters.
100 This is accomplished in the {\tt compoundEdges} step.
101
102 \subsection{\neato}
103 \label{sec:neato}
104
105 The layout computed by \neato\ is specified by a virtual physical
106 model, i.e., one in which nodes are treated as physical objects
107 influenced by forces, some of which arise from the edges in the
108 graph. The layout is then derived by finding positions of the nodes
109 which minimize the forces or total energy within the system.
110 The forces need not correspond to true physical forces, and typically
111 the solution represents some local minimum.
112 Such layouts are sometimes referred to as symmetric, as the
113 principal aesthetics of such layouts tend to be the visualization
114 of geometric symmetries within the graph. To further enhance the
115 display of symmetries, such drawings tend to use line segments for edges.
116
117 The model used by \neato\ comes from Kamada and Kawai\cite{kk},
118 though it was first introduced by Kruskal and Seely\cite{kruskal} in a
119 different format.
120 The model assumes there is a spring between
121 every pair of vertices, each with an ideal length. The ideal lengths
122 are a function of the graph edges. The layout attempts to minimize the
123 energy in this system.
124
125 \begin{verbatim}
126 initialize
127 position
128 adjust
129 splines
130 \end{verbatim}
131
132 As usual, the layout starts with an initialization step.
133 The actual layout is parameterized by the {\tt mode} and
134 {\tt model} attributes.
135 The mode attribute determines how the optimization problem
136 is solved, either by the default, stress majorization\cite{gkn} mode,
137 ({\tt mode="major"}),
138 or the gradient descent technique proposed by Kamada and Kawai\cite{kk}
139 ({\tt mode="KK"}).
140 The latter mode is typically slower than the former, and introduces the
141 possibility of cycling. It is maintained solely for backward compatibility.
142
143 The model indicates how the ideal distances are computed between all
144 pairs of nodes. By default, \neato\
145 uses a shortest path model ({\tt model="shortpath"}),
146 so that the length of the spring between
147 nodes $p$ and $q$ is the length of the shortest path between them
148 in the graph. Note that the shortest path calculation takes
149 into account the lengths of edges as specified by the {\tt "len"}
150 attribute, with one inch being the default.
151
152 If {\tt mode="KK"} and the graph attribute {\tt pack} is false,
153 \neato\ sets the distance between nodes in separate connected components
154 to $1.0 + L_{avg}\cdot\sqrt{|{\tt V}|}$,
155 where $L_{avg}$ is the average edge length and $|{\tt V}|$
156 is the number of nodes in the graph.
157 This supplies sufficient separation between components
158 so that they do not overlap. Typically, the larger components will be
159 centrally located, while smaller components will form a ring around
160 the outside.
161
162 In some cases, an application may decide to use the circuit model
163 ({\tt model="circuit"}),
164 a model based on electrical circuits
165 as first proposed by Cohen\cite{cohen}.
166 In this model, the spring length is derived from resistances using
167 Kirchoff's law. This means that the more paths between $p$ and $q$
168 in the graph, the smaller the spring length. This has the effect of
169 pulling clusters closer together.
170 We note that this approach only works if the graph is connected.
171 If the graph is not connected, the layout automatically reverts to the
172 shortest path model.
173
174 The third model is the subset model ({\tt model="subset"}).
175 This sets the length of each edge to be the number of nodes that are
176 neighbors of exactly one of the end points, and then calculates
177 remaining distances using shortest paths. This helps to separate
178 nodes with high degree.
179
180 The basic algorithm used by \neato\ performs the layout assuming
181 point nodes. Since in many cases, the final drawing uses text
182 labels and various node shapes, the drawing ends up with many
183 nodes overlapping each other. For certain uses, the effect is
184 desirable. If not, the application can use the {\tt adjust} step to
185 reposition the nodes to eliminate overlaps. This is controlled by the
186 graph attribute {\tt "overlap"}.
187
188 With nodes positioned, the algorithm proceeds to draw the
189 edges using its {\tt splines} function.
190 By default, edges are drawn as line
191 segments. If, however, the {\tt "splines"} graph attribute is
192 set to true, the edges will be constructed as
193 splines\cite{paths},
194 routing them around the nodes. Topologically, the spline
195 follows the shortest path between two nodes while avoiding all others.
196 Clearly, for this to work, there can be no node overlaps. If overlaps
197 exist, edge creation reverts back to line segments.
198 When this function returns, the positions of the nodes will be recorded
199 in their {\tt coords} attribute, in points.
200
201 The programmer should be aware of certain limitations and
202 problems with the \neato\ algorithm.
203 First, as noted above, if {\tt mode="KK"},
204 it is possible for the minimization technique used by \neato\
205 to cycle, never finishing. At present, there
206 is no way for the library to detect this, though once identified,
207 it can easily be fixed by simply picking another initial position.
208 Second, although multiedges affect the layout,
209 the spline router does not yet handle them. Thus,
210 two edges between the same nodes will receive the same spline.
211 Finally, \neato\ provides no mechanism for drawing clusters.
212 If clusters are required, one should use the \fdp\ algorithm, which belongs
213 to the same family as \neato\ and is described next.
214
215 \subsection{\fdp}
216 \label{sec:fdp}
217
218 The \fdp\ layout is similar in appearance to \neato\ and also relies
219 on a virtual physical model, this time proposed by Fruchterman and
220 Reingold\cite{fr}. This model uses springs only between nodes
221 connected with an edge, and an electrical repulsive force between
222 all pairs of nodes. Also, it achieves a layout by minimizing the forces
223 rather than energy of the system.
224
225 Unlike \neato, \fdp\ supports cluster subgraphs. In addition, it
226 allows edges between clusters and nodes, and between cluster and clusters.
227 At present, an edge from a cluster cannot connect to a node or cluster
228 with the cluster.
229
230 \begin{verbatim}
231 initialize
232 position
233 splines
234 \end{verbatim}
235
236 The layout scheme is fairly simple: initialization; layout; and a call to
237 route the edges. In \fdp, because it is necessary
238 to keep clusters separate, the removal of overlaps is (usually)
239 obligatory.
240
241 \subsection{\sfdp}
242 \label{sec:sfdp}
243
244 The \sfdp\ layout is similar to \fdp\, except it uses a refined multilevel
245 approach that enables it to handle very large graphs. The algorithm is
246 due to Hu\cite{sfdp}.
247
248 Unlike \fdp, \sfdp\ does not support cluster subgraphs. It also
249 does not model edge lengths or weights.
250
251 \begin{verbatim}
252 initialize
253 position
254 adjust
255 splines
256 \end{verbatim}
257
258 The layout scheme is fairly simple: initialization; layout; node overlap removal; and a call to
259 route the edges.
260
261 \subsection{\twopi}
262 \label{sec:twopi}
263
264 The radial layout algorithm represented by {\tt twopi} is conceptually the
265 simplest in \gviz. Following an algorithm described by Wills\cite{nicheworks},
266 it takes a node specified as the center of the layout and the root
267 of the generated spanning tree. The remaining
268 nodes are placed on a series of concentric circles about the center,
269 the circle used corresponding to the graph-theoretic distance from the
270 node to the center. Thus, for example, all of the neighbors of the
271 center node are placed on the first circle around the center.
272 The algorithm allocates angular slices to each branch of the
273 induced spanning tree to guarantee enough space for the tree on each ring.
274 At present, the algorithm does not attempt to visualize clusters.
275
276 \begin{verbatim}
277 initialize
278 position
279 adjust
280 splines
281 \end{verbatim}
282
283 As usual, the layout commences by initializing the graph.
284 This is followed by the {\tt position} step, which is parameterized
285 by the central node, specified by the graph's {\tt root} attribute.
286 If unspecified, the algorithm will select some
287 ``most central'' node, i.e., one whose minimum distance from a leaf
288 node is maximal.
289
290 As with \neato, the layout allows an {\tt adjust} step
291 to eliminate node-node overlaps. Again as with \neato, the call to
292 {\tt splines} computes drawing information for edges. See
293 Section~\ref{sec:neato} for more details.
294
295 \subsection{\circo}
296 \label{sec:circo}
297
298 The \circo\ algorithm is based on the work of Six and Tollis\cite{st,st2},
299 as modified by Kaufmann and Wiese\cite{kw}. The nodes in each
300 biconnected component are placed on a circle, with some attempt to
301 minimize edge crossings. Then, by considering each component as a single
302 node, the derived tree is laid out in a similar fashion to \twopi,
303 with some component considered as the root node.
304
305 \begin{verbatim}
306 initialize
307 position
308 splines
309 \end{verbatim}
310
311 As with \fdp, the scheme is very simple.
312 By construction, the \circo\ layout avoids node overlaps, so no
313 {\tt adjust} step is necessary.
314