Debian Codegraph @

doc/libguide/unconnect.tex

raw
1 \section{Unconnected graphs}
2 \label{sec:unconnect}
3 All of the basic layouts provided by \gviz\ are based on a connected graph.
4 Each is then extended to handle the not uncommon case of having
5 multiple components. Most of the time, the obvious approach is used:
6 draw each component separately and then assemble the drawings into a single
7 layout. The only place this is not done is in \neato\ when the mode is
8 {\tt "KK"} and {\tt pack="false"} (cf. Section~\ref{sec:neato}).
9
10 For the \dot\ algorithm, its layered drawings make the merging simple:
11 the nodes on the highest rank of each component are all put
12 on the same rank. For the other layouts, it is not obvious how to put
13 the components together.
14
15 The \gviz\ software provides the library \pack\ to assist
16 with unconnected graphs, especially by supplying a technique for
17 packing arbitrary graph drawings together quickly, aesthetically and
18 with efficient use of space. The following code indicates how the
19 library can be integrated with the
20 basic layout algorithms given an input graph {\tt g} and a \gvc\ value
21 {\tt gvc}.
22
23 \begin{verbatim}
24 Agraph_t *sg;
25 FILE *fp;
26 Agraph_t** cc;
27 int i, ncc;
28
29 cc = ccomps(g, &ncc, (char*)0);
30
31 for (i = 0; i < ncc; i++) {
32 sg = cc[i];
33 nodeInduce (sg);
34 gvLayout(gvc, sg, "neato");
35 }
36 pack_graph (ncc, cc, g, 0);
37
38 gvRender(gvc, g, "ps", stdout);
39
40 for (i = 0; i < ncc; i++) {
41 sg = cc[i];
42 gvFreeLayout(gvc, sg);
43 agdelete(g, sg);
44 }
45 \end{verbatim}
46
47 The call to {\tt ccomps} splits the graph {\tt g} into its connected
48 components. {\tt ncc} is set to the number of components.
49 The components are represented by subgraphs of the input graph, and are
50 stored in the returned array. The function gives names to the components
51 in a way that should not conflict with previously existing subgraphs.
52 If desired, the third argument to {\tt ccomps} can be used to designate
53 what the subgraphs should be called. Also, for flexibility, the
54 subgraph components do not contain the associated edges.
55
56 Certain layout algorithms, such as \neato, allow the input graph to
57 fix the position of certain nodes, indicated by {\tt ND\_pinned(n)} being
58 non-zero. In this case, all nodes with a
59 fixed position need to be laid out together, so they should all
60 occur in the same ``connected'' component. The \pack\ library
61 provides {\tt pccomps}, an analogue to {\tt ccomps} for this
62 situation. It has almost the same interface as {\tt ccomps}, but takes
63 a {\tt boolean*} third parameter. The function sets the boolean pointed
64 to to true if the graph has nodes with fixed positions. In this case,
65 the component containing these nodes is the first one in the returned array.
66
67 Continuing with the example,
68 we take one component at a time, use {\tt nodeInduce} to create the
69 corresponding node-induced subgraph, and then lay out the component
70 with {\tt gvLayout}. Here, we use \neato\ for each layout, but it
71 is possible to use a different layout for each component.\footnote{
72 At present, the \dot\ layout has a limitation that it only works on
73 a root graph. Thus, to use \dot\ for a component, one needs to create
74 a new copy of the subgraph, apply \dot\, and then copy the position
75 attributes back to the component.}
76
77 Next, we use the \pack\ function {\tt pack\_graph} to reassemble
78 the graph into a single drawing. To position the components, \pack\
79 uses the polyomino-based approach described by
80 Freivalds et al\cite{pack}. The first three arguments to the
81 function are clear. The fourth argument indicates whether or not
82 there are fixed components.
83
84 The {\tt pack\_graph} function uses the graph's {\tt packmode}
85 attribute to determine how the packing should be done. At
86 present, packing uses the single algorithm mentioned above, but allows
87 three varying granularities, represented by the values {\tt "node"},
88 {\tt "clust"} and {\tt "graph"}. In the first case, packing is done at
89 the node and edge level. This provides the tightest packing, using the
90 least area, but also allows a node of one component to lie between
91 two nodes of another component. The second value, {\tt "clust"},
92 requires that the packing treat top-level clusters with a set
93 bounding box {\tt GD\_bb} value like a large node. Nodes and edges not
94 entirely contained within a cluster are handled as in the previous
95 case. This prevents any components which do not belong to the cluster
96 from intruding within the cluster's bounding box. The
97 last case does the packing at the graph granularity. Each component
98 is treated as one large node, whose size is determined by its
99 bounding box.
100
101 Note that the library automatically computes the bounding box of
102 each of the components. Also,
103 as a side-effect, {\tt pack\_graph} finishes by recomputing and
104 setting the bounding box attribute {\tt GD\_bb} of the graph.
105
106 The final step is to free the component subgraphs.
107
108 Although \dot\ and \neato\ have their specialized approaches to
109 unconnected graphs, it should be noted that these are not without
110 their deficiencies. The approach used by \dot, aligning the drawings
111 of all components along the top, works well until the
112 number of components grows large. When this happens, the aspect
113 ratio of the final drawing can become very bad. \neato 's handling
114 of an unconnected graph can have two drawbacks. First, there can be
115 a great deal of wasted space. The value chosen to separate
116 components is a simple function of the number of nodes. With a
117 certain edge structure, component drawings may use much less area.
118 This can produce a drawing similar to a classic atom: a large nucleus
119 surrounded by a ring of electrons with a great deal of empty space
120 between them. Second, the \neato\ model is essentially quadratic.
121 If the components are drawn separately, one can see a dramatic
122 decrease in layout time, sometimes several orders of magnitudes.
123 For these reasons, it sometimes makes sense to apply the \twopi\
124 approach for unconnected graphs to the \dot\ and \neato\ layouts.
125 In fact, as we've noted, {\tt neato\_layout} typically uses the \pack\
126 library by default.